web3-react
web3-react copied to clipboard
WalletConnect V2 "Invalid chainId"
WalletConnect2 shows the following error even if including the chain ID in chains and/or optionalChains
Can be reproduced as follows:
- navigate to https://web3-react-mu.vercel.app/ (make sure no previous connections on this browser, use incognito mode if needed)
- Select any chain other than Mainnet.
- View error preventing popup.
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
?
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,
},
})
)
Have the same issue here
@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