Wallet.addEthereumChain
Suggests a chain for the wallet to add (EIP-3085). Adding a chain does not switch to it.
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. The examples
below leave the transport out.
Then switch to it
Section titled “Then switch to it”The wallet stays on its current chain after the chain is added. Ask for the switch with
Wallet.switchEthereumChain.
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
Section titled “When the user declines”The user can refuse the chain in the wallet. Catch the rejection to carry on without it.
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
Section titled “Returns”Effect<void, Method.Error, Transport>
Nothing. The effect succeeds when the wallet added the chain, and fails otherwise.
Parameters
Section titled “Parameters”- Type:
WalletChain.WalletChain
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 |
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
Section titled “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.8453is sent as"0x2105".decimalsstays a JSON number. - Nothing to return. The result is
nullby the EIP. Whatever the wallet answers is ignored: the effect succeeding is the answer.
Errors
Section titled “Errors”The three errors every method can fail with. The cases specific to this
method, checked with a MockProvider 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
Section titled “JSON-RPC method”wallet_addEthereumChain