获取慢速ETH交易

时间:2019-02-20 00:13:36

标签: web3 web3js

正在寻找一种可以更快地为特定地址挑选交易的方法。

运行此功能

getTransactionsByAccount(address, 0, web3.eth.blockNumber);

功能

// Get Wallet Transactions
function getTransactionsByAccount(myaccount, startBlockNumber, endBlockNumber) {
 if (endBlockNumber == null) {
  endBlockNumber = web3.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 = web3.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"
        + "   from            : " + e.from + "\n" 
        + "   to              : " + e.to + "\n"
        + "   value           : " + e.value + "\n"
        + "   time            : " + block.timestamp + " " + new Date(block.timestamp * 1000).toGMTString() + "\n");
      // Entering all transactions onto page
      var subVal = web3.fromWei(e.value, 'ether');
      $(".trans_list").append("<li class='trans_entry'>TX Hash: "+ e.hash +"<br/>From: "+ e.from +"<br/>To: "+ e.to +"<br/>Amount: "+ subVal +"</li>");

    }
  })
 }
}
}

从本质上讲,它只是搜索每个单独的块,以便获取该地址的交易。问题是搜索每个块都需要很长时间。是否可以通过其他任何更快的方式获得这些信息?

0 个答案:

没有答案