From 8b2ae82cfdfaf2abff31a2ad3ea0d9b8c90c6b29 Mon Sep 17 00:00:00 2001 From: MrTuxedo Date: Fri, 21 Apr 2023 17:09:00 -0700 Subject: [PATCH] Need to sort gas price list to find correct percentiles --- src/blockchain.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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),