# Tx.receipt

Says where one transaction stands now, in one request: its receipt when it is in a block, none
when it is not. It waits for nothing, and fails if the transaction reverted.

## Usage

```ts title="status.ts"
import { Effect, Option } from "effect";
import { type Hash, Tx } from "@ethect/core";

const status = (hash: Hash.Hash) =>
  Effect.gen(function* () {
    // An Option: none when it is in no block yet. No head is read.
    const receipt = yield* Tx.receipt(hash);
    return Option.match(receipt, {
      onNone: () => "not in a block yet",
      onSome: ({ blockNumber }) => `in block ${blockNumber}`,
    });
  });
```

It needs a [transport](https://ethect.dev/guides/transports/), and no signer.

### Or Tx.wait

[`Tx.wait`](https://ethect.dev/reference/tx/wait/) asks the same request first. The two differ when the answer
is none: `Tx.receipt` says so, and `Tx.wait` starts to watch the blocks.

| The transaction | `Tx.receipt(hash)` | `Tx.wait(hash)` |
| --- | --- | --- |
| is in a block | its receipt, in one request | its receipt, in the same one request |
| is in no block yet | none, at once | waits, with one look per block |
| is unknown to the node | none, at once | waits, then `TxDropped` after three heads and two minutes |
| was replaced | none, at once | follows a fee bump, or `TxDropped` with `replacedBy` |
| reverted | `TxReverted` | `TxReverted` |

Use `Tx.receipt` where a program must answer now: a list of transactions with where each
stands, a request handler, a check before something is sent again.

### What a revert may be

`errors` gives the custom errors to read the reason of a revert with, as for `Tx.wait`.
[`Err.of(calls)`](https://ethect.dev/reference/err/of/) gives those of the calls.

```ts
import { Effect, Option } from "effect";
import { type Address, Err, type Hash, Tx } from "@ethect/core";
import { Token } from "./token";

const paid = (usdc: Address.Address, to: Address.Address, hash: Hash.Hash) => {
  const call = Token.at(usdc).transfer(to, 1_000_000n);
  // none: not in a block yet. A receipt: it is paid.
  return Tx.receipt(hash, { errors: Err.of([call]) }).pipe(
    Effect.map(Option.isSome),
    Effect.catchReason("TxReverted", "ERC20InsufficientBalance", (reason) =>
      Effect.succeed(`The sender lacks ${reason.needed - reason.balance}`),
    ),
  );
};
```

```ts title="token.ts"
import { Abi, Erc20 } from "@ethect/core";

// ERC-20 is built in, and declares no error. This adds the one the token reverts with.
export const Token = Erc20.extend(
  Abi.parse(["error ERC20InsufficientBalance(address sender, uint256 balance, uint256 needed)"]),
);
```

Without `errors`, the reason is a plain [`Revert`](https://ethect.dev/reference/revert/): a message, a panic, or
the data as it is.

### Or Eth.getTransactionReceipt

[`Eth.getTransactionReceipt`](https://ethect.dev/rpc/eth/get-transaction-receipt/) is the request alone, and
gives what the node answered. A transaction that reverted has a receipt there, with
`status: "reverted"`. `Tx.receipt` never gives one: a receipt it gives is always of a
transaction that succeeded, and a revert is a failure, with why.

| The node answers | `Tx.receipt` | `Eth.getTransactionReceipt` |
| --- | --- | --- |
| no receipt | `Option.none()` | `null` |
| a receipt, `status: "success"` | `Option.some(receipt)` | the receipt |
| a receipt, `status: "reverted"` | fails with `TxReverted`: its `reason`, and the receipt in `receipts` | the receipt. Nothing says why |

### What was submitted

`Tx.receipt` takes one hash. For what [`Tx.submit`](https://ethect.dev/reference/tx/submit/) gave, ask for each
hash: the answers come in the order of the calls.

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

/** How many of the transactions are in a block. A handle has no hash: its wallet says. */
const mined = (pending: Tx.Transactions) =>
  Effect.forEach(pending.hashes, (hash) => Tx.receipt(hash), { concurrency: "unbounded" }).pipe(
    Effect.map((receipts) => receipts.filter(Option.isSome).length),
  );
```

## Returns

`Effect<Option<Receipt>, Tx.Reverted<E> | Transport.Error, Transport>`

The [receipt](https://ethect.dev/rpc/types/receipt/) of the transaction when it is in a block and succeeded.
None when the node has no receipt for it. `E` is the errors given in `errors`.

## Parameters

### transaction

- **Type:** [`Hash.Hash`](https://ethect.dev/reference/primitives/hash/)

The hash of the transaction.

### options.errors (optional)

- **Type:** `ReadonlyArray<new (value: any) => unknown>`

The custom errors a revert may be, as [`Err.make`](https://ethect.dev/reference/err/make/) builds them. They type
the `reason` of a `TxReverted`. The type of the options is `Tx.ReceiptOptions`.

## Behavior

- **It is one request.** `eth_getTransactionReceipt`, and nothing else: no head is read,
  nothing is subscribed to, nothing is polled.
- **None is not a failure.** A transaction in the pool, one that was dropped, one that was
  replaced and a hash that was never sent all have no receipt, and the node says nothing more.
  [`Tx.wait`](https://ethect.dev/reference/tx/wait/) tells them apart, by watching.
- **A transaction that reverted is `TxReverted`.** Its receipt is in `receipts`. It has no
  `call`: a hash is no call of a list.
- **The reason of a revert is found by running the transaction again**, with `eth_call` on the
  block before its own: two more requests, only when it reverted. A run that does not revert
  again, or a node that no longer has that state, leaves a `Revert.Unknown` with no data.
- **It says nothing of depth.** A receipt is of a block, which a reorg can still take. For a
  depth, `Tx.wait(hash, { confirmations })`: one more request when the transaction is already
  that deep.
- **It does not follow a replacement.** A transaction that was sped up is mined under another
  hash, and this one stays none.
- **It is asked of the transport in context.** A hash says no chain. Under
  [`Transport.on`](https://ethect.dev/reference/transport/chains/), the node of that chain is asked.

## Errors

| Error | When |
| --- | --- |
| [`Tx.Reverted`](https://ethect.dev/reference/tx/failures/#txreverted) | the transaction is in a block and reverted. `reason` is one of `errors`, or a plain revert |
| [`RpcError`](https://ethect.dev/reference/errors/rpc-error/), [`TransportError`](https://ethect.dev/reference/errors/transport-error/) | the node refused, or could not be reached. Nothing is known of the transaction |

## Under the hood

Only the `Transport` is asked.

**`Tx.receipt(hash, { errors })`**

- **your node** `eth_getTransactionReceipt(hash)`
  - `null` → none
  - `status: "success"` → the receipt
  - `status: "reverted"`:
    - **your node** `eth_getTransactionByHash(hash)`
    - **your node** `eth_call({ from, to, data, value, gas }, block - 1)`
    - → fails: `TxReverted`
