Web3.js – Getting All Transactions from Blocks in a Range of Block Numbers Using web3.js

blockchaincalldatachaindataweb3js

have seen similar post before, but can seam to get anything working. I am trying to pull block data (mostly all transactions) from a range of blocks (startBlock-endBlock) but keep getting errors or the function checks out but i return "undefined"

Using a function I found here just to grab transaction counts by block range (working up to full list) :Common useful JavaScript snippets for geth

function checkTransactionCount(startBlockNumber, endBlockNumber) {
  console.log("Searching for non-zero transaction counts between blocks "  + startBlockNumber + " and " + endBlockNumber);

  for (var i = startBlockNumber; i <= endBlockNumber; i++) {
    var block = web3.eth.getBlock(i);
    if (block != null) {
      if (block.transactions != null && block.transactions.length != 0) {
        console.log("Block #" + i + " has " + block.transactions + " transactions")
      }
    }
  }  
}

but get "undefined" when i run checkTransactionCount(startBlockNumber, endBlockNumber).

pretty new to web3.js, any help is appreciated

Best Answer

web3.eth.getBlock(i) is an asynchronous function, you should either use a callback or async / await. Because you are not waiting for the result, you get undefined simply because there is no result (yet).

Here is a version using the callback :

function checkTransactionCount(startBlockNumber, endBlockNumber) {
  console.log(
    "Searching for non-zero transaction counts between blocks " +
      startBlockNumber +
      " and " +
      endBlockNumber
  );

  for (var i = startBlockNumber; i <= endBlockNumber; i++) {
    web3.eth.getBlock(i, (err, block) => {
      if (err == null && block != null) {
        if (block.transactions != null && block.transactions.length != 0) {
          console.log(
            "\n\nBlock #" + i + " has " + block.transactions + " transactions"
          );
        }
      }
    });
  }
}

Or as stated above, you could use async / await like so :

async function checkTransactionCount(startBlockNumber, endBlockNumber) {
  console.log(
    "Searching for non-zero transaction counts between blocks " +
      startBlockNumber +
      " and " +
      endBlockNumber
  );

  for (var i = startBlockNumber; i <= endBlockNumber; i++) {
    let block = await web3.eth.getBlock(i);
    if (block != null) {
      if (block.transactions != null && block.transactions.length != 0) {
        console.log(
          "\n\nBlock #" + i + " has " + block.transactions + " transactions"
        );
      }
    }
  }
}

EDIT : Here is an example using async / await returning an array with the transactions hash that are present between startBlockNumber and endBlockNumber :

const Web3 = require("Web3");
const ENDPOINT = "https://mainnet.infura.io/v3/YOUR_PROJECT_ID";

let web3 = new Web3(ENDPOINT);

async function checkTransactionCount(startBlockNumber, endBlockNumber) {
  transactions = [];

  for (var i = startBlockNumber; i <= endBlockNumber; i++) {
    let block = await web3.eth.getBlock(i);
    if (block != null) {
      if (block.transactions != null && block.transactions.length != 0) {
        transactions = transactions.concat(block.transactions);
      }
    }
  }

  return transactions;
}

async function main() {
  console.log(
    "Searching for non-zero transaction counts between blocks 2000000 and 2000010"
  );
  let transactions = await checkTransactionCount(2000000, 2000010);

  console.log(transactions.length + " transactions found");
  console.log(transactions);
}

main();
Related Topic