diff --git a/src/blockchain.ts b/src/blockchain.ts index 91ae1f5..01de37c 100644 --- a/src/blockchain.ts +++ b/src/blockchain.ts @@ -16,10 +16,10 @@ const blockGasPricesObservable = new Observable((observer) => { if (!block) throw new Error(`Error fetching block! ${blockNumber}`); - const gasPrices = block.prefetchedTransactions.map((tx) => tx.gasPrice); - const fast = Number(formatUnits(gasPrices[Math.floor(gasPrices.length * 0.1)], "gwei")); + const gasPrices = block.prefetchedTransactions.map((tx) => tx.gasPrice).sort(); + const fast = Number(formatUnits(gasPrices[Math.floor(gasPrices.length * 0.9)], "gwei")); const average = Number(formatUnits(gasPrices[Math.floor(gasPrices.length / 2)], "gwei")); - const slow = Number(formatUnits(gasPrices[Math.floor(gasPrices.length * 0.95)], "gwei")); + const slow = Number(formatUnits(gasPrices[Math.floor(gasPrices.length * 0.05)], "gwei")); // Log averages every 10 blocks if (blockNumber % 10 == 0) console.log(`Found new block data for ${blockNumber}! Base average transaction cost: ${slow} Gwei`) @@ -36,12 +36,12 @@ const averageGasPricesObservable = blockGasPricesObservable.pipe( map((blocks) => { const { fast, average, slow } = blocks .reduce((sum, block) => - ({ + ({ // Find sums fast: sum.fast + block.fast, average: sum.average + block.average, slow: sum.slow + block.slow }), { fast: 0, average: 0, slow: 0 } as GasPrices); - return { + return { // Then average them fast: Math.round(fast / blocks.length), average: Math.round(average / blocks.length), slow: Math.round(slow / blocks.length),