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.
With a Chain
Section titled “With a Chain”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.
What it is for
Section titled “What it is for”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.
Returns
Section titled “Returns”Effect<void, Signer.Rejected | Signer.Error, Connection.Connection>
Nothing. The new chain is in the state.
Parameters
Section titled “Parameters”- 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.
Behavior
Section titled “Behavior”- The wallet is asked where it is afterwards. Not every wallet says that it changed
chain. After the switch,
eth_chainIdis 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
SignerRejectedand the state is as it was. - It waits for a reconnection. While the state is
reconnectingandreconnectruns, 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, writeTransport.on(state.chainId).
Errors
Section titled “Errors”| 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 |
Under the hood
Section titled “Under the hood”Only the wallet is asked.
- the wallet
wallet_switchEthereumChain({ chainId }) 4902, and a
Chainwas given- the wallet
wallet_addEthereumChain({ chainId, chainName, nativeCurrency, rpcUrls, blockExplorerUrls }) - the wallet
wallet_switchEthereumChain({ chainId })again
- the wallet
4902, and only an idfails with
SignerError"UnknownChain"4001fails with
SignerRejected- the wallet
eth_chainIdwhere the wallet is now: the state has it another chain than askedfails with
SignerError"ChainMismatch"