Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metamask still does not have a consistent way of switching networks. #10597

Closed
jordanlesich opened this issue Mar 5, 2021 · 25 comments
Closed
Labels

Comments

@jordanlesich
Copy link

"wallet_addEthereumChain" was touted as a way to send requests to the wallet to switch networks.

#5101

While this does work for non-default chains, MM deliberately throws errors for default chains. This makes it difficult to build a consistent UX around this feature for multi platform apps.

If there was a way to request a network switch, I could detect whether the network to be switched is default or not and send a corresponding request. But that isn't possible under the current build.

Most experienced users will already know to switch themselves, but this is not the case for new users who might need guidance. Having this arbitrary mismatch of functionality makes having a comprehensive, consistent mechanism for switching networks impossible.

@brad-decker
Copy link
Contributor

@jordanlesich you might be interested in ethereum/EIPs#3326. While it did take us a long time to go from EIP -> implementation for wallet_addEthereumChain we will likely be able to more rapidly release the implementation for this one, because of the existing UI for switching to custom networks. We'll need to do some thinking around security and presenting the appropriate options in the case of multiple networks with the same chainId.

@jordanlesich
Copy link
Author

I appreciate the information and fast reply. I'll stay subscribed to both threads. Thanks!

@rhlsthrm
Copy link

This feature is definitely more and more necessary in a cross-chain/L2 world! Connext is eagerly waiting for this to improve user experience.

@pizzarob
Copy link

Cargo is also looking forward to this feature. We will be adding support for multiple chains and while we can easily change the chain to something like xDai using wallet_addEthereumChain we cannot easily change it back to Ethereum for the user.

@matthewlilley
Copy link

Yeah, this is an issue for us too. We have a network switch component but currently cannot switch to the native network.

@spudgy
Copy link

spudgy commented May 21, 2021

Same issue here :(

@derivativedegen
Copy link

same issue! Switching to Matic is smooth and easy. But trying to switch back to ETH mainnet doesnt work at all. I've even tried this on sushiswap and it seems they can switch the wallet to any network but cannot switch it back to ETH.

hopefully this is resolved soon!

@sebenns
Copy link

sebenns commented May 27, 2021

My current workaround - which I'm sure everyone here uses - is to catch the error at the moment and ask the user to manually switch on default MM chains. For other networks it is no problem to both add and switch them.
Looking forward to it, I'll stay subscribed to both threads ✌️

@aureliusbtc
Copy link

Following up on this as well as it's an issue for Nerve similar to other people above. The sooner switching to ETH (and default chains) is implemented, the better the experience it'd provide to end users.

@Umar-Abbas
Copy link

Any updates on this?

@ma12ki
Copy link

ma12ki commented Jun 7, 2021

We'd love to be able to switch to default networks with custom URL. We're working on a tool called Automate which provides extra features via custom RPC and not being able to switch to mainnet programatically really hurts the user experience.

@helderjnpinto
Copy link

hey when wallet_switchEthereumChain is available?

@froggiedev
Copy link

we also need wallet_switchToEthereumChain().

@brad-decker
Copy link
Contributor

This has landed in 9.6.0 🎉

@derivativedegen
Copy link

Is there any documentation or examples to utilize the new functionality to switch to default chains?

TIA

@snwokenk
Copy link

snwokenk commented Jun 24, 2021

@Yhozen
Copy link

Yhozen commented Jun 24, 2021

@snwokenk thank you!

PD: the hyperlink is pointing to metamask's issues, I almost thought I got rick-rolled 😂

@snwokenk
Copy link

@snwokenk thank you!

PD: the hyperlink is pointing to metamask's issues, I almost thought I got rick-rolled 😂

lol fixed

@akinmail
Copy link

akinmail commented Jan 9, 2022

@snwokenk This link still points to old documentation where it says it does not allow switching to default chain
#10597 (comment)

@akinmail
Copy link

akinmail commented Jan 9, 2022

Futhermore, bringing this back, Is there any documentation or examples to utilize the new functionality to switch to default chains?

@Sotatek-SonNguyen2
Copy link

Sotatek-SonNguyen2 commented Jan 10, 2022

Ohh, It so easy.
Please check below code:
refer: This link

  await ethereum.request({
    method: 'wallet_switchEthereumChain',
    params: [{ chainId: '0xf00' }],
  });
} catch (switchError) {
  // This error code indicates that the chain has not been added to MetaMask.
  if (switchError.code === 4902) {
    try {
      await ethereum.request({
        method: 'wallet_addEthereumChain',
        params: [{ chainId: '0xf00', rpcUrl: 'https://...' /* ... */ }],
      });
    } catch (addError) {
      // handle "add" error
    }
  }
  // handle other "switch" errors
}`

@patniemeyer
Copy link

FYI, The pattern above of calling switch chain, catching a possible exception and then attempting to add the chain if needed works on desktop and works for most chains on mobile but for some chains I get the "-32602 May not specify default MetaMask chain" exception. Specifically this is happening for us when trying to add Optimism on iOS Mobile.

@marino39
Copy link

FYI, The pattern above of calling switch chain, catching a possible exception and then attempting to add the chain if needed works on desktop and works for most chains on mobile but for some chains I get the "-32602 May not specify default MetaMask chain" exception. Specifically this is happening for us when trying to add Optimism on iOS Mobile.

I have the same issue. Have you found any way around it?

@skeptrunedev
Copy link

FYI, The pattern above of calling switch chain, catching a possible exception and then attempting to add the chain if needed works on desktop and works for most chains on mobile but for some chains I get the "-32602 May not specify default MetaMask chain" exception. Specifically this is happening for us when trying to add Optimism on iOS Mobile.

+1 . I am also having this same issue.

@patniemeyer
Copy link

Since this issue is closed you may want to follow this issue for the problem: MetaMask/core#740

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests