Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use eth_feeHistory as a fallback for gas estimates #614

Merged
merged 1 commit into from
Dec 2, 2021

Conversation

mcmire
Copy link
Contributor

@mcmire mcmire commented Oct 8, 2021

If we are on an EIP-1559-supported network and the Metaswap API fails
for some reason, fall back to using eth_feeHistory to calculate gas
estimates (which the API uses anyway). This code is more or less taken
from the code for the API (1).


Fixes #600.

@mcmire mcmire requested a review from a team as a code owner October 8, 2021 22:51
src/gas/fetchFeeHistory.ts Outdated Show resolved Hide resolved
src/gas/fetchGasEstimatesViaEthFeeHistory.test.ts Outdated Show resolved Hide resolved
src/gas/fetchGasEstimatesViaEthFeeHistory.ts Outdated Show resolved Hide resolved
@@ -20,50 +18,6 @@ const GAS_PRICE = 'gasPrice';
const FAIL = 'lol';
const PASS = '0x1';

const mockFlags: { [key: string]: any } = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure why these were here. I assume that they existed once to test out the query function, but we don't actually need a real EthQuery object to test that.

src/gas/fetchFeeHistory.ts Outdated Show resolved Hide resolved
src/gas/fetchFeeHistory.ts Outdated Show resolved Hide resolved
src/gas/fetchGasEstimatesViaEthFeeHistory.ts Outdated Show resolved Hide resolved
@mcmire mcmire force-pushed the add-eth-fee-history-fallback-for-gas-estimates branch from a886e92 to cbcf79f Compare October 11, 2021 16:21
@@ -69,7 +75,12 @@ export default async function determineGasFeeSuggestions({

if (isEIP1559Compatible) {
strategies.push(async () => {
const estimates = await fetchGasEstimates(fetchGasEstimatesUrl, clientId);
let estimates: GasFeeEstimates;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danjm Is this what you had in mind? i.e., do we still want to use eth_feeHistory as a fallback for the EIP-1559 Metaswap API, and then continue to use eth_gasPrice as an ultimate fallback whether EIP-1559 or not?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this looks right

if (typeof ethQuery[method] === 'function') {
ethQuery[method](...args, cb);
} else {
ethQuery.sendAsync({ method, params: args }, cb);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this need because ethQuery doesn't yet have explicit support for eth_feeHistory?

Copy link
Contributor Author

@mcmire mcmire Oct 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly. web3.js does, but eth-query does not. A bit annoying :/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose we could just use sendAsync directly in this case without "patching" eth-query, but eth-query's sendAsync doesn't return a promise, it only accepts a callback, which is another thing that this query function tries to paper over.

@mcmire mcmire force-pushed the add-eth-fee-history-fallback-for-gas-estimates branch from e7359cd to 5cef2af Compare October 26, 2021 18:17
@mcmire mcmire changed the base branch from refactor-determine-gas-fee-suggestions to extract-gas-fee-recommendations October 26, 2021 18:17
.eslintrc.js Outdated Show resolved Hide resolved
@mcmire mcmire force-pushed the extract-gas-fee-recommendations branch 2 times, most recently from 4d5a23b to ecac47e Compare October 28, 2021 20:04
@mcmire mcmire force-pushed the add-eth-fee-history-fallback-for-gas-estimates branch from 5cef2af to 883b2fe Compare October 28, 2021 20:15
@mcmire mcmire marked this pull request as draft November 1, 2021 16:48
@mcmire
Copy link
Contributor Author

mcmire commented Nov 1, 2021

Will update this with some new changes today, stay tuned.

@mcmire mcmire changed the title Use eth_feeHistory as a fallback for gas estimates [WIP] Use eth_feeHistory as a fallback for gas estimates Nov 1, 2021
@mcmire mcmire force-pushed the add-eth-fee-history-fallback-for-gas-estimates branch 2 times, most recently from 6873f34 to 709d386 Compare November 1, 2021 22:21
@mcmire mcmire changed the title [WIP] Use eth_feeHistory as a fallback for gas estimates Use eth_feeHistory as a fallback for gas estimates Nov 1, 2021
@mcmire mcmire marked this pull request as ready for review November 1, 2021 22:21
@mcmire mcmire force-pushed the add-eth-fee-history-fallback-for-gas-estimates branch from 709d386 to b4f23df Compare November 1, 2021 22:32
@@ -76,6 +76,7 @@
"@metamask/eslint-config-nodejs": "^9.0.0",
"@metamask/eslint-config-typescript": "^9.0.1",
"@types/jest": "^26.0.22",
"@types/jest-when": "^2.7.3",
Copy link
Contributor Author

@mcmire mcmire Nov 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jest-when lets you make assertions on different invocations of mock functions (e.g. let the return value be Y only when X is given). See the README for more: https://www.npmjs.com/package/jest-when

? await query(ethQuery, 'blockNumber')
: givenEndBlock;

const requestChunkSpecifiers = determineRequestChunkSpecifiers(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case the numberOfBlocks is more than 1024, this function will make multiple calls to eth_feeHistory in order to gather all of the desired blocks. This comes in handy later when we want to provide a fallback to Sam's "busy threshold" endpoint, which requires fetching 20,000 blocks.

type Percentile = typeof PRIORITY_LEVEL_PERCENTILES[number];

// This code is translated from the MetaSwap API:
// <https://gitlab.com/ConsenSys/codefi/products/metaswap/gas-api/-/blob/017436f628b2d5967f6e8734780a9114f9e58af9/src/eip1559/feeHistory.ts>
Copy link
Contributor Author

@mcmire mcmire Nov 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this file is not a direct translation of the MetaSwap API. I rearranged the code because it made more sense to me and was easier to follow when presented this way (also, fetchBlockFeeHistory returns data in a different format than eth_feeHistory).

@mcmire
Copy link
Contributor Author

mcmire commented Nov 1, 2021

This is now ready for review.

@mcmire mcmire force-pushed the extract-gas-fee-recommendations branch 2 times, most recently from fe524df to 809566b Compare November 30, 2021 23:23
@mcmire mcmire force-pushed the add-eth-fee-history-fallback-for-gas-estimates branch 2 times, most recently from 6c94a80 to 47e0fa0 Compare November 30, 2021 23:35
Base automatically changed from extract-gas-fee-recommendations to eip-1559-v2 December 1, 2021 19:13
@mcmire mcmire force-pushed the add-eth-fee-history-fallback-for-gas-estimates branch from 47e0fa0 to fbbc906 Compare December 2, 2021 00:29
If we are on an EIP-1559-supported network and the Metaswap API fails
for some reason, fall back to using `eth_feeHistory` to calculate gas
estimates (which the API uses anyway). This code is more or less taken
from the code for the API ([1]).

[1]: https://gitlab.com/ConsenSys/codefi/products/metaswap/gas-api/-/blob/eae6927b1a0c445e02cb3cba9e9e6b0f35857a12/src/eip1559/feeHistory.ts
@mcmire mcmire force-pushed the add-eth-fee-history-fallback-for-gas-estimates branch from fbbc906 to 46c3788 Compare December 2, 2021 00:32
Copy link
Contributor

@jpuri jpuri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 great work

@mcmire mcmire merged commit e1d1419 into eip-1559-v2 Dec 2, 2021
@mcmire mcmire deleted the add-eth-fee-history-fallback-for-gas-estimates branch December 2, 2021 16:57
mcmire added a commit that referenced this pull request Dec 10, 2021
If we are on an EIP-1559-supported network and the Metaswap API fails
for some reason, fall back to using `eth_feeHistory` to calculate gas
estimates (which the API uses anyway). This code is more or less taken
from the code for the API ([1]).

[1]: https://gitlab.com/ConsenSys/codefi/products/metaswap/gas-api/-/blob/eae6927b1a0c445e02cb3cba9e9e6b0f35857a12/src/eip1559/feeHistory.ts
MajorLift pushed a commit that referenced this pull request Oct 11, 2023
If we are on an EIP-1559-supported network and the Metaswap API fails
for some reason, fall back to using `eth_feeHistory` to calculate gas
estimates (which the API uses anyway). This code is more or less taken
from the code for the API ([1]).

[1]: https://gitlab.com/ConsenSys/codefi/products/metaswap/gas-api/-/blob/eae6927b1a0c445e02cb3cba9e9e6b0f35857a12/src/eip1559/feeHistory.ts
MajorLift pushed a commit that referenced this pull request Oct 11, 2023
If we are on an EIP-1559-supported network and the Metaswap API fails
for some reason, fall back to using `eth_feeHistory` to calculate gas
estimates (which the API uses anyway). This code is more or less taken
from the code for the API ([1]).

[1]: https://gitlab.com/ConsenSys/codefi/products/metaswap/gas-api/-/blob/eae6927b1a0c445e02cb3cba9e9e6b0f35857a12/src/eip1559/feeHistory.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants