How to Detect Wallet Chain Changes in a Dapp: Read and Listen

A dapp can keep its chain state in sync by reading the connected wallet’s chain ID on load and listening for changes. When there is no injected wallet provider, show a connect or install message instead of trying to make a request.

How does a dapp detect the connected chain?

For an injected EIP-1193 provider, call eth_chainId to get the current chain ID, then listen for chainChanged to learn when it changes. Both use a hexadecimal string: Ethereum mainnet returns 0x1.

Suppose your app loads while MetaMask is connected to Ethereum mainnet. The initial request gives you 0x1, so you can map that value to your app’s Ethereum entry and load chain-specific data. The request reads the provider’s current state; it does not set the wallet’s chain.

In JavaScript, check that a provider exists before calling it. Then handle both the initial read and later events:

Illustrative code:const provider = window.ethereum; First test provider; when it is absent, show your connect or install message. Otherwise, call await provider.request({method: 'eth_chainId'}) and pass the result to your chain-state update function. Register provider.on('chainChanged', handler); inside handler, update the chain ID and reset chain-dependent state.

EIP-1193 standardises the provider request and the chainChanged event for injected wallets. The event supplies the new chain ID as a hex string, so the same mapping logic can process both the first read and subsequent switches. If the request fails or returns an ID your app does not support, show an error or unsupported-chain state rather than treating the chain as known.

How should your app handle a chain switch?

When the user switches networks, update the displayed chain and clear or reload data tied to the previous chain. If the app had a pending action, keep it from using stale chain-specific state. Account and chain changes can arrive independently, so keep each piece of provider state current.

Build your supported-chain mapping from chain IDs, such as the IDs in Chainlist’s EVM network directory. Chainlist records network chain IDs alongside RPC endpoints, native currencies, and related information; it can help you identify the ID your app should recognise. To go further on this question, Chainlist covers EVM networks and their chain IDs.

At chainlist.cc, the directory’s chain IDs connect the wallet’s reported value to the network details your dapp needs to display. Compare the provider’s hex value with your supported entries, normalising the format consistently before comparing. When there is no match, show which chain the wallet is on if you can identify it, and prompt the user to switch to a supported one.

Do not use net_version as a substitute for eth_chainId. The former returns a network ID, which can differ from the chain ID used to identify an EVM chain. Ethereum Classic illustrates the difference: its chain ID is 61, while its network ID is 1. Comparing that network ID with a chain-ID list could make your app mistake Ethereum Classic for Ethereum mainnet.

Read the chain again immediately before sending a transaction. The user may have switched wallets or networks since the page loaded, so validate the latest ID against the action’s supported chain before asking the wallet to sign. The change to check is the current chain; the transaction’s intended chain stays the same, which is why a mismatch should pause the send and prompt for a switch.

Related

How do I prompt users to switch to the right network?

Show the required network and the chain your app currently detects, then let the user switch through their wallet. Re-read eth_chainId after the switch and resume only when it matches the required chain.

How does WalletConnect report the active chain?

Use the connected WalletConnect provider’s EIP-1193 request and chain-change event when available, applying the same initial-read and live-update pattern. Handle provider errors and unsupported chain IDs just as you would for an injected wallet.

Comments

Popular posts from this blog

How to Judge a Changing Swap Quote Before You Sign

Token Allowlists Explained for Cross-Chain Routes

How Do You Set a Bitcoin Fee for a Swap?