Skip to Content
dApp Integration

wallet_switchEthereumChain

Requests that the wallet switch to the specified chain. If the chain is not already configured in the wallet, an error will be thrown.


Parameters

Array containing a single object:

FieldTypeRequiredDescription
chainIdstringYesChain ID (hex string with 0x prefix)

Returns

null on success


Example

try { await window.$onekey.ethereum.request({ method: 'wallet_switchEthereumChain', params: [{ chainId: '0x89' }] // Polygon }) console.log('Switched to Polygon') } catch (error) { if (error.code === 4902) { // Chain not added, try adding it await window.$onekey.ethereum.request({ method: 'wallet_addEthereumChain', params: [{ /* chain config */ }] }) } }

Common Chain IDs

ChainChain ID (Hex)Chain ID (Decimal)
Ethereum0x11
Polygon0x89137
BSC0x3856
Arbitrum0xa4b142161
Optimism0xa10
Base0x21058453
Avalanche0xa86a43114

Errors

CodeMessageDescription
4001User rejected the requestUser denied the switch
4902Unrecognized chain IDChain not configured in wallet

Notes

  • If chain is not configured, use wallet_addEthereumChain first
  • User may need to approve the switch
  • Listen to chainChanged event to detect when switch completes
  • Specified by EIP-3326 
window.$onekey.ethereum.on('chainChanged', (chainId) => { console.log('Chain changed to:', chainId) // Recommended: reload the page window.location.reload() })
Last updated on