diff --git a/src/gas/fetchBlockFeeHistory.test.ts b/src/gas/fetchBlockFeeHistory.test.ts index 5602b2057f..23e17db732 100644 --- a/src/gas/fetchBlockFeeHistory.test.ts +++ b/src/gas/fetchBlockFeeHistory.test.ts @@ -308,4 +308,65 @@ describe('fetchBlockFeeHistory', () => { expect(feeHistory).toStrictEqual([]); }); }); + + describe('given includeNextBlock = true', () => { + const latestBlockNumber = 3; + const numberOfRequestedBlocks = 3; + + it('includes an extra block with an estimated baseFeePerGas', async () => { + when(mockedQuery) + .calledWith(ethQuery, 'eth_feeHistory', [ + toHex(numberOfRequestedBlocks), + toHex(latestBlockNumber), + [], + ]) + .mockResolvedValue({ + oldestBlock: toHex(1), + // Note that this array contains 6 items when we requested 5. Per + // , + // baseFeePerGas will always include an extra item which is the calculated base fee for the + // next (future) block. + baseFeePerGas: [ + toHex(10_000_000_000), + toHex(20_000_000_000), + toHex(30_000_000_000), + toHex(40_000_000_000), + ], + gasUsedRatio: [0.1, 0.2, 0.3], + }); + + const feeHistory = await fetchBlockFeeHistory({ + ethQuery, + numberOfBlocks: numberOfRequestedBlocks, + includeNextBlock: true, + }); + + expect(feeHistory).toStrictEqual([ + { + number: new BN(1), + baseFeePerGas: new BN(10_000_000_000), + gasUsedRatio: 0.1, + priorityFeesByPercentile: {}, + }, + { + number: new BN(2), + baseFeePerGas: new BN(20_000_000_000), + gasUsedRatio: 0.2, + priorityFeesByPercentile: {}, + }, + { + number: new BN(3), + baseFeePerGas: new BN(30_000_000_000), + gasUsedRatio: 0.3, + priorityFeesByPercentile: {}, + }, + { + number: new BN(4), + baseFeePerGas: new BN(40_000_000_000), + gasUsedRatio: null, + priorityFeesByPercentile: null, + }, + ]); + }); + }); }); diff --git a/src/gas/fetchBlockFeeHistory.ts b/src/gas/fetchBlockFeeHistory.ts index ca2a194e8b..235a40c163 100644 --- a/src/gas/fetchBlockFeeHistory.ts +++ b/src/gas/fetchBlockFeeHistory.ts @@ -36,9 +36,9 @@ export type EthFeeHistoryResponse = { }; /** - * @type FeeHistoryBlock + * @type ExistingFeeHistoryBlock * - * Historical data for a particular block. + * Historical data for a particular block that exists on the blockchain. * @property number - The number of the block, as a BN. * @property baseFeePerGas - The base fee per gas for the block in WEI, as a BN. * @property gasUsedRatio - A number between 0 and 1 that represents the ratio between the gas paid @@ -48,13 +48,52 @@ export type EthFeeHistoryResponse = { * used for the block, indexed by those percentiles. (See docs for {@link fetchBlockFeeHistory} for more * on how this works.) */ -export type FeeHistoryBlock = { +export type ExistingFeeHistoryBlock = { number: BN; baseFeePerGas: BN; gasUsedRatio: number; priorityFeesByPercentile: Record; }; +/** + * @type NextFeeHistoryBlock + * + * Historical data for a theoretical block that could exist in the future. + * @property number - The number of the block, as a BN. + * @property baseFeePerGas - The estimated base fee per gas for the block in WEI, as a BN. + */ +export type NextFeeHistoryBlock = { + number: BN; + baseFeePerGas: BN; +}; + +/** + * @type FeeHistoryBlock + * + * Historical data for a particular block. + * @property number - The number of the block, as a BN. + * @property baseFeePerGas - The base fee per gas for the block in WEI, as a BN. + * @property gasUsedRatio - A number between 0 and 1 that represents the ratio between the gas paid + * for the block and its set gas limit. + * @property priorityFeesByPercentile - The priority fees paid for the transactions in the block + * that occurred at particular levels at which those transactions contributed to the overall gas + * used for the block, indexed by those percentiles. (See docs for {@link fetchBlockFeeHistory} for more + * on how this works.) + */ +export type FeeHistoryBlock = + | ExistingFeeHistoryBlock + | NextFeeHistoryBlock; + +/** + * @type ExtractPercentileFrom + * + * Extracts the percentiles that the type assigned to an array of FeeHistoryBlock has been created + * with. This makes use of the `infer` keyword to read the type argument. + */ +export type ExtractPercentileFrom = T extends FeeHistoryBlock[] + ? P + : never; + const MAX_NUMBER_OF_BLOCKS_PER_ETH_FEE_HISTORY_CALL = 1024; /** @@ -85,6 +124,9 @@ const MAX_NUMBER_OF_BLOCKS_PER_ETH_FEE_HISTORY_CALL = 1024; * recorded. Hence, `priorityFeesByPercentile` represents the priority fees of transactions at key * gas used contribution levels, where earlier levels have smaller contributions and later levels * have higher contributions. + * @param args.includeNextBlock - Whether to include an extra block that represents the next + * block after the latest one. Only the `baseFeePerGas` will be filled in for this block (which is + * estimated). * @returns The list of blocks and their fee data, sorted from oldest to newest. */ export default async function fetchBlockFeeHistory({ @@ -92,11 +134,13 @@ export default async function fetchBlockFeeHistory({ numberOfBlocks: totalNumberOfBlocks, endBlock: givenEndBlock = 'latest', percentiles: givenPercentiles = [], + includeNextBlock = false, }: { ethQuery: EthQuery; numberOfBlocks: number; - endBlock?: 'latest' | 'pending' | BN; + endBlock?: 'latest' | BN; percentiles?: readonly Percentile[]; + includeNextBlock?: boolean; }): Promise[]> { const percentiles = givenPercentiles.length > 0 @@ -105,7 +149,7 @@ export default async function fetchBlockFeeHistory({ const finalEndBlockNumber = givenEndBlock === 'latest' - ? await query(ethQuery, 'blockNumber') + ? fromHex(await query(ethQuery, 'blockNumber')) : givenEndBlock; const requestChunkSpecifiers = determineRequestChunkSpecifiers( @@ -114,13 +158,22 @@ export default async function fetchBlockFeeHistory({ ); const blockChunks = await Promise.all( - requestChunkSpecifiers.map(({ numberOfBlocks, endBlockNumber }) => { - return makeRequestForChunk({ - ethQuery, - numberOfBlocks, - endBlockNumber, - percentiles, - }); + requestChunkSpecifiers.map(({ numberOfBlocks, endBlockNumber }, i) => { + return i === requestChunkSpecifiers.length - 1 + ? makeRequestForChunk({ + ethQuery, + numberOfBlocks, + endBlockNumber, + percentiles, + includeNextBlock, + }) + : makeRequestForChunk({ + ethQuery, + numberOfBlocks, + endBlockNumber, + percentiles, + includeNextBlock: false, + }); }), ); @@ -130,6 +183,74 @@ export default async function fetchBlockFeeHistory({ ); } +/** + * Builds an ExistingFeeHistoryBlock. + * + * @param args - The args to this function. + * @param args.number - The number of the block. + * @param args.baseFeePerGas - The base fee per gas of the block. + * @param args.blockIndex - The index of the block in the source chunk. + * @param args.gasUsedRatios - The gas used ratios for the block. + * @param args.priorityFeePercentileGroups - The priority fee percentile groups for the block. + * @param args.percentiles - The percentiles used to fetch the source chunk. + * @returns The ExistingFeeHistoryBlock. + */ +function buildExistingFeeHistoryBlock({ + baseFeePerGas, + number, + blockIndex, + gasUsedRatios, + priorityFeePercentileGroups, + percentiles, +}: { + baseFeePerGas: BN; + number: BN; + blockIndex: number; + gasUsedRatios: number[]; + priorityFeePercentileGroups: string[][]; + percentiles: readonly Percentile[]; +}): ExistingFeeHistoryBlock { + const gasUsedRatio = gasUsedRatios[blockIndex]; + const priorityFeesForEachPercentile = priorityFeePercentileGroups[blockIndex]; + const priorityFeesByPercentile = percentiles.reduce( + (obj, percentile, percentileIndex) => { + const priorityFee = priorityFeesForEachPercentile[percentileIndex]; + return { ...obj, [percentile]: fromHex(priorityFee) }; + }, + {} as Record, + ); + + return { + number, + baseFeePerGas, + gasUsedRatio, + priorityFeesByPercentile, + }; +} + +/** + * Builds a NextFeeHistoryBlock. + * + * @param args - The args to this function. + * @param args.baseFeePerGas - The base fee per gas of the block. + * @param args.number - The number of the block. + * @returns The NextFeeHistoryBlock. + */ +function buildNextFeeHistoryBlock({ + baseFeePerGas, + number, +}: { + baseFeePerGas: BN; + number: BN; +}) { + return { + number, + baseFeePerGas, + gasUsedRatio: null, + priorityFeesByPercentile: null, + }; +} + /** * Uses eth_feeHistory to request historical data about a group of blocks (max size 1024). * @@ -140,6 +261,9 @@ export default async function fetchBlockFeeHistory({ * @param args.endBlockNumber - The end of the requested block range. * @param args.percentiles - A set of numbers betwen 1 and 100 that will be used to pull priority * fees for each block. + * @param args.includeNextBlock - Whether to include an extra block that represents the next + * block after the latest one. Only the `baseFeePerGas` will be filled in for this block (which is + * estimated). * @returns A list of block data. */ async function makeRequestForChunk({ @@ -147,11 +271,13 @@ async function makeRequestForChunk({ numberOfBlocks, endBlockNumber, percentiles, + includeNextBlock, }: { ethQuery: EthQuery; numberOfBlocks: number; endBlockNumber: BN; percentiles: readonly Percentile[]; + includeNextBlock: boolean; }): Promise[]> { const response: EthFeeHistoryResponse = await query( ethQuery, @@ -169,33 +295,27 @@ async function makeRequestForChunk({ // Per // , // baseFeePerGas will always include an extra item which is the calculated base fee for the - // next (future) block. We don't care about this, so chop it off. - const baseFeesPerGasAsHex = response.baseFeePerGas.slice(0, numberOfBlocks); + // next (future) block. We may or may not care about this; if we don't, chop it off. + const baseFeesPerGasAsHex = includeNextBlock + ? response.baseFeePerGas + : response.baseFeePerGas.slice(0, numberOfBlocks); const gasUsedRatios = response.gasUsedRatio; const priorityFeePercentileGroups = response.reward ?? []; return baseFeesPerGasAsHex.map((baseFeePerGasAsHex, blockIndex) => { const baseFeePerGas = fromHex(baseFeePerGasAsHex); - const gasUsedRatio = gasUsedRatios[blockIndex]; const number = startBlockNumber.addn(blockIndex); - const priorityFeesForEachPercentile = - priorityFeePercentileGroups[blockIndex]; - - const priorityFeesByPercentile = percentiles.reduce( - (obj, percentile, percentileIndex) => { - const priorityFee = priorityFeesForEachPercentile[percentileIndex]; - return { ...obj, [percentile]: fromHex(priorityFee) }; - }, - {} as Record, - ); - - return { - number, - baseFeePerGas, - gasUsedRatio, - priorityFeesByPercentile, - }; + return blockIndex > numberOfBlocks - 1 + ? buildNextFeeHistoryBlock({ baseFeePerGas, number }) + : buildExistingFeeHistoryBlock({ + baseFeePerGas, + number, + blockIndex, + gasUsedRatios, + priorityFeePercentileGroups, + percentiles, + }); }); } diff --git a/src/gas/fetchGasEstimatesViaEthFeeHistory.test.ts b/src/gas/fetchGasEstimatesViaEthFeeHistory.test.ts index d5b39899bd..d492d94ae7 100644 --- a/src/gas/fetchGasEstimatesViaEthFeeHistory.test.ts +++ b/src/gas/fetchGasEstimatesViaEthFeeHistory.test.ts @@ -93,7 +93,7 @@ describe('fetchGasEstimatesViaEthFeeHistory', () => { }, }, ], - tinyRangeWithPending: [ + tinyRangeWithNextBlock: [ { number: new BN(5), baseFeePerGas: new BN(1), @@ -114,6 +114,23 @@ describe('fetchGasEstimatesViaEthFeeHistory', () => { }, }, ], + latestWithNextBlock: [ + { + number: new BN(6), + baseFeePerGas: new BN(1), + gasUsedRatio: 1, + priorityFeesByPercentile: { + 10: new BN('0'), + 95: new BN('0'), + }, + }, + { + number: new BN(7), + baseFeePerGas: new BN(1), + gasUsedRatio: null, + priorityFeesByPercentile: null, + }, + ], }; const levelSpecificEstimates = { low: { @@ -143,28 +160,8 @@ describe('fetchGasEstimatesViaEthFeeHistory', () => { const networkCongestion = 0.5; mockedFetchLatestBlock.mockResolvedValue(latestBlock); - mockedBlockFeeHistoryDatasetFetcherConstructor.prototype.forLongRange.mockResolvedValue( - blocksByDataset.longRange, - ); - - mockedBlockFeeHistoryDatasetFetcherConstructor.prototype.forMediumRange.mockResolvedValue( - blocksByDataset.mediumRange, - ); - - mockedBlockFeeHistoryDatasetFetcherConstructor.prototype.forSmallRange.mockResolvedValue( - blocksByDataset.smallRange, - ); - - mockedBlockFeeHistoryDatasetFetcherConstructor.prototype.forTinyRange.mockResolvedValue( - blocksByDataset.tinyRange, - ); - - mockedBlockFeeHistoryDatasetFetcherConstructor.prototype.forTinyRangeWithPending.mockResolvedValue( - blocksByDataset.tinyRangeWithPending, - ); - - mockedBlockFeeHistoryDatasetFetcherConstructor.prototype.forLatest.mockResolvedValue( - blocksByDataset.latest, + mockedBlockFeeHistoryDatasetFetcherConstructor.prototype.forAll.mockResolvedValue( + blocksByDataset, ); when(mockedCalculateGasFeeEstimatesForPriorityLevels) @@ -176,7 +173,7 @@ describe('fetchGasEstimatesViaEthFeeHistory', () => { .mockReturnValue(historicalBaseFeeRange); when(mockedCalculateBaseFeeTrend) - .calledWith(blocksByDataset.tinyRangeWithPending) + .calledWith(blocksByDataset.latestWithNextBlock) .mockReturnValue(baseFeeTrend); when(mockedCalculatePriorityFeeRange) diff --git a/src/gas/fetchGasEstimatesViaEthFeeHistory.ts b/src/gas/fetchGasEstimatesViaEthFeeHistory.ts index ee70699cc4..bc717545f0 100644 --- a/src/gas/fetchGasEstimatesViaEthFeeHistory.ts +++ b/src/gas/fetchGasEstimatesViaEthFeeHistory.ts @@ -1,4 +1,3 @@ -import { BN } from 'ethereumjs-util'; import { fromWei } from 'ethjs-unit'; import { GWEI } from '../constants'; import { GasFeeEstimates } from './GasFeeController'; @@ -12,50 +11,6 @@ import calculatePriorityFeeTrend from './fetchGasEstimatesViaEthFeeHistory/calcu import calculateNetworkCongestion from './fetchGasEstimatesViaEthFeeHistory/calculateNetworkCongestion'; import fetchLatestBlock from './fetchGasEstimatesViaEthFeeHistory/fetchLatestBlock'; -/** - * Uses {@link BlockFeeHistoryDatasetFetcher} to retrieve all of the data - * necessary to return a complete set of gas fee estimates. - * - * @param ethQuery - An EthQuery instance. - * @param endBlockNumber - The desired block number which represents the end of - * the requested data. - * @returns The data. - */ -async function fetchBlockFeeHistoryDatasets( - ethQuery: EthQuery, - endBlockNumber: BN, -) { - const fetcher = new BlockFeeHistoryDatasetFetcher({ - ethQuery, - endBlockNumber, - }); - - const [ - longRange, - mediumRange, - smallRange, - tinyRange, - tinyRangeWithPending, - latest, - ] = await Promise.all([ - fetcher.forLongRange(), - fetcher.forMediumRange(), - fetcher.forSmallRange(), - fetcher.forTinyRange(), - fetcher.forTinyRangeWithPending(), - fetcher.forLatest(), - ]); - - return { - longRange, - mediumRange, - smallRange, - tinyRange, - tinyRangeWithPending, - latest, - }; -} - /** * Generates gas fee estimates based on gas fees that have been used in the recent past so that * those estimates can be displayed to users. @@ -75,10 +30,11 @@ export default async function fetchGasEstimatesViaEthFeeHistory( ethQuery: EthQuery, ): Promise { const latestBlock = await fetchLatestBlock(ethQuery); - const blocksByDataset = await fetchBlockFeeHistoryDatasets( + const fetcher = new BlockFeeHistoryDatasetFetcher({ ethQuery, - latestBlock.number, - ); + endBlockNumber: latestBlock.number, + }); + const blocksByDataset = await fetcher.forAll(); const levelSpecificEstimates = calculateGasFeeEstimatesForPriorityLevels( blocksByDataset.smallRange, @@ -88,7 +44,7 @@ export default async function fetchGasEstimatesViaEthFeeHistory( blocksByDataset.mediumRange, ); const baseFeeTrend = calculateBaseFeeTrend( - blocksByDataset.tinyRangeWithPending, + blocksByDataset.latestWithNextBlock, ); const latestPriorityFeeRange = calculatePriorityFeeRange( blocksByDataset.latest, diff --git a/src/gas/fetchGasEstimatesViaEthFeeHistory/BlockFeeHistoryDatasetFetcher.ts b/src/gas/fetchGasEstimatesViaEthFeeHistory/BlockFeeHistoryDatasetFetcher.ts index af3219f6cb..4818ad3ea1 100644 --- a/src/gas/fetchGasEstimatesViaEthFeeHistory/BlockFeeHistoryDatasetFetcher.ts +++ b/src/gas/fetchGasEstimatesViaEthFeeHistory/BlockFeeHistoryDatasetFetcher.ts @@ -1,5 +1,9 @@ import { BN } from 'ethereumjs-util'; -import fetchBlockFeeHistory, { FeeHistoryBlock } from '../fetchBlockFeeHistory'; +import fetchBlockFeeHistory, { + ExistingFeeHistoryBlock, + ExtractPercentileFrom, + FeeHistoryBlock, +} from '../fetchBlockFeeHistory'; import { EthQuery } from './types'; export default class BlockFeeHistoryDatasetFetcher { @@ -18,48 +22,88 @@ export default class BlockFeeHistoryDatasetFetcher { this.endBlockNumber = endBlockNumber; } + async forAll() { + const [ + longRange, + mediumRange, + smallRange, + tinyRange, + latestWithNextBlock, + ] = await Promise.all([ + this.forLongRange(), + this.forMediumRange(), + this.forSmallRange(), + this.forTinyRange(), + this.forLatestWithNextBlock(), + ]); + + const latest = latestWithNextBlock.slice(0, -1) as ExistingFeeHistoryBlock< + ExtractPercentileFrom + >[]; + + return { + longRange, + mediumRange, + smallRange, + tinyRange, + latest, + latestWithNextBlock, + }; + } + forLongRange() { - return this.fetch({ numberOfBlocks: 20_000 }); + return this.fetchExcludingNextBlock({ numberOfBlocks: 20_000 }); } forMediumRange() { - return this.fetch({ numberOfBlocks: 200, percentiles: [10, 95] }); + return this.fetchExcludingNextBlock({ + numberOfBlocks: 200, + percentiles: [10, 95], + }); } forSmallRange() { - return this.fetch({ numberOfBlocks: 5, percentiles: [10, 20, 30] }); - } - - forTinyRange() { - return this.fetch({ - numberOfBlocks: 2, - percentiles: [50], + return this.fetchExcludingNextBlock({ + numberOfBlocks: 5, + percentiles: [10, 20, 30], }); } - forTinyRangeWithPending() { - return this.fetch({ + forTinyRange() { + return this.fetchExcludingNextBlock({ numberOfBlocks: 2, - endBlock: 'pending', percentiles: [50], }); } - forLatest() { - return this.fetch({ + forLatestWithNextBlock() { + return this.fetchIncludingNextBlock({ numberOfBlocks: 1, percentiles: [10, 95], }); } - private fetch(args: { + private fetchExcludingNextBlock(args: { + numberOfBlocks: number; + endBlock?: BN; + percentiles?: T[]; + }): Promise[]> { + return fetchBlockFeeHistory({ + ethQuery: this.ethQuery, + endBlock: this.endBlockNumber, + ...args, + }) as Promise[]>; + } + + private fetchIncludingNextBlock(args: { numberOfBlocks: number; - endBlock?: BN | 'pending'; + endBlock?: BN; percentiles?: T[]; }): Promise[]> { return fetchBlockFeeHistory({ ethQuery: this.ethQuery, endBlock: this.endBlockNumber, + includeNextBlock: true, ...args, }); } diff --git a/src/gas/fetchGasEstimatesViaEthFeeHistory/calculateGasFeeEstimatesForPriorityLevels.ts b/src/gas/fetchGasEstimatesViaEthFeeHistory/calculateGasFeeEstimatesForPriorityLevels.ts index e7d00e9fb7..5f660d081e 100644 --- a/src/gas/fetchGasEstimatesViaEthFeeHistory/calculateGasFeeEstimatesForPriorityLevels.ts +++ b/src/gas/fetchGasEstimatesViaEthFeeHistory/calculateGasFeeEstimatesForPriorityLevels.ts @@ -1,7 +1,7 @@ import { BN } from 'ethereumjs-util'; import { fromWei } from 'ethjs-unit'; import { Eip1559GasFee, GasFeeEstimates } from '../GasFeeController'; -import { FeeHistoryBlock } from '../fetchBlockFeeHistory'; +import { ExistingFeeHistoryBlock } from '../fetchBlockFeeHistory'; import { GWEI } from '../../constants'; import medianOf from './medianOf'; @@ -54,7 +54,7 @@ const SETTINGS_BY_PRIORITY_LEVEL = { */ function calculateEstimatesForPriorityLevel( priorityLevel: PriorityLevel, - blocks: FeeHistoryBlock[], + blocks: ExistingFeeHistoryBlock[], ): Eip1559GasFee { const settings = SETTINGS_BY_PRIORITY_LEVEL[priorityLevel]; @@ -95,7 +95,7 @@ function calculateEstimatesForPriorityLevel( * @returns The estimates. */ export default function calculateGasFeeEstimatesForPriorityLevels( - blocks: FeeHistoryBlock[], + blocks: ExistingFeeHistoryBlock[], ): Pick { return PRIORITY_LEVELS.reduce((obj, priorityLevel) => { const gasEstimatesForPriorityLevel = calculateEstimatesForPriorityLevel( diff --git a/src/gas/fetchGasEstimatesViaEthFeeHistory/calculatePriorityFeeRange.ts b/src/gas/fetchGasEstimatesViaEthFeeHistory/calculatePriorityFeeRange.ts index 669771a84e..8e702c1d35 100644 --- a/src/gas/fetchGasEstimatesViaEthFeeHistory/calculatePriorityFeeRange.ts +++ b/src/gas/fetchGasEstimatesViaEthFeeHistory/calculatePriorityFeeRange.ts @@ -1,7 +1,7 @@ import { BN } from 'ethereumjs-util'; import { fromWei } from 'ethjs-unit'; import { GWEI } from '../../constants'; -import { FeeHistoryBlock } from '../fetchBlockFeeHistory'; +import { ExistingFeeHistoryBlock } from '../fetchBlockFeeHistory'; import { FeeRange } from './types'; /** @@ -14,7 +14,7 @@ import { FeeRange } from './types'; * @returns The range. */ export default function calculatePriorityFeeRange( - blocks: FeeHistoryBlock<10 | 95>[], + blocks: ExistingFeeHistoryBlock<10 | 95>[], ): FeeRange { const sortedLowPriorityFees = blocks .map((block) => block.priorityFeesByPercentile[10]) diff --git a/src/gas/fetchGasEstimatesViaEthFeeHistory/calculatePriorityFeeTrend.ts b/src/gas/fetchGasEstimatesViaEthFeeHistory/calculatePriorityFeeTrend.ts index cca2f13364..f7611398fa 100644 --- a/src/gas/fetchGasEstimatesViaEthFeeHistory/calculatePriorityFeeTrend.ts +++ b/src/gas/fetchGasEstimatesViaEthFeeHistory/calculatePriorityFeeTrend.ts @@ -1,5 +1,5 @@ import { BN } from 'ethereumjs-util'; -import { FeeHistoryBlock } from '../fetchBlockFeeHistory'; +import { ExistingFeeHistoryBlock } from '../fetchBlockFeeHistory'; /** * Given a collection of blocks, returns an indicator of whether the base fee is moving up, down, or @@ -9,7 +9,7 @@ import { FeeHistoryBlock } from '../fetchBlockFeeHistory'; * @returns The indicator ("up", "down", or "level"). */ export default function calculatePriorityFeeTrend( - blocks: FeeHistoryBlock<50>[], + blocks: ExistingFeeHistoryBlock<50>[], ) { const priorityFees = blocks .map((block) => block.priorityFeesByPercentile[50]) diff --git a/src/gas/fetchGasEstimatesViaEthFeeHistory/fetchLatestBlock.ts b/src/gas/fetchGasEstimatesViaEthFeeHistory/fetchLatestBlock.ts index 6b93947870..01def5bfd5 100644 --- a/src/gas/fetchGasEstimatesViaEthFeeHistory/fetchLatestBlock.ts +++ b/src/gas/fetchGasEstimatesViaEthFeeHistory/fetchLatestBlock.ts @@ -1,16 +1,26 @@ -import { query } from '../../util'; +import { query, fromHex } from '../../util'; import { EthBlock, EthQuery } from './types'; /** * Returns information about the latest completed block. * * @param ethQuery - An EthQuery instance + * @param includeFullTransactionData - Whether or not to include all data for transactions as + * opposed to merely hashes. False by default. * @returns The block. */ export default async function fetchLatestBlock( ethQuery: EthQuery, + includeFullTransactionData = false, ): Promise { const blockNumber = await query(ethQuery, 'blockNumber'); - const block = await query(ethQuery, 'getBlockByNumber', blockNumber); - return block; + const block = await query(ethQuery, 'getBlockByNumber', [ + blockNumber, + includeFullTransactionData, + ]); + return { + ...block, + number: fromHex(block.number), + baseFeePerGas: fromHex(block.baseFeePerGas), + }; }