From 6f81db8fc673a2d7672f809bba77ea44df4bc83f Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Sat, 29 Apr 2023 21:41:29 +0300 Subject: [PATCH] fix: handle changing networks with custom names --- commands/metamask.js | 19 ++++++++++++++++++- helpers.js | 31 +++++++++++++++++++++++++++---- tests/e2e/specs/metamask-spec.js | 7 +++++++ 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/commands/metamask.js b/commands/metamask.js index b32c62856..2cd64f8f3 100644 --- a/commands/metamask.js +++ b/commands/metamask.js @@ -32,7 +32,7 @@ const { const { confirmationPageElements, } = require('../pages/metamask/confirmation-page'); -const { setNetwork } = require('../helpers'); +const { setNetwork, getNetwork } = require('../helpers'); let extensionInitialUrl; let extensionId; @@ -372,6 +372,23 @@ const metamask = { return true; }, async changeNetwork(network) { + const currentNetwork = getNetwork(); + + if ( + typeof network === 'string' && + (currentNetwork.networkDisplayName === network.toLowerCase() || + currentNetwork.networkName === network.toLowerCase()) + ) + return false; + + if ( + typeof network === 'object' && + (currentNetwork.networkDisplayName === + network.networkName.toLowerCase() || + currentNetwork.networkName === network.networkName.toLowerCase()) + ) + return false; + await switchToMetamaskIfNotActive(); await playwright.waitAndClick(mainPageElements.networkSwitcher.button); if (typeof network === 'string') { diff --git a/helpers.js b/helpers.js index 8c05b72fb..b02f4355e 100644 --- a/helpers.js +++ b/helpers.js @@ -9,21 +9,30 @@ const packageJson = require('./package.json'); const PRESET_NETWORKS = Object.freeze({ mainnet: { networkName: 'mainnet', + networkDisplayName: 'Ethereum Mainnet', networkId: 1, isTestnet: false, }, goerli: { networkName: 'goerli', + networkDisplayName: 'Goerli Test Network', networkId: 5, isTestnet: true, }, sepolia: { networkName: 'sepolia', + networkDisplayName: 'Sepolia Test Network', networkId: 11155111, isTestnet: true, }, }); +// map(networkName => networkInfo) +const ADDED_NETWORKS = { + 'ethereum mainnet': PRESET_NETWORKS.mainnet, + 'goerli test network': PRESET_NETWORKS.goerli, + 'sepolia test network': PRESET_NETWORKS.sepolia, +}; let selectedNetwork = PRESET_NETWORKS.mainnet; module.exports = { @@ -32,24 +41,38 @@ module.exports = { if (Object.keys(PRESET_NETWORKS).includes(network)) { selectedNetwork = PRESET_NETWORKS[network]; + return; } if (network === 'localhost') { const provider = new ethers.JsonRpcProvider('http://127.0.0.1:8545'); const { chainId, name } = await provider.getNetwork(); selectedNetwork = { - networkName: name, + networkName: name.toLowerCase(), + networkDisplayName: name, networkId: chainId, isTestnet: true, }; - } else if (typeof network === 'object') { + ADDED_NETWORKS[name] = selectedNetwork; + return; + } + + if (typeof network === 'object') { selectedNetwork = { - networkName: network.networkName, + networkName: network.networkName.toLowerCase(), + networkDisplayName: network.networkName, networkId: Number(network.chainId), isTestnet: network.isTestnet, }; + ADDED_NETWORKS[network.networkName] = selectedNetwork; + return; + } + + if (typeof network === 'string') { + const addedNetwork = ADDED_NETWORKS[network.toLowerCase()]; + if (!addedNetwork) throw new Error('Network not found'); + selectedNetwork = addedNetwork; } - // todo: handle a case when setNetwork() is triggered by changeNetwork() with a string of already added custom networks }, getNetwork: () => { log(`Current network data: ${selectedNetwork}`); diff --git a/tests/e2e/specs/metamask-spec.js b/tests/e2e/specs/metamask-spec.js index 782942798..b3f856005 100644 --- a/tests/e2e/specs/metamask-spec.js +++ b/tests/e2e/specs/metamask-spec.js @@ -103,6 +103,13 @@ describe('Metamask', () => { expect(network.isTestnet).to.be.true; }); }); + + it(`should discard changing network if it is current one`, () => { + cy.changeMetamaskNetwork('goerli').then(networkChanged => { + expect(networkChanged).to.be.false; + }); + }); + it(`changeMetamaskNetwork should change network using custom network name`, () => { if (Cypress.env('USE_ANVIL')) { cy.changeMetamaskNetwork('anvil').then(networkChanged => {