# Connection.switchChain

Asks the connected wallet to change chain. Given a [`Chain`](https://ethect.dev/reference/chain/), a wallet that
does not know the chain is offered to add it.

## Usage

```ts
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

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.

```ts
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`](https://ethect.dev/reference/chain/) is plain data that your
application writes.

### What it is for

A send does not need it. With [`Signer.connected`](https://ethect.dev/reference/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

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

Nothing. The new chain is in the [state](https://ethect.dev/reference/connection/state/).

## Parameters

### chain

- **Type:** `number | Chain.Chain`

The chain to switch to: its id, or [the chain itself](https://ethect.dev/reference/chain/). Only a `Chain` says
enough for a wallet to add it.

## Behavior

- **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`](https://ethect.dev/reference/connection/connect/#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`](https://ethect.dev/reference/transport/). To read on the wallet's chain, write
  `Transport.on(state.chainId)`.

## 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

Only the wallet is asked.

- **the wallet** `wallet_switchEthereumChain({ chainId })`
  - 4902, and a `Chain` was given:
    - **the wallet** `wallet_addEthereumChain({ chainId, chainName, nativeCurrency, rpcUrls, blockExplorerUrls })`
    - **the wallet** `wallet_switchEthereumChain({ chainId })`: again
  - 4902, and only an id → fails: `SignerError` `"UnknownChain"`
  - 4001 → fails: `SignerRejected`
- **the wallet** `eth_chainId`: where the wallet is now: the state has it
  - another chain than asked → fails: `SignerError` `"ChainMismatch"`
