Skip to content

Connection.switchChain

Asks the connected wallet to change chain. Given a Chain, a wallet that does not know the chain is offered to add it.

import { Effect } from "effect";
import { Connection } from "@ethect/core";
// The wallet is on a chain this app has no node for: a button brings it to Base.
const toBase = Effect.gen(function* () {
yield* Connection.switchChain(8453);
return yield* Connection.get;
// { status: "connected", wallet: "io.metamask", accounts: ["0x…"], chainId: 8453 }
}).pipe(
Effect.catchTag("SignerRejected", () => Connection.get), // the user refused: the state is as it was
);

When it succeeds, the wallet is on that chain and the state says so.

A wallet only switches to a chain it knows. Give the chain itself, and a wallet that does not know it is offered it first.

import { Effect } from "effect";
import { type Chain, Connection } from "@ethect/core";
const base: Chain.Chain = {
id: 8453,
name: "Base",
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
rpcUrls: ["https://mainnet.base.org"],
explorer: "https://basescan.org",
};
// A wallet that knows Base switches. One that does not is asked to add it, then to switch.
const toBase = Connection.switchChain(base);
The wallet switchChain(8453) switchChain(base)
knows the chain switches switches. The chain is not offered
does not know it fails with SignerError "UnknownChain" is offered the chain, then switches

There is no registry of chains: a Chain is plain data that your application writes.

A send does not need it. With Signer.connected, a transaction for another chain than the wallet’s asks the wallet to switch by itself. switchChain is for a button: the wallet is on a chain your app does not support, and you offer the way back.

Effect<void, Signer.Rejected | Signer.Error, Connection.Connection>

Nothing. The new chain is in the state.

  • Type: number | Chain.Chain

The chain to switch to: its id, or the chain itself. Only a Chain says enough for a wallet to add it.

  • The wallet is asked where it is afterwards. Not every wallet says that it changed chain. After the switch, eth_chainId is asked, and the state has that answer.
  • A switch is checked. A wallet that answers yes and is still on another chain fails with SignerError "ChainMismatch". The state then has the chain the wallet is on.
  • A chain is offered once. A wallet that still does not know the chain after it was offered fails with "UnknownChain".
  • A refusal changes nothing. Whether the user refuses to switch or to add the chain, the failure is SignerRejected and the state is as it was.
  • It waits for a reconnection. While the state is reconnecting and reconnect runs, it waits for it to end.
  • The chain of your program does not move. Reads still go to the node of your Transport. To read on the wallet’s chain, write Transport.on(state.chainId).
Error Reason When
SignerRejected 4001: the user refused to switch, or to add the chain
SignerError "Disconnected" no wallet is connected, or it answered 4900, 4901
SignerError "UnknownChain" 4902: the wallet does not know the chain, and no Chain was given. cause is the wallet’s error
SignerError "ChainMismatch" the wallet said it switched, and is on another chain
SignerError "Unknown" any other answer, with the wallet’s error as cause, or the CodecError of an answer that cannot be read

Only the wallet is asked.

  1. the walletwallet_switchEthereumChain({ chainId })
  2. 4902, and a Chain was given

    1. the walletwallet_addEthereumChain({ chainId, chainName, nativeCurrency, rpcUrls, blockExplorerUrls })
    2. the walletwallet_switchEthereumChain({ chainId })again
  3. 4902, and only an idfails with SignerError "UnknownChain"

  4. 4001fails with SignerRejected

  5. the walleteth_chainIdwhere the wallet is now: the state has it
  6. another chain than askedfails with SignerError "ChainMismatch"