Uniswap / web3-react

A simple, maximally extensible, dependency minimized framework for building modern Ethereum dApps
https://web3-react-mu.vercel.app/
GNU General Public License v3.0
5.52k stars 1.52k forks source link

WalletConnect V2 "Invalid chainId" #867

Open 0xSaga opened 1 year ago

0xSaga commented 1 year ago

WalletConnect2 shows the following error even if including the chain ID in chains and/or optionalChains

Can be reproduced as follows:

  1. navigate to https://web3-react-mu.vercel.app/ (make sure no previous connections on this browser, use incognito mode if needed)
  2. Select any chain other than Mainnet.
  3. View error preventing popup.
Screenshot 2023-07-27 at 12 54 48 PM
razgraf commented 1 year ago

Got the same issue - it has to do with the way web3-react expects the desiredChainId to be included in the chains array (which afaik is now an antipattern due to optionalChains). The WC2 API became quite messy 🫣.

A quick fix (although dirty as you'd be touching "private" attributes) is to re-configuring the connector before attempting to activate. Assuming you have a chain picker before the user asks to connect with WC2 (just like in the example):

  connector.chains = [desiredChainId];
  connector.options.chain = [desiredChainId];
  connector.options.optionalChains = CHAINS.filter((c) => c !== desiredChainId);

Not sure what the fix at web3-react's level would be here but probably involved re-building the chains parameter based on desiredChainId (without checking for it in the initial array provided to the connector). For context, this is wagmi's implementation.

Later edit 8.5.0 : looks like optionalChains will now crash if it doesn't contain the desiredChainId/defaultChainId due to getChainsWithDefault?

RitzyDevBox commented 1 year ago

You can update the walletconnectv2 connector to include all chains

const [mainnet] = Object.keys(MAINNET_CHAINS).map(Number)
const [...optionalChains] = Object.keys(MAINNET_CHAINS).map(Number)

const projectId = process.env.walletConnectProjectId;
export const [walletConnectV2, hooks] = initializeConnector<WalletConnectV2>(
  (actions) =>
    new WalletConnectV2({
      actions,
      options: {
        projectId: projectId,
        chains: [mainnet],
        rpc: getInfuraUrlFor('mainnet'),
        optionalChains,
        showQrModal: true,
      },
    })
)
oddaf commented 1 year ago

Have the same issue here

dellwatson commented 1 year ago

@RitzyDevBox doesnt solve issue.

weird bug, somehow previously there's a time it was working, now it doesnt. not even in the example's url. i guess i will just migrate everything to wagmi