Skip to Content
dApps

wallet_addEthereumChain

新しいチェーンの追加をウォレットに要求します。すでに存在するチェーンの場合は、そのチェーンに切り替わることがあります。


パラメーター

Array 単一のチェーン設定オブジェクトを含みます:

フィールド型必須説明
chainIdstringはいチェーン ID (0x プレフィックス付きの 16 進数文字列)
chainNamestringはい読みやすいチェーン名
nativeCurrencyobjectはいネイティブ通貨の情報
nativeCurrency.namestringはい通貨名
nativeCurrency.symbolstringはいネイティブ通貨のシンボル
nativeCurrency.decimalsnumberはい通貨の小数桁数 (通常は 18)
rpcUrlsstring[]はいRPC URL の配列 (1 つ以上)
blockExplorerUrlsstring[]いいえブロックエクスプローラー URL の配列
iconUrlsstring[]いいえ任意のアイコン URL。表示はウォレットによって異なります

戻り値

null 成功時


例

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

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'] }] })

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'] }] })

エラー

コードメッセージ説明
4001ユーザーがリクエストを拒否ユーザーがチェーンの追加を拒否
-32602無効なパラメーター無効なチェーン設定

注意事項

  • 有効な RPC エンドポイントを指定し、対象ネットワークが求めるメタデータを使用してください
  • ウォレットは RPC エンドポイントが正しく応答することを検証します
  • チェーンを追加しても、そのチェーンへの切り替えは保証されません。操作後に eth_chainId を読み取って確認してください
  • 次で規定されています: EIP-3085 

追加と切り替えの組み合わせ

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] }) await window.$onekey.ethereum.request({ method: "wallet_switchEthereumChain", params: [{ chainId: chainConfig.chainId }] }) } else { throw error } } }
最終更新日