# Eth.getTransactionReceipt

Returns the receipt of a mined transaction, or `null` while the transaction is pending or when
the node does not know it.

## Usage

```ts
import { Effect } from "effect";
import { FetchHttpClient } from "effect/http";
import { Transport } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

const receipt = await Eth.getTransactionReceipt(
  "0xa23b52004fe574000765fbe20a311b2785a318c9dbe6b0bf6a4c1f8936f57ff3",
).pipe(
  Effect.provide(Transport.http("http://127.0.0.1:8545")),
  Effect.provide(FetchHttpClient.layer),
  Effect.runPromise,
);
// receipt?.status: "success"
// receipt?.gasUsed: 21000n
// receipt?.blockNumber: 1n
```

The call works over every [transport](https://ethect.dev/guides/transports/). The examples below leave the
transport out.

### Check that it succeeded

A mined transaction can have reverted. The receipt says which with `status`.

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

const hash = "0xa23b52004fe574000765fbe20a311b2785a318c9dbe6b0bf6a4c1f8936f57ff3";

const outcome = Effect.gen(function* () {
  const receipt = yield* Eth.getTransactionReceipt(hash);
  if (receipt === null) return "not mined yet";
  if (receipt.status === "reverted") return "reverted";
  return `succeeded in block ${receipt.blockNumber}`;
});
```

### Wait for a receipt

[`Tx.wait(hash)`](https://ethect.dev/reference/tx/wait/) waits for a transaction: it looks at once, then once
per block, follows a replacement, and fails when the transaction reverted. With this method alone, ask
again until the result is not `null`.

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

const waitForReceipt = (hash: Hash.Hash) =>
  Eth.getTransactionReceipt(hash).pipe(
    Effect.delay("1 second"),
    Effect.repeat({ until: (receipt) => receipt !== null }),
    Effect.timeout("2 minutes"),
  );
```

Each attempt waits a second, then asks. The result is a `Receipt`, without the `null`, and the
wait fails with a `TimeoutError` after two minutes: a transaction the node drops never gets a
receipt.

To send and wait in one call, use
[`Eth.sendTransactionSync`](https://ethect.dev/rpc/eth/send-transaction-sync/) or
[`Eth.sendRawTransactionSync`](https://ethect.dev/rpc/eth/send-raw-transaction-sync/) where the node
serves them.

### What it cost

The fee paid is the gas used times the price paid per gas, in wei.

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

const hash = "0xa23b52004fe574000765fbe20a311b2785a318c9dbe6b0bf6a4c1f8936f57ff3";

const fee = Effect.gen(function* () {
  const receipt = yield* Eth.getTransactionReceipt(hash);
  if (receipt === null || receipt.effectiveGasPrice === undefined) return null;
  return receipt.gasUsed * receipt.effectiveGasPrice; // 21000n * 2000000000n
});
```

`effectiveGasPrice` is optional: a node may leave it out.

### The address of a new contract

When the transaction created a contract, the receipt holds its address.

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

const deployedAt = (hash: Hash.Hash) =>
  Eth.getTransactionReceipt(hash).pipe(
    // absent when the transaction did not create a contract
    Effect.map((receipt) => receipt?.contractAddress),
  );
```

## Returns

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

A [`Receipt`](https://ethect.dev/rpc/types/receipt/), or `null` when the transaction is not mined.

## Parameters

### hash

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

The hash of the transaction: 32 bytes of hex.

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

const receipt = Eth.getTransactionReceipt(
  "0xa23b52004fe574000765fbe20a311b2785a318c9dbe6b0bf6a4c1f8936f57ff3",
);
```

## Behavior

- **`null` until mined.** A pending transaction has no receipt, and neither has a hash the node
  does not know. The two cases look the same here:
  [`Eth.getTransactionByHash`](https://ethect.dev/rpc/eth/get-transaction-by-hash/) tells them apart.
- **A revert is not an error.** A transaction that reverted has a receipt with
  `status: "reverted"`. The call itself succeeds.
- **Logs included.** `logs` holds every log the transaction emitted, each with its position in
  the block.
- **A whole block at once.** [`Eth.getBlockReceipts`](https://ethect.dev/rpc/eth/get-block-receipts/)
  returns the receipts of every transaction of a block in one request.

## Errors

The three [errors](https://ethect.dev/reference/errors/) every method can fail with. The case specific to this
method:

| Error | Reason | When |
| --- | --- | --- |
| `CodecError` | `on: "params"` | the hash is not 32 bytes of hex: nothing is sent |

## JSON-RPC method

`eth_getTransactionReceipt`
