# Eth.getBlockByNumber

Returns a block by number or tag, 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.getBlockByNumber("latest", false).pipe(
  Effect.provide(Transport.http("http://127.0.0.1:8545")),
  Effect.provide(FetchHttpClient.layer),
  Effect.runPromise,
);
// block?.number: 1n
// block?.transactions:
//   ["0xa23b52004fe574000765fbe20a311b2785a318c9dbe6b0bf6a4c1f8936f57ff3"]
```

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 moved = Effect.gen(function* () {
  const block = yield* Eth.getBlockByNumber("latest", true);
  if (block === null) return 0n;
  return block.transactions.reduce((sum, transaction) => sum + transaction.value, 0n);
});
```

### A block the node does not have

A number above the head is not an error: the result is `null`.

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

const outcome = Eth.getBlockByNumber(1_000_000_000n, false).pipe(
  Effect.map((block) =>
    block === null ? "no such block" : `block ${block.number} is ${block.hash}`,
  ),
);
// "no such block"
```

### A flag known at run time

Pass a `boolean` when the program only learns which form it wants as it runs. The block is
then a `Block.Any`: one shape or the other.

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

/** The hashes of the latest block's transactions, whichever form was asked for. */
const hashes = (full: boolean) =>
  Eth.getBlockByNumber("latest", full).pipe(
    // block: Block.Any | null
    Effect.map((block) =>
      (block?.transactions ?? []).map((transaction) =>
        typeof transaction === "string" ? transaction : transaction.hash,
      ),
    ),
  );
```

### By tag

A tag names a block by its place in the chain, whatever its number is.

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

const heads = Effect.all([
  Eth.getBlockByNumber("latest", false),
  Eth.getBlockByNumber("safe", false),
  Eth.getBlockByNumber("finalized", false),
]).pipe(Effect.map((blocks) => blocks.map((block) => block?.number)));
```

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

### block

- **Type:** [`BlockId.NumberOrTag`](https://ethect.dev/rpc/types/block-id/)

A block number as a `bigint`, or a tag: `"latest"`, `"earliest"`, `"pending"`, `"safe"`,
`"finalized"`.

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

const blocks = Effect.all([
  Eth.getBlockByNumber(19_000_000n, false),
  Eth.getBlockByNumber("finalized", false),
]);
```

To name a block by its hash, use [`Eth.getBlockByHash`](https://ethect.dev/rpc/eth/get-block-by-hash/).

### full

- **Type:** `boolean`

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

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

// Effect<Block.Block | null, …>
const withHashes = Eth.getBlockByNumber("latest", false);
// Effect<Block.Full | null, …>
const inFull = Eth.getBlockByNumber("latest", true);
// Effect<Block.Any | null, …>
const either = (full: boolean) => Eth.getBlockByNumber("latest", full);
```

The result type follows the literal you write. A value typed `boolean` gives the union of the
two shapes.

## Behavior

- **`null`, not an error.** A block number the chain has not reached gives `null`.
- **`"pending"` is the block being built.** anvil answers it with the transactions waiting in
  its pool.
- **One block, two sizes.** With `true` the node sends every transaction of the block. Ask for
  hashes when you do not read them.
- **Only the header.** [`Eth.getHeaderByNumber`](https://ethect.dev/rpc/eth/block-details/#getheaderbynumber)
  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 block number is negative: nothing is sent |

## JSON-RPC method

`eth_getBlockByNumber`
