diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index a33ee2754..60802c581 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -568,8 +568,12 @@ export default class MetamaskController extends EventEmitter { ), getCurrentAccountEIP1559Compatibility: this.getCurrentAccountEIP1559Compatibility.bind(this), + legacyAPIEndpoint: `${gasApiBaseUrl}/networks//gasPrices`, EIP1559APIEndpoint: `${gasApiBaseUrl}/networks//suggestedGasFees`, - getCurrentNetworkLegacyGasAPICompatibility: () => false, + getCurrentNetworkLegacyGasAPICompatibility: () => { + const { chainId } = this.networkController.state.providerConfig; + return chainId === CHAIN_IDS.BSC; + }, getChainId: () => this.networkController.state.providerConfig.chainId, }); diff --git a/test/e2e/helpers.js b/test/e2e/helpers.js index d6a4bb2ab..8c0ffb76a 100644 --- a/test/e2e/helpers.js +++ b/test/e2e/helpers.js @@ -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; diff --git a/test/e2e/mock-e2e.js b/test/e2e/mock-e2e.js index 29035f46c..a3e48208d 100644 --- a/test/e2e/mock-e2e.js +++ b/test/e2e/mock-e2e.js @@ -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') diff --git a/test/e2e/tests/gas-estimates.spec.js b/test/e2e/tests/gas-estimates.spec.js index a7d121f31..f9f27029a 100644 --- a/test/e2e/tests/gas-estimates.spec.js +++ b/test/e2e/tests/gas-estimates.spec.js @@ -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(),