diff --git a/src/blockchain.ts b/src/blockchain.ts index 7decffd..c1ce774 100644 --- a/src/blockchain.ts +++ b/src/blockchain.ts @@ -17,9 +17,12 @@ 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.9)], "gwei")); + const fast = Number(formatUnits(gasPrices[Math.floor(gasPrices.length * 0.1)], "gwei")); const average = Number(formatUnits(gasPrices[Math.floor(gasPrices.length / 2)], "gwei")); - const slow = Number(formatUnits(gasPrices[Math.floor(gasPrices.length * 0.05)], "gwei")); + const slow = Number(formatUnits(gasPrices[Math.floor(gasPrices.length * 0.95)], "gwei")); + + // Log averages every 10 blocks + if (blockNumber % 10 == 0) console.log(`Found new block data for ${blockNumber}! Base average transaction cost: ${slow} Gwei`) observer.next({ fast, average, slow } as GasPrices); } catch (error) { @@ -35,9 +38,9 @@ const averageGasPricesObservable = blockGasPricesObservable.pipe( const averageSum = blocks.reduce((sum, block) => sum + block.average, 0); const slowSum = blocks.reduce((sum, block) => sum + block.slow, 0); return { - fast: fastSum / blocks.length, - average: averageSum / blocks.length, - slow: slowSum / blocks.length, + fast: Math.round(fastSum / blocks.length), + average: Math.round(averageSum / blocks.length), + slow: Math.round(slowSum / blocks.length), }; }) );