0

I am trying to subscribe to an event emitted by an existing library's smart contract. I've found examples of how to subscribe to events emitted by my contract using web sockets and my contract ABI as below from web3 docs, but am still confused as to how to listen for events emitted by other contracts.

Is it possible to capture other contracts' events by taking this approach:

const otherContractInterface = require('../build/contracts/otherContract.json')
const deployedAddress = otherContractInterface.networks[networkId].address
token = new web3.eth.Contract(otherContractInterface.abi,deployedAddress)

const subscription = web3.eth.subscribe('logs', {
address: '0x123', // Address of other contract I'm trying to listen to
topics: ['0x345'] // Index of event I'm trying to listen to
}, function(error, result){
if (!error)
    console.log(result);
});

I found a post on this exchange from 2016 (Can a contract listen to events of another contract?) that indicates it is not possible to listen to other contracts' emitted events--is this still the case with Web3 1.0.0?

  • 1
    A "contract" cannot listen events. But with web3 it is possible to listen events from any contract you will need its abi to interpret the parameters. – Ismael Jun 4 at 1:54
  • By listen I meant subscribe. So can I have my contract (contract A) listen to another contract's (contract B) event by plugging in contract B's abi and address as in the code snippet in my post? – jason Jun 4 at 16:58
  • 1
    A contract cannot "subscribe" or "listen" to another contract events. It is possible for an external application to listen for a contract's events and send transaction to notify another contract. – Ismael Jun 4 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.