switch_Ethereum chain doesn't exist Error
Description
Add basic switchEthereumChain feature for Dapps to prompt and suggest a user to switch to a specified chain that they already have added to MetaMask. This method will work with default networks like mainnet.
Technical Requirements
- Implement the
wallet_switchEthereumChainRPC Method (EIP3326) - Implement the UI that allows the user to accept or reject the request
- Figma - https://www.figma.com/file/dzapXt5s0pKBxb9lZkIoDM/%231352---Confirm-transactions?node-id=3474%3A3906
- Implement warnings in case the parameters are malformed or the chainId is invalid
Acceptance Criteria
- It should successfully switch networks in the case the request is accepted
- It should throw an error for malformed requests or invalid chainId
References
- Sentry issue: https://sentry.io/organizations/metamask/issues/2396504126/?environment=production&project=2299799&query=is%3Aunresolved&sort=user&statsPeriod=14d
- Related to custom network parity issue https://www.notion.so/Allow-me-to-easily-safely-switch-from-a-custom-network-to-mainnet-default-testnets-via-Network--4b8d0f7dce9e4446a111a4146ad6315e
- Technical guidance from Brad: https://consensys.slack.com/archives/G1L7H42BT/p1624408934213000
- Merged PR on extension - https://github.com/MetaMask/metamask-extension/pull/10905
- Proposal in EIP 3326 - https://github.com/rekmarks/EIPs/blob/3326-create/EIPS/eip-3326.md eip-3326.md
Products POV
- really high on Sentry,
- couple GH issues opened by devs,
- extension parity issue.
- Fixed on extension, looks like lower effort than transaction state (confirm with engineers?) ** can't answer this yet until grooming/specing out work is done
Engineers didn't get a chance to groom this before planning. So this is part of the work to be done in this sprint, which is to groom the work to be done, in the hopes that it'll make it to next sprint.
@Cal-L @andrepimenta will groom this story this sprint.
Hi there! I opened a issue 4 days ago which is #3021 , but yesterday I found a dapp which can switch network in smart phone,the dapp is flux ,you can try to access it in your phone with metamask mobile.But I don't know their methods to receive that.Wish you could tell me the reason.Thanks in advance!
Here is the screen shot(reference to flux):

My comment above shows that they can make it,maybe they use another rpc api which is I don't know? I'm so confused now :[
We would really need this feature as well. @KBKUN024 that's very interesting! Did you open their website in MetaMask Browser in mobile app, correct?
Yes,I'm pretty sure,you can try.Now I can use addEthereumChain to add and if some network is already exist in metamask,this method will do switch job。I also have a problem about I can not delete network in metamask mobile😂,why😂
发自我的iPhone
------------------ Original ------------------ From: David Hauser @.> Date: Fri,Aug 20,2021 4:54 AM To: MetaMask/metamask-mobile @.> Cc: KBKUN024 @.>, Mention @.> Subject: Re: [MetaMask/metamask-mobile] switch_Ethereum chain doesn't exist Error (#2944)
@kinggongzilla
I find the way to delete network,I must hold some seconds...
Yes,I'm pretty sure,you can try.Now I can use addEthereumChain to add and if some network is already exist in metamask,this method will do switch job。I also have a problem about I can not delete network in metamask mobile😂,why😂
发自我的iPhone
------------------ Original ------------------
From: David Hauser @.***>
Date: Fri,Aug 20,2021 4:54 AM
To: MetaMask/metamask-mobile @.***>
Cc: KBKUN024 @.>, Mention @.>
Subject: Re: [MetaMask/metamask-mobile] switch_Ethereum chain doesn't exist Error (#2944)
Oh okay, so you can use addEthereumChain to add a new chain and the same method does the switch job if the network already exists?
Yes,I'm pretty sure,you can try.Now I can use addEthereumChain to add and if some network is already exist in metamask,this method will do switch job。I also have a problem about I can not delete network in metamask mobile😂,why😂 发自我的iPhone ------------------ Original ------------------ From: David Hauser @.> Date: Fri,Aug 20,2021 4:54 AM To: MetaMask/metamask-mobile _@_.> Cc: KBKUN024 _@.>, Mention @.**_> Subject: Re: [MetaMask/metamask-mobile] switch_Ethereum chain doesn't exist Error (#2944)
Oh okay, so you can use addEthereumChain to add a new chain and the same method does the switch job if the network already exists?
exactly
Now I got another problem that is when I click "switch network" on the bottom right corner,the browser within metamask mobile will refresh the page,this is not a expected situation for me,so is there any solution to prevent the browser refresh the page? @kinggongzilla

Hi there! I opened a issue 4 days ago which is #3021 , but yesterday I found a dapp which can switch network in smart phone,the dapp is flux ,you can try to access it in your phone with metamask mobile.But I don't know their methods to receive that.Wish you could tell me the reason.Thanks in advance!
Here is the screen shot(reference to flux):
Need help, calling wallet_switchEthereumChain or add only opens metamask app on phone, but i cant get popup for confirmation to switch networks, help please, thanks
Now I got another problem that is when I click "switch network" on the bottom right corner,the browser within metamask mobile will refresh the page,this is not a expected situation for me,so is there any solution to prevent the browser refresh the page? @kinggongzilla
Hi, did you tried following code? Actually i tried it doesnot work on mobile metamask browser. Still refresh the page
window.ethereum.on('chainChanged', (chainId) => {
window.ethereum.autoRefreshOnNetworkChange = false;
})
This code works on desktop, however not on metamask mobile browser, any tips please? On mobile, it only switches if the network is saved (I've used toHex since I'm dealing with base10 numbers and not Hexadecimal, it does work on PC though)
if (window?.ethereum) {
try {
if (chain.chainId === 0) return;
// @ts-ignore: Unreachable code error
await window?.ethereum?.request({
method: "wallet_switchEthereumChain",
params: [{ chainId: Web3.utils.toHex(chain.chainId) }],
});
// console.log("hello", Web3.utils.toHex(chain.chainId));
} catch (error: any) {
// console.log("error.code", error.code);
if (error.code === 4902) {
try {
await window?.ethereum?.request({
method: "wallet_addEthereumChain",
params: [
{
chainId: Web3.utils.toHex(chain.chainId),
chainName: chain.chainName,
nativeCurrency: chain.nativeCurrency,
rpcUrls: [chain.rpcUrl],
blockExplorerUrls: [chain.blockExplorerUrl],
},
],
});
} catch (addError) {
// console.log("Error while adding");
// console.log(addError);
}
}
}
}
Metamask on mobile does not throw error.code = 4902 for some reason. error.code is -32603. You can remove if (error.code === 4902) check and just try to request wallet_addEthereumChain on any error.
Any fix this issue ? Or need to create some crutch for that ? My solution
if (err && (err.code === 4902 || err.code === -32603)) {
// trigger flow to add new network
} else {
//show some error
}
Any fix this issue ? Or need to create some crutch for that ? My solution
if (err && (err.code === 4902 || err.code === -32603)) { // trigger flow to add new network } else { //show some error }
this solution is correct. thanks