Solidity Events – Can a Contract Listen to Events of Another Contract

contract-designcontract-developmenteventslogsweb3js

I read that to listen to events you need to use web3.js. Are there other ways of doing it? Can a contract even somehow listen to events of another contract? Thanks!

Best Answer

A contract cannot listen to events of another contract. From Solidity docs:

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

web3.js is a wrapper around JSON-RPC, so another way of accessing event data is via "filters" in JSON-RPC such as eth_newFilter.

Note the dichotomy that a contract can't access events and web3.js is needed, but web3.js can't access return values from a contract invocation. So a pattern of using both an event and a return value like this may be needed:

event FooEvent(uint256 n);
function foo() returns (uint256) {
  FooEvent(1337);
  return 1337;
}
Related Topic