# BlockId

How a call names the block it is about: a block number, a tag such as `"latest"`, or an object
that names the block by number or by hash (EIP-1898).

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

const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");

const balances = Effect.all([
  Eth.getBalance(ADDRESS, 19_000_000n),
  Eth.getBalance(ADDRESS, "finalized"),
  Eth.getBalance(ADDRESS, { blockHash: `0x${"ab".repeat(32)}` }),
]);
```

## Schemas

| Schema | Type | Describes | Used by |
| --- | --- | --- | --- |
| `BlockId.tagSchema` | `BlockId.Tag` | one of the five tags | the two schemas below |
| `BlockId.numberOrTagSchema` | `BlockId.NumberOrTag` | a block number or a tag | [`Eth.getBlockByNumber`](https://ethect.dev/rpc/eth/get-block-by-number/) and the other methods named `…ByNumber` or `…ByBlockNumber…`, [`Eth.feeHistory`](https://ethect.dev/rpc/eth/fee-history/), `fromBlock` and `toBlock` of a [`Filter`](https://ethect.dev/rpc/types/filter/) |
| `BlockId.schema` | `BlockId.BlockId` | a block number, a tag, or an object naming the block by number or by hash | [`Eth.getBalance`](https://ethect.dev/rpc/eth/get-balance/) and the other state methods, [`Eth.call`](https://ethect.dev/rpc/eth/call/), [`Eth.estimateGas`](https://ethect.dev/rpc/eth/estimate-gas/), [`Eth.getBlockReceipts`](https://ethect.dev/rpc/eth/get-block-receipts/) |

Import a type to name it in your own signatures.

```ts
import { Address } from "@ethect/core";
import { type BlockId, Eth } from "@ethect/core/rpc";

const balanceAt = (address: Address.Address, block: BlockId.BlockId) =>
  Eth.getBalance(address, block);
```

## Forms

What you write, and what the node receives for it.

| You write | Sent as | Names |
| --- | --- | --- |
| `19_000_000n` | `"0x121eac0"` | the block with that number |
| `"latest"` | `"latest"` | a block by tag |
| `{ blockNumber: 1n }` | `{"blockNumber":"0x1"}` | the block with that number, in the object form |
| `{ blockHash }` | `{"blockHash":"0x…"}` | the block with that hash |
| `{ blockHash, requireCanonical: true }` | `{"blockHash":"0x…","requireCanonical":true}` | the same, refused when that block is not on the canonical chain |

`BlockId.NumberOrTag` covers the first two rows. `BlockId.BlockId` covers all five.

## Tags

`BlockId.Tag` is one of five strings.

| Tag | Names |
| --- | --- |
| `"latest"` | the most recent block of the chain |
| `"earliest"` | the oldest block the node has: the genesis block, unless the node pruned its history |
| `"pending"` | the block the node is building from its pool |
| `"safe"` | the most recent block the consensus layer holds as safe |
| `"finalized"` | the most recent finalized block |

## Behavior

- **A number is a `bigint`.** `19_000_000n`, not `19_000_000`: a plain `number` is a type
  error. A negative one fails with a `CodecError` on the params, and nothing is sent.
- **Left out means not sent.** Where a method marks its block optional, there is no default:
  see [Naming a block](https://ethect.dev/rpc/guides/calling-methods/#naming-a-block).
- **A hash pins the block.** After a reorg `"latest"` and a number can name another block. An
  object with `blockHash` cannot.
- **`…ByNumber` methods take no object.** A method typed with `BlockId.NumberOrTag` refuses
  `{ blockHash }` at compile time. Its sibling named `…ByHash` takes the hash.
- **A block the node does not have.** What happens depends on the method. A block lookup
  returns `null`. On anvil 1.9 a state method fails with an `RpcError`: reason `InvalidParams`
  for a number above the head, `ResourceNotFound` for an unknown `blockHash`.
