2

I have used following code to grab transaction hash every time a transfer event is triggered and store them in an array.

contarct1=web3.eth.contract(ABiDefination).at(address);
var arr=[];
var i=0;
function Eventt(){
     contract1.Transfer(function(error,result){
          if(!error){
               arr[i]=result.transactionHash;
               console.log(i);
               console.log(arr[i]);
               document.getElementById('txhash1').innerText="array  is "+ arr ;
               i=i+1;
          }
      })
}

Here Transfer is the event which is declared in my contract as event Transfer(address indexed from, address indexed to, uint256 value);.

Now in my code I am calling this Eventt() inside transfer(..) function (and inside transfer(..) Transfer event is fired). I am also able to get transaction hashes.
But the issue is when I call transfer(..) function for first time, I get a transaction hash which is corresponding to that transaction, but the second time I call transfer(..) function, I get 3 elements inside my array,first is for the first transaction and 2nd and 3rd are same. And this keeps on going, for third time I get 6 elements inside my array.
I am unable to track why I am getting repeated values. Any solutions, or any alternate way to capture transaction hashes?

I also not sure

  1. whether the line contract1.Transfer(function(error,result){}) watches changes in event or anything else?
  2. and what is meaning of indexed in line event Transfer(address indexed from, address indexed to, uint256 value);
2
  1. Every time you call Eventt() you add a listener to contract1.Transfer(). So after 2 times, you have 2 listeners, with the second one starting at a later block. And because your arr=[] is global, both listeners add to the same array. This explains why you get [tx1, tx2, tx2]. After 3 times, you of course get [tx1, tx2, tx2, tx3, tx3, tx3].

What you need to do is call Eventt() once only on page load.

And if ever you need to stop, look at .stopWatching()

  1. indexed means that you will be able to query per values of this field for all instance of this event. Look at .filter().

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.