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

DataDope
  • 33
  • 1
  • 4

1 Answers1

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();

hroussille
  • 7,661
  • 2
  • 6
  • 29
  • 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] (https://observablehq.com/d/a9be02e6ce765558), but if I run the function the call `data = await functionName(startblocknumber, endblocknumber)’ I still get undefined – DataDope Mar 27 '22 at 04: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 '22 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 '22 at 02:47
  • even with the edit i still end up with undefined when i call main() or data = main().then(console.log) – DataDope Mar 30 '22 at 03: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 '22 at 07:36
  • Yah, learning JS is defiantly a priority. Thanks for the help! the call worked as you suggested. I guess i don't understand the point of the main() function above. here is that call i used in the end blocksdata = { console.log( "Searching for non-zero transaction counts between blocks 2000000 and 2000010" ); let transactions = await checkTransactionCount(startBlockNumber, endBlockNumber); console.log(transactions.length + " transactions found"); console.log(transactions); return transactions; } – DataDope Apr 01 '22 at 14:05
  • This was just to get a top level async function to show how to use the checkTransactionCount function, but don't bother with that, it was just for the example. – hroussille Apr 01 '22 at 14:41