# Block

A block as a node returns it, in three shapes: the header alone, the block with the hashes of
its transactions, and the block with its transactions in full.

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

const latest = Effect.gen(function* () {
  const block = yield* Eth.getBlockByNumber("latest", false);
  if (block === null) return "no such block";
  // block: Block.Block
  return `block ${block.number} has ${block.transactions.length} transaction(s)`;
});
```

## Schemas

| Schema | Type | Describes | Returned by |
| --- | --- | --- | --- |
| `Block.headerSchema` | `Block.Header` | the header fields | [`Eth.getHeaderByNumber`](https://ethect.dev/rpc/eth/block-details/#getheaderbynumber), [`Eth.subscribe.newHeads`](https://ethect.dev/rpc/eth/subscribe/new-heads/) |
| `Block.schema` | `Block.Block` | the header, the body, and `transactions` as hashes | [`Eth.getBlockByNumber(id, false)`](https://ethect.dev/rpc/eth/get-block-by-number/) |
| `Block.fullSchema` | `Block.Full` | the same with `transactions` as [`Transaction`](https://ethect.dev/rpc/types/transaction/) objects | [`Eth.getBlockByNumber(id, true)`](https://ethect.dev/rpc/eth/get-block-by-number/) |
| `Block.anySchema` | `Block.Any` | either of the two: `Block.Block \| Block.Full` | [`Eth.getBlockByNumber(id, full)`](https://ethect.dev/rpc/eth/get-block-by-number/#a-flag-known-at-run-time) with a `boolean` |

Import a type to name it in your own signatures.

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

const gasUsedPercent = (block: Block.Header): number =>
  Number((block.gasUsed * 10_000n) / block.gasLimit) / 100;
```

## Header fields

`Block.Header`, and the first part of `Block.Block` and `Block.Full`.

| Field | Type | Notes |
| --- | --- | --- |
| `hash` | `Hash \| null` | `null` on a pending block, from a node that has no hash for it yet |
| `parentHash` | `Hash` | |
| `sha3Uncles` | `Hash` | |
| `miner` | `Address \| null` | the fee recipient. `null` on a pending block, from a node that has none yet |
| `stateRoot` | `Hash` | |
| `transactionsRoot` | `Hash` | |
| `receiptsRoot` | `Hash` | |
| `logsBloom` | `Hex \| null` | can be `null` on a pending block |
| `number` | `bigint \| null` | can be `null` on a pending block |
| `gasLimit` | `bigint` | |
| `gasUsed` | `bigint` | |
| `timestamp` | `bigint` | seconds since the Unix epoch |
| `extraData` | `Hex` | |
| `difficulty` | `bigint` (optional) | |
| `mixHash` | `Hash` (optional) | `prevRandao` since the merge |
| `nonce` | `Hex` (optional) | |
| `totalDifficulty` | `bigint` (optional) | |
| `baseFeePerGas` | `bigint` (optional) | since London |
| `withdrawalsRoot` | `Hash` (optional) | since Shanghai |
| `blobGasUsed` | `bigint` (optional) | since Cancun |
| `excessBlobGas` | `bigint` (optional) | since Cancun |
| `parentBeaconBlockRoot` | `Hash` (optional) | since Cancun |
| `requestsHash` | `Hash` (optional) | since Prague |
| `blockAccessListHash` | `Hash` (optional) | on a chain with block access lists (EIP-7928) |
| `slotNumber` | `bigint` (optional) | where the node reports the slot of the block |
| `size` | `bigint` (optional) | the size of the whole block, from a client that gives it with a header, as BSC does |

## Body fields

`Block.Block` and `Block.Full` add the body.

| Field | Type | Notes |
| --- | --- | --- |
| `size` | `bigint` | the encoded block, in bytes. Required on a block, optional on a header alone. |
| `uncles` | `ReadonlyArray<Hash>` | always empty since the merge |
| `withdrawals` | `ReadonlyArray<`[`Withdrawal`](https://ethect.dev/rpc/types/withdrawal/)`>` (optional) | since Shanghai |
| `transactions` | `ReadonlyArray<Hash>` in `Block.Block` | |
| `transactions` | `ReadonlyArray<`[`Transaction`](https://ethect.dev/rpc/types/transaction/)`>` in `Block.Full` | |

## Behavior

- **Optional means absent.** A field marked optional is left out of the object when the node
  omits it or sends `null`. Test it with `block.baseFeePerGas !== undefined`.
- **`null` means pending.** `hash`, `miner`, `number` and `logsBloom` can be `null` only for a
  block that is not mined yet, and which of them are depends on the node: anvil 1.9 gives its
  pending block a hash, a miner and a number. They are always present, so they are typed
  `| null`, not optional.
- **A pending block may have no `nonce`.** The field is optional, and absent when the node
  sends `null`.
- **Unknown fields are dropped.** A chain that adds fields to its blocks still decodes. The
  extra fields are not in the result: [extend the schema](#extending-the-schema) to read them.
- **Numbers.** Every quantity of a block is a `bigint`. See
  [values on the wire](https://ethect.dev/rpc/guides/values/).

### Extending the schema

A schema is an Effect `Schema.Struct`: copy its fields into a new one to add the fields of your
chain, then define a method with it.

```ts
import { Schema } from "effect";
import { Quantity } from "@ethect/core";
import { Block, BlockId, Method } from "@ethect/core/rpc";

// A rollup whose blocks carry the L1 block they were derived from.
const RollupBlock = Schema.Struct({
  ...Block.schema.fields,
  l1BlockNumber: Quantity.schema,
});

const getRollupBlock = Method.make<[block: unknown, full: unknown]>()(
  "eth_getBlockByNumber",
  [BlockId.numberOrTagSchema, Schema.Literal(false)],
  Schema.NullOr(RollupBlock),
);
```

`getRollupBlock("latest", false)` now returns the standard fields plus `l1BlockNumber` as a
`bigint`. See [Custom methods](https://ethect.dev/rpc/guides/custom-methods/).
