1

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

1 Answer 1

3

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.

4
  • So after reading your answer, a simple question comes to my mind: How can I create a log entry for each transaction? Commented Jan 17, 2017 at 11:27
  • You need to: make a contract that will call one of the LOGx opcodes, or write one in Solidity with the event construct; have the contract call the event every time it receives a transaction. It is not possible to have just any transaction log something. Commented Jan 17, 2017 at 11:52
  • Thanks. It is what I thought too (creating an event in Solidity). Is there some papers talking about this LOGx opcodes ? Commented Jan 17, 2017 at 13:29
  • 2
    Better you rely on Solidity events solidity.readthedocs.io/en/develop/… which compile to LOGx. Commented Jan 17, 2017 at 16:14

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.