# Call.blockNumber

The state of the chain, as calls. `Call.blockNumber`, `Call.timestamp`, `Call.chainId`,
`Call.baseFee`, `Call.gasLimit`, `Call.balance` and `Call.blockHash` are read like any call.
Beside others, they are read inside the same `eth_call`, so they are of the same block.

## Usage

```ts
import { Effect } from "effect";
import { FetchHttpClient } from "effect/http";
import { Address, Call, Erc20, Transport } from "@ethect/core";

const USDC = Address.makeUnsafe("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");
const USER = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");

const program = Effect.gen(function* () {
  // alone: the plain method, here eth_blockNumber
  const head = yield* Call.blockNumber;
  // beside others: one eth_call. The ether, the tokens, the time and `block` are of one block
  const position = yield* Call.all({
    block: Call.blockNumber,
    time: Call.timestamp,
    ether: Call.balance(USER),
    tokens: Erc20.at(USDC).balanceOf(USER),
  });
  return { head, ...position };
});

const state = await program.pipe(
  Effect.provide(Transport.http("https://ethereum-rpc.publicnode.com")),
  Effect.provide(FetchHttpClient.layer),
  Effect.runPromise,
);
```

Each is a [`Call.View`](https://ethect.dev/reference/call/#what-a-call-holds): an effect, a value for
[`Call.all`](https://ethect.dev/reference/call/all/), and a call [`Call.read`](https://ethect.dev/reference/call/read/) takes
options for.

```ts
import { Call, type Address } from "@ethect/core";

const then = (user: Address.Address) => [
  // what an account held at a past block
  Call.read(Call.balance(user), { block: 19_000_000n }),
  // the time of the last finalized block
  Call.read(Call.timestamp, { block: "finalized" }),
];
```

## The seven calls

| Call | Gives | Read alone | Read beside others, inside their `eth_call` |
| --- | --- | --- | --- |
| [`Call.blockNumber`](#blocknumber) | `bigint` | `eth_blockNumber` | Multicall3's `getBlockNumber()`. On ArbOS, `ArbSys.arbBlockNumber()` |
| [`Call.timestamp`](#timestamp) | `bigint`, in seconds | the header of the block | Multicall3's `getCurrentBlockTimestamp()` |
| [`Call.chainId`](#chainid) | `number` | `eth_chainId`, once per transport | Multicall3's `getChainId()` |
| [`Call.baseFee`](#basefee) | `bigint`, in wei per gas | the header of the block | Multicall3's `getBasefee()`, in a call that offers a fee |
| [`Call.gasLimit`](#gaslimit) | `bigint` | the header of the block | Multicall3's `getCurrentBlockGasLimit()` |
| [`Call.balance(account)`](#balance) | `bigint`, in wei | `eth_getBalance` | Multicall3's `getEthBalance(account)` |
| [`Call.blockHash(block)`](#blockhash) | `Hash.Hash` | the header of that block | the contract of EIP-2935, then Multicall3's `getBlockHash(block)` |

"Beside others" is a [`Call.all`](https://ethect.dev/reference/call/all/), or reads issued separately that
[merged](https://ethect.dev/reference/call/batching/#what-joins-a-group).

- **Beside others, a value comes from the execution that reads the rest.** It is of their
  block by construction: nothing is asked before the call, and no block is named to it.
- **Alone, it is the plain method**: one lighter request, for the same value.
- **Two values can need the node**, each on few nodes, and each says so in its first lines
  below: [the base fee](#basefee) and [the hash of an old block](#blockhash).

## Under an override

Only a contract sees a `stateOverride` or a `blockOverride`. A state call read with one is
therefore asked of a contract, with the override, and not of the plain method. It is the
contract a group asks, so the value is the one a group gives and the node gives, as the
override leaves it.

```ts
import { Call, type Address } from "@ethect/core";

// what the account would hold, were it given one ether
const funded = (user: Address.Address) =>
  Call.read(Call.balance(user), {
    stateOverride: { [user]: { balance: 10n ** 18n } },
  });
```

| Call | Under an override, asked of |
| --- | --- |
| `Call.balance`, `Call.timestamp`, `Call.gasLimit`, `Call.chainId` | its getter of Multicall3 |
| `Call.blockNumber` | Multicall3's getter, and ArbSys on ArbOS. Also at a block named by its number, when the override is of the block: a contract may then see another |
| `Call.baseFee` | Multicall3's getter, in a call that offers a fee. The same override pays for it |
| `Call.blockHash(block)` | the contract of EIP-2935 and `blockhash`, or ArbSys on ArbOS. A block neither holds is the node's |

- **Each call is a request of its own that carries the override**: one for most, two for a
  hash.
- **The getter needs Multicall3 at that block.** Where it is not deployed, the read fails with
  an [`AbiError`](https://ethect.dev/reference/errors/abi-error/): it returned no data.
- **`from` and `gas` alone change nothing of the chain.** A read with only those keeps the
  plain method.

## blockNumber

`Call.View<bigint>`

The number of the block the read is at. Beside others it is read inside their `eth_call`: it is
the number of the block those reads ran at, and there is no request before it.

```ts
import { Effect } from "effect";
import { Call, type Address } from "@ethect/core";

// a balance, and the block to read the next ones from: one eth_call
const snapshot = (user: Address.Address) =>
  Effect.map(
    Call.all([Call.blockNumber, Call.balance(user)]),
    ([block, ether]) => ({ block, ether }),
  );
```

- **It is the number the node knows the block by.** On most chains that is what a contract
  sees, and Multicall3 is asked. On ArbOS, the system of Arbitrum One and of the chains built
  with it, a contract sees the number of an L1 block: there, ArbSys is asked, which says the
  number of its own.
- **Whether a chain is ArbOS is asked once per node**, with an `eth_getCode` that leaves
  beside the first group, not before it. On ArbOS, that first group is asked a second time.
- **Only an answer of the node counts.** While the node does not say whether there is code
  where ArbSys is, the read fails with why, and the node is asked again the next time: a
  number that could be the one of another chain is not given.
- **At a block named by its number, it is that number**, and nothing is asked.
- **A chain that is neither**, where a contract sees another number than the node gives and
  that has no ArbSys, gets what a contract sees. None is known to this library.

## timestamp

`Call.View<bigint>`

The time of the block, in seconds since 1970.

## chainId

`Call.View<number>`

The id of the chain. Alone it is [`Transport.chainId`](https://ethect.dev/reference/transport/chains/#chainid): asked
once per transport, and not at all under [`Transport.on`](https://ethect.dev/reference/transport/chains/).

## baseFee

`Call.View<bigint>`

The base fee of the block, in wei per gas. On a chain that has none, the read fails with a
[`Revert.Unknown`](https://ethect.dev/reference/revert/#what-is-unknown) that has no data.

**On two kinds of node it is a second request**, because the call cannot be made to see it:
a node that refuses state overrides, and zkSync Era, which answers the fee a call offers in
place of the base fee. There the header of the block is read first, and the group is read at
the hash of that block, so the fee and the reads are of one block whatever happens to the
chain in between.

Everywhere else it is read inside the call:

- **A call that offers no fee is run with a base fee of zero** by most clients: Geth, reth,
  Nethermind, Nitro and those built on them. So a group that reads the base fee offers one:
  `maxFeePerGas` of 2^64 - 1 wei, and no tip. The node then runs it with the base fee of the
  block.
- **A fee has to be paid from a balance.** The same `eth_call` sets one for its sender, the
  zero address, with a state override. Nothing is spent: it is a call.
- **What the other reads of that group see changes in two places.** `tx.gasprice` is the base
  fee, where it is zero in a call that offers nothing. And the ether of the zero address is
  the balance that was set: a group that reads `Call.balance` of the zero address beside
  `Call.baseFee` gets that balance.
- **Whether a node can be asked this way is asked once**, with one small `eth_call`, before
  the first group that reads the base fee.

## gasLimit

`Call.View<bigint>`

The gas limit of the block.

## balance

`(account: Address.Address) => Call.View<bigint>`

The ether `account` holds, in wei.

One account is not what it holds on the chain in one case: the zero address, in a group that
also reads [`Call.baseFee`](#basefee). That group's call is paid from a balance set at that
address, and that balance is what it reads there.

## blockHash

`(block: bigint) => Call.View<Hash.Hash>`

The hash of the block of that number. For a block the node does not have, 32 zero bytes.

**For a block the chain no longer holds, it is a second request**, `eth_getBlockByNumber`,
because no contract can say it. A block that old does not change.

What a chain holds, and so what is read inside the call:

| Chain | Blocks a contract can give the hash of |
| --- | --- |
| with the contract of EIP-2935: Ethereum since Prague, and most chains that follow it | the last 8,191 |
| without it | the last 256, with `blockhash` |
| ArbOS | the last 8,191, and the last 256 from ArbSys. `blockhash` knows none of its blocks |

The block of the call itself has no hash yet inside the call: asked for, it is the node's too.

## Behavior

- **Each is a view on a getter of Multicall3**, at `0xcA11bde05977b3631167028862bE2a173976CA11`.
  That is what [`Call.encode`](https://ethect.dev/reference/call/encode/) and [`Call.key`](https://ethect.dev/reference/call/key/)
  say of it. The request says which state it reads, so a [reader](https://ethect.dev/reference/call/reader/)
  that sends it alone asks the node directly: no Multicall3 is needed to read one.
- **The same value alone and in a group.** Alone, the node says it. In a group, a contract
  says it, asked in the way that makes it say what the node says: checked on Geth, reth,
  Nethermind, Arbitrum Nitro, the OP Stack, Polygon, Scroll, BSC, zkSync Era and Rootstock.
- **A group is one request.** Its values are read where its other reads are, so nothing ties
  two requests together, and nothing can come between them.
- **They take the options of a read.** With `from`, `gas` or an override, a read does not
  join a group. With an override it is asked of the getter, so that the override
  [applies](#under-an-override).

## Errors

As [any read](https://ethect.dev/reference/call/#what-a-read-fails-with). None declares an error of its own.

| Error | When |
| --- | --- |
| [`Revert.Unknown`](https://ethect.dev/reference/revert/#what-is-unknown) | `Call.baseFee` on a chain that has no base fee |
| [`AbiError`](https://ethect.dev/reference/errors/abi-error/) | a read with an override, at a block where Multicall3 is not deployed: nothing came back |
| [`TransportError`](https://ethect.dev/reference/errors/transport-error/) | the node does not have the block the read is at: `MalformedResponse`, "The node has no block …" |
| [`RpcError`](https://ethect.dev/reference/errors/rpc-error/), `TransportError` | the node answered with an error, or did not answer |

## Under the hood

Alone, each is one request to the `Transport`, made by
[`Call.direct`](https://ethect.dev/reference/call/reader/#under-the-hood):

- **Call.blockNumber** `eth_blockNumber`: nothing at a block named by its number
- **Call.timestamp** `eth_getBlockByNumber(block, false)`: or `eth_getBlockByHash`
- **Call.chainId** `eth_chainId`: asked once per transport
- **Call.baseFee** `eth_getBlockByNumber(block, false)`: or `eth_getBlockByHash`
- **Call.gasLimit** `eth_getBlockByNumber(block, false)`: or `eth_getBlockByHash`
- **Call.balance(a)** `eth_getBalance(a, block)`
- **Call.blockHash(n)** `eth_getBlockByNumber(n, false)`

With a `stateOverride` or a `blockOverride`, each is asked of the contract a group asks, in
requests of its own:

**`Call.read(Call.balance(a), { stateOverride })`**

- **your node** `eth_call({ to: Multicall3, data: getEthBalance(a) }, block, stateOverride)`

**`Call.read(Call.blockNumber, { stateOverride })`**

- **your node** `eth_call({ to: Multicall3, data: getBlockNumber() }, block, stateOverride)`
  - on ArbOS:
    - **your node** `eth_call({ to: ArbSys, data: arbBlockNumber() }, block, stateOverride)`

**`Call.read(Call.baseFee, { stateOverride })`**

- **your node** `eth_call({ to: Multicall3, data: getBasefee(), maxFeePerGas: 2^64 - 1, maxPriorityFeePerGas: 0 }, block, { …stateOverride, [the sender]: { balance: 2^200 } })`

**`Call.read(Call.blockHash(n), { stateOverride })`**

- **your node** `eth_call({ to: 0x0000F908…2935, data: n }, block, stateOverride)`
- **your node** `eth_call({ to: Multicall3, data: getBlockHash(n) }, block, stateOverride)`
  - neither holds it:
    - **your node** `eth_getBlockByNumber(n, false)`

In a group, each is one more call of the `aggregate3` that carries the reads:

**`Call.all([Call.blockNumber, Call.timestamp, Call.balance(a), token.balanceOf(a)])`**

- **your node** `eth_call({ to: Multicall3, data: aggregate3([ getBlockNumber(), getCurrentBlockTimestamp(), getEthBalance(a), balanceOf(a) ]) }, "latest")`
  - on ArbOS: the first call is `{ target: ArbSys, callData: arbBlockNumber() }`

**`Call.all([Call.baseFee, token.balanceOf(a)])`**

- **your node** `eth_call({ to: Multicall3, data: aggregate3([ getBasefee(), balanceOf(a) ]), maxFeePerGas: 2^64 - 1, maxPriorityFeePerGas: 0 }, "latest", { [0x0000…0000]: { balance: 2^200 } })`

**`Call.all([Call.blockHash(n), token.balanceOf(a)])`**

- **your node** `eth_call({ to: Multicall3, data: aggregate3([ { target: 0x0000F908…2935, callData: n }, getBlockHash(n), balanceOf(a) ]) }, "latest")`: the first call is to the contract of EIP-2935. On ArbOS the second is `ArbSys.arbBlockHash(n)`
  - neither holds it:
    - **your node** `eth_getBlockByNumber(n, false)`

Where the base fee is the node's:

**`Call.all([Call.baseFee, token.balanceOf(a)])`**

- **your node** `eth_getBlockByNumber("latest", false)`: its base fee, and its hash H
- **your node** `eth_call({ to: token, data: balanceOf(a) }, { blockHash: H })`

What a group has to know of its node is asked once, and kept with the transport:

**is the chain ArbOS? Asked beside the first group that reads a block number or a hash**

- **your node** `eth_getCode(ArbSys, "latest")`
  - no code: no
  - code:
    - **your node** `eth_call({ to: ArbSys, data: arbChainID(), gas: 100000 }, "latest")`
      - the chain id: yes
      - anything else, or a revert: no

**does a fee make the base fee true? Asked before the first group that reads the base fee**

- **your node** `eth_call({ to: Multicall3, data: aggregate3([ getBasefee() ]), maxFeePerGas, … }, block, { [Multicall3]: { code }, [0x0000…0000]: { balance } })`
  - the fee it offered, or an error: no
  - anything else: yes
