Scholarpeak

Block Methods

Query information about blockchain blocks.

eth_blockNumber

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.

typescript
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;
}

eth_getBlockByNumber

Get detailed information about a block

Returns all data about a specific block, including timestamp, miner, transactions, and more.

typescript
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).

Use Cases

Monitoring blockchain progress
Creating time-locked features (e.g., unlock after 100 blocks)
Analyzing network statistics
Verifying transaction mining
Building blockchain explorers