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.
With the transactions in full
Section titled “With the transactions in full”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 block the node does not have
Section titled “A block the node does not have”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"A flag known at run time
Section titled “A flag known at run time”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, ), ), );By tag
Section titled “By tag”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)));Returns
Section titled “Returns”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.
Parameters
Section titled “Parameters”- Type:
BlockId.NumberOrTag
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.
Behavior
Section titled “Behavior”null, not an error. A block number the chain has not reached givesnull."pending"is the block being built. anvil answers it with the transactions waiting in its pool.- One block, two sizes. With
truethe node sends every transaction of the block. Ask for hashes when you do not read them. - Only the header.
Eth.getHeaderByNumberreturns the block without its body.
Errors
Section titled “Errors”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 |
JSON-RPC method
Section titled “JSON-RPC method”eth_getBlockByNumber