WalletConnect / WalletConnectFlutterV2

WalletConnect v2 client made in Dart for Flutter.
https://pub.dev/packages/walletconnect_flutter_v2
Apache License 2.0
117 stars 61 forks source link

Issue with Network Switching in WalletConnectFlutter v2 and WalletConnectModal #275

Closed ianwhite2 closed 8 months ago

ianwhite2 commented 8 months ago

Describe the bug Scenario:

Attempted Solution: I attempted to use the onRelayClientMessage callback to detect network changes while the application is active. However, this method has proven to be unreliable. It fails to trigger under certain conditions, such as when the application is reopened, leading to inconsistent behavior in network detection.

Issue Details: The necessity for a reliable detection mechanism becomes evident during transactions. After switching from Network A to Network B in MetaMask, any transaction initiated (eth_sendTransaction) deep link MetaMask to request a network switch. Upon switching the network, MetaMask closes without executing the transaction. Consequently, the await web3App.request() call remains pending indefinitely, as it does not receive any response. This forces the user to initiate the transaction again, leading to a poor user experience.

Smartphone (please complete the following information):

ianwhite2 commented 8 months ago

@quetool - Need some of your expert help here

quetool commented 8 months ago

When I switch the network to Network B directly in the MetaMask app, I need my application to detect this change and update accordingly.

In order to do this you should listen to chainChanged event emitted by the wallet. (Assuming Metamask is emitting it, from docs for wallets => https://docs.walletconnect.com/web3wallet/wallet-usage?platform=flutter#extra-platform-specific)

web3App?.onSessionEvent.subscribe(_onSessionEvent);

Event received will look like this:

SessionEvent(id: 1709*****, topic: 7542255a9f51b269ab**************, name: chainChanged, chainId: eip155:42161, data: 42161)
ianwhite2 commented 8 months ago

Did tried it but it is not consistent Also another question is there a way where I can check if the metamask is in another chain before doing the eth_sendTransaction , The problem which I am facing is lets say both A and B are approved chain in my app but when my app initiates the transaction with chain A and if the metamask app is on chain B then metamask will ask the the user to change the network This action closes the app, and I have to initiate the transaction from my app again. In this case, the eth_sendTransaction call is in a limbo state where it doesn't return an error or anything."

web3App.request(
      topic: topic,
      chainId: ethChain.namespace,
      request: SessionRequestParams(
        method: 'eth_sendTransaction',
        // Check the `web3dart_extension` file for this function
        params: [t.toJson(fromAddress: address)],
      ),
    )
```; 
ianwhite2 commented 8 months ago

@quetool not sure why it got closed but i still have one more question above , can you please have a look at it

quetool commented 8 months ago

Did tried it but it is not consistent

What do you mean? Metamask it is sending the chainChanged like in a random way?


Also another question is there a way where I can check if the metamask is in another chain before doing the eth_sendTransaction

Nope a direct one. When you first approve the session you should see the current network inside the approved namespaces, since MM approves one network at a time the first time you connect you'll see just one account. Going forward, either you request a network change by requesting wallet_switchEthereumChain or wallet_addEthereumChain or Metamaks should emit a chainChanged event so you can update the current network accordingly on your dapp.


The problem which I am facing is lets say both A and B are approved chain in my app but when my app initiates the transaction with chain A and if the metamask app is on chain B then metamask will ask the the user to change the network

This is not a problem, this is how it should work indeed.


This action closes the app

What do you mean by this? Is your app crashing when changing the network in Metamask?

It seems to me that Metamask is having issues implementing the protocol, do you mind trying other wallets?