Switch Ethereum Chain
Switch between EVM compatible chains
Start from @blocto/sdk
version ^0.6.0
, we support two new RPC method wallet_switchEthereumChain
and wallet_addEthereumChain
which let developers be able to switch between EVM compatible chains easier.
wallet_addEthereumChain
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 EIP-3085.
Parameters
An array containing an object containing the following metadata about the chain to be added to Blocto SDK:
chainId
- The chain ID as a0x
-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.
Example
We recommend using this method before wallet_switchEthereumChain
:
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.
This method is specified by EIP-3326.
Parameters
An array containing an object containing chainId
, the chain ID as a 0x
-prefixed hexadecimal string.
Returns
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
.
Example
See the wallet_addEthereumChain
Example.
Batch Add Ethereum Chain
You can also add multiple params in wallet_addEthereumChain
.
Example
Then you can switch between BSC and polygon using wallet_switchEthereumChain
method.
Sample Code
Last updated