Skip to content

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.

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.

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),
),
);

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

Effect<void, Method.Error, Transport>

Nothing. The effect succeeds when the wallet switched its active chain, and fails otherwise.

  • 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";
// Base
const switching = Wallet.switchEthereumChain({ chainId: 8453 });
  • The chain id is a number. { chainId: 10 } is sent as { "chainId": "0xa" }. See values on the wire.
  • Nothing to return. The result is null by the EIP. Whatever the wallet answers is ignored: the effect succeeding is the answer.
  • The change is also an event. Eip1193.chainChanged is the stream of the wallet’s chain id, each time it changes: see Eip1193.

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

wallet_switchEthereumChain