# Failures

What sending fails with. Each failure is a value with a tag and a message, and says what was
sent before it.

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

const outcome = (calls: ReadonlyArray<Call.Call<unknown, unknown>>) =>
  Tx.send(calls).pipe(
    Effect.map((receipts) => `Mined: ${receipts.length} receipts`),
    Effect.catchTags({
      SignerRejected: () => Effect.succeed("You refused to sign"),
      TxReverted: (error) => Effect.succeed(`Reverted, after ${error.receipts.length} receipts`),
      TxDropped: (error) => Effect.succeed(`Replaced by ${error.replacedBy ?? "nothing known"}`),
      TxPartial: (error) => Effect.succeed(`Stopped at call ${error.failedAt}`),
    }),
    // every failure has a message
    Effect.catch((error) => Effect.succeed(error.message)),
  );
```

| Failure | `_tag` | From | What was sent |
| --- | --- | --- | --- |
| [`Tx.Reverted`](#txreverted) | `"TxReverted"` | `submit`, `wait`, `receipt`, `send`, `simulate` | nothing, or what its `receipts` show |
| [`Tx.Dropped`](#txdropped) | `"TxDropped"` | `wait`, `send` | a transaction that will not be mined |
| [`Tx.Unsupported`](#txunsupported) | `"TxUnsupported"` | `plan`, `submit`, `send`, `simulate` | nothing |
| [`Tx.Partial`](#txpartial) | `"TxPartial"` | `submit`, `send` | the transactions in `sent` |
| [`Signer.Rejected`](https://ethect.dev/reference/signer/) | `"SignerRejected"` | `submit`, `send` | nothing: the user refused |
| [`Signer.Error`](https://ethect.dev/reference/signer/#failures) | `"SignerError"` | `plan`, `submit`, `wait`, `send` | nothing from `plan` and `submit`. From a wait, [nobody could say](#after-a-failure) |
| [`AbiError`](https://ethect.dev/reference/errors/abi-error/) | `"AbiError"` | `submit`, `send` | nothing: a call did not encode |
| [`Transport.UnknownChain`](https://ethect.dev/reference/transport/chains/#unknownchain) | `"TransportUnknownChain"` | `wait` | not this wait's to say: what was submitted is on another chain |
| [`RpcError`](https://ethect.dev/reference/errors/rpc-error/) | `"RpcError"` | all | see below |
| [`TransportError`](https://ethect.dev/reference/errors/transport-error/) | `"TransportError"` | all | see below |

`Tx.Failure<E>` is what [`Tx.send`](https://ethect.dev/reference/tx/send/) fails with: all of them but
`TransportUnknownChain`, since a send waits on the transport it has just submitted on. `E` is
the errors the calls declare.

`Tx.Dropped` and `Tx.Unsupported` are their own Schemas, as `Signer.Rejected`, `AbiError` and
`Transport.UnknownChain` are: declared in the failure of a command, each is written as its tag
and its fields, and read back as an instance of its class. `new` checks nothing: with fields of their types, it never throws.

A `Transport.Error` from `submit` means the signer could not hand the transaction over. A local
signer has then given its nonce back, or sent the same bytes again for two minutes: see
[`Signer.Nonces`](https://ethect.dev/reference/signer/nonces/#what-the-node-answered). From `wait`, it means the
node could not say where the transaction stands, and the transaction may still be mined: wait
again. From [`Tx.receipt`](https://ethect.dev/reference/tx/receipt/) it means the same: ask again.

## After a failure

Whether to sign again depends on what the failure says of what was sent.

| Failure | What it says | Then |
| --- | --- | --- |
| `Tx.Reverted`, `Tx.Dropped` | it is over: in a block and reverted, or never to be in one. Nothing is still on its way | sign again, if it is still wanted |
| `Signer.Rejected`, `Tx.Unsupported` | nothing was sent | sign again, or another way |
| `Tx.Partial` | a part was sent: the transactions in `sent` | wait for `sent`, then send the calls from `failedAt` on |
| `RpcError`, `TransportError`, `Transport.UnknownChain`, `Signer.Error`, from a wait | nobody could say where it stands. It may still be mined | wait again. Do not sign again |

A wallet that does not know a batch is of the last kind: `Signer.Error` `"UnknownBatch"`. It is
not the wallet that took the batch, or it has forgotten it, and neither says the batch is over.

## Tx.isOutcome

`Tx.isOutcome(error)` is the first row of that table: `true` for `Tx.Reverted` and
`Tx.Dropped`, which say how a wait ended. Anything else from a wait means nobody could tell,
and waiting again may see what was sent mined.

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

const settled = (pending: Tx.Pending, step: Tx.Step) =>
  Tx.wait(pending, { errors: Err.of(step.calls) }).pipe(
    Effect.retry({
      // reverted or dropped is the answer. Anything else: nobody could tell, ask again
      while: (error) => !Tx.isOutcome(error),
      schedule: Schedule.spaced("3 seconds"),
    }),
  );
```

