Query information about blockchain blocks.
Get the current block number
Returns the number of the most recent block. Useful for tracking blockchain progress or generating unique identifiers based on block height.
async function getCurrentBlock() {
const blockNum = await window.ethereum.request({
method: 'eth_blockNumber'
});
// Convert hex to decimal
const blockNumber = Number(BigInt(blockNum));
console.log('Current block:', blockNumber);
return blockNumber;
}Get detailed information about a block
Returns all data about a specific block, including timestamp, miner, transactions, and more.
async function getBlockDetails() {
const block = await window.ethereum.request({
method: 'eth_getBlockByNumber',
params: ['latest', true] // true = include full transactions
});
console.log('Block number:', Number(BigInt(block.number)));
console.log('Timestamp:', new Date(Number(BigInt(block.timestamp)) * 1000));
console.log('Miner:', block.miner);
console.log('Transactions:', block.transactions.length);
console.log('Gas used:', Number(BigInt(block.gasUsed)));
console.log('Gas limit:', Number(BigInt(block.gasLimit)));
return block;
}Tip: Set the second parameter to true to get full transaction objects. Set to false for just transaction hashes (faster).