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.

block.js

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

output

➜ 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: [
'0x86025d602b75fadab1e2ec4f8c3d69867bb85abd665c14b739367f489799c18f',
'0x3eae0a476bcd9bd718e5e8dfe6f64261990e19dc5ebe3296d7354296c8dc4bf3',
'0x79ea4c1501328bb5c02a4d6d7841478b652e9d4aaefe71796f6959e26417618c',
...
],
baseFeePerGas: BigNumber { _hex: '0x099d452c96', _isBigNumber: true },
_difficulty: BigNumber { _hex: '0x2c97ff39a3b876', _isBigNumber: true }
}

Next example: Querying Transactions