eth_getBlockTransactionCountByNumber | Arbitrum

Arbitrum API method that returns the number of transactions in a block specified by block number or tag. This information can be useful for analytics purposes.

👍

Get you own node endpoint today

Start for free and get your app to production levels immediately. No credit card required.

You can sign up with your GitHub, X, Google, or Microsoft account.

Parameters

  • quantity or tag — the integer of a block encoded as hexadecimal or the string with:

    • latest — the most recent block in the blockchain and the current state of the blockchain at the most recent block

    • earliest — the earliest available or genesis block

    • pending — the pending state and transactions block. The current state of transactions that have been broadcast to the network but have not yet been included in a block.

      📘

      See the default block parameter.

Response

  • quantity — the integer value representing how many transactions are included in the block.

eth_getBlockTransactionCountByNumber code examples

const Web3 = require("web3");
const NODE_URL = "CHAINSTACK_NODE_URL";
const web3 = new Web3(NODE_URL);

async function getTransactionsCount(blockId) {
  const count = await web3.eth.getBlockTransactionCount(blockId)
  console.log(count);
}

getTransactionsCount('latest')
const ethers = require('ethers');
const NODE_URL = "CHAINSTACK_NODE_URL";
const provider = new ethers.JsonRpcProvider(NODE_URL);

const getTransactionsCount = async (blockId) => {
    const count = await provider.send("eth_getBlockTransactionCountByNumber", [blockId]);
     console.log(count);
   };

   getTransactionsCount('latest')
from web3 import Web3  
node_url = "CHAINSTACK_NODE_URL" 

web3 = Web3(Web3.HTTPProvider(node_url)) 
print (web3.eth.get_block_transaction_count(69800577)) # A hex value starting with "0x" is accepted as well.

Use case

eth_getBlockTransactionCountByNumber can be used to analyze how many transactions are included on the Arbitrum blockchain in a certain period. On average, a new block is generated on the Arbitrum mainnet every 0.3 seconds, resulting in approximately 12,000 blocks per hour. Using the ethers.js library, one can inspect the past 12,000 blocks starting from the latest block, retrieve the hash of each block, and use eth_getBlockTransactionCountByNumber to find the number of transactions in each block to sum them.

const ethers = require('ethers');
const NODE_URL = "CHAINSTACK_NODE_URL";
const provider = new ethers.JsonRpcProvider(NODE_URL);

async function retrieveTransactionCounts() {
    // Get the current block number
    const currentBlockNumber = await provider.getBlockNumber();
  
    // Calculate the block number from one hour ago
    const oneHourAgoBlockNumber = currentBlockNumber - 12000; // 1200/blocks/hr on average on the Arbitrum mainnet
  
    // Initialize a variable to store the total number of transactions
    let totalTransactionCount = 0;
  
    // Loop through all blocks from the current block number to the block number from one hour ago
    for (let block = currentBlockNumber; block > oneHourAgoBlockNumber; block--) {
  
      // Get the number of transactions in this block
      const transactionCount = await provider.send("eth_getBlockTransactionCountByNumber", [block]);

      // Make the necessery conversions from hex to decimal
      const stringCount = BigInt(transactionCount).toString();
      const decimalCount = Number(stringCount)

      // Add the number of transactions in this block to the total
      totalTransactionCount += decimalCount;

      const stringBlock = BigInt(block).toString();
      const decimalBlock = Number(stringBlock)
      console.log(`Block ${decimalBlock} has ${decimalCount} transactions.`);
    }
  
    console.log(`The total number of transactions in the previous hour is: ${totalTransactionCount}`);
  }
  
  retrieveTransactionCounts();

Using eth_getBlockTransactionCountByNumber is a bit more straightforward compared to the example shown using eth_getBlockTransactionCountByHash for this same use case, you don't need to extract the block hash; instead, you can query the transaction count directly.

This example is made using the ethers.js library, and you can notice that it requires more parsing compared to web3.js, and the results are given as hexadecimal.

Try the eth_getBlockTransactionCountByNumber RPC method yourself

Language
Click Try It! to start a request and see the response here!