# Eth.subscribe.transactionReceipts

A stream of the receipts of each block added to the chain: all of them, or only those of the
transactions you name.

## Usage

```ts
import { Effect, Stream } from "effect";
import { Socket } from "effect/socket";
import { Transport } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

const program = Eth.subscribe.transactionReceipts().pipe(
  Stream.take(1),
  Stream.runCollect,
  Effect.map(([receipts]) => receipts?.map((receipt) => receipt.status)),
);

const statuses = await program.pipe(
  Effect.provide(Transport.webSocket("ws://127.0.0.1:8545")),
  Effect.provide(Socket.layerWebSocketConstructorGlobal),
  Effect.runPromise,
);
// ["success"]
```

The program subscribes, waits for the next block that has transactions, and unsubscribes when
`Stream.take(1)` ends the stream. An item is the list of the receipts of one block.

A subscription needs a transport that can push: WebSocket here, or IPC or EIP-1193. The
examples below leave the transport out.

### The receipt of a transaction you sent

Name the transaction to get its receipt alone, when its block arrives.

```ts
import { Effect, Stream } from "effect";
import { type Hash } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

/** The receipt of a pending transaction, once it is mined. */
const receiptOf = (hash: Hash.Hash) =>
  Eth.subscribe.transactionReceipts({ transactionHashes: [hash] }).pipe(
    Stream.flatMap((receipts) => Stream.fromIterable(receipts)),
    Stream.runHead,
  );
// Effect<Option<Receipt>, Method.Error, Subscriptions>
```

Subscribe before the transaction is mined: a subscription pushes what happens after it is
opened. For a transaction that may already be in a block, call
[`Eth.getTransactionReceipt`](https://ethect.dev/rpc/eth/get-transaction-receipt/).

### Every receipt, one at a time

Flatten the blocks to act on each receipt.

```ts
import { Effect, Stream } from "effect";
import { Eth } from "@ethect/core/rpc";

const watch = Eth.subscribe.transactionReceipts().pipe(
  Stream.flatMap((receipts) => Stream.fromIterable(receipts)),
  Stream.filter((receipt) => receipt.status === "reverted"),
  Stream.runForEach((receipt) => Effect.log(`${receipt.transactionHash} reverted`)),
);
```

## Returns

[`Stream<ReadonlyArray<Receipt>, Method.Error, Subscriptions>`](https://ethect.dev/rpc/subscription/#opening-a-subscription)

Each item is the list of the [`Receipt`](https://ethect.dev/rpc/types/receipt/)s of one block, in the
order of the block.

## Parameters

### filter (optional)

- **Type:** `{ transactionHashes?: ReadonlyArray<Hash> }`

The transactions whose receipts to push. Left out, the receipts of every transaction are
pushed.

```ts
import { Stream } from "effect";
import { type Hash } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

const mine = (hashes: ReadonlyArray<Hash.Hash>) =>
  Eth.subscribe.transactionReceipts({
    transactionHashes: hashes,
  });
```

## Behavior

- **One item per block.** The receipts of a block arrive together, as one array.
- **A block with nothing to report pushes nothing.** anvil 1.9 sends no item for an empty
  block, and none for a block that holds none of the transactions named.
- **Not every node serves this kind.** anvil 1.9 does. When a node refuses the subscription,
  the stream fails with its `RpcError`.
- **Unsubscribes when the stream ends.** When the stream ends or is interrupted, the transport
  sends `eth_unsubscribe`.
- **Carries on over a reconnection.** The subscription is issued again on the new connection.
  The receipts of the blocks mined in between are not replayed.

## Errors

The three [errors](https://ethect.dev/reference/errors/) every method can fail with. Here they fail the stream:

| Error | Reason | When |
| --- | --- | --- |
| `RpcError` | the node's own | the node refuses the subscription |
| `CodecError` | `on: "params"` | a hash of the filter is not 32 bytes: nothing is sent |
| `CodecError` | `on: "result"` | an item is not a list of receipts |
| `TransportError` | `Unreachable` | the connection is lost and the reconnect schedule runs out |

## JSON-RPC method

`eth_subscribe` with the kind `transactionReceipts`
