Skip to content

Eth.getBlockByNumber

Returns a block by number or tag, with its transactions as hashes or in full, or null when the node does not have the block.

import { Effect } from "effect";
import { FetchHttpClient } from "effect/http";
import { Transport } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const block = await Eth.getBlockByNumber("latest", false).pipe(
Effect.provide(Transport.http("http://127.0.0.1:8545")),
Effect.provide(FetchHttpClient.layer),
Effect.runPromise,
);
// block?.number: 1n
// block?.transactions:
// ["0xa23b52004fe574000765fbe20a311b2785a318c9dbe6b0bf6a4c1f8936f57ff3"]

The call works over every transport. The examples below leave the transport out.

Pass true and transactions holds Transaction objects instead of hashes.

import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";
const moved = Effect.gen(function* () {
const block = yield* Eth.getBlockByNumber("latest", true);
if (block === null) return 0n;
return block.transactions.reduce((sum, transaction) => sum + transaction.value, 0n);
});

A number above the head is not an error: the result is null.

import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";
const outcome = Eth.getBlockByNumber(1_000_000_000n, false).pipe(
Effect.map((block) =>
block === null ? "no such block" : `block ${block.number} is ${block.hash}`,
),
);
// "no such block"

Pass a boolean when the program only learns which form it wants as it runs. The block is then a Block.Any: one shape or the other.

import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";
/** The hashes of the latest block's transactions, whichever form was asked for. */
const hashes = (full: boolean) =>
Eth.getBlockByNumber("latest", full).pipe(
// block: Block.Any | null
Effect.map((block) =>
(block?.transactions ?? []).map((transaction) =>
typeof transaction === "string" ? transaction : transaction.hash,
),
),
);

A tag names a block by its place in the chain, whatever its number is.

import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";
const heads = Effect.all([
Eth.getBlockByNumber("latest", false),
Eth.getBlockByNumber("safe", false),
Eth.getBlockByNumber("finalized", false),
]).pipe(Effect.map((blocks) => blocks.map((block) => block?.number)));

Effect<Block.Block | null, Method.Error, Transport> with false, and Effect<Block.Full | null, Method.Error, Transport> with true. With a value typed boolean, the block is a Block.Any | null.

The two shapes are on the Block page. They differ only in transactions, and Block.Any is their union.

A block number as a bigint, or a tag: "latest", "earliest", "pending", "safe", "finalized".

import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";
const blocks = Effect.all([
Eth.getBlockByNumber(19_000_000n, false),
Eth.getBlockByNumber("finalized", false),
]);

To name a block by its hash, use Eth.getBlockByHash.

  • Type: boolean

false for the hashes of the transactions, true for the transactions in full. It is required.

import { Eth } from "@ethect/core/rpc";
// Effect<Block.Block | null, …>
const withHashes = Eth.getBlockByNumber("latest", false);
// Effect<Block.Full | null, …>
const inFull = Eth.getBlockByNumber("latest", true);
// Effect<Block.Any | null, …>
const either = (full: boolean) => Eth.getBlockByNumber("latest", full);

The result type follows the literal you write. A value typed boolean gives the union of the two shapes.

  • null, not an error. A block number the chain has not reached gives null.
  • "pending" is the block being built. anvil answers it with the transactions waiting in its pool.
  • One block, two sizes. With true the node sends every transaction of the block. Ask for hashes when you do not read them.
  • Only the header. Eth.getHeaderByNumber returns the block without its body.

The three errors every method can fail with. The case specific to this method:

Error Reason When
CodecError on: "params" the block number is negative: nothing is sent

eth_getBlockByNumber