From dd48bba89f3ae1a39fa5b170686c84bdf9ce0e3d Mon Sep 17 00:00:00 2001 From: Elliot Winkler Date: Mon, 14 Mar 2022 10:41:43 -0600 Subject: [PATCH] Remove non-critical gas estimate fallback data (#712) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If a request to the Gas API fails for any reason, we attempt to hit Infura directly and compute the same pieces of data that the Gas API computes. However, considering that this data supports the new gas fee UI design in the extension, it is not necessary for us to emulate the API 100%. We only need data that is critical to allow users to see and select fees for different priority levels in order to make transactions. Everything else — a snapshot of recent base and priority fees, whether the base and priority fees have risen or fallen recently, and how busy the network is — is not important. This commit removes these extra computations so as to further limit the amount of requests that are made to Infura. --- src/gas/GasFeeController.ts | 17 +- src/gas/fetchBlockFeeHistory.ts | 4 +- .../fetchGasEstimatesViaEthFeeHistory.test.ts | 165 ++++-------------- src/gas/fetchGasEstimatesViaEthFeeHistory.ts | 49 ++---- .../BlockFeeHistoryDatasetFetcher.test.ts | 113 ------------ .../BlockFeeHistoryDatasetFetcher.ts | 103 ----------- .../calculateBaseFeeRange.test.ts | 29 --- .../calculateBaseFeeRange.ts | 23 --- .../calculateBaseFeeTrend.test.ts | 79 --------- .../calculateBaseFeeTrend.ts | 23 --- ...lculateGasFeeEstimatesForPriorityLevels.ts | 16 +- .../calculateNetworkCongestion.test.ts | 33 ---- .../calculateNetworkCongestion.ts | 30 ---- .../calculatePriorityFeeRange.test.ts | 40 ----- .../calculatePriorityFeeRange.ts | 32 ---- .../calculatePriorityFeeTrend.test.ts | 97 ---------- .../calculatePriorityFeeTrend.ts | 26 --- 17 files changed, 75 insertions(+), 804 deletions(-) delete mode 100644 src/gas/fetchGasEstimatesViaEthFeeHistory/BlockFeeHistoryDatasetFetcher.test.ts delete mode 100644 src/gas/fetchGasEstimatesViaEthFeeHistory/BlockFeeHistoryDatasetFetcher.ts delete mode 100644 src/gas/fetchGasEstimatesViaEthFeeHistory/calculateBaseFeeRange.test.ts delete mode 100644 src/gas/fetchGasEstimatesViaEthFeeHistory/calculateBaseFeeRange.ts delete mode 100644 src/gas/fetchGasEstimatesViaEthFeeHistory/calculateBaseFeeTrend.test.ts delete mode 100644 src/gas/fetchGasEstimatesViaEthFeeHistory/calculateBaseFeeTrend.ts delete mode 100644 src/gas/fetchGasEstimatesViaEthFeeHistory/calculateNetworkCongestion.test.ts delete mode 100644 src/gas/fetchGasEstimatesViaEthFeeHistory/calculateNetworkCongestion.ts delete mode 100644 src/gas/fetchGasEstimatesViaEthFeeHistory/calculatePriorityFeeRange.test.ts delete mode 100644 src/gas/fetchGasEstimatesViaEthFeeHistory/calculatePriorityFeeRange.ts delete mode 100644 src/gas/fetchGasEstimatesViaEthFeeHistory/calculatePriorityFeeTrend.test.ts delete mode 100644 src/gas/fetchGasEstimatesViaEthFeeHistory/calculatePriorityFeeTrend.ts diff --git a/src/gas/GasFeeController.ts b/src/gas/GasFeeController.ts index 40f5572b337..1196997bae5 100644 --- a/src/gas/GasFeeController.ts +++ b/src/gas/GasFeeController.ts @@ -122,7 +122,9 @@ export type Eip1559GasFee = { * level of the network, with 0 meaning not congested and 1 meaning extremely congested */ -export type GasFeeEstimates = { +export type GasFeeEstimates = SourcedGasFeeEstimates | FallbackGasFeeEstimates; + +type SourcedGasFeeEstimates = { low: Eip1559GasFee; medium: Eip1559GasFee; high: Eip1559GasFee; @@ -135,6 +137,19 @@ export type GasFeeEstimates = { networkCongestion: number; }; +type FallbackGasFeeEstimates = { + low: Eip1559GasFee; + medium: Eip1559GasFee; + high: Eip1559GasFee; + estimatedBaseFee: string; + historicalBaseFeeRange: null; + baseFeeTrend: null; + latestPriorityFeeRange: null; + historicalPriorityFeeRange: null; + priorityFeeTrend: null; + networkCongestion: null; +}; + const metadata = { gasFeeEstimates: { persist: true, anonymous: false }, estimatedGasFeeTimeBounds: { persist: true, anonymous: false }, diff --git a/src/gas/fetchBlockFeeHistory.ts b/src/gas/fetchBlockFeeHistory.ts index 922e72aa5cd..553e7b01f40 100644 --- a/src/gas/fetchBlockFeeHistory.ts +++ b/src/gas/fetchBlockFeeHistory.ts @@ -50,7 +50,7 @@ export type EthFeeHistoryResponse = { * used for the block, indexed by those percentiles. (See docs for {@link fetchBlockFeeHistory} for more * on how this works.) */ -export type ExistingFeeHistoryBlock = { +type ExistingFeeHistoryBlock = { number: BN; baseFeePerGas: BN; gasUsedRatio: number; @@ -64,7 +64,7 @@ export type ExistingFeeHistoryBlock = { * @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 = { +type NextFeeHistoryBlock = { number: BN; baseFeePerGas: BN; }; diff --git a/src/gas/fetchGasEstimatesViaEthFeeHistory.test.ts b/src/gas/fetchGasEstimatesViaEthFeeHistory.test.ts index 35890b0c3d9..c82a737730f 100644 --- a/src/gas/fetchGasEstimatesViaEthFeeHistory.test.ts +++ b/src/gas/fetchGasEstimatesViaEthFeeHistory.test.ts @@ -1,43 +1,22 @@ import { BN } from 'ethereumjs-util'; import { mocked } from 'ts-jest/utils'; import { when } from 'jest-when'; -import BlockFeeHistoryDatasetFetcher from './fetchGasEstimatesViaEthFeeHistory/BlockFeeHistoryDatasetFetcher'; +import fetchBlockFeeHistory from './fetchBlockFeeHistory'; import calculateGasFeeEstimatesForPriorityLevels from './fetchGasEstimatesViaEthFeeHistory/calculateGasFeeEstimatesForPriorityLevels'; -import calculateBaseFeeRange from './fetchGasEstimatesViaEthFeeHistory/calculateBaseFeeRange'; -import calculateBaseFeeTrend from './fetchGasEstimatesViaEthFeeHistory/calculateBaseFeeTrend'; -import calculatePriorityFeeRange from './fetchGasEstimatesViaEthFeeHistory/calculatePriorityFeeRange'; -import calculatePriorityFeeTrend from './fetchGasEstimatesViaEthFeeHistory/calculatePriorityFeeTrend'; -import calculateNetworkCongestion from './fetchGasEstimatesViaEthFeeHistory/calculateNetworkCongestion'; import fetchLatestBlock from './fetchGasEstimatesViaEthFeeHistory/fetchLatestBlock'; import fetchGasEstimatesViaEthFeeHistory from './fetchGasEstimatesViaEthFeeHistory'; -jest.mock('./fetchGasEstimatesViaEthFeeHistory/BlockFeeHistoryDatasetFetcher'); +jest.mock('./fetchBlockFeeHistory'); jest.mock( './fetchGasEstimatesViaEthFeeHistory/calculateGasFeeEstimatesForPriorityLevels', ); -jest.mock('./fetchGasEstimatesViaEthFeeHistory/calculateBaseFeeRange'); -jest.mock('./fetchGasEstimatesViaEthFeeHistory/calculateBaseFeeTrend'); -jest.mock('./fetchGasEstimatesViaEthFeeHistory/calculatePriorityFeeRange'); -jest.mock('./fetchGasEstimatesViaEthFeeHistory/calculatePriorityFeeTrend'); -jest.mock('./fetchGasEstimatesViaEthFeeHistory/calculateNetworkCongestion'); jest.mock('./fetchGasEstimatesViaEthFeeHistory/fetchLatestBlock'); -const mockedBlockFeeHistoryDatasetFetcherConstructor = mocked( - BlockFeeHistoryDatasetFetcher, - true, -); +const mockedFetchBlockFeeHistory = mocked(fetchBlockFeeHistory, true); const mockedCalculateGasFeeEstimatesForPriorityLevels = mocked( calculateGasFeeEstimatesForPriorityLevels, true, ); -const mockedCalculateBaseFeeRange = mocked(calculateBaseFeeRange, true); -const mockedCalculateBaseFeeTrend = mocked(calculateBaseFeeTrend, true); -const mockedCalculatePriorityFeeRange = mocked(calculatePriorityFeeRange, true); -const mockedCalculatePriorityFeeTrend = mocked(calculatePriorityFeeTrend, true); -const mockedCalculateNetworkCongestion = mocked( - calculateNetworkCongestion, - true, -); const mockedFetchLatestBlock = mocked(fetchLatestBlock, true); describe('fetchGasEstimatesViaEthFeeHistory', () => { @@ -50,80 +29,19 @@ describe('fetchGasEstimatesViaEthFeeHistory', () => { getBlockByNumber: async () => latestBlock, }; - it('calculates target fees for low, medium, and high transaction priority levels, as well as the network congestion level', async () => { - const blocksByDataset = { - mediumRange: [ - { - number: new BN(2), - baseFeePerGas: new BN(1), - gasUsedRatio: 1, - priorityFeesByPercentile: { - 10: new BN('0'), - 95: new BN('0'), - }, - }, - ], - smallRange: [ - { - number: new BN(3), - baseFeePerGas: new BN(1), - gasUsedRatio: 1, - priorityFeesByPercentile: { - 10: new BN('0'), - 20: new BN('0'), - 30: new BN('0'), - }, - }, - ], - tinyRange: [ - { - number: new BN(4), - baseFeePerGas: new BN(1), - gasUsedRatio: 1, - priorityFeesByPercentile: { - 50: new BN('0'), - }, - }, - ], - tinyRangeWithNextBlock: [ - { - number: new BN(5), - baseFeePerGas: new BN(1), - gasUsedRatio: 1, - priorityFeesByPercentile: { - 50: new BN('0'), - }, - }, - ], - latest: [ - { - number: new BN(6), - baseFeePerGas: new BN(1), - gasUsedRatio: 1, - priorityFeesByPercentile: { - 10: new BN('0'), - 95: new BN('0'), - }, - }, - ], - latestWithNextBlock: [ - { - number: new BN(6), - baseFeePerGas: new BN(1), - gasUsedRatio: 1, - priorityFeesByPercentile: { - 10: new BN('0'), - 95: new BN('0'), - }, + it('calculates target fees for low, medium, and high transaction priority levels', async () => { + const blocks = [ + { + number: new BN(3), + baseFeePerGas: new BN(1), + gasUsedRatio: 1, + priorityFeesByPercentile: { + 10: new BN('0'), + 20: new BN('0'), + 30: new BN('0'), }, - { - number: new BN(7), - baseFeePerGas: new BN(1), - gasUsedRatio: null, - priorityFeesByPercentile: null, - }, - ], - }; + }, + ]; const levelSpecificEstimates = { low: { minWaitTimeEstimate: 15_000, @@ -144,55 +62,32 @@ describe('fetchGasEstimatesViaEthFeeHistory', () => { suggestedMaxFeePerGas: '252.94', }, }; - const historicalBaseFeeRange: [string, string] = ['100', '200']; - const baseFeeTrend = 'up'; - const latestPriorityFeeRange: [string, string] = ['1', '2']; - const historicalPriorityFeeRange: [string, string] = ['2', '4']; - const priorityFeeTrend = 'down'; - const networkCongestion = 0.5; mockedFetchLatestBlock.mockResolvedValue(latestBlock); - mockedBlockFeeHistoryDatasetFetcherConstructor.prototype.forAll.mockResolvedValue( - blocksByDataset, - ); + when(mockedFetchBlockFeeHistory) + .calledWith({ + ethQuery, + endBlock: latestBlock.number, + numberOfBlocks: 5, + percentiles: [10, 20, 30], + }) + .mockResolvedValue(blocks); when(mockedCalculateGasFeeEstimatesForPriorityLevels) - .calledWith(blocksByDataset.smallRange) + .calledWith(blocks) .mockReturnValue(levelSpecificEstimates); - when(mockedCalculateBaseFeeRange) - .calledWith(blocksByDataset.mediumRange) - .mockReturnValue(historicalBaseFeeRange); - - when(mockedCalculateBaseFeeTrend) - .calledWith(blocksByDataset.latestWithNextBlock) - .mockReturnValue(baseFeeTrend); - - when(mockedCalculatePriorityFeeRange) - .calledWith(blocksByDataset.latest) - .mockReturnValue(latestPriorityFeeRange) - .calledWith(blocksByDataset.mediumRange) - .mockReturnValue(historicalPriorityFeeRange); - - when(mockedCalculatePriorityFeeTrend) - .calledWith(blocksByDataset.tinyRange) - .mockReturnValue(priorityFeeTrend); - - when(mockedCalculateNetworkCongestion) - .calledWith([]) - .mockReturnValue(networkCongestion); - const gasFeeEstimates = await fetchGasEstimatesViaEthFeeHistory(ethQuery); expect(gasFeeEstimates).toStrictEqual({ ...levelSpecificEstimates, estimatedBaseFee: '100', - historicalBaseFeeRange, - baseFeeTrend, - latestPriorityFeeRange, - historicalPriorityFeeRange, - priorityFeeTrend, - networkCongestion, + historicalBaseFeeRange: null, + baseFeeTrend: null, + latestPriorityFeeRange: null, + historicalPriorityFeeRange: null, + priorityFeeTrend: null, + networkCongestion: null, }); }); }); diff --git a/src/gas/fetchGasEstimatesViaEthFeeHistory.ts b/src/gas/fetchGasEstimatesViaEthFeeHistory.ts index 202f88c0572..0a829364374 100644 --- a/src/gas/fetchGasEstimatesViaEthFeeHistory.ts +++ b/src/gas/fetchGasEstimatesViaEthFeeHistory.ts @@ -2,14 +2,9 @@ import { fromWei } from 'ethjs-unit'; import { GWEI } from '../constants'; import { GasFeeEstimates } from './GasFeeController'; import { EthQuery } from './fetchGasEstimatesViaEthFeeHistory/types'; -import BlockFeeHistoryDatasetFetcher from './fetchGasEstimatesViaEthFeeHistory/BlockFeeHistoryDatasetFetcher'; -import calculateGasFeeEstimatesForPriorityLevels from './fetchGasEstimatesViaEthFeeHistory/calculateGasFeeEstimatesForPriorityLevels'; -import calculateBaseFeeRange from './fetchGasEstimatesViaEthFeeHistory/calculateBaseFeeRange'; -import calculateBaseFeeTrend from './fetchGasEstimatesViaEthFeeHistory/calculateBaseFeeTrend'; -import calculatePriorityFeeRange from './fetchGasEstimatesViaEthFeeHistory/calculatePriorityFeeRange'; -import calculatePriorityFeeTrend from './fetchGasEstimatesViaEthFeeHistory/calculatePriorityFeeTrend'; -import calculateNetworkCongestion from './fetchGasEstimatesViaEthFeeHistory/calculateNetworkCongestion'; +import fetchBlockFeeHistory from './fetchBlockFeeHistory'; import fetchLatestBlock from './fetchGasEstimatesViaEthFeeHistory/fetchLatestBlock'; +import calculateGasFeeEstimatesForPriorityLevels from './fetchGasEstimatesViaEthFeeHistory/calculateGasFeeEstimatesForPriorityLevels'; /** * Generates gas fee estimates based on gas fees that have been used in the recent past so that @@ -22,6 +17,9 @@ import fetchLatestBlock from './fetchGasEstimatesViaEthFeeHistory/fetchLatestBlo * calculate reasonable max priority and max fees for three different priority levels (higher * priority = higher fee). * + * Note that properties are returned for other data that are normally obtained via the API; however, + * to prevent extra requests to Infura, these properties are empty. + * * @param ethQuery - An EthQuery instance. * @returns Base and priority fee estimates, categorized by priority level, as well as an estimate * for the next block's base fee. @@ -30,39 +28,26 @@ export default async function fetchGasEstimatesViaEthFeeHistory( ethQuery: EthQuery, ): Promise { const latestBlock = await fetchLatestBlock(ethQuery); - const fetcher = new BlockFeeHistoryDatasetFetcher({ + const blocks = await fetchBlockFeeHistory({ ethQuery, - endBlockNumber: latestBlock.number, + endBlock: latestBlock.number, + numberOfBlocks: 5, + percentiles: [10, 20, 30], }); - const blocksByDataset = await fetcher.forAll(); + const estimatedBaseFee = fromWei(latestBlock.baseFeePerGas, GWEI); const levelSpecificEstimates = calculateGasFeeEstimatesForPriorityLevels( - blocksByDataset.smallRange, - ); - const estimatedBaseFee = fromWei(latestBlock.baseFeePerGas, GWEI); - const historicalBaseFeeRange = calculateBaseFeeRange( - blocksByDataset.mediumRange, - ); - const baseFeeTrend = calculateBaseFeeTrend( - blocksByDataset.latestWithNextBlock, - ); - const latestPriorityFeeRange = calculatePriorityFeeRange( - blocksByDataset.latest, - ); - const historicalPriorityFeeRange = calculatePriorityFeeRange( - blocksByDataset.mediumRange, + blocks, ); - const priorityFeeTrend = calculatePriorityFeeTrend(blocksByDataset.tinyRange); - const networkCongestion = calculateNetworkCongestion([]); return { ...levelSpecificEstimates, estimatedBaseFee, - historicalBaseFeeRange, - baseFeeTrend, - latestPriorityFeeRange, - historicalPriorityFeeRange, - priorityFeeTrend, - networkCongestion, + historicalBaseFeeRange: null, + baseFeeTrend: null, + latestPriorityFeeRange: null, + historicalPriorityFeeRange: null, + priorityFeeTrend: null, + networkCongestion: null, }; } diff --git a/src/gas/fetchGasEstimatesViaEthFeeHistory/BlockFeeHistoryDatasetFetcher.test.ts b/src/gas/fetchGasEstimatesViaEthFeeHistory/BlockFeeHistoryDatasetFetcher.test.ts deleted file mode 100644 index 4864dc6ba48..00000000000 --- a/src/gas/fetchGasEstimatesViaEthFeeHistory/BlockFeeHistoryDatasetFetcher.test.ts +++ /dev/null @@ -1,113 +0,0 @@ -import { BN } from 'ethereumjs-util'; -import { mocked } from 'ts-jest/utils'; -import { when } from 'jest-when'; -import fetchBlockFeeHistory, { FeeHistoryBlock } from '../fetchBlockFeeHistory'; -import { EthQuery } from './types'; -import BlockFeeHistoryDatasetFetcher from './BlockFeeHistoryDatasetFetcher'; - -jest.mock('../fetchBlockFeeHistory'); - -const mockedFetchBlockFeeHistory = mocked(fetchBlockFeeHistory, true); - -describe('BlockFeeHistoryDatasetFetcher', () => { - let fakeEthQuery: EthQuery; - let fakeEndBlockNumber: BN; - let fakeBlocks: FeeHistoryBlock[]; - - beforeEach(() => { - fakeEthQuery = { - async getBlockByNumber() { - return { number: new BN(1), baseFeePerGas: new BN(1) }; - }, - }; - fakeEndBlockNumber = new BN(1); - fakeBlocks = [ - { - number: new BN(1), - baseFeePerGas: new BN(1), - gasUsedRatio: 1, - }, - ]; - }); - - describe('forMediumRange', () => { - it('returns 200 blocks along with the 10th and 95th reward percentiles (excluding the next block)', async () => { - when(mockedFetchBlockFeeHistory) - .calledWith({ - ethQuery: fakeEthQuery, - endBlock: fakeEndBlockNumber, - numberOfBlocks: 200, - percentiles: [10, 95], - }) - .mockResolvedValue(fakeBlocks); - - const fetcher = new BlockFeeHistoryDatasetFetcher({ - ethQuery: fakeEthQuery, - endBlockNumber: fakeEndBlockNumber, - }); - - expect(await fetcher.forMediumRange()).toStrictEqual(fakeBlocks); - }); - }); - - describe('forSmallRange', () => { - it('returns 5 blocks along with the 10th, 20th, and 30th reward percentiles (excluding the next block)', async () => { - when(mockedFetchBlockFeeHistory) - .calledWith({ - ethQuery: fakeEthQuery, - endBlock: fakeEndBlockNumber, - numberOfBlocks: 5, - percentiles: [10, 20, 30], - }) - .mockResolvedValue(fakeBlocks); - - const fetcher = new BlockFeeHistoryDatasetFetcher({ - ethQuery: fakeEthQuery, - endBlockNumber: fakeEndBlockNumber, - }); - - expect(await fetcher.forSmallRange()).toStrictEqual(fakeBlocks); - }); - }); - - describe('forTinyRange', () => { - it('returns 2 blocks along with the 50th reward percentiles (excluding the next block)', async () => { - when(mockedFetchBlockFeeHistory) - .calledWith({ - ethQuery: fakeEthQuery, - endBlock: fakeEndBlockNumber, - numberOfBlocks: 2, - percentiles: [50], - }) - .mockResolvedValue(fakeBlocks); - - const fetcher = new BlockFeeHistoryDatasetFetcher({ - ethQuery: fakeEthQuery, - endBlockNumber: fakeEndBlockNumber, - }); - - expect(await fetcher.forTinyRange()).toStrictEqual(fakeBlocks); - }); - }); - - describe('forLatestWithNextBlock', () => { - it('returns 1 block along with the 10th and 90th reward percentiles (including the next block)', async () => { - when(mockedFetchBlockFeeHistory) - .calledWith({ - ethQuery: fakeEthQuery, - endBlock: fakeEndBlockNumber, - numberOfBlocks: 1, - includeNextBlock: true, - percentiles: [10, 95], - }) - .mockResolvedValue(fakeBlocks); - - const fetcher = new BlockFeeHistoryDatasetFetcher({ - ethQuery: fakeEthQuery, - endBlockNumber: fakeEndBlockNumber, - }); - - expect(await fetcher.forLatestWithNextBlock()).toStrictEqual(fakeBlocks); - }); - }); -}); diff --git a/src/gas/fetchGasEstimatesViaEthFeeHistory/BlockFeeHistoryDatasetFetcher.ts b/src/gas/fetchGasEstimatesViaEthFeeHistory/BlockFeeHistoryDatasetFetcher.ts deleted file mode 100644 index 4d703f55f7b..00000000000 --- a/src/gas/fetchGasEstimatesViaEthFeeHistory/BlockFeeHistoryDatasetFetcher.ts +++ /dev/null @@ -1,103 +0,0 @@ -import { BN } from 'ethereumjs-util'; -import fetchBlockFeeHistory, { - ExistingFeeHistoryBlock, - ExtractPercentileFrom, - FeeHistoryBlock, -} from '../fetchBlockFeeHistory'; -import { EthQuery } from './types'; - -export default class BlockFeeHistoryDatasetFetcher { - private ethQuery: EthQuery; - - private endBlockNumber: BN; - - constructor({ - ethQuery, - endBlockNumber, - }: { - ethQuery: EthQuery; - endBlockNumber: BN; - }) { - this.ethQuery = ethQuery; - this.endBlockNumber = endBlockNumber; - } - - async forAll() { - const [ - mediumRange, - smallRange, - tinyRange, - latestWithNextBlock, - ] = await Promise.all([ - this.forMediumRange(), - this.forSmallRange(), - this.forTinyRange(), - this.forLatestWithNextBlock(), - ]); - - const latest = latestWithNextBlock.slice(0, -1) as ExistingFeeHistoryBlock< - ExtractPercentileFrom - >[]; - - return { - mediumRange, - smallRange, - tinyRange, - latest, - latestWithNextBlock, - }; - } - - forMediumRange() { - return this.fetchExcludingNextBlock({ - numberOfBlocks: 200, - percentiles: [10, 95], - }); - } - - forSmallRange() { - return this.fetchExcludingNextBlock({ - numberOfBlocks: 5, - percentiles: [10, 20, 30], - }); - } - - forTinyRange() { - return this.fetchExcludingNextBlock({ - numberOfBlocks: 2, - percentiles: [50], - }); - } - - forLatestWithNextBlock() { - return this.fetchIncludingNextBlock({ - numberOfBlocks: 1, - percentiles: [10, 95], - }); - } - - 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; - percentiles?: T[]; - }): Promise[]> { - return fetchBlockFeeHistory({ - ethQuery: this.ethQuery, - endBlock: this.endBlockNumber, - includeNextBlock: true, - ...args, - }); - } -} diff --git a/src/gas/fetchGasEstimatesViaEthFeeHistory/calculateBaseFeeRange.test.ts b/src/gas/fetchGasEstimatesViaEthFeeHistory/calculateBaseFeeRange.test.ts deleted file mode 100644 index d8d44c894d2..00000000000 --- a/src/gas/fetchGasEstimatesViaEthFeeHistory/calculateBaseFeeRange.test.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { BN } from 'ethereumjs-util'; -import calculateBaseFeeRange from './calculateBaseFeeRange'; - -describe('calculateBaseFeeRange', () => { - it('returns the min and max of base fees across the given blocks', () => { - const baseFeeRange = calculateBaseFeeRange([ - { - number: new BN(1), - baseFeePerGas: new BN(300_000_000_000), - gasUsedRatio: 1, - priorityFeesByPercentile: {}, - }, - { - number: new BN(2), - baseFeePerGas: new BN(100_000_000_000), - gasUsedRatio: 1, - priorityFeesByPercentile: {}, - }, - { - number: new BN(3), - baseFeePerGas: new BN(200_000_000_000), - gasUsedRatio: 1, - priorityFeesByPercentile: {}, - }, - ]); - - expect(baseFeeRange).toStrictEqual(['100', '300']); - }); -}); diff --git a/src/gas/fetchGasEstimatesViaEthFeeHistory/calculateBaseFeeRange.ts b/src/gas/fetchGasEstimatesViaEthFeeHistory/calculateBaseFeeRange.ts deleted file mode 100644 index 4fc6465853f..00000000000 --- a/src/gas/fetchGasEstimatesViaEthFeeHistory/calculateBaseFeeRange.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { fromWei } from 'ethjs-unit'; -import { GWEI } from '../../constants'; -import { FeeHistoryBlock } from '../fetchBlockFeeHistory'; -import { FeeRange } from './types'; - -/** - * Calculates reasonable minimum and maximum values for base fees over the last 200 blocks. - * - * @param blocks - A set of blocks obtained via {@link BlockFeeHistoryDatasetFetcher}. - * @returns The ranges. - */ -export default function calculateBaseFeeRange( - blocks: FeeHistoryBlock[], -): FeeRange { - const sortedBaseFeesPerGas = blocks - .map((block) => block.baseFeePerGas) - .sort((a, b) => a.cmp(b)); - - return [ - fromWei(sortedBaseFeesPerGas[0], GWEI), - fromWei(sortedBaseFeesPerGas[sortedBaseFeesPerGas.length - 1], GWEI), - ]; -} diff --git a/src/gas/fetchGasEstimatesViaEthFeeHistory/calculateBaseFeeTrend.test.ts b/src/gas/fetchGasEstimatesViaEthFeeHistory/calculateBaseFeeTrend.test.ts deleted file mode 100644 index db831db2c38..00000000000 --- a/src/gas/fetchGasEstimatesViaEthFeeHistory/calculateBaseFeeTrend.test.ts +++ /dev/null @@ -1,79 +0,0 @@ -import { BN } from 'ethereumjs-util'; -import calculateBaseFeeTrend from './calculateBaseFeeTrend'; - -describe('calculateBaseFeeTrend', () => { - it('returns "up" if the base fee of the last block is greater than the first', () => { - const baseFeeTrend = calculateBaseFeeTrend([ - { - number: new BN(1), - baseFeePerGas: new BN(100_000_000_000), - gasUsedRatio: 1, - priorityFeesByPercentile: {}, - }, - { - number: new BN(1), - baseFeePerGas: new BN(95_000_000_000), - gasUsedRatio: 1, - priorityFeesByPercentile: {}, - }, - { - number: new BN(1), - baseFeePerGas: new BN(110_000_000_000), - gasUsedRatio: 1, - priorityFeesByPercentile: {}, - }, - ]); - - expect(baseFeeTrend).toStrictEqual('up'); - }); - - it('returns "down" if the base fee of the last block is less than the first', () => { - const baseFeeTrend = calculateBaseFeeTrend([ - { - number: new BN(1), - baseFeePerGas: new BN(100_000_000_000), - gasUsedRatio: 1, - priorityFeesByPercentile: {}, - }, - { - number: new BN(1), - baseFeePerGas: new BN(110_000_000_000), - gasUsedRatio: 1, - priorityFeesByPercentile: {}, - }, - { - number: new BN(1), - baseFeePerGas: new BN(95_000_000_000), - gasUsedRatio: 1, - priorityFeesByPercentile: {}, - }, - ]); - - expect(baseFeeTrend).toStrictEqual('down'); - }); - - it('returns "level" if the base fee of the last block is the same as the first', () => { - const baseFeeTrend = calculateBaseFeeTrend([ - { - number: new BN(1), - baseFeePerGas: new BN(100_000_000_000), - gasUsedRatio: 1, - priorityFeesByPercentile: {}, - }, - { - number: new BN(1), - baseFeePerGas: new BN(110_000_000_000), - gasUsedRatio: 1, - priorityFeesByPercentile: {}, - }, - { - number: new BN(1), - baseFeePerGas: new BN(100_000_000_000), - gasUsedRatio: 1, - priorityFeesByPercentile: {}, - }, - ]); - - expect(baseFeeTrend).toStrictEqual('level'); - }); -}); diff --git a/src/gas/fetchGasEstimatesViaEthFeeHistory/calculateBaseFeeTrend.ts b/src/gas/fetchGasEstimatesViaEthFeeHistory/calculateBaseFeeTrend.ts deleted file mode 100644 index 96122996d5b..00000000000 --- a/src/gas/fetchGasEstimatesViaEthFeeHistory/calculateBaseFeeTrend.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { FeeHistoryBlock } from '../fetchBlockFeeHistory'; - -/** - * Given a collection of blocks, returns an indicator of whether the base fee is moving up, down, or - * holding steady, based on comparing the last base fee in the collection to the first. - * - * @param blocks - A set of blocks obtained via {@link BlockFeeHistoryDatasetFetcher}. - * @returns The indicator ("up", "down", or "level"). - */ -export default function calculateBaseFeeTrend( - blocks: FeeHistoryBlock[], -) { - const baseFeesPerGas = blocks.map((block) => block.baseFeePerGas); - const first = baseFeesPerGas[0]; - const last = baseFeesPerGas[baseFeesPerGas.length - 1]; - - if (last.gt(first)) { - return 'up'; - } else if (first.gt(last)) { - return 'down'; - } - return 'level'; -} diff --git a/src/gas/fetchGasEstimatesViaEthFeeHistory/calculateGasFeeEstimatesForPriorityLevels.ts b/src/gas/fetchGasEstimatesViaEthFeeHistory/calculateGasFeeEstimatesForPriorityLevels.ts index 5f660d081e6..7eedae2c2cf 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 { ExistingFeeHistoryBlock } from '../fetchBlockFeeHistory'; +import { FeeHistoryBlock } from '../fetchBlockFeeHistory'; import { GWEI } from '../../constants'; import medianOf from './medianOf'; @@ -54,7 +54,7 @@ const SETTINGS_BY_PRIORITY_LEVEL = { */ function calculateEstimatesForPriorityLevel( priorityLevel: PriorityLevel, - blocks: ExistingFeeHistoryBlock[], + blocks: FeeHistoryBlock[], ): Eip1559GasFee { const settings = SETTINGS_BY_PRIORITY_LEVEL[priorityLevel]; @@ -63,9 +63,13 @@ function calculateEstimatesForPriorityLevel( const adjustedBaseFee = latestBaseFeePerGas .mul(settings.baseFeePercentageMultiplier) .divn(100); - const priorityFees = blocks.map((block) => { - return block.priorityFeesByPercentile[settings.percentile]; - }); + const priorityFees = blocks + .map((block) => { + return 'priorityFeesByPercentile' in block + ? block.priorityFeesByPercentile[settings.percentile] + : null; + }) + .filter(BN.isBN); const medianPriorityFee = medianOf(priorityFees); const adjustedPriorityFee = medianPriorityFee .mul(settings.priorityFeePercentageMultiplier) @@ -95,7 +99,7 @@ function calculateEstimatesForPriorityLevel( * @returns The estimates. */ export default function calculateGasFeeEstimatesForPriorityLevels( - blocks: ExistingFeeHistoryBlock[], + blocks: FeeHistoryBlock[], ): Pick { return PRIORITY_LEVELS.reduce((obj, priorityLevel) => { const gasEstimatesForPriorityLevel = calculateEstimatesForPriorityLevel( diff --git a/src/gas/fetchGasEstimatesViaEthFeeHistory/calculateNetworkCongestion.test.ts b/src/gas/fetchGasEstimatesViaEthFeeHistory/calculateNetworkCongestion.test.ts deleted file mode 100644 index 8d320e7c66e..00000000000 --- a/src/gas/fetchGasEstimatesViaEthFeeHistory/calculateNetworkCongestion.test.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { BN } from 'ethereumjs-util'; -import calculateNetworkCongestion from './calculateNetworkCongestion'; - -describe('calculateNetworkCongestion', () => { - it('returns a number between 0 and 1 based on the base fee of the last given block vs. the base fees across all blocks', () => { - const networkCongestion = calculateNetworkCongestion([ - { - number: new BN(1), - baseFeePerGas: new BN(300_000_000_000), - gasUsedRatio: 1, - priorityFeesByPercentile: {}, - }, - { - number: new BN(2), - baseFeePerGas: new BN(100_000_000_000), - gasUsedRatio: 1, - priorityFeesByPercentile: {}, - }, - { - number: new BN(3), - baseFeePerGas: new BN(200_000_000_000), - gasUsedRatio: 1, - priorityFeesByPercentile: {}, - }, - ]); - expect(networkCongestion).toStrictEqual(0.5); - }); - - it('returns 0.5 when given an empty array', () => { - const networkCongestion = calculateNetworkCongestion([]); - expect(networkCongestion).toStrictEqual(0.5); - }); -}); diff --git a/src/gas/fetchGasEstimatesViaEthFeeHistory/calculateNetworkCongestion.ts b/src/gas/fetchGasEstimatesViaEthFeeHistory/calculateNetworkCongestion.ts deleted file mode 100644 index 3f16cf12648..00000000000 --- a/src/gas/fetchGasEstimatesViaEthFeeHistory/calculateNetworkCongestion.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { FeeHistoryBlock } from '../fetchBlockFeeHistory'; - -/** - * Calculates the approximate normalized ranking of the latest base fee in the given blocks among - * the entirety of the blocks. That is, sorts all of the base fees, then finds the rank of the first - * base fee that meets or exceeds the latest base fee among the base fees. The result is the rank - * normalized as a number between 0 and 1, where 0 means that the latest base fee is the least of - * all and 1 means that the latest base fee is the greatest of all. This can ultimately be used to - * render a visualization of the status of the network for users. - * - * @param blocks - A set of blocks obtained via {@link BlockFeeHistoryDatasetFetcher}. - * @returns A number between 0 and 1. - */ -export default function fetchNetworkCongestionLevel( - blocks: FeeHistoryBlock[], -): number { - if (blocks.length > 0) { - const latestBaseFeePerGas = blocks[blocks.length - 1].baseFeePerGas; - const sortedBaseFeesPerGas = blocks - .map((block) => block.baseFeePerGas) - .sort((a, b) => a.cmp(b)); - const indexOfBaseFeeNearestToLatest = sortedBaseFeesPerGas.findIndex( - (baseFeePerGas) => baseFeePerGas.gte(latestBaseFeePerGas), - ); - return indexOfBaseFeeNearestToLatest !== -1 - ? indexOfBaseFeeNearestToLatest / (blocks.length - 1) - : 0; - } - return 0.5; -} diff --git a/src/gas/fetchGasEstimatesViaEthFeeHistory/calculatePriorityFeeRange.test.ts b/src/gas/fetchGasEstimatesViaEthFeeHistory/calculatePriorityFeeRange.test.ts deleted file mode 100644 index 6bdf7dcffba..00000000000 --- a/src/gas/fetchGasEstimatesViaEthFeeHistory/calculatePriorityFeeRange.test.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { BN } from 'ethereumjs-util'; -import calculatePriorityFeeRange from './calculatePriorityFeeRange'; - -describe('calculatePriorityFeeRange', () => { - it('returns the min and max of priority fees across the given blocks (omitting 0)', () => { - const blocks = [ - { - number: new BN(1), - baseFeePerGas: new BN(1), - gasUsedRatio: 1, - priorityFeesByPercentile: { - 10: new BN(0), - 95: new BN(1_000_000_000), - }, - }, - { - number: new BN(2), - baseFeePerGas: new BN(1), - gasUsedRatio: 1, - priorityFeesByPercentile: { - 10: new BN(500_000_000), - 95: new BN(1_600_000_000), - }, - }, - { - number: new BN(3), - baseFeePerGas: new BN(1), - gasUsedRatio: 1, - priorityFeesByPercentile: { - 10: new BN(500_000_000), - 95: new BN(2_000_000_000), - }, - }, - ]; - - const priorityFeeRange = calculatePriorityFeeRange(blocks); - - expect(priorityFeeRange).toStrictEqual(['0.5', '2']); - }); -}); diff --git a/src/gas/fetchGasEstimatesViaEthFeeHistory/calculatePriorityFeeRange.ts b/src/gas/fetchGasEstimatesViaEthFeeHistory/calculatePriorityFeeRange.ts deleted file mode 100644 index 8e702c1d35b..00000000000 --- a/src/gas/fetchGasEstimatesViaEthFeeHistory/calculatePriorityFeeRange.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { BN } from 'ethereumjs-util'; -import { fromWei } from 'ethjs-unit'; -import { GWEI } from '../../constants'; -import { ExistingFeeHistoryBlock } from '../fetchBlockFeeHistory'; -import { FeeRange } from './types'; - -/** - * Calculates reasonable minimum and maximum values for priority fees over the last 200 blocks. - * Although some priority fees may be 0, these are discarded as they are not useful for suggestion - * purposes. - * - * @param blocks - A set of blocks populated with data for priority fee percentiles 10 and 95, - * obtained via {@link BlockFeeHistoryDatasetFetcher}. - * @returns The range. - */ -export default function calculatePriorityFeeRange( - blocks: ExistingFeeHistoryBlock<10 | 95>[], -): FeeRange { - const sortedLowPriorityFees = blocks - .map((block) => block.priorityFeesByPercentile[10]) - .filter((priorityFee) => !priorityFee.eq(new BN(0))) - .sort((a, b) => a.cmp(b)); - - const sortedHighPriorityFees = blocks - .map((block) => block.priorityFeesByPercentile[95]) - .sort((a, b) => a.cmp(b)); - - return [ - fromWei(sortedLowPriorityFees[0], GWEI), - fromWei(sortedHighPriorityFees[sortedHighPriorityFees.length - 1], GWEI), - ]; -} diff --git a/src/gas/fetchGasEstimatesViaEthFeeHistory/calculatePriorityFeeTrend.test.ts b/src/gas/fetchGasEstimatesViaEthFeeHistory/calculatePriorityFeeTrend.test.ts deleted file mode 100644 index 93ff6cd59d8..00000000000 --- a/src/gas/fetchGasEstimatesViaEthFeeHistory/calculatePriorityFeeTrend.test.ts +++ /dev/null @@ -1,97 +0,0 @@ -import { BN } from 'ethereumjs-util'; -import calculatePriorityFeeTrend from './calculatePriorityFeeTrend'; - -describe('calculatePriorityFeeTrend', () => { - it('returns "up" if the last priority fee at the 50th percentile among the blocks is greater than the first', () => { - const priorityFeeTrend = calculatePriorityFeeTrend([ - { - number: new BN(1), - baseFeePerGas: new BN(1), - gasUsedRatio: 1, - priorityFeesByPercentile: { - 50: new BN(1_000_000_000), - }, - }, - { - number: new BN(1), - baseFeePerGas: new BN(1), - gasUsedRatio: 1, - priorityFeesByPercentile: { - 50: new BN(900_000_000), - }, - }, - { - number: new BN(1), - baseFeePerGas: new BN(1), - gasUsedRatio: 1, - priorityFeesByPercentile: { - 50: new BN(1_100_000_000), - }, - }, - ]); - - expect(priorityFeeTrend).toStrictEqual('up'); - }); - - it('returns "down" if the last priority fee at the 50th percentile among the blocks is less than the first', () => { - const priorityFeeTrend = calculatePriorityFeeTrend([ - { - number: new BN(1), - baseFeePerGas: new BN(1), - gasUsedRatio: 1, - priorityFeesByPercentile: { - 50: new BN(1_000_000_000), - }, - }, - { - number: new BN(1), - baseFeePerGas: new BN(1), - gasUsedRatio: 1, - priorityFeesByPercentile: { - 50: new BN(1_100_000_000), - }, - }, - { - number: new BN(1), - baseFeePerGas: new BN(1), - gasUsedRatio: 1, - priorityFeesByPercentile: { - 50: new BN(900_000_000), - }, - }, - ]); - - expect(priorityFeeTrend).toStrictEqual('down'); - }); - - it('returns "level" if the last priority fee at the 50th percentile among the blocks is the same as the first', () => { - const priorityFeeTrend = calculatePriorityFeeTrend([ - { - number: new BN(1), - baseFeePerGas: new BN(1), - gasUsedRatio: 1, - priorityFeesByPercentile: { - 50: new BN(1_000_000_000), - }, - }, - { - number: new BN(1), - baseFeePerGas: new BN(1), - gasUsedRatio: 1, - priorityFeesByPercentile: { - 50: new BN(1_100_000_000), - }, - }, - { - number: new BN(1), - baseFeePerGas: new BN(1), - gasUsedRatio: 1, - priorityFeesByPercentile: { - 50: new BN(1_000_000_000), - }, - }, - ]); - - expect(priorityFeeTrend).toStrictEqual('level'); - }); -}); diff --git a/src/gas/fetchGasEstimatesViaEthFeeHistory/calculatePriorityFeeTrend.ts b/src/gas/fetchGasEstimatesViaEthFeeHistory/calculatePriorityFeeTrend.ts deleted file mode 100644 index f7611398fa5..00000000000 --- a/src/gas/fetchGasEstimatesViaEthFeeHistory/calculatePriorityFeeTrend.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { BN } from 'ethereumjs-util'; -import { ExistingFeeHistoryBlock } from '../fetchBlockFeeHistory'; - -/** - * Given a collection of blocks, returns an indicator of whether the base fee is moving up, down, or - * holding steady, based on comparing the last base fee in the collection to the first. - * - * @param blocks - A set of blocks obtained via {@link BlockFeeHistoryDatasetFetcher}. - * @returns The indicator ("up", "down", or "level"). - */ -export default function calculatePriorityFeeTrend( - blocks: ExistingFeeHistoryBlock<50>[], -) { - const priorityFees = blocks - .map((block) => block.priorityFeesByPercentile[50]) - .filter((priorityFee) => !priorityFee.eq(new BN(0))); - const first = priorityFees[0]; - const last = priorityFees[priorityFees.length - 1]; - - if (last.gt(first)) { - return 'up'; - } else if (first.gt(last)) { - return 'down'; - } - return 'level'; -}