The eth_getBlockTransactionCountByNumber
method returns the number of transactions in a block, identified by the block's number or block tag. This lightweight method provides a simple way to check transaction volume and network activity without retrieving complete block data, making it essential for monitoring chain activity.
This method allows querying transaction counts for any block in the chain, including special block tags like 'latest' and 'pending'.
Block number in hex format or tags: latest, earliest, pending, safe, finalized
The number of transactions in the specified block (hexadecimal)
{
"jsonrpc": "2.0",
"id": 1,
"result": "0x9B" // 155 transactions
}
This method supports these special block tags as parameters:
latest
: Most recently mined block (most commonly used)earliest
: Genesis block (block 0)pending
: Currently pending block (transactions in mempool waiting to be mined)safe
: Latest block considered safe by network (finality in progress)finalized
: Latest block that has achieved finality (post-Merge only)The response is a hexadecimal string that needs to be converted to decimal:
// Convert hex transaction count to decimal
const txCountHex = "0x9B";
const txCountDecimal = parseInt(txCountHex, 16); // 155
Transaction counts provide valuable insights into:
// Example: Track transaction throughput over time
async function analyzeNetworkThroughput(startBlock, blockCount = 100) {
const counts = [];
let totalTxs = 0;
for (let i = 0; i < blockCount; i++) {
const blockNum = (BigInt(startBlock) + BigInt(i)).toString(16);
const txCount = await provider.send('eth_getBlockTransactionCountByNumber', [`0x${blockNum}`]);
const block = await provider.send('eth_getBlockByNumber', [`0x${blockNum}`, false]);
const timestamp = parseInt(block.timestamp, 16);
const count = parseInt(txCount, 16);
counts.push({
blockNumber: `0x${blockNum}`,
timestamp,
transactionCount: count,
});
totalTxs += count;
}
// Calculate averages and trends
const avgTxPerBlock = totalTxs / blockCount;
const timespan = counts[counts.length - 1].timestamp - counts[0].timestamp;
const txPerSecond = totalTxs / timespan;
return {
blocks: counts,
averageTransactionsPerBlock: avgTxPerBlock,
transactionsPerSecond: txPerSecond,
totalTransactions: totalTxs,
timeSpanSeconds: timespan,
};
}
// Usage
const stats = await analyzeNetworkThroughput('0x118F500', 50);
console.log(`Average TPS: ${stats.transactionsPerSecond.toFixed(2)}`);
console.log(`Average transactions per block: ${stats.averageTransactionsPerBlock.toFixed(2)}`);
Transaction counts vary significantly across different Ethereum-compatible networks:
This variation reflects differences in blocktime, fee structures, and ecosystem usage patterns.