Switch network#
Chain switch#
wallet_switchEthereumChain
Description
This request asks the user if they are switching to a chain with a specified chainId
and returns a value of confirmation.
As with any method that returns a confirmation, wallet_switchEthereumChain
should only be called in response to a direct user action, such as a button click.
OKX will automatically reject the request under the following circumstances:
- If the chain ID is incorrectly formatted;
- If the chain with the specified chain ID has not been added to OKX.
We recommend that you use wallet_addEthereumChain
together with it.
try {
await okxwallet.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: '0xf00' }],
});
} catch (switchError) {
// This error code indicates that the chain has not been added to OKX.
if (switchError.code === 4902) {
try {
await okxwallet.request({
method: 'wallet_addEthereumChain',
params: [
{
chainId: '0xf00',
chainName: '...',
rpcUrls: ['https://...'] /* ... */,
},
],
});
} catch (addError) {
// handle "add" error
}
}
// handle other "switch" errors
}
Parameters
-
Array
SwitchEthereumChainParameter
- The metadata of the chain that OKX will switch to.
interface SwitchEthereumChainParameter {
chainId: string; // A 0x-prefixed hexadecimal string
}
Chain IDs
These are the IDs of the Ethereum chains that OKX Wallet supports by default. Consult chainid.network for more.
Hex | Decimal | Network |
---|---|---|
0x1 | 1 | Ethereum Main Network (Mainnet) |
0x2711 | 10001 | ETHW |
0x42 | 66 | OKT Chain Mainnet |
0x38 | 56 | Binance Smart Chain Mainnet |
0x89 | 137 | Matic Mainnet |
0xa86a | 43114 | Avax Mainnet |
0xfa | 250 | Fantom Mainnet |
0xa4b1 | 42161 | Arbitrum Mainnet |
0xa | 10 | Optimism Mainnet |
0x19 | 25 | Cronos Mainnet |
0x2019 | 8217 | Klaytn Mainnet |
0x141 | 321 | KCC Mainnet |
0x440 | 1088 | Metis Mainnet |
0x120 | 288 | Boba Mainnet |
0x64 | 100 | Gnosis Mainnet |
0x505 | 1285 | Moonriver Mainnet |
0x504 | 1284 | Moonbeam Mainnet |
0x406 | 1030 | Conflux eSpace |
Return value
null
- The method returns null
if the request was successful; otherwise, it will return an error.
If the error code (error.code
) is 4902
, then the requested chain has not been added by OKX, and you have to request to add it via wallet_addEthereumChain
.
Example
Open in codeopen.
<button class="connectEthereumButton btn">Connect Ethereum</button>
<button class="switchChainButton btn">Switch Chain</button>
const connectEthereumButton = document.querySelector('.connectEthereumButton');
const switchChainButton = document.querySelector('.switchChainButton');
let accounts = [];
//Sending Ethereum to an address
switchChainButton.addEventListener('click', () => {
try {
const chainId = okxwallet.chainId === "0x42" ? "0x38" : "0x42";
await okxwallet.request({
method: "wallet_switchEthereumChain",
params: [{ chainId: chainId }]
});
} catch (switchError) {
// This error code indicates that the chain has not been added to OKX Wallet.
if (error.code === 4902) {
try {
await okxwallet.request({
method: "wallet_addEthereumChain",
params: [{ chainId: "0xf00", rpcUrl: "https://..." /* ... */ }]
});
} catch (addError) {
// handle "add" error
}
}
// handle other "switch" errors
}
});
connectEthereumButton.addEventListener('click', () => {
getAccount();
});
async function getAccount() {
try{
accounts = await okxwallet.request({ method: 'eth_requestAccounts' });
}catch(error){
console.log(error);
}
}