このページの内容
wallet_switchEthereumChain
指定されたチェーンへの切り替えをウォレットに要求します。そのチェーンがウォレットに設定されていない場合は、エラーがスローされます。
パラメーター
Array 単一のオブジェクトを含みます:
| フィールド | 型 | 必須 | 説明 |
|---|---|---|---|
chainId | string | はい | チェーン 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 進数) |
|---|---|---|
| Ethereum | 0x1 | 1 |
| Polygon | 0x89 | 137 |
| BSC | 0x38 | 56 |
| Arbitrum | 0xa4b1 | 42161 |
| Optimism | 0xa | 10 |
| Base | 0x2105 | 8453 |
| Avalanche | 0xa86a | 43114 |
エラー
| コード | メッセージ | 説明 |
|---|---|---|
| 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()
})最終更新日