Skip to content

Headers, counts & uncles

The methods that read one part of a block: its header, how many transactions and uncles it has, one of its uncles, and its access list.

Method JSON-RPC Returns Description
getHeaderByNumber eth_getHeaderByNumber Block.Header | null A block header by number or tag.
getHeaderByHash eth_getHeaderByHash Block.Header | null A block header by hash.
getBlockTransactionCountByNumber eth_getBlockTransactionCountByNumber number | null How many transactions a block has, by number or tag.
getBlockTransactionCountByHash eth_getBlockTransactionCountByHash number | null How many transactions a block has, by hash.
getUncleCountByBlockNumber eth_getUncleCountByBlockNumber number | null How many uncles a block has, by number or tag.
getUncleCountByBlockHash eth_getUncleCountByBlockHash number | null How many uncles a block has, by hash.
getUncleByBlockNumberAndIndex eth_getUncleByBlockNumberAndIndex Block.Block | null An uncle of a block, by block number or tag and position.
getUncleByBlockHashAndIndex eth_getUncleByBlockHashAndIndex Block.Block | null An uncle of a block, by block hash and position.
getBlockAccessList eth_getBlockAccessList BlockAccessList | null What a block read and wrote, per account.

Each method names its block with block, a number or a tag, or with blockHash. Each returns an Effect of the type in the table, which fails with a Method.Error and needs a Transport. For the whole block, use Eth.getBlockByNumber or Eth.getBlockByHash.

Returns the header of a block by number or tag: the block without its transactions, uncles and withdrawals.

import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";
const baseFee = Effect.gen(function* () {
const header = yield* Eth.getHeaderByNumber("latest");
if (header === null) return null;
return header.baseFeePerGas; // 1000000000n on a fresh anvil
});

Returns the header of a block by its hash.

import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";
const parent = Effect.gen(function* () {
const head = yield* Eth.getHeaderByNumber("latest");
if (head === null) return null;
return yield* Eth.getHeaderByHash(head.parentHash);
});
  • JSON-RPC: eth_getHeaderByHash
  • Parameters: blockHash (Hash).
  • Returns: Block.Header, or null when no block has the hash.

Returns how many transactions a block has, by number or tag.

import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";
const count = Eth.getBlockTransactionCountByNumber("latest").pipe(
Effect.map((count) => (count === null ? "no such block" : `${count} transaction(s)`)),
);
  • JSON-RPC: eth_getBlockTransactionCountByNumber
  • Parameters: block (BlockId.NumberOrTag).
  • Returns: number, or null when the node does not have the block. With "pending", anvil counts the transactions waiting in its pool.

Returns how many transactions a block has, by block hash.

import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";
const count = Eth.getBlockTransactionCountByHash(
"0xe5178f52e018186949d9fe9168c6011820f9c6bd449a93dbb07c09dfc0af3dfa",
);
  • JSON-RPC: eth_getBlockTransactionCountByHash
  • Parameters: blockHash (Hash).
  • Returns: number, or null when no block has the hash.

Returns how many uncles a block has, by number or tag. It is always 0 for a block mined since the merge.

import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";
const uncles = Eth.getUncleCountByBlockNumber(15_000_000n);
  • JSON-RPC: eth_getUncleCountByBlockNumber
  • Parameters: block (BlockId.NumberOrTag).
  • Returns: number, or null.
  • For a block it does not have, anvil 1.9 does not answer null: the call fails with an RpcError whose reason is ResourceNotFound.

Returns how many uncles a block has, by block hash.

import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";
const uncles = Eth.getUncleCountByBlockHash(
"0xe5178f52e018186949d9fe9168c6011820f9c6bd449a93dbb07c09dfc0af3dfa",
);
  • JSON-RPC: eth_getUncleCountByBlockHash
  • Parameters: blockHash (Hash).
  • Returns: number, or null.
  • For a hash no block has, anvil 1.9 does not answer null: the call fails with an RpcError whose reason is ResourceNotFound.

Returns one uncle of a block, by the number or tag of the block and the position of the uncle.

import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";
const uncle = Eth.getUncleByBlockNumberAndIndex(15_000_000n, 0).pipe(
Effect.map((uncle) => uncle?.miner),
);
  • JSON-RPC: eth_getUncleByBlockNumberAndIndex
  • Parameters: block (BlockId.NumberOrTag), then index (number).
  • Returns: Block.Block, or null when the block has no uncle at that position.

Returns one uncle of a block, by the hash of the block and the position of the uncle.

import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";
const uncle = Eth.getUncleByBlockHashAndIndex(
"0xe5178f52e018186949d9fe9168c6011820f9c6bd449a93dbb07c09dfc0af3dfa",
0,
);
// null: the block has no uncle
  • JSON-RPC: eth_getUncleByBlockHashAndIndex
  • Parameters: blockHash (Hash), then index (number).
  • Returns: Block.Block, or null when the block has no uncle at that position.

Returns what a block read and wrote, account by account, as defined by EIP-7928.

import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";
const touched = Effect.gen(function* () {
const accessList = yield* Eth.getBlockAccessList("latest");
if (accessList === null) return [];
return accessList.map((account) => account.address);
});
  • JSON-RPC: eth_getBlockAccessList
  • Parameters: block (BlockId.BlockId): a number, a tag, or an object that names the block by number or by hash.
  • Returns: BlockAccessList.BlockAccessList, or null for a block from before the Amsterdam fork, which has no access list.
  • For a block above its head, anvil 1.9 fails with an RpcError whose reason is InvalidParams.
  • anvil 1.9 started with --hardfork amsterdam sends storage slots and values in a short form ("0x7"). They are read as 32-byte words.
  • null means the node has nothing to give. The block does not exist, or it has no such uncle or access list. The two uncle counts on anvil are the exception noted above.
  • Counts and positions are number. A transaction count, an uncle count and an index are small: they are not bigint. See values on the wire.
  • Hashes are checked before sending. A blockHash that is not 32 bytes of hex fails with a CodecError on the params, and nothing is sent.