Switch Ethereum Chain
Switch between EVM compatible chains
Last updated
Was this helpful?
Switch between EVM compatible chains
Last updated
Was this helpful?
Start from @blocto/sdk
version ^0.4.1
, we support two new RPC method wallet_switchEthereumChain
and wallet_addEthereumChain
let developers easier to switch between EVM compatible chains​.
This method will add the specified chain into Blocto SDK instance.
Blocto SDK will validate the parameters for this method, and rejects the request if any parameter is incorrectly formatted. Blocto SDK also rejects the request if any of the following occurs:
Provided chainId
isn't supported yet.
The RPC endpoint isn't in our whitelist.
The RPC endpoint doesn't respond to RPC calls.
The RPC endpoint returns a different chain ID when eth_chainId
is called.
This method is specified by .
An array containing an object containing the following metadata about the chain to be added to Blocto SDK:
chainId
- The chain ID as a 0x
-prefixed hexadecimal string or number.
rpcUrls
- An array of RPC URL strings. At least one item is required, and only the first item is used.
Note: At least one metadata is required, and only the first metadata in array is used. If you wish to load multiple chain metadata, please check Batch Add Ethereum Chain part.
Returns
null
if the request was successful, and an error otherwise.
We recommend using this method before wallet_switchEthereumChain
:
This method will switch to the chain with the specified chain ID.
Blocto SDK rejects the request if any of the following occurs:
The chain ID is malformed.
The chain with the specified chain ID hasn't been added to Blocto SDK.
An array containing an object containing chainId
, the chain ID as a 0x
-prefixed hexadecimal string.
null
if the request was successful, and an error otherwise.
If the error code is 4902
, the requested chain hasn't been added by Blocto SDK, and you must request to add it using wallet_addEthereumChain
.
See the wallet_addEthereumChain
Example​.
You can also add multiple params in wallet_addEthereumChain
.
Then you can switch between BSC and polygon using wallet_switchEthereumChain
method.
This method is specified by .