Block
A block as a node returns it, in three shapes: the header alone, the block with the hashes of its transactions, and the block with its transactions in full.
import { Effect } from "effect";import { Eth } from "@ethect/core/rpc";
const latest = Effect.gen(function* () { const block = yield* Eth.getBlockByNumber("latest", false); if (block === null) return "no such block"; // block: Block.Block return `block ${block.number} has ${block.transactions.length} transaction(s)`;});Schemas
Section titled “Schemas”| Schema | Type | Describes | Returned by |
|---|---|---|---|
Block.headerSchema |
Block.Header |
the header fields | Eth.getHeaderByNumber, Eth.subscribe.newHeads |
Block.schema |
Block.Block |
the header, the body, and transactions as hashes |
Eth.getBlockByNumber(id, false) |
Block.fullSchema |
Block.Full |
the same with transactions as Transaction objects |
Eth.getBlockByNumber(id, true) |
Block.anySchema |
Block.Any |
either of the two: Block.Block | Block.Full |
Eth.getBlockByNumber(id, full) with a boolean |
Import a type to name it in your own signatures.
import type { Block } from "@ethect/core/rpc";
const gasUsedPercent = (block: Block.Header): number => Number((block.gasUsed * 10_000n) / block.gasLimit) / 100;Header fields
Section titled “Header fields”Block.Header, and the first part of Block.Block and Block.Full.
| Field | Type | Notes |
|---|---|---|
hash |
Hash | null |
null on a pending block, from a node that has no hash for it yet |
parentHash |
Hash |
|
sha3Uncles |
Hash |
|
miner |
Address | null |
the fee recipient. null on a pending block, from a node that has none yet |
stateRoot |
Hash |
|
transactionsRoot |
Hash |
|
receiptsRoot |
Hash |
|
logsBloom |
Hex | null |
can be null on a pending block |
number |
bigint | null |
can be null on a pending block |
gasLimit |
bigint |
|
gasUsed |
bigint |
|
timestamp |
bigint |
seconds since the Unix epoch |
extraData |
Hex |
|
difficulty |
bigint (optional) |
|
mixHash |
Hash (optional) |
prevRandao since the merge |
nonce |
Hex (optional) |
|
totalDifficulty |
bigint (optional) |
|
baseFeePerGas |
bigint (optional) |
since London |
withdrawalsRoot |
Hash (optional) |
since Shanghai |
blobGasUsed |
bigint (optional) |
since Cancun |
excessBlobGas |
bigint (optional) |
since Cancun |
parentBeaconBlockRoot |
Hash (optional) |
since Cancun |
requestsHash |
Hash (optional) |
since Prague |
blockAccessListHash |
Hash (optional) |
on a chain with block access lists (EIP-7928) |
slotNumber |
bigint (optional) |
where the node reports the slot of the block |
size |
bigint (optional) |
the size of the whole block, from a client that gives it with a header, as BSC does |
Body fields
Section titled “Body fields”Block.Block and Block.Full add the body.
| Field | Type | Notes |
|---|---|---|
size |
bigint |
the encoded block, in bytes. Required on a block, optional on a header alone. |
uncles |
ReadonlyArray<Hash> |
always empty since the merge |
withdrawals |
ReadonlyArray<Withdrawal> (optional) |
since Shanghai |
transactions |
ReadonlyArray<Hash> in Block.Block |
|
transactions |
ReadonlyArray<Transaction> in Block.Full |
Behavior
Section titled “Behavior”- Optional means absent. A field marked optional is left out of the object when the node
omits it or sends
null. Test it withblock.baseFeePerGas !== undefined. nullmeans pending.hash,miner,numberandlogsBloomcan benullonly for a block that is not mined yet, and which of them are depends on the node: anvil 1.9 gives its pending block a hash, a miner and a number. They are always present, so they are typed| null, not optional.- A pending block may have no
nonce. The field is optional, and absent when the node sendsnull. - Unknown fields are dropped. A chain that adds fields to its blocks still decodes. The extra fields are not in the result: extend the schema to read them.
- Numbers. Every quantity of a block is a
bigint. See values on the wire.
Extending the schema
Section titled “Extending the schema”A schema is an Effect Schema.Struct: copy its fields into a new one to add the fields of your
chain, then define a method with it.
import { Schema } from "effect";import { Quantity } from "@ethect/core";import { Block, BlockId, Method } from "@ethect/core/rpc";
// A rollup whose blocks carry the L1 block they were derived from.const RollupBlock = Schema.Struct({ ...Block.schema.fields, l1BlockNumber: Quantity.schema,});
const getRollupBlock = Method.make<[block: unknown, full: unknown]>()( "eth_getBlockByNumber", [BlockId.numberOrTagSchema, Schema.Literal(false)], Schema.NullOr(RollupBlock),);getRollupBlock("latest", false) now returns the standard fields plus l1BlockNumber as a
bigint. See Custom methods.