1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 09:57:02 +01:00

Use legacy gas API for BSC (#19763)

The legacy gas API is still useful for BSC, which is a network our APIs
support that is not EIP-1559 compatible. The legacy gas API will now be
used for BSC prior to using RPC methods as a fallback.

This brings extension closer in alignment with mobile, which also uses
the legacy gas API for BSC.

The E2E network mock function has been updated to use a variable for
the initial test network. This made it easier to write the e2e test for
the BSC case.
This commit is contained in:
Mark Stacey 2023-07-10 14:09:39 -02:30 committed by GitHub
parent 9c278c3610
commit 431712aaf9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 113 additions and 17 deletions

View File

@ -568,8 +568,12 @@ export default class MetamaskController extends EventEmitter {
),
getCurrentAccountEIP1559Compatibility:
this.getCurrentAccountEIP1559Compatibility.bind(this),
legacyAPIEndpoint: `${gasApiBaseUrl}/networks/<chain_id>/gasPrices`,
EIP1559APIEndpoint: `${gasApiBaseUrl}/networks/<chain_id>/suggestedGasFees`,
getCurrentNetworkLegacyGasAPICompatibility: () => false,
getCurrentNetworkLegacyGasAPICompatibility: () => {
const { chainId } = this.networkController.state.providerConfig;
return chainId === CHAIN_IDS.BSC;
},
getChainId: () => this.networkController.state.providerConfig.chainId,
});

View File

@ -105,7 +105,9 @@ async function withFixtures(options, testSuite) {
});
}
}
const mockedEndpoint = await setupMocking(mockServer, testSpecificMock);
const mockedEndpoint = await setupMocking(mockServer, testSpecificMock, {
chainId: ganacheOptions?.chainId || 1337,
});
await mockServer.start(8000);
driver = (await buildWebDriver(driverOptions)).driver;

View File

@ -20,7 +20,16 @@ const emptyStalelist = {
lastUpdated: 0,
};
async function setupMocking(server, testSpecificMock) {
/**
* Setup E2E network mocks.
*
* @param {object} server - The mock server used for network mocks.
* @param {Function} testSpecificMock - A function for setting up test-specific network mocks
* @param {object} options - Network mock options.
* @param {string} options.chainId - The chain ID used by the default configured network.
* @returns
*/
async function setupMocking(server, testSpecificMock, { chainId }) {
await server.forAnyRequest().thenPassThrough({
beforeRequest: (req) => {
const { host } = req.headers;
@ -99,7 +108,9 @@ async function setupMocking(server, testSpecificMock) {
});
await server
.forGet('https://gas-api.metaswap.codefi.network/networks/1337/gasPrices')
.forGet(
`https://gas-api.metaswap.codefi.network/networks/${chainId}/gasPrices`,
)
.thenCallback(() => {
return {
statusCode: 200,
@ -130,7 +141,7 @@ async function setupMocking(server, testSpecificMock) {
await server
.forGet(
'https://gas-api.metaswap.codefi.network/networks/1337/suggestedGasFees',
`https://gas-api.metaswap.codefi.network/networks/${chainId}/suggestedGasFees`,
)
.thenCallback(() => {
return {
@ -203,7 +214,7 @@ async function setupMocking(server, testSpecificMock) {
});
await server
.forGet('https://token-api.metaswap.codefi.network/tokens/1337')
.forGet(`https://token-api.metaswap.codefi.network/tokens/${chainId}`)
.thenCallback(() => {
return {
statusCode: 200,
@ -343,7 +354,7 @@ async function setupMocking(server, testSpecificMock) {
});
await server
.forGet('https://token-api.metaswap.codefi.network/token/1337')
.forGet(`https://token-api.metaswap.codefi.network/token/${chainId}`)
.thenCallback(() => {
return {
statusCode: 200,
@ -352,15 +363,15 @@ async function setupMocking(server, testSpecificMock) {
});
// It disables loading of token icons, e.g. this URL: https://static.metafi.codefi.network/api/v1/tokenIcons/1337/0x0000000000000000000000000000000000000000.png
await server
.forGet(
/^https:\/\/static\.metafi\.codefi\.network\/api\/v1\/tokenIcons\/1337\/.*\.png/u,
)
.thenCallback(() => {
return {
statusCode: 200,
};
});
const tokenIconRegex = new RegExp(
`^https:\\/\\/static\\.metafi\\.codefi\\.network\\/api\\/vi\\/tokenIcons\\/${chainId}\\/.*\\.png`,
'u',
);
await server.forGet(tokenIconRegex).thenCallback(() => {
return {
statusCode: 200,
};
});
await server
.forGet('https://min-api.cryptocompare.com/data/price')

View File

@ -4,6 +4,7 @@ const {
logInWithBalanceValidation,
} = require('../helpers');
const FixtureBuilder = require('../fixture-builder');
const { CHAIN_IDS } = require('../../../shared/constants/network');
describe('Gas estimates generated by MetaMask', function () {
const baseGanacheOptions = {
@ -139,7 +140,85 @@ describe('Gas estimates generated by MetaMask', function () {
});
describe('Send on a network that is not EIP-1559 compatible', function () {
it('show expected gas defaults', async function () {
it('show expected gas defaults on a network supported by legacy gas API', async function () {
await withFixtures(
{
fixtures: new FixtureBuilder().build(),
ganacheOptions: {
...preLondonGanacheOptions,
chainId: parseInt(CHAIN_IDS.BSC, 16),
},
title: this.test.title,
},
async ({ driver, ganacheServer }) => {
await driver.navigate();
await logInWithBalanceValidation(driver, ganacheServer);
await driver.clickElement('[data-testid="eth-overview-send"]');
await driver.fill(
'input[placeholder="Enter public address (0x) or ENS name"]',
'0x2f318C334780961FB129D2a6c30D0763d9a5C970',
);
await driver.fill('.unit-input__input', '1');
// Check that the gas estimation is what we expect
await driver.findElement({
cass: '[data-testid="confirm-gas-display"]',
text: '0.000042',
});
},
);
});
it('show expected gas defaults on a network supported by legacy gas API when that API is down', async function () {
await withFixtures(
{
fixtures: new FixtureBuilder().build(),
ganacheOptions: {
...preLondonGanacheOptions,
chainId: parseInt(CHAIN_IDS.BSC, 16),
},
testSpecificMock: (mockServer) => {
mockServer
.forGet(
`https://gas-api.metaswap.codefi.network/networks/${parseInt(
CHAIN_IDS.BSC,
16,
)}/gasPrices`,
)
.thenCallback(() => {
return {
statusCode: 422,
};
});
},
title: this.test.title,
},
async ({ driver, ganacheServer }) => {
await driver.navigate();
await logInWithBalanceValidation(driver, ganacheServer);
await driver.clickElement('[data-testid="eth-overview-send"]');
await driver.fill(
'input[placeholder="Enter public address (0x) or ENS name"]',
'0x2f318C334780961FB129D2a6c30D0763d9a5C970',
);
await driver.fill('.unit-input__input', '1');
// Check that the gas estimation is what we expect
await driver.findElement({
cass: '[data-testid="confirm-gas-display"]',
text: '0.000042',
});
},
);
});
it('show expected gas defaults on a network not supported by legacy gas API', async function () {
await withFixtures(
{
fixtures: new FixtureBuilder().build(),