# Transactions (Tx)

Sending [calls](https://ethect.dev/reference/call/) as transactions, in four functions. `plan` says how many
signatures the calls take, `submit` asks for one, `wait` follows what was submitted into a
block, and `send` is the three together. `receipt` says where one transaction stands now, and
waits for nothing. A [`Signer`](https://ethect.dev/reference/signer/) signs, the transport follows what was sent,
and every failure is a value with a `message`.

```ts
import { Effect } from "effect";
import { type Call, Tx } from "@ethect/core";

const four = (calls: ReadonlyArray<Call.Call<unknown, unknown>>) =>
  Effect.gen(function* () {
    const steps = yield* Tx.plan(calls); // one step per signature. Nothing is signed
    for (const step of steps) {
      const pending = yield* Tx.submit(step); // one signature. Hashes, or an id of the wallet
      yield* Tx.wait(pending); // in a block, as deep as asked
    }
    return yield* Tx.send(calls); // all of it, in one line
  });
```

| Export | Kind | Description |
| --- | --- | --- |
| [`Tx.send(calls, options?)`](https://ethect.dev/reference/tx/send/) | function | All of it: `plan`, then `submit` and `wait` for each signature. One call gives a receipt, several give the receipts in order. |
| [`Tx.plan(calls, options?)`](https://ethect.dev/reference/tx/plan/) | function | How many signatures the calls take, and what is in each. Asks nothing of the user. |
| [`Tx.submit(step, options?)`](https://ethect.dev/reference/tx/submit/) | function | One signature: a step, or calls to sign as one. Hands them to the network and does not wait. |
| [`Tx.wait(pending, options?)`](https://ethect.dev/reference/tx/wait/) | function | Waits for what was submitted to be in a block, as deep as asked. Fails if it reverted. |
| [`Tx.receipt(hash, options?)`](https://ethect.dev/reference/tx/receipt/) | function | Where one transaction stands now, in one request: its receipt, or none when it is in no block. Waits for nothing. |
| [`Tx.toJson(pending)`](https://ethect.dev/reference/tx/wait/#txtojson) | function | What a signer answered, as text: for local storage, a cookie, a database. |
| [`Tx.fromJson(text)`](https://ethect.dev/reference/tx/wait/#txfromjson) | function | The pending a text holds, or none. It never throws. |
| [`Tx.pendingSchema`](https://ethect.dev/reference/tx/wait/#txpendingschema) | schema | The schema of a pending, for data of your own: a field of a command or a message. It encodes the data `Tx.toJson` writes. |
| [`Tx.simulate(calls, options?)`](https://ethect.dev/reference/tx/simulate/) | function | Runs the calls in order, each seeing the ones before it. Sends nothing and needs no signer. |
| [`Tx.Broadcast`](https://ethect.dev/reference/tx/broadcast/) | reference | How the bytes a local signer signed reach the network. |
| [`Tx.Reverted`](https://ethect.dev/reference/tx/failures/#txreverted) | error | A call reverted, on chain or before anything was sent. `reason` is typed by the errors given. |
| [`Tx.Dropped`](https://ethect.dev/reference/tx/failures/#txdropped) | error | A transaction left the mempool, another one took its nonce, or a wallet gave up on a batch. |
| [`Tx.Unsupported`](https://ethect.dev/reference/tx/failures/#txunsupported) | error | The signer cannot do what was asked. `reason` says what. |
| [`Tx.Partial`](https://ethect.dev/reference/tx/failures/#txpartial) | error | Some of the calls went out before one was refused. `sent` has them. |
| [`Tx.isOutcome(error)`](https://ethect.dev/reference/tx/failures/#txisoutcome) | function | Whether a failed wait said how it ended: `Tx.Reverted` or `Tx.Dropped`. Anything else means nobody could tell. |
| [`Tx.Failure<E>`](https://ethect.dev/reference/tx/failures/) | type | Everything sending can fail with. `E` is the errors the calls declare. |
| [`Tx.Fields`](https://ethect.dev/reference/tx/submit/#txfields) | type | Transaction fields to set instead of leaving them to the wallet or to an estimate: `nonce`, `gas`, the fees, `accessList`, `type`. |
| [`Tx.Step`](https://ethect.dev/reference/tx/plan/#txstep) | type | One thing to sign, as `plan` decided it. Today a `Tx.Step.Calls`: `calls`, `atomic`, `from`, `chainId`. |
| [`Tx.Pending`](https://ethect.dev/reference/tx/wait/#txpending) | type | What a signer answered, as plain data: `Tx.Transactions` or `Tx.Handle`. |
| [`Tx.Transactions`](https://ethect.dev/reference/tx/wait/#txtransactions) | type | Transaction hashes, one per call, with their sender and chain. With a local key, what each went out with: its nonce, its gas and its fees. |
| [`Tx.Handle`](https://ethect.dev/reference/tx/wait/#txhandle) | type | An id only the signer can resolve, such as a wallet batch. `Tx.Handle.Status` is where it stands. |
| [`Tx.PlanOptions`](https://ethect.dev/reference/tx/plan/#parameters) | type | `batch` and `from`. |
| [`Tx.SubmitOptions`](https://ethect.dev/reference/tx/submit/#parameters) | type | `from`, `id`, `capabilities` and `transaction`. |
| [`Tx.WaitOptions`](https://ethect.dev/reference/tx/wait/#parameters) | type | `confirmations`, and the `errors` a revert may be. |
| [`Tx.ReceiptOptions`](https://ethect.dev/reference/tx/receipt/#parameters) | type | The `errors` a revert may be. |
| [`Tx.SendOptions`](https://ethect.dev/reference/tx/send/#parameters) | type | The options of the three, for `send`. |
| [`Tx.SimulateOptions`](https://ethect.dev/reference/tx/simulate/#parameters) | type | `from`, `block`, `stateOverride` and `method`. |
| [`Tx.Simulation`](https://ethect.dev/reference/tx/simulate/#returns) | type | What simulated calls did: `results`, and `gas` when the node gave it. |

## Failures

| Failure | `_tag` | Its `message` |
| --- | --- | --- |
| `Tx.Reverted` | `"TxReverted"` | built from `call` and `reason`: `The call at index 1 reverted: paused`. What the node said follows, when there is a `cause` |
| `Tx.Dropped` | `"TxDropped"` | built from `transaction` and `replacedBy` |
| `Tx.Unsupported` | `"TxUnsupported"` | given by the signer |
| `Tx.Partial` | `"TxPartial"` | given by the signer |

Sending also fails with [`Signer.Rejected`](https://ethect.dev/reference/signer/),
[`Signer.Error`](https://ethect.dev/reference/signer/), [`AbiError`](https://ethect.dev/reference/errors/abi-error/) and
[`Transport.Error`](https://ethect.dev/reference/transport/#types). `Tx.receipt` fails with `Tx.Reverted` and
`Transport.Error` only. `Tx.wait` alone fails with
[`Transport.UnknownChain`](https://ethect.dev/reference/transport/chains/#unknownchain), for what was submitted on another chain. Each is on the page of the
[failures](https://ethect.dev/reference/tx/failures/).

## Under the hood

The signer signs, and the node says what is in a block. Each function has its requests on its
page.

**`Tx.plan(calls)`**

- **the wallet** `wallet_getCapabilities`: for several calls: whether the wallet runs them as one. A local key is asked nothing

**`Tx.submit(step)`, with a wallet**

- **the wallet** `eth_chainId`: is the wallet on the chain of the step?
- **the wallet** `eth_sendTransaction`: one call: a hash
- **the wallet** `wallet_sendCalls`: several calls: the id of a batch

**`Tx.submit(step)`, with a local key**

- **your node**: the fees and the gas that are not set
- **your node** `eth_getTransactionCount(from, "pending")`: the first time only
- **your program**: the transaction is signed
- **your node** `eth_sendRawTransaction`: once per call

**`Tx.wait(pending)`**

- **your node** `eth_getTransactionReceipt`: asked first, one per transaction. All in a block: no block is watched
- **your node** `eth_getTransactionReceipt`: for a transaction that is in no block yet, at each head after the first
- **the wallet** `wallet_getCallsStatus`: for a batch, asked first: only its wallet knows the id, at each head while the batch is pending

**`Tx.receipt(hash)`**

- **your node** `eth_getTransactionReceipt(hash)`: once: the receipt, or none. Nothing is waited for

See [`Tx.plan`](https://ethect.dev/reference/tx/plan/#under-the-hood), [`Tx.submit`](https://ethect.dev/reference/tx/submit/#under-the-hood),
[`Tx.wait`](https://ethect.dev/reference/tx/wait/#under-the-hood), [`Tx.receipt`](https://ethect.dev/reference/tx/receipt/#under-the-hood)
and [`Tx.send`](https://ethect.dev/reference/tx/send/#under-the-hood).
