Need to sort gas price list to find correct percentiles

This commit is contained in:
MrTuxedo 2023-04-21 17:09:00 -07:00
parent f64fbfb71d
commit 8b2ae82cfd

View File

@ -16,10 +16,10 @@ 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.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),