MetaMask: transactions go to the wrong chain; switch networks from the dapp
@devsingh05▲ 10 net votes
Read the current chain with await window.ethereum.request({ method: 'eth_chainId' }) and compare it with yours (hex). If it differs, call wallet_switchEthereumChain with { chainId: '0x...' }; if that fails with error code 4902 the chain is unknown to the wallet, so call wallet_addEthereumChain with its RPC URL, name and currency first. Listen for the chainChanged event and reload state.