interface icon indicating copy to clipboard operation
interface copied to clipboard

Random chain switch after connecting with WalletConnect2

Open muratogat opened this issue 2 years ago • 0 comments

Bug Description When trying to connect to the interface on https://app.uniswap.org/swap from a mobile app using WalletConnect2, the connected chain changes unnecessarily after establishing the connection. The selected network icon on the top right corner doesn't change, but the network logs show a wrong Infura endpoint being queried and this prevents further operations. Switching network manually in the frontend back and forth seem to temporarily solve the problem.

Steps to Reproduce

  1. Go to https://app.uniswap.org/swap
  2. Confirm that by default Ethereum Mainnet is selected and check network logs that mainnet.infura.io is being used as chain data provider.
  3. Click on Connect and WalletConnect that opens the QR dialog.
  4. The Session Proposal I receive is as follows (manually formatted)
id: "....", 
pairingTopic: "....",
proposer:  "Uniswap Interface", 
description: "Swap or provide liquidity on the Uniswap Protocol", 
url: "https://app.uniswap.org/", 
icons: ....
requiredNamespaces: [
  "eip155": WalletConnectSign.ProposalNamespace(
    chains: Optional(Set([eip155:1])), 
    methods: Set(["eth_sendTransaction", "personal_sign"]), 
    events: Set(["accountsChanged", "chainChanged"]))
], 
optionalNamespaces: Optional([
  "eip155": WalletConnectSign.ProposalNamespace(
    chains: Optional(Set([eip155:44787, eip155:5, eip155:42220, eip155:421613, eip155:80001, eip155:420, eip155:8453, eip155:1, eip155:137, eip155:56, eip155:11155111, eip155:42161, eip155:10, eip155:43114])), 
    methods: Set(["eth_signTypedData_v4", "eth_sign", "eth_signTypedData", "eth_sendTransaction", "personal_sign"]), 
    events: Set(["disconnect", "message", "chainChanged", "accountsChanged", "connect"]))
]), 
sessionProperties: nil,
  1. Approve the Session with the following supportedNamespaces
sessionProposal: sessionProposal,
chains: [Blockchain("eip155:1")!, Blockchain("eip155:10")!, Blockchain("eip155:137")!, Blockchain("eip155:324")!, Blockchain("eip155:8453")!, Blockchain("eip155:42161")!, Blockchain("eip155:43114")!],
methods: ["eth_sign", "personal_sign", "eth_signTransaction", "eth_sendTransaction", "eth_signTypedData", "eth_signTypedData_v4"],
events: ["chainChanged", "accountsChanged"],
accounts: [
  WalletConnectUtils.Account(blockchain: Blockchain("eip155:1")!, address: connectedAddress)!,   // Ethereum Mainnet
  WalletConnectUtils.Account(blockchain: Blockchain("eip155:10")!, address: connectedAddress)!,  // Optimism
  WalletConnectUtils.Account(blockchain: Blockchain("eip155:137")!, address: connectedAddress)!,  // Polygon
  WalletConnectUtils.Account(blockchain: Blockchain("eip155:324")!, address: connectedAddress)!,  // zkSync Era
  WalletConnectUtils.Account(blockchain: Blockchain("eip155:8453")!, address: connectedAddress)!, // Base
  WalletConnectUtils.Account(blockchain: Blockchain("eip155:42161")!, address: connectedAddress)!, // Arbitrum
  WalletConnectUtils.Account(blockchain: Blockchain("eip155:43114")!, address: connectedAddress)!, // Avalanche C-Chain
]

Most notably this returns a list of supported chains and the same address on each of those chains.

  1. Session is established and interface seems connected. The network doesn't seem to be switched, as still the Ethereum Mainnet icon is displayed in the interface.

  2. Check network logs to confirm that another Infura endpoint is queried now, such as polygon-mainnet.infura.io. This seems like random between the returned chains. (sometimes it switches to Arbitrum, sometimes Polygon, ..). So maybe you were lucky and stayed in Ethereum Mainnet. Try again and again before concluding there is no error.

  3. Switch networks manually in the interface and confirm it switches back to the correct Infura endpoint.

Expected Behavior The connected chain should stay the same after connection. The Infura requests should be sent to the correct endpoint.

Additional Context This could be due to an error or undeterministic handling of the supportedNamespaces response. Note that in WalletConnect2 context there is no "chain selection" in the wallet anymore. Wallets only provide a list of chains they support and it is up to do the dApp to choose, use and send requests on the correct chain. It may / should still handle chainChanged events from the wallet, but it should not expect the wallet to send a limited supportedNamespaces response based on what chain is active at the moment.

muratogat avatar Nov 24 '23 10:11 muratogat