interface icon indicating copy to clipboard operation
interface copied to clipboard

Switched chain is not reflected in the address and balance

Open mahnunchik opened this issue 10 months ago • 10 comments

If the wallet uses different addresses for different chains, switching the cryptocurrency for exchange does not update the address and balance.

Image

Page reload updates the address and balance to the correct values.

Image

WalletConnect approved session namespace:

{
    "eip155": {
        "chains": [
            "eip155:1",
            "eip155:42161",
            "eip155:56"
        ],
        "methods": [
            "eth_accounts",
            "eth_requestAccounts",
            "eth_sendRawTransaction",
            "eth_sign",
            "eth_signTransaction",
            "eth_signTypedData",
            "eth_signTypedData_v3",
            "eth_signTypedData_v4",
            "eth_sendTransaction",
            "personal_sign",
            "wallet_switchEthereumChain",
            "wallet_addEthereumChain",
            "wallet_getPermissions",
            "wallet_requestPermissions",
            "wallet_registerOnboarding",
            "wallet_watchAsset",
            "wallet_scanQRCode",
            "wallet_sendCalls",
            "wallet_getCapabilities",
            "wallet_getCallsStatus",
            "wallet_showCallsStatus"
        ],
        "events": [
            "chainChanged",
            "accountsChanged",
            "message",
            "disconnect",
            "connect"
        ],
        "accounts": [
            "eip155:1:0xB6bB59b3aC844e8e4A98A281D57E3E0628bcb471",
            "eip155:42161:0xAd8eDE8697aF92AF31Fe66EDAD9A52A74F74464E",
            "eip155:56:0x9B0d038121EB94CeA52FD08B416056B2824524dd"
        ]
    }
}

mahnunchik avatar Mar 12 '25 22:03 mahnunchik

Thanks for the report, what wallet was used for wallet connect?

plondon avatar Mar 13 '25 14:03 plondon

Wallet https://coin.space/wallet/

@plondon Most likely, all wallets that use different addresses for different chains are affected.

Switching the chain on the wallet side correctly switches the chain on the app side:

await web3wallet.emitSessionEvent({
  topic: session.topic,
  event: {
    name: 'chainChanged',
    data: chain,
  },
  chainId: `${prefix}:${chain}`,
});

await await web3wallet.emitSessionEvent({
  topic: session.topic,
  event: {
    name: 'accountsChanged',
    data: [address],
  },
  chainId: `${prefix}:${chain}`,
});

mahnunchik avatar Mar 13 '25 17:03 mahnunchik

hey @plondon can i take this issue?

ktarun1419 avatar Mar 19 '25 18:03 ktarun1419

@ktarun1419 @plondon for your information, this issue is a bug in the underlying library: https://github.com/WalletConnect/walletconnect-monorepo/issues/5706 and PR https://github.com/WalletConnect/walletconnect-monorepo/pull/5713

The following issue is a bit related https://github.com/Uniswap/interface/issues/7864

mahnunchik avatar Mar 20 '25 14:03 mahnunchik

Similar issue has been fixed in PancakeSwap https://github.com/pancakeswap/pancake-frontend/issues/11325 PR: https://github.com/pancakeswap/pancake-frontend/pull/11338

mahnunchik avatar Mar 21 '25 15:03 mahnunchik

Thanks for following up @mahnunchik we'll update the necessary packages

plondon avatar Apr 02 '25 13:04 plondon

Hi @mahnunchik , a couple of things, the interface delays the switchChain request until its required for the transaction. I tried updating the wagmi library to include the shared fix, and added a checkSwitchReloadNeeded (shared below). If reload is needed we can refresh and switchChain, however I noticed that on refresh the dapp reverts back to mainnet (video attached)

export const checkSwitchReloadNeeded = async (
  chainId: number,
  connector?: Connector,
  address?: string,
): Promise<boolean> => {
  if (!connector || !address) {
    return false
  }

  try {
    const provider = await (connector as WalletConnectConnector).getProvider()
    const accounts = provider?.session?.namespaces?.eip155?.accounts
    const isSwitchNeeded =
      Array.isArray(accounts) && !accounts.some((account: string) => account?.includes(`${chainId}:${address}`))

    return isSwitchNeeded
  } catch (error) {
    logger.debug('Web3Provider', 'checkSwitchReloadNeeded', 'Error checking if reload needed:', error)

    return false
  }
}

https://github.com/user-attachments/assets/0735d2d9-05f4-4a93-9f82-64f113b8e317

Connected to coin space wallet above, 0xd5 is my mainnet account, 0x89 is my base address

plondon avatar Apr 03 '25 20:04 plondon

Closing this due to inactivity

plondon avatar May 15 '25 13:05 plondon

@mahnunchik can you provide more information here

plondon avatar May 26 '25 22:05 plondon

@plondon everything remains as it was before.

  1. Connect the wallet https://coin.space/wallet/settings/walletconnect
  2. Switch the blockchain (token to exchange in Uniswap interface)
  3. Address AND balance does not change

mahnunchik avatar May 27 '25 10:05 mahnunchik