Skip to Content
dApp Integration

wallet_addEthereumChain

Requests that the wallet adds a new chain. If the chain already exists, the wallet may switch to it.


Parameters

Array containing a single chain configuration object:

FieldTypeRequiredDescription
chainIdstringYesChain ID (hex string with 0x prefix)
chainNamestringYesHuman-readable chain name
nativeCurrencyobjectYesNative currency info
nativeCurrency.namestringYesCurrency name
nativeCurrency.symbolstringYesCurrency symbol (2-6 chars)
nativeCurrency.decimalsnumberYesCurrency decimals (usually 18)
rpcUrlsstring[]YesArray of RPC URLs (at least one)
blockExplorerUrlsstring[]NoArray of block explorer URLs
iconUrlsstring[]NoArray of icon URLs (currently ignored)

Returns

null on success


Example

await window.$onekey.ethereum.request({ method: 'wallet_addEthereumChain', params: [{ chainId: '0x89', chainName: 'Polygon Mainnet', nativeCurrency: { name: 'MATIC', symbol: 'MATIC', decimals: 18 }, rpcUrls: ['https://polygon-rpc.com'], blockExplorerUrls: ['https://polygonscan.com'] }] })

Add Base

await window.$onekey.ethereum.request({ method: 'wallet_addEthereumChain', params: [{ chainId: '0x2105', chainName: 'Base', nativeCurrency: { name: 'Ethereum', symbol: 'ETH', decimals: 18 }, rpcUrls: ['https://mainnet.base.org'], blockExplorerUrls: ['https://basescan.org'] }] })

Add Arbitrum

await window.$onekey.ethereum.request({ method: 'wallet_addEthereumChain', params: [{ chainId: '0xa4b1', chainName: 'Arbitrum One', nativeCurrency: { name: 'Ethereum', symbol: 'ETH', decimals: 18 }, rpcUrls: ['https://arb1.arbitrum.io/rpc'], blockExplorerUrls: ['https://arbiscan.io'] }] })

Errors

CodeMessageDescription
4001User rejected the requestUser denied adding the chain
-32602Invalid paramsInvalid chain configuration

Notes

  • Only the first element of rpcUrls and blockExplorerUrls arrays is used
  • The wallet validates that the RPC endpoint responds correctly
  • Cannot add chains with non-18 decimal native currencies (in some wallets)
  • Specified by EIP-3085 

Combined Add and Switch

async function switchToChain(chainConfig) { try { await window.$onekey.ethereum.request({ method: 'wallet_switchEthereumChain', params: [{ chainId: chainConfig.chainId }] }) } catch (error) { if (error.code === 4902) { await window.$onekey.ethereum.request({ method: 'wallet_addEthereumChain', params: [chainConfig] }) } else { throw error } } }
Last updated on