3

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

1 Answer 1

1

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();
7
  • So, this function seams to work, but I still can’t call it into a dataset/ object, what am I missing here…this is my example [notebook] (observablehq.com/d/a9be02e6ce765558), but if I run the function the call `data = await functionName(startblocknumber, endblocknumber)’ I still get undefined
    – DataDope
    Mar 27, 2022 at 4:06
  • Well your function is not returning anything, so that's what you'd have to change, do you need an example of that ? I just made the function you provided work, now if you want it to return the set of transactions just tell me and I can add the appropriate versions.
    – hroussille
    Mar 27, 2022 at 11:59
  • Yah, That would be super helpful. I’m using 1.6 with infuria. Thanks for helping a newb, will have to work on my Java script!
    – DataDope
    Mar 28, 2022 at 2:47
  • even with the edit i still end up with undefined when i call main() or data = main().then(console.log)
    – DataDope
    Mar 30, 2022 at 3:22
  • You definitely need to learn more JavaScript, the edit makes the function checkTransactionCount return an array of transactions, it should be called like in my example : ` let transactions = await checkTransactionCount(2000000, 2000010);` where you can change the start and end block of course.
    – hroussille
    Mar 30, 2022 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.