metamask-mobile icon indicating copy to clipboard operation
metamask-mobile copied to clipboard

switch_Ethereum chain doesn't exist Error

Open omnat opened this issue 5 years ago • 17 comments

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_switchEthereumChain RPC 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

omnat avatar Jul 28 '21 19:07 omnat

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.

mobularay avatar Aug 02 '21 18:08 mobularay

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): 129440891-7a4c9397-0baa-4609-b325-85030472045e

KBKUN024 avatar Aug 18 '21 03:08 KBKUN024

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 :[

KBKUN024 avatar Aug 18 '21 03:08 KBKUN024

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?

kinggongzilla avatar Aug 19 '21 20:08 kinggongzilla

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)

KBKUN024 avatar Aug 19 '21 23:08 KBKUN024

@kinggongzilla

KBKUN024 avatar Aug 20 '21 09:08 KBKUN024

I find the way to delete network,I must hold some seconds...

KBKUN024 avatar Aug 20 '21 09:08 KBKUN024

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?

kinggongzilla avatar Aug 20 '21 09:08 kinggongzilla

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

KBKUN024 avatar Aug 20 '21 09:08 KBKUN024

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

KBKUN024 avatar Aug 20 '21 09:08 KBKUN024

IMG_8743

KBKUN024 avatar Aug 20 '21 10:08 KBKUN024

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): 129440891-7a4c9397-0baa-4609-b325-85030472045e

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

emadhamad171 avatar Sep 02 '21 19:09 emadhamad171

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;
 })

fahimaltinordu avatar Sep 14 '21 11:09 fahimaltinordu

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);
        }
      }
    }
  }

DYORdev avatar Nov 19 '21 06:11 DYORdev

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.

Niceman35 avatar Nov 23 '21 18:11 Niceman35

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
      }

Jlomaka avatar May 11 '22 19:05 Jlomaka

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

ManekiMarket avatar Oct 27 '22 03:10 ManekiMarket