# Wallet.addEthereumChain

Suggests a chain for the wallet to add (EIP-3085). Adding a chain does not switch to it.

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

const add = Wallet.addEthereumChain({
  chainId: 8453,
  chainName: "Base",
  nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
  rpcUrls: ["https://mainnet.base.org"],
  blockExplorerUrls: ["https://basescan.org"],
});

await add.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.

### Then switch to it

The wallet stays on its current chain after the chain is added. Ask for the switch with
[`Wallet.switchEthereumChain`](https://ethect.dev/rpc/wallet/switch-ethereum-chain/).

```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"],
};

const useBase = Effect.gen(function* () {
  yield* Wallet.addEthereumChain(base);
  yield* Wallet.switchEthereumChain({ chainId: base.chainId });
});
```

### When the user declines

The user can refuse the chain in the wallet. Catch the rejection to carry on without it.

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

const added = Wallet.addEthereumChain({ chainId: 8453, chainName: "Base" }).pipe(
  Effect.as(true),
  Effect.catchReason("RpcError", "UserRejectedRequest", () =>
    Effect.succeed(false),
  ),
);
```

## Returns

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

Nothing. The effect succeeds when the wallet added the chain, and fails otherwise.

## Parameters

### chain

- **Type:** [`WalletChain.WalletChain`](https://ethect.dev/rpc/types/wallet-chain/)

The chain to add, as one object.

| Field | Type | Description |
| --- | --- | --- |
| `chainId` | `number` | the id of the chain |
| `chainName` | `string` (optional) | the name the wallet shows |
| `nativeCurrency` | `{ name, symbol, decimals }` (optional) | the currency that pays for gas |
| `rpcUrls` | `ReadonlyArray<string>` (optional) | the endpoints the wallet reaches the chain through |
| `blockExplorerUrls` | `ReadonlyArray<string>` (optional) | block explorers for the chain |
| `iconUrls` | `ReadonlyArray<string>` (optional) | icons for the chain |

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

const add = Wallet.addEthereumChain({
  chainId: 10,
  chainName: "OP Mainnet",
  nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
  rpcUrls: ["https://mainnet.optimism.io"],
  blockExplorerUrls: ["https://optimistic.etherscan.io"],
  iconUrls: ["https://app.example/optimism.svg"],
});
```

## Behavior

- **Does not switch.** By the EIP, an app must not assume that the wallet selects a chain
  once it is added.
- **Give more than the id.** The type only requires `chainId`, as the EIP does. A wallet that
  does not know the chain rejects a request without a name, a currency and RPC URLs.
- **The chain id is a `number`.** `8453` is sent as `"0x2105"`. `decimals` stays a JSON number.
- **Nothing to return.** The result is `null` by the EIP. Whatever the wallet answers is
  ignored: the effect succeeding is the answer.

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