Skip to Content
dApps
このページの内容

wallet_switchEthereumChain

指定されたチェーンへの切り替えをウォレットに要求します。そのチェーンがウォレットに設定されていない場合は、エラーがスローされます。


パラメーター

Array 単一のオブジェクトを含みます:

フィールド型必須説明
chainIdstringはいチェーン ID (0x プレフィックス付きの 16 進数文字列)

戻り値

null 成功時


例

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: [chainConfig] // Define the full configuration for this chain first. }) await window.$onekey.ethereum.request({ method: "wallet_switchEthereumChain", params: [{ chainId: "0x89" }] }) } else { throw error } }

主なチェーン ID

チェーンチェーン ID (16 進数)チェーン ID (10 進数)
Ethereum0x11
Polygon0x89137
BSC0x3856
Arbitrum0xa4b142161
Optimism0xa10
Base0x21058453
Avalanche0xa86a43114

エラー

コードメッセージ説明
4001ユーザーがリクエストを拒否ユーザーが切り替えを拒否
4902認識できないチェーン IDウォレットにチェーンが未設定

注意事項

  • チェーンが未設定の場合は、先に wallet_addEthereumChain を実行します
  • 切り替えにはユーザーの承認が必要な場合があります
  • chainChanged イベントを監視して切り替え完了を検出します
  • 次で規定されています: EIP-3326 
window.$onekey.ethereum.on('chainChanged', (chainId) => { console.log('Chain changed to:', chainId) // Recommended: reload the page window.location.reload() })
最終更新日