0

I developed a contract which uses another contract in it state variable, but looks like the events from the contract in the state variable aren’t dispatched.

contract A {
  event Hello(uint num);
  function doSomething() {
   Hello(1);
  }
}

contract B {
  A public a;

  function doAnotherThing() public {
    a = new A();
    a.doSomething();
  }
}

What is expected: To be possible to capture the events from the contract A which is a state variable in the contract B. So when call a.doSomething() in the contract B should be dispatched the event Hello(1) and be possible to capture in the watch.

2 Answers 2

1

It looks like you're confusing inheritance with deployment.

Your contract B deploys a new instance of contract A. contract A has an event emitter and a function doSomething() and you can invoke it directly with addressA.dosomething() or indirectly with addressB.doSomethingElse(). In either case, you can listen to events from contractA but contractB will not emit events.

This would be an entirely different picture in the case of inheritance.

You could say: contract B is A { ...}

In this case, you would be able to invoke addressB.doSomething() and listen for the event at addressB because all of A would become part of B. You would drop the references to A as an external thing in that case.

contract B is A {
  // A public a;

  function doAnotherThing() public {
    // a = new A();
    doSomething();
  }
}

Hope it helps.

4
  • Thanks, Rob, but I'm not confusing I think, looks like the events are tied to the contract which has an address and is called in a transaction so they generate events, but the state variable can't generate event because isn't called directly in the transaction. I'm not sure if I'm totally right about that, but now it makes more sense to me. Oct 16, 2017 at 19:07
  • contract A has an address, too. You can do event LogNewA(address newContract) ... LogNewA(a); and see the address in B's event log. Oct 16, 2017 at 19:30
  • For sure, when it runs the a = new A() the state variable a will receive an address too :) is possible to watch that variable by his address, right ? Oct 16, 2017 at 19:38
  • a is cast as contract A which is directly convertible to type address. You might find some inspiration with the pattern laid out over here: ethereum.stackexchange.com/questions/13415/… Oct 16, 2017 at 20:40
0

The events are triggered and recorded in the transaction receipt. In your case what is failing is that the source of the events is not the contract B, but contract A.

A new instance of contrac A is created before calling a.doSomething(), that will be the source of the event.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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