Skip to the article
Web3 Hub

Markets, protocols and policy, reported

Check wallet chain ID before a swap to block wrong-chain requests

How to check a wallet chain ID before a token swap: call eth_chainId on the selected provider, compare it with your target, and block requests until they match.

By Web3 Hub Newsroom2 min read

Check wallet chain ID before a swap to block wrong-chain requests

Call eth_chainId on the selected EIP-1193 wallet provider and compare its normalized value with the intended chain ID before requesting a swap; application configuration can differ from wallet state.

CoW Swap is a decentralized exchange interface that uses signed trade intents and solver-run batch auctions. When preparing a trade there, cowswap fees are another detail to consider alongside the chain used for the quote and signature.

Have the provider selected by the user and a trusted chain configuration ready. Store the target chain ID as a value you can validate; Ethereum mainnet’s ID is 1, represented as 0x1.

Validate the wallet chain before the request

  1. Read the selected provider. Call await provider.request({method: 'eth_chainId'}) on the same EIP-1193 provider that will handle the wallet request. The result should be a hexadecimal string. A backend RPC call identifies that RPC endpoint’s chain, so it cannot establish which chain the user’s wallet provider is using.
  2. Validate and compare the IDs. Reject a response that is not a hexadecimal chain ID, then convert the returned value and trusted target to BigInt and compare them numerically. For example, 0x1 and 0x01 represent the same numeric ID. A matching comparison lets the interface show the selected network as ready; a mismatch keeps the swap request blocked.
  3. Resolve a mismatch, then read again. Offer a network switch only after a user action, using wallet_switchEthereumChain with the configured target. The wallet can reject the request or report that the chain is unsupported; show that outcome and keep the request blocked. After a successful switch, call eth_chainId again and compare the new result rather than assuming the switch took effect.
  4. Revalidate before signing. In a CoW Swap integration, keep the wallet chain, quote endpoint and signing domain aligned. Handle EIP-1193’s chainChanged event by invalidating pending quote or signing preparation and rereading the provider’s chain ID. Read and compare once more immediately before issuing the signing request. The task is done when matching IDs enable the request and the interface shows the matching network; malformed output, provider failure or a mismatch leaves it blocked.