5

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:

3
  • Is this .then part a success callback?
    – niksmac
    Nov 28, 2016 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, 2016 at 5:40
  • I think contract.watch is global, you don't have to use it with callback.
    – niksmac
    Nov 28, 2016 at 6:03

2 Answers 2

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.

1
  • What do you mean not chaining along? Apr 25, 2017 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 and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.