It is a type guard: where it is `true`, the failure is a `Tx.Reverted` or a `Tx.Dropped`, with
the errors the calls declare.

| Signature | Gives |
| --- | --- |
| `(error: unknown) => error is Tx.Reverted<unknown> \| Tx.Dropped` | whether `error` is either of the two, by its `_tag` |

`Tx.send` submits and waits. An `RpcError`, a `TransportError` or a `Signer.Error` from it does
not say which half failed. A program that must not sign twice keeps the `Tx.Pending` of
[`Tx.submit`](https://ethect.dev/reference/tx/submit/) and [waits](https://ethect.dev/reference/tx/wait/) for it itself.

## Tx.Reverted

A call reverted: in its block, or before anything was sent.

| Field | Type | Holds |
| --- | --- | --- |
| `reason` | `E \| Revert.Revert` | why: one of the errors of the calls, or a plain [`Revert`](https://ethect.dev/reference/revert/) |
| `call` | `number`, optional | which call, when it is known: its index among the calls given to the function that failed |
| `receipts` | `ReadonlyArray<Receipt>` | what is in a block, the reverted transaction included. Empty when nothing was sent |
| `cause` | [`RpcError`](https://ethect.dev/reference/errors/rpc-error/), optional | what the node said of a call that failed and did not revert, such as one that ran out of gas |
| `message` | `string` | `The call at index 1 reverted: ` and the message of the reason. With a `cause`, what the node said follows |

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

const why = <E extends { readonly _tag: string }>(call: Call.Call<unknown, E>) =>
  Tx.send(call).pipe(
    Effect.catchTag("TxReverted", (error) => {
      const reason: E | Revert.Revert = error.reason;
      // nothing sent when the estimate found it, one reverted receipt when a block did
      return Effect.succeed([reason._tag, error.receipts.length] as const);
    }),
  );
```

| From | When | `call` | `receipts` |
| --- | --- | --- | --- |
| `Tx.submit` | a local signer estimates, and a call would revert | its index | empty |
| `Tx.wait`, on `Tx.Transactions` | a transaction is in a block, reverted | its index among the hashes | every receipt of the wait |
| `Tx.wait`, on a hash or a handle | the same | none | every receipt of the wait |
| `Tx.receipt` | the same | none | the receipt of the transaction |
| `Tx.send` | either of the two | its index among the calls sent | those of the signatures before, then the above |
| `Tx.simulate` | a call fails as the calls are run | its index | empty |

`call` counts among the calls given to the function that failed. `Tx.wait(pending)` is given
the transactions of one signature: its `call` is an index among the calls of that step.
`Tx.send` is given every call: its `call` is an index in that list, whatever the steps were. A
program that plans and waits step by step adds the calls of the steps before.

The reason of a batch is the wallet's. A wallet runs several calls as one with code of its own,
and a batch that reverts reverts with what that code gives. So the `reason` may be a
[`Revert.Unknown`](https://ethect.dev/reference/revert/#what-is-unknown) even when the error of the contract is
declared. [`TestWallet`](https://ethect.dev/reference/testing/test-wallet/) reverts with the index of the call
that failed, as 32 bytes, followed by what that call reverted with.

A `cause` comes from a [simulation](https://ethect.dev/reference/tx/simulate/#behavior) with `eth_simulateV1`
alone: there the node answers for each call, and says why one failed when it did not revert.
A revert has no `cause`: its `reason` holds what the node said. What the node said ends the
`message`:

```text
The call at index 1 reverted: Reverted without data (the node said: out of gas)
```

## Tx.Dropped

A transaction left the mempool, another one took its nonce, or a wallet gave up on a batch.

| Field | Type | Holds |
| --- | --- | --- |
| `transaction` | [`Hash.Hash`](https://ethect.dev/reference/primitives/hash/), optional | the transaction that will not be mined. Absent for a wallet batch |
| `replacedBy` | `Hash.Hash`, optional | the transaction that took its nonce, when it was found |
| `receipts` | `ReadonlyArray<Receipt>` | what was mined before it |
| `message` | `string` | built from the two hashes |

Its `receipts` are written as a node writes them: quantities in hex, addresses in EIP-55. Its
`message` is not written: it is made from the hashes, on either side.

```json
{ "_tag": "TxDropped", "transaction": "0xabab…", "replacedBy": "0xcdcd…", "receipts": [] }
```

| When | `transaction` | `replacedBy` |
| --- | --- | --- |
| another call took its nonce: a cancellation | its hash | the hash that took it |
| its nonce was taken before the wait began | its hash | absent |
| the node never heard of the hash: three heads in a row, and two minutes | its hash | absent |
| a signer says its handle was dropped | absent | absent |

A fee bump is not a failure: the same call under another hash is
[followed](https://ethect.dev/reference/tx/wait/#a-transaction-that-is-replaced).

A wallet says a batch is dropped with a status from 400 to 499. A wallet that does not know
the batch, error 5730, says nothing of it: that is `Signer.Error` `"UnknownBatch"`, and the
batch [may still be mined](#after-a-failure).

## Tx.Unsupported

The signer, or the node, cannot do what was asked. Nothing was sent.

| Field | Type | Holds |
| --- | --- | --- |
| `reason` | see below | what cannot be done |
| `message` | `string` | given by whoever refused, with what it answered |

| `reason` | Means | Then |
| --- | --- | --- |
| `"atomic"` | several calls cannot go as one: the signer does not batch, the batch is too large, or it has transaction fields | plan with `batch: "never"`, as `Tx.send` does under `"auto"` |
| `"upgradeRejected"` | the user declined the upgrade of the account that a batch needs | the same |
| `"capability"` | the wallet lacks a capability that is not `optional` | mark it `optional`, or tell the user |
| `"gas"` | the gas of calls sent together cannot be measured: the node has no `eth_simulateV1` | set `transaction.gas`, or send one per call |
| `"signing"` | the signer cannot sign this: a transaction type, or a signature without sending | use another signer |
| `"simulation"` | the node can simulate the calls neither way | [`Tx.simulate`](https://ethect.dev/reference/tx/simulate/) |

## Tx.Partial

Some of the calls went out before one was refused. Only a signer that sends several calls as
several transactions fails this way: a local key.

| Field | Type | Holds |
| --- | --- | --- |
| `sent` | [`Tx.Transactions`](https://ethect.dev/reference/tx/wait/#txtransactions) | what did go out: it can be waited for |
| `failedAt` | `number` | the first call that did not |
| `message` | `string` | given by the signer, with what the node answered |

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

const sendAll = (calls: ReadonlyArray<Call.Call<unknown, unknown>>) =>
  Tx.send(calls).pipe(
    Effect.catchTag("TxPartial", (error) =>
      // the calls before `failedAt` are on their way: their receipts, then what is left to send
      Effect.map(
        Tx.wait(error.sent),
        (receipts) => [receipts, calls.slice(error.failedAt)] as const,
      ),
    ),
  );
```
