From d28cc9b3f7187e88ce52ab8be278daadc9d2e08e Mon Sep 17 00:00:00 2001 From: MrTuxedo Date: Fri, 21 Apr 2023 16:19:16 -0700 Subject: [PATCH] fix the formulas for fast/slow --- src/blockchain.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) 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), }; }) );