Web3.js – How to Obtain All Transactions of a Contract

dapp-developmentmetamaskropstenweb3js

I'm trying to obtain all the transactions of a contract, but in all cases I only obtained [] or undefined.

I'm following this posts:

And I read others posts too, but i can't solve my problem.

I try to use this:

var filter=web3.eth.filter({fromBlock: 866705, toBlock: 909023, address: contractAddress});
filter.get(function(error, log) {
  console.log(JSON.stringify(log));
});
filter.stopWatching(); 

With different values of fromblock, to block and the others options ('latest',fromblock: 0 , …). But i don't obtain any result.

I'm trying to do this :

var filter = web3.eth.filter({fromBlock:9000, toBlock: 'latest', address: contractAddress});
filter.get(function(error, result) {
  if(!error){
    var info = web3.eth.getBlock(result , function(error, result){
       if(!error){
         var trans = web3.eth.getTransaction(result.transactions[1], function(error,result){
           if(!error){
             var str = web3.toAscii(result.input,);
             console.log(str);
           }else{
              console.log(error);
           }
         });
       }else{
          console.log(error);
       }
     });
 }else{
    console.log(error);
  }
})

(If i forget some token sorry)

All of this only for obtain the data of a transaction like "from", "to" and "input". Where is the problem?

More information:

  • I'm using metamask and meteor.js
  • I'm running this in Ropsten test-net
  • If I use the function web3.eth.getBlock wiht a correct blocknumber I can obtain all the data, the problem is the filter and how I use it.

Best Answer

Another way (perhaps much simpler) to get a list of transactions on an account is to use an API from a block scraper such as http://etherscan.io. The trouble with this is it's fully centralized.

A fully decentralized way is go against the node (as you're doing). The trouble here is that, while it is decentralized, it's slow. Especially if your smart contract has a very long transaction history.

An even deeper problem with going against the node is that simply getting the transactions is not enough. You will also need to get the transaction receipts. You need these to determine if the transactions completed with an error or not.

Worse yet even than that is incoming message calls (what used to be called internal transactions). These are "transactions" initiated by an outside smart contract directly into your address. These "transactions" do not appear on the blockchain directly, but are buried down in an internal trace of the initiating transaction. If you're looking only at your address, these will be missed.