Skip to content

Eth.getBlockByHash

Returns a block by its hash, 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.getBlockByHash(
"0xe5178f52e018186949d9fe9168c6011820f9c6bd449a93dbb07c09dfc0af3dfa",
false,
).pipe(
Effect.provide(Transport.http("http://127.0.0.1:8545")),
Effect.provide(FetchHttpClient.layer),
Effect.runPromise,
);
// block?.number: 1n

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 blockHash = "0xe5178f52e018186949d9fe9168c6011820f9c6bd449a93dbb07c09dfc0af3dfa";
const senders = Effect.gen(function* () {
const block = yield* Eth.getBlockByHash(blockHash, true);
if (block === null) return [];
return block.transactions.map((transaction) => transaction.from);
});

A receipt names its block by hash. Read that block to know when the transaction was mined.

import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";
const hash = "0xa23b52004fe574000765fbe20a311b2785a318c9dbe6b0bf6a4c1f8936f57ff3";
const minedAt = Effect.gen(function* () {
const receipt = yield* Eth.getTransactionReceipt(hash);
if (receipt === null) return null;
const block = yield* Eth.getBlockByHash(receipt.blockHash, false);
return block === null ? null : new Date(Number(block.timestamp) * 1000);
});

Every block holds the hash of the one before it.

import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";
const parent = Effect.gen(function* () {
const head = yield* Eth.getBlockByNumber("latest", false);
if (head === null) return null;
return yield* Eth.getBlockByHash(head.parentHash, false);
});

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.

The hash of the block: 32 bytes of hex. A string literal works, and so does a hash read from another result.

import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";
const block = Eth.getBlockByHash(
"0xe5178f52e018186949d9fe9168c6011820f9c6bd449a93dbb07c09dfc0af3dfa",
false,
);
  • Type: boolean

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

import { type Hash } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
// Effect<Block.Any | null, …>
const read = (blockHash: Hash.Hash, full: boolean) =>
Eth.getBlockByHash(blockHash, full);

The result type follows the literal you write: Block.Block with false, Block.Full with true. A value typed boolean gives Block.Any, the union of the two.

  • null, not an error. A hash no block has gives null.
  • A hash names one block for good. After a reorg, a block number can name another block. A block hash cannot.
  • Only the header. Eth.getHeaderByHash 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 hash is not 32 bytes of hex: nothing is sent

eth_getBlockByHash