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.
getHeaderByNumber
Section titled “getHeaderByNumber”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});- JSON-RPC:
eth_getHeaderByNumber - Parameters:
block(BlockId.NumberOrTag). - Returns:
Block.Header, ornullwhen the node does not have the block.
getHeaderByHash
Section titled “getHeaderByHash”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, ornullwhen no block has the hash.
getBlockTransactionCountByNumber
Section titled “getBlockTransactionCountByNumber”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, ornullwhen the node does not have the block. With"pending", anvil counts the transactions waiting in its pool.
getBlockTransactionCountByHash
Section titled “getBlockTransactionCountByHash”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, ornullwhen no block has the hash.
getUncleCountByBlockNumber
Section titled “getUncleCountByBlockNumber”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, ornull. - For a block it does not have, anvil 1.9 does not answer
null: the call fails with anRpcErrorwhose reason isResourceNotFound.
getUncleCountByBlockHash
Section titled “getUncleCountByBlockHash”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, ornull. - For a hash no block has, anvil 1.9 does not answer
null: the call fails with anRpcErrorwhose reason isResourceNotFound.
getUncleByBlockNumberAndIndex
Section titled “getUncleByBlockNumberAndIndex”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), thenindex(number). - Returns:
Block.Block, ornullwhen the block has no uncle at that position.
getUncleByBlockHashAndIndex
Section titled “getUncleByBlockHashAndIndex”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), thenindex(number). - Returns:
Block.Block, ornullwhen the block has no uncle at that position.
getBlockAccessList
Section titled “getBlockAccessList”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, ornullfor a block from before the Amsterdam fork, which has no access list. - For a block above its head, anvil 1.9 fails with an
RpcErrorwhose reason isInvalidParams. - anvil 1.9 started with
--hardfork amsterdamsends storage slots and values in a short form ("0x7"). They are read as 32-byte words.
Behavior
Section titled “Behavior”nullmeans 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 anindexare small: they are notbigint. See values on the wire. - Hashes are checked before sending. A
blockHashthat is not 32 bytes of hex fails with aCodecErroron the params, and nothing is sent.