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 anvilThe call works over every transport. The examples below leave the transport out.
Count confirmations
Section titled “Count confirmations”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.
Read the block itself
Section titled “Read the block itself”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),);Returns
Section titled “Returns”Effect<bigint, Method.Error, Transport>
The number of the block at the head of the chain, as this node sees it.
Parameters
Section titled “Parameters”None.
Behavior
Section titled “Behavior”- A
bigint. The node answers"0x6"and you get6n. Every block number in ethect is abigint. See values on the wire. - One request per call. To be told about each new block instead of asking, use
Eth.subscribe.newHeads.
JSON-RPC method
Section titled “JSON-RPC method”eth_blockNumber