fix the formulas for fast/slow

This commit is contained in:
MrTuxedo 2023-04-21 16:19:16 -07:00
parent deee0f205a
commit d28cc9b3f7
1 changed files with 8 additions and 5 deletions

View File

@ -17,9 +17,12 @@ const blockGasPricesObservable = new Observable<GasPrices>((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),
};
})
);