Contracts cannot watch each other's events. Events are for the outside world, like web3.
You can, however, implement the functionality yourself. It won't be cheap to run it, though.
A (incomplete and very insecure, but you'll get the idea) example:
pragma solidity 0.4.23;
interface EventHandler {
function handleEvent(uint256 value) external;
}
// A
contract EventEmitter {
EventHandler[] public eventHandlers;
function emitEvent(uint256 _value) public {
for(uint256 i = 0; i < eventHandlers.length; i++){
eventHandlers[i].handleEvent(_value);
}
}
function registerHandler(EventHandler _handler) public {
// Push handler
eventHandlers.push(_handler);
}
}
// B
contract SomeContract is EventHandler {
uint256 public value;
constructor(EventEmitter _emitter) public {
_emitter.registerHandler(this);
}
function handleEvent(uint256 _value) external {
value = _value;
emit ValueSet(_value);
}
event ValueSet(uint256 value);
}
First, deploy the EventEmitter
. Then, use it's address to instantiate a couple of SomeContract
s. When you call EventEmitter.emitEvent
, you'll see that the SomeContract
s react to that