Skip to content

Commit

Permalink
Merge pull request #737 from Synthetixio/ahmed/switch-account-fix
Browse files Browse the repository at this point in the history
fix: handle changing networks with custom names
  • Loading branch information
drptbl authored May 4, 2023
2 parents 6aadbfc + 262d4d0 commit 82f1890
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
19 changes: 18 additions & 1 deletion commands/metamask.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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') {
Expand Down
31 changes: 27 additions & 4 deletions helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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}`);
Expand Down
7 changes: 7 additions & 0 deletions tests/e2e/specs/metamask-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down

0 comments on commit 82f1890

Please sign in to comment.