Skip to content

Commit

Permalink
chore: revert recent gas api endpoint changes (#25230)
Browse files Browse the repository at this point in the history
> [!NOTE]
> Once this PR is merged into develop, it will be cherry-picked into the
next 11.16 hotfix as well as v12.0.0.

We need to move quickly on a hotfix for the Extension due to a caching
issue with the [infura.io](http://infura.io/) endpoint. The [API team
has
asked](https://consensys.slack.com/archives/C05B78N1T9B/p1717783170599949)
us to revert a recent gas API endpoint change until the issue is
resolved, which could take months.

Here is what is in this PR:
- apply patch using core patch branch MetaMask/core/pull/4403
- revert #23717

The renaming of `chunk` files in the `dist` folder of the
gas-fee-controller are the cause of the large .patch file. For more
context, see this [slack
thread](https://consensys.slack.com/archives/CTQAGKY5V/p1718123930090259?thread_ts=1718123750.012709&cid=CTQAGKY5V).

Although `transaction-controller` and `user-operation-controller` depend
on v17, they can be safely ignored. The only runtime dependency those
packages have is on the [enum-like
GAS_ESTIMATE_TYPES](https://github.com/MetaMask/core/blob/dcc1d9291297ca2106cd2a461c51ce2f4667d84d/packages/gas-fee-controller/src/GasFeeController.ts#L61-L66),
([example1](https://github.com/MetaMask/core/blob/dcc1d9291297ca2106cd2a461c51ce2f4667d84d/packages/user-operation-controller/src/utils/gas-fees.ts#L224),
[example2](https://github.com/MetaMask/core/blob/dcc1d9291297ca2106cd2a461c51ce2f4667d84d/packages/transaction-controller/src/gas-flows/DefaultGasFeeFlow.ts#L41-L62))
which hasn't be touched in [3
years](https://github.com/MetaMask/core/pull/494/files#diff-ac8f6d6d8ff039810f56d99da8735d4eb8c2978eed2685b1741c9124c7b8bb6fR47-R52).

1. Revert MetaMask/core/pulls/4068 in core on top of the latest
gas-fee-controller (v17)
2. Upgrade gas-fee-controller in Extension and Mobile

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/25230?quickstart=1)

- Related: #25194

1. Go to this page...
2.
3.

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

<!-- [screenshots/recordings] -->

<!-- [screenshots/recordings] -->

- [ ] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.

---------

Co-authored-by: MetaMask Bot <metamaskbot@users.noreply.github.com>
  • Loading branch information
dbrans and metamaskbot committed Jun 13, 2024
1 parent ba5c428 commit 46057c3
Show file tree
Hide file tree
Showing 14 changed files with 2,718 additions and 78 deletions.
2,630 changes: 2,630 additions & 0 deletions .yarn/patches/@metamask-gas-fee-controller-npm-15.1.2-db4d2976aa.patch

Large diffs are not rendered by default.

13 changes: 11 additions & 2 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,11 @@ import { toChecksumHexAddress } from '../../shared/modules/hexstring-utils';
///: END:ONLY_INCLUDE_IF

import { AssetType, TokenStandard } from '../../shared/constants/transaction';
import { SWAPS_CLIENT_ID } from '../../shared/constants/swaps';
import {
GAS_API_BASE_URL,
GAS_DEV_API_BASE_URL,
SWAPS_CLIENT_ID,
} from '../../shared/constants/swaps';
import {
CHAIN_IDS,
NETWORK_TYPES,
Expand Down Expand Up @@ -781,6 +785,10 @@ export default class MetamaskController extends EventEmitter {
allowedEvents: ['NetworkController:stateChange'],
});

const gasApiBaseUrl = process.env.SWAPS_USE_DEV_APIS
? GAS_DEV_API_BASE_URL
: GAS_API_BASE_URL;

this.gasFeeController = new GasFeeController({
state: initState.GasFeeController,
interval: 10000,
Expand All @@ -800,12 +808,13 @@ export default class MetamaskController extends EventEmitter {
),
getCurrentAccountEIP1559Compatibility:
this.getCurrentAccountEIP1559Compatibility.bind(this),
legacyAPIEndpoint: `${gasApiBaseUrl}/networks/<chain_id>/gasPrices`,
EIP1559APIEndpoint: `${gasApiBaseUrl}/networks/<chain_id>/suggestedGasFees`,
getCurrentNetworkLegacyGasAPICompatibility: () => {
const { chainId } = this.networkController.state.providerConfig;
return chainId === CHAIN_IDS.BSC;
},
getChainId: () => this.networkController.state.providerConfig.chainId,
infuraAPIKey: opts.infuraProjectId,
});

this.appStateController = new AppStateController({
Expand Down
1 change: 0 additions & 1 deletion lavamoat/browserify/beta/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -1469,7 +1469,6 @@
"@metamask/eth-query": true,
"@metamask/gas-fee-controller>@metamask/controller-utils": true,
"bn.js": true,
"browserify>buffer": true,
"uuid": true
}
},
Expand Down
1 change: 0 additions & 1 deletion lavamoat/browserify/flask/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -1614,7 +1614,6 @@
"@metamask/eth-query": true,
"@metamask/gas-fee-controller>@metamask/controller-utils": true,
"bn.js": true,
"browserify>buffer": true,
"uuid": true
}
},
Expand Down
1 change: 0 additions & 1 deletion lavamoat/browserify/main/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -1469,7 +1469,6 @@
"@metamask/eth-query": true,
"@metamask/gas-fee-controller>@metamask/controller-utils": true,
"bn.js": true,
"browserify>buffer": true,
"uuid": true
}
},
Expand Down
1 change: 0 additions & 1 deletion lavamoat/browserify/mmi/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -1754,7 +1754,6 @@
"@metamask/eth-query": true,
"@metamask/gas-fee-controller>@metamask/controller-utils": true,
"bn.js": true,
"browserify>buffer": true,
"uuid": true
}
},
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@
"@expo/config/glob": "^10.3.10",
"@expo/config-plugins/glob": "^10.3.10",
"@metamask/network-controller": "patch:@metamask/network-controller@npm%3A18.1.2#~/.yarn/patches/@metamask-network-controller-npm-18.1.2-1bcb8d8610.patch",
"@solana/web3.js/rpc-websockets": "^8.0.1"
"@solana/web3.js/rpc-websockets": "^8.0.1",
"@metamask/gas-fee-controller@npm:^15.1.1": "patch:@metamask/gas-fee-controller@npm%3A15.1.2#~/.yarn/patches/@metamask-gas-fee-controller-npm-15.1.2-db4d2976aa.patch"
},
"dependencies": {
"@babel/runtime": "patch:@babel/runtime@npm%3A7.24.0#~/.yarn/patches/@babel-runtime-npm-7.24.0-7eb1dd11a2.patch",
Expand Down Expand Up @@ -305,7 +306,7 @@
"@metamask/ethjs": "^0.6.0",
"@metamask/ethjs-contract": "^0.4.1",
"@metamask/ethjs-query": "^0.7.1",
"@metamask/gas-fee-controller": "^15.1.2",
"@metamask/gas-fee-controller": "patch:@metamask/gas-fee-controller@npm%3A15.1.2#~/.yarn/patches/@metamask-gas-fee-controller-npm-15.1.2-db4d2976aa.patch",
"@metamask/jazzicon": "^2.0.0",
"@metamask/keyring-api": "^6.3.1",
"@metamask/keyring-controller": "patch:@metamask/keyring-controller@npm%3A15.0.0#~/.yarn/patches/@metamask-keyring-controller-npm-15.0.0-fa070ce311.patch",
Expand Down
2 changes: 1 addition & 1 deletion privacy-snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"etherscan.io",
"execution.metamask.io",
"fonts.gstatic.com",
"gas.api.infura.io",
"gas.api.cx.metamask.io",
"github.com",
"goerli.infura.io",
"localhost:8000",
Expand Down
2 changes: 1 addition & 1 deletion shared/constants/swaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ const SWAPS_TESTNET_CHAIN_ID = '0x539';
export const SWAPS_API_V2_BASE_URL = 'https://swap.api.cx.metamask.io';
export const SWAPS_DEV_API_V2_BASE_URL = 'https://swap.dev-api.cx.metamask.io';
export const TOKEN_API_BASE_URL = 'https://tokens.api.cx.metamask.io';
export const GAS_API_BASE_URL = 'https://gas.api.infura.io';
export const GAS_API_BASE_URL = 'https://gas.api.cx.metamask.io';
export const GAS_DEV_API_BASE_URL = 'https://gas.uat-api.cx.metamask.io';

const BSC_DEFAULT_BLOCK_EXPLORER_URL = 'https://bscscan.com/';
Expand Down
3 changes: 2 additions & 1 deletion shared/lib/swaps-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import log from 'loglevel';
import { CHAIN_IDS } from '../constants/network';
import {
GAS_API_BASE_URL,
GAS_DEV_API_BASE_URL,
SWAPS_API_V2_BASE_URL,
SWAPS_CHAINID_DEFAULT_TOKEN_MAP,
SWAPS_CLIENT_ID,
Expand Down Expand Up @@ -130,7 +131,7 @@ const getBaseUrlForNewSwapsApi = (type, chainId) => {
const v2ApiBaseUrl = useDevApis
? SWAPS_DEV_API_V2_BASE_URL
: SWAPS_API_V2_BASE_URL;
const gasApiBaseUrl = GAS_API_BASE_URL;
const gasApiBaseUrl = useDevApis ? GAS_DEV_API_BASE_URL : GAS_API_BASE_URL;
const tokenApiBaseUrl = TOKEN_API_BASE_URL;
const noNetworkSpecificTypes = ['refreshTime']; // These types don't need network info in the URL.
if (noNetworkSpecificTypes.includes(type)) {
Expand Down
97 changes: 45 additions & 52 deletions test/e2e/mock-e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,60 +183,18 @@ async function setupMocking(
};
});

const gasPricesCallbackMock = () => ({
statusCode: 200,
json: {
SafeGasPrice: '1',
ProposeGasPrice: '2',
FastGasPrice: '3',
},
});
const suggestedGasFeesCallbackMock = () => ({
statusCode: 200,
json: {
low: {
suggestedMaxPriorityFeePerGas: '1',
suggestedMaxFeePerGas: '20.44436136',
minWaitTimeEstimate: 15000,
maxWaitTimeEstimate: 30000,
},
medium: {
suggestedMaxPriorityFeePerGas: '1.5',
suggestedMaxFeePerGas: '25.80554517',
minWaitTimeEstimate: 15000,
maxWaitTimeEstimate: 45000,
},
high: {
suggestedMaxPriorityFeePerGas: '2',
suggestedMaxFeePerGas: '27.277766977',
minWaitTimeEstimate: 15000,
maxWaitTimeEstimate: 60000,
},
estimatedBaseFee: '19.444436136',
networkCongestion: 0.14685,
latestPriorityFeeRange: ['0.378818859', '6.555563864'],
historicalPriorityFeeRange: ['0.1', '248.262969261'],
historicalBaseFeeRange: ['14.146999781', '28.825256275'],
priorityFeeTrend: 'down',
baseFeeTrend: 'up',
},
});

await server
.forGet(`${GAS_API_BASE_URL}/networks/${chainId}/gasPrices`)
.thenCallback(gasPricesCallbackMock);

await server
.forGet(`${GAS_API_BASE_URL}/networks/1/gasPrices`)
.thenCallback(gasPricesCallbackMock);

await server
.forGet(`${GAS_API_BASE_URL}/networks/1/suggestedGasFees`)
.thenCallback(suggestedGasFeesCallbackMock);

await server
.forGet(`${GAS_API_BASE_URL}/networks/${chainId}/suggestedGasFees`)
.thenCallback(suggestedGasFeesCallbackMock);
.thenCallback(() => {
return {
statusCode: 200,
json: {
SafeGasPrice: '1',
ProposeGasPrice: '2',
FastGasPrice: '3',
},
};
});

await server
.forGet('https://swap.api.cx.metamask.io/networks/1/token')
Expand All @@ -255,6 +213,41 @@ async function setupMocking(
};
});

