[Ethereum] web3.eth.filter({fromBlock:0, toBlock: ‘latest’, address: account}) not working

blockchaingo-ethereumtransactionsweb3js

My goal here is to list all transaction from an given account, without any success yet.

I wonder why I always have an empty result when I'm using this web3.eth.filter. I know there is a valid solution: Listing transactions in a private blockchain, but going through all blocks each time takes a considerable amount of time, so it is not really responsive on the user side.

So I wonder what is wrong with my code:

web3.eth.defaultAccount = web3.eth.accounts[0];
var account = web3.eth.accounts[0];

console.log('Account: '+ account);

var filter = web3.eth.filter({fromBlock:0, toBlock: 'latest', address: account});
filter.get(function(error, result) {
   if(!error) {
      for (i=0; i<result.length; ++i) {
        var block = web3.eth.getBlock(i, true);
        block.transactions.forEach( function(e) {

            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           : " + web3.toAscii(e.input))+"\n"; 

      })
  }
   }
   else {
      console.log('Error: '+error);
   }
});

I also tried with filter.watch, but the results remains the same: results is still empty.

Thanks

Best Answer

The web3.eth.filter command only goes though the blocks' logs. When you create (resp. receive) a transaction from (resp. to) your address, it does not ensue that a log entry has been created.

Creating a log entry is an action that needs to be done explicitly. So scanning all the blocks is indeed the way to go.

To avoid scanning every time, you ought to save your findings in a regular database.

Related Topic