# Eth.getTransactionByHash

Returns a transaction by its hash, pending or mined, or `null` 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 transaction = await Eth.getTransactionByHash(
  "0xa23b52004fe574000765fbe20a311b2785a318c9dbe6b0bf6a4c1f8936f57ff3",
).pipe(
  Effect.provide(Transport.http("http://127.0.0.1:8545")),
  Effect.provide(FetchHttpClient.layer),
  Effect.runPromise,
);
// transaction?.type: "eip1559"
// transaction?.value: 1n
// transaction?.blockNumber: 1n
```

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

### Pending or mined

A pending transaction has no place in the chain yet: its `blockNumber` is `null`.

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

const hash = "0xa23b52004fe574000765fbe20a311b2785a318c9dbe6b0bf6a4c1f8936f57ff3";

const state = Effect.gen(function* () {
  const transaction = yield* Eth.getTransactionByHash(hash);
  if (transaction === null) return "unknown";
  if (transaction.blockNumber === null) return "pending";
  return `mined in block ${transaction.blockNumber}`;
});
```

### Read the fields of one type

The result is a union of transaction types. Compare `type` to reach the fields of one.

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

const hash = "0xa23b52004fe574000765fbe20a311b2785a318c9dbe6b0bf6a4c1f8936f57ff3";

const feeCap = Effect.gen(function* () {
  const transaction = yield* Eth.getTransactionByHash(hash);
  if (transaction === null) return null;
  return transaction.type === "eip1559"
    ? transaction.maxFeePerGas // 2000000000n
    : null;
});
```

The [`Transaction`](https://ethect.dev/rpc/types/transaction/) page lists the fields of every type.

### A contract creation

A transaction that creates a contract has no recipient: `to` is `null`, and `input` is the
code that ran to create it.

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

const isCreation = (hash: Hash.Hash) =>
  Eth.getTransactionByHash(hash).pipe(
    Effect.map((transaction) =>
      // null when the node does not know the transaction
      transaction === null ? null : transaction.to === null,
    ),
  );
```

## Returns

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

A [`Transaction`](https://ethect.dev/rpc/types/transaction/), or `null` when the node does not know the
hash.

## 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 transaction = Eth.getTransactionByHash(
  "0xa23b52004fe574000765fbe20a311b2785a318c9dbe6b0bf6a4c1f8936f57ff3",
);
```

## Behavior

- **`null` for a hash the node does not know.** That covers a hash that never existed and a
  transaction the node dropped from its pool.
- **`null` fields while pending.** `blockHash`, `blockNumber` and `transactionIndex` are `null`
  until the transaction is mined.
- **Found does not mean succeeded.** A transaction that reverted is returned like any other.
  Its outcome is the `status` of its
  [receipt](https://ethect.dev/rpc/eth/get-transaction-receipt/).
- **No receipt data.** Gas used, logs and the address of a created contract are on the receipt.

## 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_getTransactionByHash`
