Skip to content

Eth.blockNumber

Returns the number of the most recent block the node has.

import { Effect } from "effect";
import { FetchHttpClient } from "effect/http";
import { Transport } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const blockNumber = await Eth.blockNumber().pipe(
Effect.provide(Transport.http("http://127.0.0.1:8545")),
Effect.provide(FetchHttpClient.layer),
Effect.runPromise,
);
// 0n on a fresh anvil

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

Subtract the block a transaction was mined in from the head to know how many blocks confirm it.

import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";
const hash = "0xa23b52004fe574000765fbe20a311b2785a318c9dbe6b0bf6a4c1f8936f57ff3";
const confirmations = Effect.gen(function* () {
const receipt = yield* Eth.getTransactionReceipt(hash);
if (receipt === null) return 0n;
const head = yield* Eth.blockNumber();
return head - receipt.blockNumber + 1n;
});

A transaction mined in block 1n has 3n confirmations when the head is 3n.

To read the latest block you do not need its number first: name it with the "latest" tag.

import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";
const latest = Eth.getBlockByNumber("latest", false).pipe(
Effect.map((block) => block?.timestamp),
);

Effect<bigint, Method.Error, Transport>

The number of the block at the head of the chain, as this node sees it.

None.

  • A bigint. The node answers "0x6" and you get 6n. Every block number in ethect is a bigint. See values on the wire.
  • One request per call. To be told about each new block instead of asking, use Eth.subscribe.newHeads.

eth_blockNumber