6

Given a function and a event in a contract:

event OtherCreated(address other, address addr1, address addr2);

function doSomething(address addr1, address addr2){
     if(aBool == true){
        address other = new OtherContract(addr1, addr2);
        others.push(other);
        OtherCreated(other, addr1, addr2);
     }
}

And given a js call in a browser:

 .then(function(tx_id){
      console.log('previous call completed');
     otherCreated = contractAddr.OtherCreated();
     otherCreated.watch(function(error, result){
        if (!error)
        {
          console.log('in event handler');            
        }
      return  contractAddr.doSomething(account0, account1, {from: account0, gas: 4700000 });
    })

The event handler is called twice. Is this an error on my part, or do I need to change something in this setup.

enter image description here

Output:

  • Is this .then part a success callback? – niksmac Nov 28 '16 at 2:18
  • Yes. When I put an extra log in the then handler I can see that that part is executed once but the event twice. – BennyM Nov 28 '16 at 5:40
  • I think contract.watch is global, you don't have to use it with callback. – niksmac Nov 28 '16 at 6:03
1

Problem was in my code, obviously. Be careful when you create your event handlers. Listening to events directly after the contract was created and not chaining along solved the issue.

  • What do you mean not chaining along? – Daniel Kobe Apr 25 '17 at 6:20
0

I've coded up a utility function to filter events by name, arguments etc. Answered in a different question:

https://ethereum.stackexchange.com/a/21661/7132

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.