# Wallet methods (Wallet)

The wallet namespace: what a wallet serves through an EIP-1193 provider such as
`window.ethereum`. Each export is one JSON-RPC method as a typed function, and the `eth_`
methods a wallet shares with a node are [re-exported from `Eth`](#shared-with-eth).

```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 program = Effect.gen(function* () {
  const accounts = yield* Wallet.requestAccounts();
  const chainId = yield* Wallet.chainId();
  return { accounts, chainId };
});

const session = await program.pipe(
  Effect.provide(Transport.eip1193(provider)),
  Effect.runPromise,
);
```

A method takes its JSON-RPC params in order and returns an
[`Effect`](https://ethect.dev/rpc/method/#calling-a-method) of the decoded result. It runs over
[`Transport.eip1193`](https://ethect.dev/reference/transport/eip1193/), which turns the provider into a
transport: see [Wallets](https://ethect.dev/guides/wallets/).

The name is the wire name without its prefix: `wallet_sendCalls` is `Wallet.sendCalls`,
`eth_requestAccounts` is `Wallet.requestAccounts` and `personal_sign` is `Wallet.personalSign`.
The exception is `wallet_sendTransaction`, which is
[`Wallet.walletSendTransaction`](https://ethect.dev/rpc/wallet/wallet-send-transaction/) because
`Wallet.sendTransaction` is `eth_sendTransaction`.

## Accounts

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`requestAccounts`](https://ethect.dev/rpc/wallet/request-accounts/) | `eth_requestAccounts` | `Address[]` | Asks the user to connect accounts to the app (EIP-1102). |
| [`connect`](https://ethect.dev/rpc/wallet/connections/#connect) | `wallet_connect` | `{ accounts }` | Connects accounts and asks for capabilities in the same prompt (ERC-7846, a draft). |
| [`disconnect`](https://ethect.dev/rpc/wallet/connections/#disconnect) | `wallet_disconnect` | `void` | Disconnects the app's accounts (ERC-7846, a draft). |
| [`addSubAccount`](https://ethect.dev/rpc/wallet/connections/#addsubaccount) | `wallet_addSubAccount` | `SubAccount` | Has the wallet create or track an account that its main account owns (ERC-7895, a draft). |
| [`getPermissions`](https://ethect.dev/rpc/wallet/permissions/#getpermissions) | `wallet_getPermissions` | `Permission[]` | The permissions the user gave the app (EIP-2255). |
| [`requestPermissions`](https://ethect.dev/rpc/wallet/permissions/#requestpermissions) | `wallet_requestPermissions` | `Permission[]` | Asks the user for permissions (EIP-2255). |
| [`revokePermissions`](https://ethect.dev/rpc/wallet/permissions/#revokepermissions) | `wallet_revokePermissions` | `void` | Gives permissions back. |
| [`grantPermissions`](https://ethect.dev/rpc/wallet/permissions/#grantpermissions) | `wallet_grantPermissions` | `PermissionGrant` | Asks the user to let a session key or account act for them (ERC-7715, a draft). |

## Chains and assets

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`switchEthereumChain`](https://ethect.dev/rpc/wallet/switch-ethereum-chain/) | `wallet_switchEthereumChain` | `void` | Asks the wallet to make a chain its active one (EIP-3326). |
| [`addEthereumChain`](https://ethect.dev/rpc/wallet/add-ethereum-chain/) | `wallet_addEthereumChain` | `void` | Suggests a chain for the wallet to add (EIP-3085). |
| [`watchAsset`](https://ethect.dev/rpc/wallet/watch-asset/) | `wallet_watchAsset` | `boolean` | Suggests a token for the wallet to track (EIP-747). |
| [`getAssets`](https://ethect.dev/rpc/wallet/get-assets/) | `wallet_getAssets` | assets by chain | The assets the wallet holds for an account (ERC-7811, a draft). |

## Signing and sending

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`personalSign`](https://ethect.dev/rpc/wallet/personal-sign/) | `personal_sign` | `Hex` | Signs a message with an account's key (EIP-191). |
| [`walletSendTransaction`](https://ethect.dev/rpc/wallet/wallet-send-transaction/) | `wallet_sendTransaction` | `Hash` | `eth_sendTransaction` under the name some wallets serve it by. |

## Call batches

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`sendCalls`](https://ethect.dev/rpc/wallet/send-calls/) | `wallet_sendCalls` | `{ id }` | Asks the wallet to send a batch of calls (EIP-5792). |
| [`getCallsStatus`](https://ethect.dev/rpc/wallet/calls-status/#getcallsstatus) | `wallet_getCallsStatus` | `CallsStatus` | Where a batch stands, with its receipts once included. |
| [`showCallsStatus`](https://ethect.dev/rpc/wallet/calls-status/#showcallsstatus) | `wallet_showCallsStatus` | `void` | Has the wallet show the user where a batch stands. |
| [`getCapabilities`](https://ethect.dev/rpc/wallet/get-capabilities/) | `wallet_getCapabilities` | capabilities by chain | What the wallet can do for an account. |

## Shared with Eth

A wallet answers some `eth_` methods itself. `Wallet` re-exports their definitions from
[`Eth`](https://ethect.dev/rpc/eth/): they are the same functions, not copies.

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

const same = Wallet.chainId === Eth.chainId; // true
```

Each one is documented on its `Eth` page.

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`accounts`](https://ethect.dev/rpc/eth/accounts/) | `eth_accounts` | `Address[]` | The accounts the node or wallet holds keys for. |
| [`chainId`](https://ethect.dev/rpc/eth/chain-id/) | `eth_chainId` | `number` | The chain id of the network. |
| [`estimateGas`](https://ethect.dev/rpc/eth/estimate-gas/) | `eth_estimateGas` | `bigint` | The gas a transaction would need. |
| [`fillTransaction`](https://ethect.dev/rpc/eth/fill-transaction/) | `eth_fillTransaction` | `{ raw, tx }` | Fills in what a request leaves out, without sending it. |
| [`sendRawTransaction`](https://ethect.dev/rpc/eth/send-raw-transaction/) | `eth_sendRawTransaction` | `Hash` | Sends a signed, encoded transaction. |
| [`sendRawTransactionSync`](https://ethect.dev/rpc/eth/send-raw-transaction-sync/) | `eth_sendRawTransactionSync` | `Receipt` | The same, and waits for the receipt. |
| [`sendTransaction`](https://ethect.dev/rpc/eth/send-transaction/) | `eth_sendTransaction` | `Hash` | Signs and sends a transaction from an account the node or wallet holds. |
| [`sign`](https://ethect.dev/rpc/eth/sign/) | `eth_sign` | `Hex` | Signs a message with an account's key. |
| [`signTransaction`](https://ethect.dev/rpc/eth/sign-transaction/) | `eth_signTransaction` | `Hex` | Signs a transaction without sending it. |
| [`signTypedDataV4`](https://ethect.dev/rpc/eth/sign-typed-data-v4/) | `eth_signTypedData_v4` | `Hex` | Signs EIP-712 typed data. |
| [`syncing`](https://ethect.dev/rpc/eth/syncing/) | `eth_syncing` | `false` or progress | Whether the node is syncing, and how far it is. |

The other `Eth` methods are not re-exported. They run over the same transport: import `Eth`
for them.

## Behavior

- **Nothing to return.** `disconnect`, `revokePermissions`, `switchEthereumChain`,
  `addEthereumChain` and `showCallsStatus` have no result. Whatever the wallet answers is
  ignored, and the effect succeeds with `void`.
- **Drafts.** `connect`, `disconnect`, `addSubAccount`, `grantPermissions` and `getAssets`
  follow ERCs that are still drafts. Their shapes can change, and a wallet may not serve them.
- **A node is not a wallet.** anvil 1.9 serves `requestAccounts`, `personalSign` and
  `sendTransaction`, and answers every `wallet_` method with the reason `MethodNotFound`.
- **Events are streams.** The accounts and the chain of a wallet change without a call. Follow
  them with `Eip1193.accountsChanged` and `Eip1193.chainChanged`: see
  [`Eip1193`](https://ethect.dev/reference/eip1193/).

## Errors

A wallet rejects a call with a JSON-RPC error object. It becomes an `RpcError` whose
[reason](https://ethect.dev/reference/errors/rpc-error/#reasons) names the code.

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

const accounts = Wallet.requestAccounts().pipe(
  // the user closed the prompt
  Effect.catchReason("RpcError", "UserRejectedRequest", () =>
    Effect.succeed([]),
  ),
);
```

The reasons a wallet answers with:

| Reason | Code | When |
| --- | --- | --- |
| `UserRejectedRequest` | 4001 | the user rejected the request |
| `Unauthorized` | 4100 | the method or the account has not been authorized by the user |
| `UnsupportedMethod` | 4200 | the provider does not support the method |
| `Disconnected` | 4900 | the provider is disconnected from all chains |
| `ChainDisconnected` | 4901 | the provider is not connected to the requested chain |
| `UnrecognizedChain` | 4902 | the wallet does not know the chain |
| `UnsupportedNonOptionalCapability` | 5700 | the wallet lacks a capability that was not marked optional |
| `UnsupportedChainId` | 5710 | the wallet does not support the chain of a batch |
| `DuplicateId` | 5720 | a batch was already sent with this id |
| `UnknownBundleId` | 5730 | no batch was sent with this id |
| `BundleTooLarge` | 5740 | the batch is too large for the wallet |
| `AtomicReadyWalletRejectedUpgrade` | 5750 | the wallet could run the batch atomically after an upgrade, and the user rejected the upgrade |
| `AtomicityNotSupported` | 5760 | the batch requires atomic execution and the wallet cannot do it |

Codes 4001 to 4901 come from EIP-1193, and codes 5700 to 5760 from EIP-5792.
