[Ethereum] Dealing with events from other contracts

eventssoliditytruffleweb3js

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();        
    }
}

Best Answer

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.

Related Topic