# Eth.getBlockByHash

Returns a block by its hash, with its transactions as hashes or in full, or `null` when the
node does not have the block.

## Usage

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

const block = await Eth.getBlockByHash(
  "0xe5178f52e018186949d9fe9168c6011820f9c6bd449a93dbb07c09dfc0af3dfa",
  false,
).pipe(
  Effect.provide(Transport.http("http://127.0.0.1:8545")),
  Effect.provide(FetchHttpClient.layer),
  Effect.runPromise,
);
// block?.number: 1n
```

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

### With the transactions in full

Pass `true` and `transactions` holds [`Transaction`](https://ethect.dev/rpc/types/transaction/) objects
instead of hashes.

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

const blockHash = "0xe5178f52e018186949d9fe9168c6011820f9c6bd449a93dbb07c09dfc0af3dfa";

const senders = Effect.gen(function* () {
  const block = yield* Eth.getBlockByHash(blockHash, true);
  if (block === null) return [];
  return block.transactions.map((transaction) => transaction.from);
});
```

### The block of a transaction

A receipt names its block by hash. Read that block to know when the transaction was mined.

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

const hash = "0xa23b52004fe574000765fbe20a311b2785a318c9dbe6b0bf6a4c1f8936f57ff3";

const minedAt = Effect.gen(function* () {
  const receipt = yield* Eth.getTransactionReceipt(hash);
  if (receipt === null) return null;
  const block = yield* Eth.getBlockByHash(receipt.blockHash, false);
  return block === null ? null : new Date(Number(block.timestamp) * 1000);
});
```

### Walk to the parent

Every block holds the hash of the one before it.

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

const parent = Effect.gen(function* () {
  const head = yield* Eth.getBlockByNumber("latest", false);
  if (head === null) return null;
  return yield* Eth.getBlockByHash(head.parentHash, false);
});
```

## Returns

[`Effect<Block.Block | null, Method.Error, Transport>`](https://ethect.dev/rpc/method/#calling-a-method)
with `false`, and
[`Effect<Block.Full | null, Method.Error, Transport>`](https://ethect.dev/rpc/method/#calling-a-method)
with `true`. With a value typed `boolean`, the block is a `Block.Any | null`.

The two shapes are on the [`Block`](https://ethect.dev/rpc/types/block/) page. They differ only in
`transactions`, and `Block.Any` is their union.

## Parameters

### blockHash

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

The hash of the block: 32 bytes of hex. A string literal works, and so does a hash read from
another result.

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

const block = Eth.getBlockByHash(
  "0xe5178f52e018186949d9fe9168c6011820f9c6bd449a93dbb07c09dfc0af3dfa",
  false,
);
```

### full

- **Type:** `boolean`

`false` for the hashes of the transactions, `true` for the transactions in full. It is
required.

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

// Effect<Block.Any | null, …>
const read = (blockHash: Hash.Hash, full: boolean) =>
  Eth.getBlockByHash(blockHash, full);
```

The result type follows the literal you write: `Block.Block` with `false`, `Block.Full` with
`true`. A value typed `boolean` gives `Block.Any`, the union of the two.

## Behavior

- **`null`, not an error.** A hash no block has gives `null`.
- **A hash names one block for good.** After a reorg, a block number can name another block.
  A block hash cannot.
- **Only the header.** [`Eth.getHeaderByHash`](https://ethect.dev/rpc/eth/block-details/#getheaderbyhash)
  returns the block without its body.

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