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!

2 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):

2 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?

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.

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.