MetaMask / metamask-mobile

Mobile web browser providing access to websites that use the Ethereum blockchain
https://metamask.io
Other
2.1k stars 1.09k forks source link

wallet_switchEthereumChain not works with walletconnect v2 #6655

Closed andriirak closed 11 months ago

andriirak commented 1 year ago

Describe the bug calling wallet_switchEthereumChain didn`t change wallet selected chain

To Reproduce Steps to reproduce the behavior

  1. use walletconnect v2 provider, metamask mobile app version v7.0.1(1126)
  2. connect metamask wallet and call wallet_switchEthereumChain action await provider.request({ method: 'wallet_switchEthereumChain', params: [{ chainId: Web3Util.toHex(chainId) }], });
  3. Select metamask wallet
  4. In the wallet there are no any request and wallet chain not changed

Expected behavior Should works as as chrome app, request to change wallet chain

Smartphone (please complete the following information):


to be added after bug submission by internal support / PM Severity

anaamolnar commented 1 year ago

@andriirak Thanks for reporting! I will pass this on to the team.

imaksp commented 1 year ago

I can also confirm after switch chain call, sign call also does not work (sign works if it is called directly without switch chain), so it looks like switch chain feature was not even tested with WalletConnect V2 before the release. Also if chain is switched manually from app then dapp/walletconnect v2 does not receive any chain changed event.

vkuts commented 1 year ago

Running into the same issue with latest Android and iOS MM app. While connected via WC2 flow, can't get MM app to display a personal_sign request if the wallet's currently selected chain does not match the dapp's chain - and the dapp can't get the wallet to switch it either. This could really use a quick fix 🙏

imaksp commented 1 year ago

Hello, it looks like wallet_switchEthereumChain is working with latest @walletconnect version. so it seem it was walletconnect issue.

finessevanes commented 1 year ago

Commenting from WalletConnect, this has been resolved. Please make sure you're using ethereum-provider 2.8.4.

zdenham commented 1 year ago

@finessevanes Still experiencing this issue after upgrading to ethereum-provider 2.8.4

https://github.com/wagmi-dev/references/issues/393

hamberluo commented 1 year ago

Running into the same issue with latest Android and iOS MM app. While connected via WC2 flow, can't get MM app to display a personal_sign request if the wallet's currently selected chain does not match the dapp's chain - and the dapp can't get the wallet to switch it either. This could really use a quick fix 🙏

same problem

andriirak commented 1 year ago

Commenting from WalletConnect, this has been resolved. Please make sure you're using ethereum-provider 2.8.4.

"@walletconnect/ethereum-provider": "^2.8.6", "@walletconnect/modal": "^2.5.9",

still not working correctly

imaksp commented 1 year ago

It still has bugs with wc v2 auto switch chain integration (among required/approved chains), but if you send wallet_switchEthereumChain or via viem's switchChain directly it works.

andriirak commented 1 year ago

@finessevanes @imaksp for the first request wallet_switchEthereumChain it works, MM received popup confirmation but all next requests failed for example if user changed blockchain manually to Goerli in the MM wallet, all next requests wallet_switchEthereumChain to Ethereum will change chaindId inside walletconnect provider, but mobile MM wallet still have Goerli chain

imaksp commented 1 year ago

Yes its very confusing, I also noticed that sometimes chain switch was successful inside walletconnect without user apporval like in multichain wallet & metamask keeps showing the previously selected network.

siosio34 commented 1 year ago

The problem also occurs with walletconnect v2.9.0.

Other library information is listed below.

wagmi 1.3.8 viem 1.2.10

The code is like the picture below, and if you manually change the chain ID in the Metamask app, no error occurs.

image
zhy023 commented 1 year ago

I encountered the same problem, waiting for repair

eb8d52c9e6a15cf055c286ad7ec37444

equalogix commented 1 year ago

I encountered the same problem, waiting for repair

eb8d52c9e6a15cf055c286ad7ec37444

This is and isn't the same issue. The error you are getting is caused by the fact that WCv2 requires that you explicitly include the methods that your provider is going to call whne you initialize the provider, and you haven't included wallet_switchEthereumChain in your array of permitted methods.

While adding it to the list prevents the specific error you posted. Unfortunately, it still wont allow you to switch chains.

jingxuan98 commented 1 year ago

still having this issue any fix?

When calling the switchNetwork() in wagmi, the walletClient, signer and network on my dapp switched to the correct network but not my metamask mobile (no-popup and stay at the previous network page), then when try to proceed with a contract interaction theres no pop-up as well, but everything works fine after switching to the correct network manually in metamask mobile

urielkelman commented 1 year ago

Still having the same issue. I'm developing a react-native DApp and when executing:

await provider.request({ method: 'wallet_switchEthereumChain', params: [{ chainId: ethers.utils.hexValue(DEFAULT_CHAIN_ID) }], });

I don't get any response from metamask. I'm using the following dependency:

"@walletconnect/modal-react-native": "^1.0.0-rc.6"

And metamask mobile version is: 7.3.0.

Also, when I try to send a transaction using a signer obtained through the web3Provider instance, metamask opens but it only shows the pop-up with all the transaction information if the provider chainId matches the current selected chain in metamask. Related to this issue, the "getNetwork()" method of my web3Provider instance is not returning the chainId corresponding to the selected network in metamask (it's returning the value of the only chain that is present on my eip155 namespace). The same happens if I directly execute:

await provider.request({ method: "eth_chainId", params: [] })

As you see, the only way to make my dapp work properly using WC2 is selecting the chain manually in the metamask app.

liubin595338764 commented 1 year ago

I have also encountered the same problem.

This issue has been occurring for a long time, and I hope it can be resolved as soon as possible. It greatly affects the user's interactive experience.

Two issues:

  1. When calling the wallet_switchEthereumChain method, after the first time, the Metamask app will not pop up the window to switch chains again.
  2. When switching chains in the Metamask app, the page will not receive a chainChanged Event.
nexus6nexus6 commented 1 year ago

Same here.

We have 3 chains that we support on dapp. We managed to connect to MM mobile using chains and optionalChains in the connector initialization step.

But when trying to switch chain after we connect our dapp with metamask mobile using wallet_switchEthereumChain method gives us and error: Error: Failed to switch to chain 'eip155:1'. The chain is not approved or the wallet does not support 'wallet_switchEthereumChain' method.

wallet_switchEthereumChain method works nicely with MM chrome plugin (10.33.1), but not with MM app on Android (7.3.0), nor iOS (7.3.0).

We are using the latest "@web3-react/walletconnect-v2": "8.3.7"

ps: We were trying to fallback to wallet_addEthereumChain method whenever wallet_switchEthereumChain fails, but then we're getting code: -32601, message: 'the method wallet_addEthereumChain does not exist/is not available' error.

To me this all looks like comming from a basic problem, Metamask not supporting multiple chains in the new WalletConnect options - see here: https://github.com/MetaMask/metamask-mobile/issues/6688#issuecomment-1647846096

dokocat commented 1 year ago

how is this not fixed yet? its not possible for dapps to switch chains in metamask, this has effective broke defi on mobile...

cosmos-redshift commented 1 year ago

any updates? I can't test and use my mobile web app with this issue

andreahaku commented 1 year ago

this should be fixed by this which should be out with v7.4.0

annielinnik commented 1 year ago

Still having this issue with MetaMask 7.4.0(1159), iOS 16.2, wagmi 1.3.8

ezra-sg commented 1 year ago

Still having this issue with MetaMask 7.4.0(1159), iOS 16.2, wagmi 1.3.8

same except wagmi 1.3.9. When attempting to send a transaction, the metamask app pops a 'switch network' dialog (and returns to the app without executing the tx), so it seems like it is somehow possible to get the metamask app to switch the network. I just can't figure out how to do it without attempting a tx

streamy3 commented 1 year ago

7.4.0 (1159), Android 11 and can't test wagmi 1.3.7 because MetaMask's totally frozen on load, even after restart.

equalogix commented 1 year ago

7.4.0 (1159), Android 11 and can't test wagmi 1.3.7 because MetaMask's totally frozen on load, even after restart.

I had the same issue with Metamask freezing after the update. I restarted my device and it worked after that. By worked, I mean the wallet generally, I didn't test the wallet_switchEthereumChain.

imaksp commented 1 year ago

same except wagmi 1.3.9. When attempting to send a transaction, the metamask app pops a 'switch network' dialog (and returns to the app without executing the tx), so it seems like it is somehow possible to get the metamask app to switch the network. I just can't figure out how to do it without attempting a txn.

you can directly call viem's switchChain to change it without executing txn.

& I have not checked but I suggest others to try with latest wagmi (1.3.9) which uses latest walletconnect version.

dokocat commented 1 year ago

still doesnt work. Just got to pancakeswap with some funds in your metamask wallet on bsc but keep the network in metamask at eth. Try to make a swap on pancakeswap after connecting metamask, metamask doesn't do anything

siosio34 commented 1 year ago

If you are a wagmi user, do not use switchNetwork, but if you use the below, the network change window will appear even if you use walletconnectv2.

      const account = await getAccount()

      const provider = await account.connector.getProvider()

      const network = await provider.request({
        method: 'wallet_switchEthereumChain',
        params: [
          {
            chainId: `0x${chainId.toString(16)}`,
          },
        ],
      })

However, there is an issue where the request continues indefinitely when you click cancel in the current network change window, and the link to the issue is reported below. https://github.com/MetaMask/metamask-mobile/issues/7023

ezra-sg commented 1 year ago

If you are a wagmi user, do not use switchNetwork, but if you use the below, the network change window will appear even if you use walletconnectv2.

      const account = await getAccount()

      const provider = await account.connector.getProvider()

      const network = await provider.request({
        method: 'wallet_switchEthereumChain',
        params: [
          {
            chainId: `0x${chainId.toString(16)}`,
          },
        ],
      })

However, there is an issue where the request continues indefinitely when you click cancel in the current network change window, and the link to the issue is reported below. #7023

hmm this worked one time for me and I have not been able to reproduce it working :/ I will continue to play with it

0xvidejk commented 11 months ago

are there any updates?

davslabs commented 11 months ago

Im still experiencing this issue using WalletConnect V2 and Metamask (7.5.0) It does not recognize chain switching either.

christopherferreira9 commented 11 months ago

This has been solved by https://github.com/MetaMask/metamask-mobile/pull/6782.

ezra-sg commented 11 months ago

This has been solved by #6782.

looks like this was merged before the last release, was it included in that release (7.5)? if not, any idea what release it will be included in?

imaksp commented 11 months ago

@ezra-guzman this was included from 7.4 version (feat: wc2 dependencies update and handle switchNetwork) see: https://github.com/MetaMask/metamask-mobile/releases/tag/v7.4.0

siosio34 commented 11 months ago

@christopherferreira9 @imaksp Is it really true that this issue has been resolved?

When you call switchNetwork with the code below in MetaMask version 7.5.0 Android, a window appears asking whether to switch or cancel the network, but no matter what you click there, switchNetwork waits indefinitely.

import {
  getNetwork,
  switchNetwork,

} from '@wagmi/core'

 const { chain } = getNetwork()

    if (chain.id === chainId) {
      return
    }4

    console.log('switchNetwork start')
    const network = await switchNetwork({
      chainId,
    })

    console.log('switchNetwork end')

    return Promise.resolve(network)
siosio34 commented 11 months ago

https://github.com/MetaMask/metamask-mobile/issues/7023

The issue with that link still exists.

ezra-sg commented 11 months ago

@ezra-guzman this was included from 7.4 version (feat: wc2 dependencies update and handle switchNetwork) see: https://github.com/MetaMask/metamask-mobile/releases/tag/v7.4.0

okay. i am confused as to why this issue is now closed, the problem still exists

davslabs commented 11 months ago

Still happening with web3-react connectors: https://github.com/Uniswap/web3-react/tree/main/packages/walletconnect-v2

philipjames44 commented 11 months ago

Also facing this issue

LouisLiu00 commented 11 months ago

Still having this issue with MetaMask 7.6.0(1169), iOS 16.6.1, @wagmi/core 1.4.2

Fyfe0203 commented 11 months ago

Still having this issue with "@walletconnect/ethereum-provider": "2.10.1", "@walletconnect/modal": "2.6.2"

malonehedges commented 10 months ago

@christopherferreira9 can you please reopen this issue

malonehedges commented 10 months ago

cc @andreahaku this still isn't resolved

christopherferreira9 commented 9 months ago

Hi all! This is the intended behaviour. The MetaMask mobile wallet does not support wallet_switchEthereumChain as an RPC call via WalletConnect. Although, the next action you try to perform for a network the wallet is not on it'll prompt to switchChain first. As for wallet_addEthereumChain, it is different as it will request to add a chain immediately. This specific behaviour was a requirement we had since the very beginning support of WalletConnect V2.

worm-emoji commented 9 months ago

The MetaMask mobile wallet does not support wallet_switchEthereumChain as an RPC call via WalletConnect.

@christopherferreira9 Please reopen – we need support for wallet_switchEthereumChain as an RPC call via WalletConnect

malonehedges commented 9 months ago

@christopherferreira9 you said this issue is resolved in this comment but now say that it wasn't ever intended to work? https://github.com/MetaMask/metamask-mobile/issues/6655#issuecomment-1708093071

christopherferreira9 commented 9 months ago

@malonehedges what was solved was the ability to change chains upon a request for a chain different than the one selected arrives to the wallet and not the ability to change chains with wallet_switchEthereumChain. I'm sorry if my comment was misleading.

malonehedges commented 9 months ago

ok, was confusing as we were searching for metamask issues related to wallet_switchEthereumChain and that's the title of this issue

davslabs commented 8 months ago

@christopherferreira9 which actions can be used to prompt switch chain? we were using read actions such as get chain id and its not prompting

DenysRakhuba commented 7 months ago

Hello, I'm not sure what the final result of all of it? Should it work or no? I'm using next dependencies:

"@walletconnect/ethereum-provider": "^2.11.0",
"@walletconnect/modal": "^2.6.2",

Metamask mobile (Android) version 7.12.3

Logic example:

this._provider = await EthereumProvider.init({
      projectId: environment.WALLET_CONNECT_PROJECT_ID,
      methods: ['wallet_switchEthereumChain', 'wallet_addEthereumChain'],
      events: ['chainChanged', 'accountsChanged', 'disconnect'],
      chains: [+chainId],
      optionalChains: [0x38, 0x1, 0x89, 0xa4b1, 0xa86a, 0xa],
      showQrModal: true
    });
await this._provider.enable();
if (numberToHex(+this._provider.chainId) !== this.chainParams.chainId) {
      try {
        await this._provider.request({
          method: 'wallet_switchEthereumChain',
          params: [{chainId: this.chainParams.chainId}]
        });
      } catch (e: any) {

It seems it works fine when you switching the chain for the first time (for example you've been connected to Ethereum and then switched to BSC), but then, if you trying to switch again to another new chain (for example Polygon), it just stucks at 'wallet_switchEthereumChain' without any further progress. No errors catched, no notificanions appears asking to switch the chain in Metamask app. Am I missing something or this behaviour just don't work and thats all? Because with other wallets everything is fine. Thanks