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

## Usage

```ts
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`](https://ethect.dev/reference/transport/eip1193/). The examples
below leave the transport out.

### A chain the wallet does not know

Catch the reason `UnrecognizedChain`, add the chain with
[`Wallet.addEthereumChain`](https://ethect.dev/rpc/wallet/add-ethereum-chain/), then switch again.

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

The user can refuse the switch in the wallet. Catch the rejection to stay on the current
chain.

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

`Wallet.chainId` returns the chain the wallet is on, as a `number`.

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

[`Effect<void, Method.Error, Transport>`](https://ethect.dev/rpc/method/#calling-a-method)

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

## Parameters

### chain

- **Type:** `{ chainId: number }`

The chain to switch to, by its id. The method takes one object, as the EIP defines it.

```ts
import { Wallet } from "@ethect/core/rpc";

// Base
const switching = Wallet.switchEthereumChain({ chainId: 8453 });
```

## Behavior

- **The chain id is a `number`.** `{ chainId: 10 }` is sent as `{ "chainId": "0xa" }`. See
  [values on the wire](https://ethect.dev/rpc/guides/values/).
- **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`](https://ethect.dev/reference/eip1193/).

## Errors

The three [errors](https://ethect.dev/reference/errors/) every method can fail with. The cases specific to this
method, checked with a [`MockProvider`](https://ethect.dev/reference/testing/mock-provider/) 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

`wallet_switchEthereumChain`
