Web3 by Example: Querying Blocks

Block is a collection of required information (a block header) about the comprised transactions, and a set of other block headers known as ommers. Blocks are added to the Ethereum network by miners.

We use a default provider to query block information on the ethereum mainnet.

Get the block from the network at the latest height.

Or specify a block number.

Let's see what the block contains.


const { ethers } = require("ethers");
(async () => {
const provider = new ethers.providers.AlchemyProvider();
const block = await provider.getBlock(14292113);


➜ node block.js
hash: '0xd0735d21ebe47e306bb7a00df4a877509aa6f8ffa5c65f8de2885faf13b9f95e',
parentHash: '0x2553435a83165b2cf4ed84482f6a4e829155b4a9115e699ed16789dd28d503b7',
number: 14292113,
timestamp: 1646017635,
nonce: '0xf66f9be27a2db4e1',
difficulty: null,
gasLimit: BigNumber { _hex: '0x01c9c380', _isBigNumber: true },
gasUsed: BigNumber { _hex: '0x01c99d16', _isBigNumber: true },
miner: '0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8',
extraData: '0x6575726f70652d77657374332d34',
transactions: [
baseFeePerGas: BigNumber { _hex: '0x099d452c96', _isBigNumber: true },
_difficulty: BigNumber { _hex: '0x2c97ff39a3b876', _isBigNumber: true }

Next example: Querying Transactions