0

I have 2 contracts. One contract is the one which I'm interacting directly with.

The event I want to listen is not triggered by the main contract, but the one it is called internally.

In the following example, if I invoke "testing" method, how can I listen for "MyEvent" triggered by the other contract?

contract OtherContract {

event MyEvent(uint8);
function doSomething() {
         MyEvent(1);
    }
}

contract Test {

    OtherContract constant otherContract  = OtherContract(0x0b258ee7bf483bb49a5956407702ca5b08197b4c);

    function testing() {
         otherContract.doSomething();        
    }
}
1

You could handle this with the Web3 JavaScript API. You would just need to reference the instance of the contract that you would like to watch events for.

var event = myContractInstance.MyEvent({parameters} [Filters])

// watch for changes
event.watch(function(error, result){
  if (!error)
    console.log(result);
});

See here for info about filters.

1

Smart contracts can't see events, from the docs:

Log and event data is not accessible from within contracts (not even from the contract that created them).

If you want contracts to interact with each other, they have to directly call each other (like you did with this function otherContract.doSomething();)

0

Add a method and an event in Test contract. The method should listen to the event in Other Contract and emit the event in Test contract. Listen to the Test contract event from your calling application.

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.