5

I'm trying to obtain the all 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.
  • IN the first snippet, I suspect that filter.stopWatching executes before the callback has a chance to work. The way it's laid out suggests an expectation that those commands will execute in order, but it's not the case with JavaScript. I would consider commenting out filter.stopWatching and start from block 0, then build up complexity after I see it work. Hope it helps. – Rob Hitchens - B9lab May 12 '17 at 18:29
  • @Rob Hitchens yes man, I think the problem lies in the order and speed of javascript execution, but i don't found how to fix it. – Gawey May 15 '17 at 7:20
7

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.

  • My smart contract don't ahve a very long transaction but is slow too. I read about internal trnasactions, but i test it and my transaction don't are internals because in etherescan.io they doesn't put this transactions like internals transactions in their blocks scaned's. I will try the api of etehrscan.io, but this is a hotfix not a real fix for this problem :/ – Gawey May 15 '17 at 7:24
  • Etherscan's public API for this use case seems to be deprecated... – CQM Aug 16 '17 at 22:29
  • @CQM I'm not seeing that, but maybe I missed it. Do you have s link? – Thomas Jay Rush Aug 16 '17 at 22:56
  • etherscan.io/apis#tokens see the deprecated ones, is there a more applicable endpoint? – CQM Aug 16 '17 at 23:12
  • I'm guessing, but I would bet they depreciated it because it was a maintenance issue. You don't have etherscan, you could use the RPC directly against a node. – Thomas Jay Rush Aug 17 '17 at 3:37
1

Arrays are indexed starting from '0'. You're indexing from '1'. Try transactions[0].

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.