Wallet.switchEthereumChain
Asks the wallet to make a chain its active one (EIP-3326). A wallet that does not know the
chain fails with the reason UnrecognizedChain.
import { Effect } from "effect";import { type Eip1193, Transport } from "@ethect/core";import { Wallet } from "@ethect/core/rpc";
// The wallet's EIP-1193 provider: `window.ethereum`, or the one a wallet SDK returns.declare const provider: Eip1193.Provider;
await Wallet.switchEthereumChain({ chainId: 10 }).pipe( Effect.provide(Transport.eip1193(provider)), Effect.runPromise,);A wallet method runs over Transport.eip1193. The examples
below leave the transport out.
A chain the wallet does not know
Section titled “A chain the wallet does not know”Catch the reason UnrecognizedChain, add the chain with
Wallet.addEthereumChain, then switch again.
import { Effect } from "effect";import { Wallet } from "@ethect/core/rpc";
const base = { chainId: 8453, chainName: "Base", nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 }, rpcUrls: ["https://mainnet.base.org"], blockExplorerUrls: ["https://basescan.org"],};
const toBase = Wallet.switchEthereumChain({ chainId: base.chainId }).pipe( Effect.catchReason("RpcError", "UnrecognizedChain", () => Wallet.addEthereumChain(base).pipe( Effect.andThen(Wallet.switchEthereumChain({ chainId: base.chainId })), ), ),);Adding a chain does not switch to it, which is why the switch is asked a second time.
When the user declines
Section titled “When the user declines”The user can refuse the switch in the wallet. Catch the rejection to stay on the current chain.
import { Effect } from "effect";import { Wallet } from "@ethect/core/rpc";
const switched = Wallet.switchEthereumChain({ chainId: 10 }).pipe( Effect.as(true), Effect.catchReason("RpcError", "UserRejectedRequest", () => Effect.succeed(false), ),);Check the active chain
Section titled “Check the active chain”Wallet.chainId returns the chain the wallet is on, as a number.
import { Effect } from "effect";import { Wallet } from "@ethect/core/rpc";
const onOptimism = Effect.gen(function* () { if ((yield* Wallet.chainId()) === 10) return; yield* Wallet.switchEthereumChain({ chainId: 10 });});Returns
Section titled “Returns”Effect<void, Method.Error, Transport>
Nothing. The effect succeeds when the wallet switched its active chain, and fails otherwise.
Parameters
Section titled “Parameters”- Type:
{ chainId: number }
The chain to switch to, by its id. The method takes one object, as the EIP defines it.
import { Wallet } from "@ethect/core/rpc";
// Baseconst switching = Wallet.switchEthereumChain({ chainId: 8453 });Behavior
Section titled “Behavior”- The chain id is a
number.{ chainId: 10 }is sent as{ "chainId": "0xa" }. See values on the wire. - Nothing to return. The result is
nullby the EIP. Whatever the wallet answers is ignored: the effect succeeding is the answer. - The change is also an event.
Eip1193.chainChangedis the stream of the wallet’s chain id, each time it changes: seeEip1193.
Errors
Section titled “Errors”The three errors every method can fail with. The cases specific to this
method, checked with a MockProvider and against
anvil 1.9:
| Error | Reason | When |
|---|---|---|
RpcError |
UnrecognizedChain |
the wallet does not know the chain (code 4902) |
RpcError |
UserRejectedRequest |
the user rejected the request (code 4001, from EIP-1193) |
RpcError |
MethodNotFound |
the call goes to a node: anvil does not serve the method |
CodecError |
on: "params" |
chainId is negative or not an integer: nothing is sent |
JSON-RPC method
Section titled “JSON-RPC method”wallet_switchEthereumChain