eth_getHeaderByNumber
JSON-RPC method returns the block header information for a given block number. This method provides header data without the transaction list, offering a lightweight way to access block metadata and is more efficient than fetching the full block when only header information is needed.
null
if the block doesn’t exist.
hash
— The block hashparentHash
— Hash of the parent blocknumber
— The block numbertimestamp
— The unix timestamp when the block was collatedgasLimit
— The maximum gas allowed in this blockgasUsed
— The total gas used by all transactions in this blockdifficulty
— The difficulty for this blocktotalDifficulty
— The total difficulty of the chain until this blockminer
— The address of the beneficiary to whom the mining rewards were givennonce
— The nonce used to generate this blocksha3Uncles
— SHA3 of the uncles data in the blocklogsBloom
— The bloom filter for the logs of the blocktransactionsRoot
— The root of the transaction trie of the blockstateRoot
— The root of the final state trie of the blockreceiptsRoot
— The root of the receipts trie of the block"latest"
— The most recent block in the chain"earliest"
— The genesis block (block 0)"pending"
— The pending state/transactions// Get block header by number
const getHeaderByNumber = async (blockNumber) => {
const response = await fetch('https://hyperliquid-mainnet.core.chainstack.com/YOUR_ENDPOINT/evm', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
jsonrpc: '2.0',
method: 'eth_getHeaderByNumber',
params: [blockNumber],
id: 1
})
});
const data = await response.json();
return data.result;
};
// Monitor block headers efficiently
const monitorBlockHeaders = async (callback) => {
let lastBlockNumber = 0;
const checkLatestHeader = async () => {
try {
const header = await getHeaderByNumber('latest');
const currentBlock = parseInt(header.number, 16);
if (currentBlock > lastBlockNumber) {
lastBlockNumber = currentBlock;
callback(header);
}
} catch (error) {
console.error('Error fetching header:', error);
}
};
// Poll every 2 seconds
setInterval(checkLatestHeader, 2000);
};
// Analyze block timing
const analyzeBlockTiming = async (fromBlock, toBlock) => {
const analysis = {
blocks: [],
averageBlockTime: 0,
minBlockTime: Infinity,
maxBlockTime: 0
};
for (let i = fromBlock; i <= toBlock; i++) {
const header = await getHeaderByNumber(`0x${i.toString(16)}`);
if (header) {
const blockTime = parseInt(header.timestamp, 16);
analysis.blocks.push({
number: i,
timestamp: blockTime,
gasUsed: parseInt(header.gasUsed, 16),
gasLimit: parseInt(header.gasLimit, 16)
});
}
}
// Calculate timing statistics
for (let i = 1; i < analysis.blocks.length; i++) {
const timeDiff = analysis.blocks[i].timestamp - analysis.blocks[i-1].timestamp;
analysis.minBlockTime = Math.min(analysis.minBlockTime, timeDiff);
analysis.maxBlockTime = Math.max(analysis.maxBlockTime, timeDiff);
}
const totalTime = analysis.blocks[analysis.blocks.length - 1].timestamp -
analysis.blocks[0].timestamp;
analysis.averageBlockTime = totalTime / (analysis.blocks.length - 1);
return analysis;
};
// Usage
getHeaderByNumber('latest').then(header => {
console.log('Latest block header:', header);
});
monitorBlockHeaders((header) => {
console.log(`New block ${parseInt(header.number, 16)}: ${header.hash}`);
});
curl -X POST \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "eth_getHeaderByNumber",
"params": [
"latest"
],
"id": 1
}' \
https://hyperliquid-mainnet.core.chainstack.com/4f8d8f4040bdacd1577bff8058438274/evm
eth_getHeaderByNumber
method is useful for applications that need to: