0

In order to get a list of transactions by account address I used the following code snippet -

function getTransactionsByAccount(myaccount, startBlockNumber, endBlockNumber) {
   if (endBlockNumber == null) {
     endBlockNumber = eth.blockNumber;
     console.log("Using endBlockNumber: " + endBlockNumber);
   }
   if (startBlockNumber == null) {
     startBlockNumber = endBlockNumber - 1000;
     console.log("Using startBlockNumber: " + startBlockNumber);
   }
   console.log("Searching for transactions to/from account \"" + myaccount + "\" within blocks "  + startBlockNumber + " and " + endBlockNumber);

   for (var i = startBlockNumber; i <= endBlockNumber; i++) {
     if (i % 1000 == 0) {
       console.log("Searching block " + i);
     }
     var block = eth.getBlock(i, true);
     if (block != null && block.transactions != null) {
       block.transactions.forEach( function(e) {
         if (myaccount == "*" || myaccount == e.from || myaccount == e.to) {
           console.log("  tx hash          : " + e.hash + "\n"
             + "   nonce           : " + e.nonce + "\n"
             + "   blockHash       : " + e.blockHash + "\n"
             + "   blockNumber     : " + e.blockNumber + "\n"
             + "   transactionIndex: " + e.transactionIndex + "\n"
             + "   from            : " + e.from + "\n" 
             + "   to              : " + e.to + "\n"
             + "   value           : " + e.value + "\n"
             + "   time            : " + block.timestamp + " " + new Date(block.timestamp * 1000).toGMTString() + "\n"
             + "   gasPrice        : " + e.gasPrice + "\n"
             + "   gas             : " + e.gas + "\n"
             + "   input           : " + e.input);
         }
       })
     }
   }
 }

However, upon running the script, I am getting the following error.

Error: invalid argument 0: hex string without 0x prefix
    at web3.js:3143:20
    at web3.js:6347:15
    at web3.js:5081:36
    at getTransactionsByAccount (/root/files/js/getTransactionsByAccount.js:16:18)
    at <anonymous>:1:43
0

Upon further troubleshooting found that blocks were less than 1000

eth.blockNumber
> 41

As a result startBlockNumber is negative (since 41-1000). As a result eth.getBlock(i, true); searches for a non-positive block.

It would have been better if the error would have been indicative.

Solution -

...
if (startBlockNumber == null) {
     startBlockNumber = endBlockNumber - 1000;
     if(startBlockNumber<0)
     startBlockNumber=0;
     console.log("Using startBlockNumber: " + startBlockNumber);
   }
...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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