Connect to Extension Wallet
Switch network

Switch network#

Chain switch#

wallet_switchEthereumChain

EIP-3326
This method is detailed in EIP-3326.

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

    1. 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.

HexDecimalNetwork
0x11Ethereum Main Network (Mainnet)
0x271110001ETHW
0x4266OKT Chain Mainnet
0x3856Binance Smart Chain Mainnet
0x89137Matic Mainnet
0xa86a43114Avax Mainnet
0xfa250Fantom Mainnet
0xa4b142161Arbitrum Mainnet
0xa10Optimism Mainnet
0x1925Cronos Mainnet
0x20198217Klaytn Mainnet
0x141321KCC Mainnet
0x4401088Metis Mainnet
0x120288Boba Mainnet
0x64100Gnosis Mainnet
0x5051285Moonriver Mainnet
0x5041284Moonbeam Mainnet
0x4061030Conflux 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.

HTML
JavaScript
<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);
  }
}