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

[WIP] Extend GasFeeController to poll for network status #609

Closed
wants to merge 10 commits into from

Conversation

mcmire
Copy link
Contributor

@mcmire mcmire commented Sep 28, 2021

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. 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 if the API is not available).


References MetaMask/metamask-extension#11307.

@mcmire mcmire requested a review from a team as a code owner September 28, 2021 23:48
@mcmire mcmire marked this pull request as draft September 28, 2021 23:48
@mcmire mcmire force-pushed the add-network-congestion-gauge branch 3 times, most recently from d02f93f to 118fda6 Compare September 29, 2021 15:38
yarn.lock Outdated Show resolved Hide resolved
.eslintrc.js Outdated Show resolved Hide resolved
src/gas/GasFeeController.test.ts Show resolved Hide resolved
src/gas/GasFeeController.ts Show resolved Hide resolved
src/gas/GasFeeController.ts Outdated Show resolved Hide resolved
src/gas/GasFeeController.ts Outdated Show resolved Hide resolved
src/gas/fetchFeeHistory.ts Outdated Show resolved Hide resolved
src/gas/fetchFeeHistory.ts Outdated Show resolved Hide resolved
.eslintrc.js Outdated Show resolved Hide resolved
const currentAccountIsEIP1559Compatible =
this.getCurrentAccountEIP1559Compatibility?.() ?? true;
private async getEIP1559Compatibility(): Promise<boolean> {
try {
Copy link
Contributor Author

@mcmire mcmire Sep 29, 2021

Choose a reason for hiding this comment

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

This try/catch was copied from _fetchGasFeeEstimateData so that we could use the same behavior in poll.

src/gas/fetchFeeHistory.ts Outdated Show resolved Hide resolved
.eslintrc.js Outdated Show resolved Hide resolved
src/gas/GasFeeController.ts Outdated Show resolved Hide resolved
@mcmire
Copy link
Contributor Author

mcmire commented Oct 14, 2021

I'm going to divide this PR into two to make it easier to review. Stay tuned... This PR has been rebased on top of some other work so it should be simpler now.

The tests for GasFeeController are currently lacking in a few ways:

* `nock` is being used to test that requests are being used to obtain
  data; however, GasFeeController takes functions as arguments which can
  be used to stub out those requests, thereby removing the need for
  `nock`.
* The logic within `getGasFeeEstimatesAndStartPolling` is not being
  fully exercised, specifically the polling code.
* There are no tests for `disconnectPoller`.
* There are no explicit tests for `stopPolling`.
* The logic within `_fetchGasFeeEstimateData` is not being fully
  exercised, specifically with regard to  how the result of `getChainId`
  changes the URL that ends up being hit for both EIP-1559 and
  non-EIP-1559 flows.
* There are tests categorized under a `getChainId` describe block which
  are actually for `_fetchGasFeeEstimateData`.

This commit attempts to address these issues so that future changes to
GasFeeController do not break existing behavior.
We would like to introduce another fallback to how we estimate gas fees
in a future commit — namely, we want to use `eth_feeHistory` for
EIP-1559 networks if the usual API is down. To do this, the existing
code around gas fee estimates needs to be refactored. This is the first
in a two-part refactor, extracting the majority of the code in
`GasFeeController._fetchGasFeeEstimateData` to `gas-util`. In addition
to making room for more changes, this also allows us to test this code
in isolation from the polling code that already exists in
GasFeeController.

Note that I am using the term "suggestions" to encompass the data that
`_fetchGasFeeEstimateData` returns. This is because `gasFeeEstimates` is
an intermediate value that can also be packaged with
`estimatedGasFeeTimeBounds`, so "estimates" could be confusing. Also, I
replaced "fetch" with "determine" as in the future we may be performing
calculations that APIs have done for us in the past ("fetch" seems like
a more low-level action).
@mcmire mcmire changed the base branch from main to extract-gas-fee-recommendations October 28, 2021 20:13
@mcmire mcmire force-pushed the add-network-congestion-gauge branch from 7ce8597 to e655c35 Compare October 29, 2021 22:55
@mcmire mcmire changed the base branch from extract-gas-fee-recommendations to add-eth-fee-history-fallback-for-gas-estimates October 29, 2021 22:56
@mcmire mcmire changed the title [WIP] Support polling for network congestion level Extend GasFeeController to poll for network status Oct 29, 2021
@@ -0,0 +1,28 @@
import { BN } from 'ethereumjs-util';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mcmire mcmire marked this pull request as ready for review November 1, 2021 16:52
The tests for GasFeeController are currently lacking in a few ways:

* `nock` is being used to test that requests are being used to obtain
  data; however, GasFeeController takes functions as arguments which can
  be used to stub out those requests, thereby removing the need for
  `nock`.
* The logic within `getGasFeeEstimatesAndStartPolling` is not being
  fully exercised, specifically the polling code.
* There are no tests for `disconnectPoller`.
* There are no explicit tests for `stopPolling`.
* The logic within `_fetchGasFeeEstimateData` is not being fully
  exercised, specifically with regard to  how the result of `getChainId`
  changes the URL that ends up being hit for both EIP-1559 and
  non-EIP-1559 flows.
* There are tests categorized under a `getChainId` describe block which
  are actually for `_fetchGasFeeEstimateData`.

This commit attempts to address these issues so that future changes to
GasFeeController do not break existing behavior.
@mcmire mcmire force-pushed the add-eth-fee-history-fallback-for-gas-estimates branch from b864f9e to 6873f34 Compare November 1, 2021 22:10
@mcmire mcmire force-pushed the add-network-congestion-gauge branch from e655c35 to a741072 Compare November 1, 2021 22:12
The tests for GasFeeController are currently lacking in a few ways:

* `nock` is being used to test that requests are being used to obtain
  data; however, GasFeeController takes functions as arguments which can
  be used to stub out those requests, thereby removing the need for
  `nock`.
* The logic within `getGasFeeEstimatesAndStartPolling` is not being
  fully exercised, specifically the polling code.
* There are no tests for `disconnectPoller`.
* There are no explicit tests for `stopPolling`.
* The logic within `_fetchGasFeeEstimateData` is not being fully
  exercised, specifically with regard to  how the result of `getChainId`
  changes the URL that ends up being hit for both EIP-1559 and
  non-EIP-1559 flows.
* There are tests categorized under a `getChainId` describe block which
  are actually for `_fetchGasFeeEstimateData`.

This commit attempts to address these issues so that future changes to
GasFeeController do not break existing behavior.
@mcmire mcmire force-pushed the add-eth-fee-history-fallback-for-gas-estimates branch from 6873f34 to 709d386 Compare November 1, 2021 22:21
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 709d386 to b4f23df Compare November 1, 2021 22:32
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. 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` if the API is not available).
@mcmire mcmire force-pushed the add-network-congestion-gauge branch 2 times, most recently from 5e18a39 to 888b63d Compare November 3, 2021 20:31
@mcmire
Copy link
Contributor Author

mcmire commented Nov 16, 2021

This may end up being obsoleted by #632. I'll leave this open until that's merged.

@mcmire mcmire marked this pull request as draft November 23, 2021 17:54
@mcmire mcmire changed the title Extend GasFeeController to poll for network status [WIP] Extend GasFeeController to poll for network status Nov 23, 2021
@mcmire mcmire force-pushed the add-eth-fee-history-fallback-for-gas-estimates branch 4 times, most recently from fbbc906 to 46c3788 Compare December 2, 2021 00:32
Base automatically changed from add-eth-fee-history-fallback-for-gas-estimates to eip-1559-v2 December 2, 2021 16:57
@mcmire
Copy link
Contributor Author

mcmire commented Dec 10, 2021

Closing since we can now use networkCongestion to gauge busyness.

@mcmire mcmire closed this Dec 10, 2021
sambacha added a commit to sambacha/gas-reporting that referenced this pull request Dec 21, 2021
## network Congestion weight
this adds the new field used by metamask for network congestion

### metamask references
https://github.com/MetaMask/controllers/pull/632/files
MetaMask/core#609
MetaMask/metamask-extension#13044
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.

5 participants