-
-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend GasFeeController to poll for network status
When a user is about to send a transaction or is looking at a swap quote, we would like to inform them if the network is busy so that we can push them to use a lower fee setting. The GasFeeController already provides a way to poll for fee estimates, which we employ on transaction preview screens. This commit updates the polling code so that we also gauge network status as we pull estimates. This is done by hitting another endpoint in the MetaSwap API which specifically gives us a base fee threshold we can use to determine whether the network is busy (falling back to using `eth_feeHistory`).
- Loading branch information
Showing
10 changed files
with
492 additions
and
71 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { BN } from 'ethereumjs-util'; | ||
import { mocked } from 'ts-jest/utils'; | ||
import calculateBusyThreshold from './calculateBusyThreshold'; | ||
import fetchBlockFeeHistory from './fetchBlockFeeHistory'; | ||
|
||
jest.mock('./fetchBlockFeeHistory'); | ||
|
||
const mockedFetchFeeHistory = mocked(fetchBlockFeeHistory, true); | ||
|
||
describe('calculateBusyThreshold', () => { | ||
const ethQuery = {}; | ||
|
||
beforeEach(() => { | ||
const baseFeesPerGas = [ | ||
3_000_000_000, | ||
8_000_000_000, | ||
4_000_000_000, | ||
6_000_000_000, | ||
11_000_000_000, | ||
5_000_000_000, | ||
10_000_000_000, | ||
2_000_000_000, | ||
7_000_000_000, | ||
1_000_000_000, | ||
9_000_000_000, | ||
]; | ||
mockedFetchFeeHistory.mockResolvedValue( | ||
baseFeesPerGas.map((baseFeePerGas, i) => { | ||
return { | ||
number: new BN(i + 1), | ||
baseFeePerGas: new BN(baseFeePerGas), | ||
gasUsedRatio: 0, | ||
priorityFeesByPercentile: {}, | ||
}; | ||
}), | ||
); | ||
}); | ||
|
||
it('sorts the base fees returned by eth_feeHistory, then returns the base fee 9/10 of the way through the list', async () => { | ||
const busyThreshold = await calculateBusyThreshold(ethQuery); | ||
|
||
expect(busyThreshold).toStrictEqual(new BN(9_000_000_000)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { BN } from 'ethereumjs-util'; | ||
import fetchBlockFeeHistory from './fetchBlockFeeHistory'; | ||
|
||
type EthQuery = any; | ||
|
||
const NUMBER_OF_BLOCKS_TO_FETCH = 20_000; | ||
|
||
/** | ||
* Uses historical base fees to determine a threshold we can use to determine whether the network is | ||
* busy. Specifically, pulls the last 20,000 blocks (which at the time of this writing represents | ||
* around 2 days), sorts the base fees of those blocks, then chooses the base fee which is 9/10 of | ||
* the way into the list (i.e. the 90th percentile). | ||
* | ||
* @param ethQuery - An EthQuery instance. | ||
* @returns A promise for the 90th percentile base fee in WEI, as a BN. | ||
*/ | ||
export default async function calculateBusyThreshold( | ||
ethQuery: EthQuery, | ||
): Promise<BN> { | ||
const blocks = await fetchBlockFeeHistory({ | ||
ethQuery, | ||
numberOfBlocks: NUMBER_OF_BLOCKS_TO_FETCH, | ||
}); | ||
const sortedBaseFeesPerGas = blocks | ||
.map((block) => block.baseFeePerGas) | ||
.sort((a, b) => a.cmp(b)); | ||
const indexAtPercentile90 = Math.floor(sortedBaseFeesPerGas.length * 0.9) - 1; | ||
return sortedBaseFeesPerGas[indexAtPercentile90]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.