How to send switch network request from dapp to MetaMask Mobile

Hi,

So. I build a small dapp. A user is able to connect through Metamask or Wallet-Connect.
If a user connects with MetaMask (Desktop) I use this:

if (!isClaimed[stateId]){
      providerInstance.currentProvider.request({
        method: 'wallet_switchEthereumChain',
        params: [{ chainId: chainId}]
      }).catch((err) => {
        if (err.code == 4902){
          addFuseNetwork(chainId);
        } else {
          setError({status: null, code: err.code});
        }
      });

    }

For desktop, this works.
The same request done while a user is connected through WalletConnect with their mobile Metamask, doesn’t trigger anything?
What do I need to do or change in order for sending the switch network request to the mobile wallet through WalletConnect?
Is this possible at all? (The docs don’t state it as unavailable?)

Thanks in advance!

5 Likes

Hey @DeMewlingOak, welcome to the MetaMask community! :fox_face:

Try using this source code:

https://docs.metamask.io/guide/rpc-api.html#:~:text=We%20recommend%20using%20this%20method%20with%20wallet_addEthereumChain%3A

If you are also interested, you can join the developer community on ConsenSys Discord (MetaMask is a part of ConsenSys):

5 Likes

thanks first of all for the response.
I have used that approach.

And for desktop this works.
It’s just mobile metamask doesn’t get the request, or sends out the ‘chainChanged’ event when a user does it manually.

What can cause this?
Should I look into metamask, or is rather a problem with using walletConnect as the provider?

2 Likes

Tried searching for the issue for you, but there doesn’t seem to be any answers for it :frowning:

I would suggest try posting it in https://ethereum.stackexchange.com/ also.

1 Like

Hi, I’m facing the same issue for mobile too.

I called the wallet_switchEthereumChain as well and all it did is redirects me to Metamask and back without doing anything.

Is there any fix or workaround for this already? Can’t seem to search this anywhere in ethereum stackexchange also.

4 Likes

Hello @YYee

have you found any solution to this?

2 Likes

I am facing the same problem. I don´t know how to switch the network via MetaMask mobile.

Hello @robsonsf ! Welcome to MetaMask community!

Can you provide your code with more verbose error output?
here or create a new topic on the forum

2 Likes

I don´t have an error. I just want to know how to switch the network in MetaMask mobile using code.

1 Like

I got it. I already wrote about it in the next topic.
Let me know if something didn’t work out for you :wink:

4 Likes