0

This question already has an answer here:

All the existing questions explains to catch the event on client end or server. How to catch the event on smart contract itself?

Here's what I'm looking for:

  • Contract A emits the event
  • Contract B catched the event and stores the params.
  • Both of these contracts have different address.

marked as duplicate by Ismael, mirg, Richard Horrocks, Achala Dissanayake, Linmao Song May 12 '18 at 5:54

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • What would be the point to have this kind of pubsub functionality in the smart contract itself rather than calling directly a function of B? – Greg Jeanmart May 11 '18 at 7:13
0

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 SomeContracts. When you call EventEmitter.emitEvent, you'll see that the SomeContracts react to that

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