await server
.forGet(`${GAS_API_BASE_URL}/networks/${chainId}/suggestedGasFees`)
.thenCallback(() => {
return {
statusCode: 200,
json: {
low: {
suggestedMaxPriorityFeePerGas: '1',
suggestedMaxFeePerGas: '20.44436136',
minWaitTimeEstimate: 15000,
maxWaitTimeEstimate: 30000,
},
medium: {
suggestedMaxPriorityFeePerGas: '1.5',
suggestedMaxFeePerGas: '25.80554517',
minWaitTimeEstimate: 15000,
maxWaitTimeEstimate: 45000,
},
high: {
suggestedMaxPriorityFeePerGas: '2',
suggestedMaxFeePerGas: '27.277766977',
minWaitTimeEstimate: 15000,
maxWaitTimeEstimate: 60000,
},
estimatedBaseFee: '19.444436136',
networkCongestion: 0.14685,
latestPriorityFeeRange: ['0.378818859', '6.555563864'],
historicalPriorityFeeRange: ['0.1', '248.262969261'],
historicalBaseFeeRange: ['14.146999781', '28.825256275'],
priorityFeeTrend: 'down',
baseFeeTrend: 'up',
},
};
});

await server
.forGet('https://swap.api.cx.metamask.io/featureFlags')
.thenCallback(() => {
Expand Down
2 changes: 1 addition & 1 deletion test/jest/constants.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const METASWAP_BASE_URL = 'https://swap.api.cx.metamask.io';
export const GAS_API_URL = 'https://gas.api.infura.io';
export const GAS_API_URL = 'https://gas.api.cx.metamask.io';
13 changes: 1 addition & 12 deletions ui/pages/swaps/swaps.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ const CACHE_REFRESH_FIVE_MINUTES = 300000;
const USD_CURRENCY_CODE = 'usd';

const clientIdHeader = { 'X-Client-Id': SWAPS_CLIENT_ID };
const infuraAuthHeader = {
Authorization: `Basic ${Buffer.from(
`${process.env.INFURA_PROJECT_ID}:`,
).toString('base64')}`,
};

type Validator = {
property: string;
Expand Down Expand Up @@ -276,13 +271,7 @@ export async function fetchSwapsGasPrices(chainId: any): Promise<
const gasPricesUrl = getBaseApi('gasPrices', chainId);
const response = await fetchWithCache({
url: gasPricesUrl,
fetchOptions: {
method: 'GET',
headers: {
...clientIdHeader,
...infuraAuthHeader,
},
},
fetchOptions: { method: 'GET', headers: clientIdHeader },
cacheOptions: { cacheRefreshTime: 30000 },
functionName: 'fetchSwapsGasPrices',
});
Expand Down
25 changes: 23 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5408,7 +5408,7 @@ __metadata:
languageName: node
linkType: hard

"@metamask/gas-fee-controller@npm:^15.1.1, @metamask/gas-fee-controller@npm:^15.1.2":
"@metamask/gas-fee-controller@npm:15.1.2":
version: 15.1.2
resolution: "@metamask/gas-fee-controller@npm:15.1.2"
dependencies:
Expand Down Expand Up @@ -5450,6 +5450,27 @@ __metadata:
languageName: node
linkType: hard

"@metamask/gas-fee-controller@patch:@metamask/gas-fee-controller@npm%3A15.1.2#~/.yarn/patches/@metamask-gas-fee-controller-npm-15.1.2-db4d2976aa.patch":
version: 15.1.2
resolution: "@metamask/gas-fee-controller@patch:@metamask/gas-fee-controller@npm%3A15.1.2#~/.yarn/patches/@metamask-gas-fee-controller-npm-15.1.2-db4d2976aa.patch::version=15.1.2&hash=c5be0a"
dependencies:
"@metamask/base-controller": "npm:^5.0.2"
"@metamask/controller-utils": "npm:^9.1.0"
"@metamask/eth-query": "npm:^4.0.0"
"@metamask/ethjs-unit": "npm:^0.3.0"
"@metamask/network-controller": "npm:^18.1.0"
"@metamask/polling-controller": "npm:^6.0.2"
"@metamask/utils": "npm:^8.3.0"
"@types/bn.js": "npm:^5.1.5"
"@types/uuid": "npm:^8.3.0"
bn.js: "npm:^5.2.1"
uuid: "npm:^8.3.2"
peerDependencies:
"@metamask/network-controller": ^18.0.0
checksum: 10/4785ff26e541911ffc5a280a9e1a9a648fb3d89e672afeedcd4951a8f176d8e6ae91192afe56fa943f6b4eeff3ce0dba7ec05228a62a8fee1664bb82642fcc15
languageName: node
linkType: hard

"@metamask/jazzicon@npm:^2.0.0":
version: 2.0.0
resolution: "@metamask/jazzicon@npm:2.0.0"
Expand Down Expand Up @@ -24946,7 +24967,7 @@ __metadata:
"@metamask/ethjs-contract": "npm:^0.4.1"
"@metamask/ethjs-query": "npm:^0.7.1"
"@metamask/forwarder": "npm:^1.1.0"
"@metamask/gas-fee-controller": "npm:^15.1.2"
"@metamask/gas-fee-controller": "patch:@metamask/gas-fee-controller@npm%3A15.1.2#~/.yarn/patches/@metamask-gas-fee-controller-npm-15.1.2-db4d2976aa.patch"
"@metamask/jazzicon": "npm:^2.0.0"
"@metamask/keyring-api": "npm:^6.3.1"
"@metamask/keyring-controller": "patch:@metamask/keyring-controller@npm%3A15.0.0#~/.yarn/patches/@metamask-keyring-controller-npm-15.0.0-fa070ce311.patch"
Expand Down

0 comments on commit 46057c3

Please sign in to comment.