0

Is there a way to watch events from multiple contracts with just one watch-statement (something like [contract1.event, contract2.event, ...].watchAll() ) in Javascript?

Code is just for illustration purposes:

   var event = contract.EventName({from: address}); // watching multiple contracts/events possible?
   event.watch(function(err, result) {
         ...
   });
1

There is a build in function to listen all the events in one watch statement

var events = myContractInstance.allEvents([additionalFilterObject]);

// watch for changes
events.watch(function(error, result){
  if (!error)
    console.log(result);
});

additionalFilterObject can used to filter the results; example adding an address would be {address: myAddress}

On the other hand doesn't seems possible to filter the events using the names unless you specify the topics on the additionalFilterObject where the topic addresses are the event/function hash (keccak256 of your event name eg. keccak256('EventName1(uint)')) then you would have something like:

var events = myContractInstance.allEvents({address: myAddress, topics: [['0x1234...', '0x5678...']]});

// watch for changes
events.watch(function(error, result){
  if (!error)
    console.log(result);
});

but you can check the event result and filter the event name according to the events you want to listen:

var events = myContractInstance.allEvents([additionalFilterObject]);

// watch for changes
events.watch(function(error, result){
  if (!error) {
    if(['EventName1', 'EventName2'].includes(result.event)) {
       //do
    }
  }
});

and that would be more readable maybe. Another less elegant solution could be

var event1 = contract.EventName1({from: address});
var event2 = contract.EventName2({from: address});
event1.watch(myCallback);
event2.watch(myCallback);

function myCallback(err, result) {
}
  • Thanks for answering, I'll take from it, that it is not possible watching different contracts and their events (just all events from one contract respectively). – Curzon Dax Mar 13 '18 at 15:33
  • 1
    sorry I misunderstood your question. I read it too quick and I find out just now that you was talking about different contracts. In that case you might take a look at web3.eth.filter. Maybe it can helps you github.com/ethereum/wiki/wiki/JavaScript-API#web3ethfilter – mirg Mar 14 '18 at 2:23
0

method allEvents() will not work for "web3": "^1.0.0-beta.36". This will throw an error

allEvents is not a function

This will not work even for some other versions. Refer here https://github.com/ethereum/web3.js/issues/989 It didnt work for me as well, I am using "web3": "^1.0.0-beta.36". If someone has an alternative, do share.

Your Answer

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

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