Skip to main content

wallet_switchEthereumChain

Request UKey Wallet to switch to the specified EVM network. If the network has not been added to your wallet yet, please add it via wallet_addEthereumChain first.


Inputs

Pass an array whose first item is an object:

FieldVariantNeedRemarks
chainIdstringyesTarget chain ID in 0x-prefixed hex

return value

Returns null on success.


Demo

try {
await window.$ukey.ethereum.request({
method: "wallet_switchEthereumChain",
requestParams: [{ chainId: "0x89" }], // Note: Polygon
});
console.log("Changed over to Polygon");
} catch (error) {
if (error.code === 4902) {
// If the chain is missing, try adding it first
await window.$ukey.ethereum.request({
method: "wallet_addEthereumChain",
requestParams: [
{
/* Note: Chain configuration */
},
],
});
}
}

Common chain ID

chainhexadecimaldecimal
Ethereum0x11
Polygon0x89137
BSC0x3856
Arbitrum0xa4b142161
Optimism0xa10
Base0x21058453
Avalanche0xa86a43114

error code

CodeMessageDetails
4001User rejected the requestNetwork switch was rejected by the user
4902Chain ID is not recognizedWallet does not have this network configured

illustrate

  • If the target chain is not configured, 4902 will be returned, and the user can be guided to add a network.
  • Users may need to confirm the switch operation in their wallet.
  • chainChanged will be triggered after the switch is completed, please refresh the chain-related status in the event.
  • Follows EIP-3326 specification.
window.$ukey.ethereum.on("chainChanged", (chainId) => {
console.log("Changed over to chain:", chainId);
// Suggested action: reload the page
window.location.reload();
});