# Wallet.sendCalls

Asks the wallet to send a batch of calls, and returns the id of the batch (EIP-5792).

## Usage

```ts
import { Effect } from "effect";
import { Address, 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 FROM = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const WETH = Address.makeUnsafe("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2");
const FRIEND = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");

const program = Wallet.sendCalls({
  version: "2.0.0",
  from: FROM,
  chainId: 1,
  atomicRequired: true,
  calls: [
    // deposit() on the WETH contract, with one ether
    { to: WETH, value: 10n ** 18n, data: "0xd0e30db0" },
    // then one ether to another account
    { to: FRIEND, value: 10n ** 18n },
  ],
});

const { id } = await program.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.

### Follow the batch

The id is what [`Wallet.getCallsStatus`](https://ethect.dev/rpc/wallet/calls-status/#getcallsstatus) takes.

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

const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");

const status = Effect.gen(function* () {
  const { id } = yield* Wallet.sendCalls({
    version: "2.0.0",
    chainId: 1,
    atomicRequired: false,
    calls: [{ to: TO, value: 1n }],
  });
  const { status } = yield* Wallet.getCallsStatus(id);
  return status; // 100 while the batch is pending
});
```

### All or nothing

`atomicRequired: true` asks the wallet to run every call or none. A wallet that cannot fails
with the reason `AtomicityNotSupported`.

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

const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");

const sent = Effect.gen(function* () {
  const calls = [{ to: TO, value: 1n }, { to: TO, value: 2n }];
  return yield* Wallet.sendCalls({
    version: "2.0.0",
    chainId: 1,
    atomicRequired: true,
    calls,
  }).pipe(
    Effect.catchReason("RpcError", "AtomicityNotSupported", () =>
      Wallet.sendCalls({ version: "2.0.0", chainId: 1, atomicRequired: false, calls }),
    ),
  );
});
```

### Ask for a capability

`capabilities` asks the wallet for optional features by name, such as a paymaster service. Mark
one `optional` so that a wallet without it still sends the batch.

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

const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");

const sponsored = Wallet.sendCalls({
  version: "2.0.0",
  chainId: 8453,
  atomicRequired: false,
  calls: [{ to: TO, value: 1n }],
  capabilities: {
    paymasterService: { url: "https://paymaster.example", optional: true },
  },
});
```

### When the user declines

The user can refuse the batch in the wallet. By the EIP, the wallet then sends none of the
calls.

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

const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");

const id = Effect.gen(function* () {
  const sent = yield* Wallet.sendCalls({
    version: "2.0.0",
    chainId: 1,
    atomicRequired: false,
    calls: [{ to: TO, value: 1n }],
  }).pipe(
    Effect.catchReason("RpcError", "UserRejectedRequest", () =>
      Effect.succeed(undefined),
    ),
  );
  return sent?.id;
});
```

## Returns

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

An object with the `id` of the batch, a `string`, and the `capabilities` the wallet attached to
its answer, if any. See [`Calls`](https://ethect.dev/rpc/types/calls/).

## Parameters

### calls

- **Type:** [`Calls.Calls`](https://ethect.dev/rpc/types/calls/)

The batch, as one object.

| Field | Type | Description |
| --- | --- | --- |
| `version` | `"2.0.0"` | the version of the EIP the request follows |
| `id` | `string` (optional) | an id of your choosing, unique per sender. The wallet makes one when left out |
| `from` | [`Address`](https://ethect.dev/reference/primitives/address/) (optional) | the sending account. The wallet lets the user choose when left out |
| `chainId` | `number` | the chain every call is sent on |
| `atomicRequired` | `boolean` | whether the wallet must run all the calls or none |
| `calls` | `ReadonlyArray<Calls.Call>` | the calls, in the order to send them |
| `capabilities` | [`WalletCapabilities.Request`](https://ethect.dev/rpc/types/wallet-capabilities/) (optional) | capabilities asked for, by name |

Each call has four optional fields.

| Field | Type | Description |
| --- | --- | --- |
| `to` | [`Address`](https://ethect.dev/reference/primitives/address/) (optional) | the account or contract to call |
| `data` | [`Hex`](https://ethect.dev/reference/primitives/hex/) (optional) | the calldata |
| `value` | `bigint` (optional) | the wei to send with the call |
| `capabilities` | [`WalletCapabilities.Request`](https://ethect.dev/rpc/types/wallet-capabilities/) (optional) | capabilities asked for this call only |

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

const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");

const sent = Wallet.sendCalls({
  version: "2.0.0",
  // your own id for the batch
  id: "0x2a",
  chainId: 1,
  atomicRequired: false,
  calls: [{ to: TO, value: 1n }],
});
```

## Behavior

- **An id, not a hash.** The result names the batch. The wallet may send it as one transaction
  or as several: their receipts come from
  [`Wallet.getCallsStatus`](https://ethect.dev/rpc/wallet/calls-status/#getcallsstatus).
- **Does not wait.** By the EIP, the wallet answers without waiting for the calls to be
  included in a block.
- **One version.** The type accepts `version: "2.0.0"` only.
- **Quantities are encoded.** `chainId: 1` is sent as `"0x1"` and `value: 10n ** 18n` as
  `"0xde0b6b3a7640000"`. Addresses are sent in their EIP-55 form, the one every wallet takes.
- **Your id comes back.** By the EIP, a wallet returns the `id` you gave, and rejects one it
  has already seen with the reason `DuplicateId`.
- **A bare id is read too.** A wallet that answers the id alone, as a string, gives `{ id }`.

## Errors

The three [errors](https://ethect.dev/reference/errors/) every method can fail with. The cases specific to this
method, from EIP-5792 and checked with a
[`MockProvider`](https://ethect.dev/reference/testing/mock-provider/):

| Error | Reason | When |
| --- | --- | --- |
| `RpcError` | `UserRejectedRequest` | the user rejected the batch (code 4001) |
| `RpcError` | `Unauthorized` | `from` is not connected or not in the wallet (code 4100) |
| `RpcError` | `UnsupportedNonOptionalCapability` | the wallet lacks a capability that was not marked `optional` (code 5700) |
| `RpcError` | `UnsupportedChainId` | the wallet does not support `chainId` (code 5710) |
| `RpcError` | `DuplicateId` | a batch was already sent with this `id` (code 5720) |
| `RpcError` | `BundleTooLarge` | the batch is too large for the wallet (code 5740) |
| `RpcError` | `AtomicReadyWalletRejectedUpgrade` | the wallet could run the batch atomically after an upgrade, and the user rejected the upgrade (code 5750) |
| `RpcError` | `AtomicityNotSupported` | `atomicRequired` is `true` and the wallet cannot run the batch atomically (code 5760) |
| `CodecError` | `on: "result"` | the wallet answers something that is neither an object with an `id` nor a string |

## JSON-RPC method

`wallet_sendCalls`
