Encountering an issue with addAndSwitchChain when using the default EVM provider in MetaMask. The following error is displayed:
{
"code": -32603,
"message": "The request has been rejected due to a change in selected network. Please verify the selected network and retry the request.",
"data": {
"cause": {
"stack": "Error: The request has been rejected due to a change in selected network. Please verify the selected network and retry the request.\n at ...
}
}
}
It seems the network switch is successfully handled by MetaMask, but the error appears in the console when switching from the network initially used to configure Web3AuthProvider to a new network. Interestingly, switching back to the original network does not trigger this error.
Hi Benny! Thank you for reaching out and providing all those details. Let’s summarize the information for our support team:
Issue Summary
SDK Details: Help - Plug n Play
Error: MetaMask Error with addAndSwitchChain
Details:
Encountering an issue with addAndSwitchChain when using the default EVM provider in MetaMask.
Error message: “The request has been rejected due to a change in selected network. Please verify the selected network and retry the request.”
Additional Context:
The network switch appears to be handled successfully by MetaMask.
Error occurs only when switching from the initially configured network to a new network; switching back to the original network does not trigger this error.
Double-check that the network you’re adding matches the network configurations set in Web3AuthProvider.
Ensure that MetaMask is fully updated, as issues like this can sometimes arise from outdated versions.
If you could provide any additional details like the Web3Auth initialisation code, a screenshot of the console error, or confirmation of the specific networks involved, that might help in troubleshooting further.
I hope this information assists in resolving your issue! Feel free to ask any more questions if needed.
Note: This response was generated by an AI bot and may require further verification. The Web3Auth support team has tracked this issue and will be responding as soon as possible.
Unfortunately, IWeb3AuthCoreOptions only accepts a single chainConfig?: CustomChainConfig; rather than an array of CustomChainConfig. Therefore, I don’t believe this suggestion is applicable.
Made more testing; the issue appears when switching from any mainnets to any testnets (baseSepolia in my case). I think the issue is minor as long as it relates to switching to a testnet and this won’t affect production.
useEffect(() => {
(async () => {
if (!provider) {
console.log("provider is null");
return;
}
const chainConfig = {
chainNamespace: CHAIN_NAMESPACES.EIP155,
chainId: "0x" + baseSepolia.id.toString(16),
rpcTarget: baseSepolia.rpcUrls.default.http[0], // This is the public RPC we have added, please pass on your own endpoint while creating an app
displayName: baseSepolia.name,
tickerName: baseSepolia.nativeCurrency?.name,
ticker: baseSepolia.nativeCurrency?.symbol,
blockExplorerUrl: baseSepolia.blockExplorers?.default.url,
};
if (isConnected) {
await addAndSwitchChain(chainConfig);
setIsChainUpdated(true);
}
})();
}, [provider, isConnected]);
It is actually not related to testnet, affects switch from mainnet to mainnet as well.
Metamask version 12.9.3, Linux Chrome extension.
And the initial Web3AuthProvider config:
const Providers = ({
children,
}: Readonly<{
children: React.ReactNode;
}>) => {
const chainConfig = {
chainNamespace: CHAIN_NAMESPACES.EIP155,
chainId: "0x" + arbitrum.id.toString(16),
rpcTarget: arbitrum.rpcUrls.default.http[0], // This is the public RPC we have added, please pass on your own endpoint while creating an app
displayName: arbitrum.name,
tickerName: arbitrum.nativeCurrency?.name,
ticker: arbitrum.nativeCurrency?.symbol,
blockExplorerUrl: arbitrum.blockExplorers?.default.url,
};
const privateKeyProvider = new EthereumPrivateKeyProvider({
config: { chainConfig },
});
const web3AuthOptions: IWeb3AuthCoreOptions = {
clientId: process.env.NEXT_PUBLIC_WEB3AUTH_CLIENT_ID!,
web3AuthNetwork: WEB3AUTH_NETWORK.SAPPHIRE_MAINNET,
privateKeyProvider,
};
// Social adapters
const authAdapter = new AuthAdapter({
...
});
// EVM adapters
const injectedAdapters = getInjectedAdapters({
options: { ...web3AuthOptions, chainConfig },
});
const config = {
web3AuthOptions,
adapters: [authAdapter, ...injectedAdapters],
};
return <Web3AuthProvider config={config}>{children}</Web3AuthProvider>;
};
Is it it possible to pass all the chains during creation of the Web3AuthProvider? I think it relates to the fact that Web3Auth.provider always returns the initial provider.chainId used at creation, even after the chain is correctly switched in Metamask.
Additional question: how to not force updating the user’s RPC if they already have a RPC configured? This is confusing for the user. I want to push that RPC if and only if the user does not have this chain configured in their wallet.
In another section of the code, I call addChainAndSwitch() within a button’s onClick handler, and it works without any issues. However, the problem arises when I invoke addChainAndSwitch() inside a useEffect.
Thanks for sharing the details, I tried the addChainAndSwitch on button click as well which works as expected. I’ll try to invoke inside useEffect and check.
Moreover on the issue of forcing user to update the RPC, I’ll talk to the team. This is happening because ChainConfig in Web3Auth has mandatory property rpcTarget. Since the chain you are trying to switch has different rpc url in Metamask, it shows the popup.
Maybe a workaround would be to try and switch the chain instead of adding. If it throws an error, maybe then do addChainAndSwitch. When you try to switch chain which is not present it’ll throw an error that chain is not present.
Hey, I’m still not able to reproduce the issue on my side, can you please join our Office Hours tomorrow. You can share your screen, and we can debug it together. Web3Auth Developer Office Hours
@ayush Are you saying you successfully called addChainAndSwitch() from within a useEffect? I’m quite confident that approach doesn’t work reliably. For example, when the user refreshes the page, the provider gets reconfigured to use the chainConfig specified in the Web3AuthProvider, which may not match the network expected by the current page. Forcing a network switch in a useEffect in such cases is unlikely to work.
Incidentally, the AAVE app behaves similarly: if the app’s selected network doesn’t match the provider’s network, they display a button to prompt the user to switch networks rather than forcing it within a hook.
I resolved this issue by adopting a similar approach to AAVE—displaying a button that prompts the user to switch networks if the provider’s chain ID doesn’t match the expected one. You can consider this issue resolved on my end.