0

I am unable to watch multiple solidity events from the same JS file. I know the problem is not with either one of the events because they work individually, just not when several watch() functions are present. Here is my code:

...

function solidityEventListener() {
 let solidityEvent1;
 let solidityEvent2;
 Contract.deployed().then(function(i) {
  solidityEvent1 = i.productCreated({fromBlock: 0, toBlock: 'latest'});
  solidityEvent2 = i.productPurchased({fromBlock: 0, toBlock: 'latest'});

  solidityEvent1.watch(function(err, result) {
   if (err) {
    console.log(err)
    return;
   }
   createProduct(result.args);
  });

  solidityEvent2.watch(function(err, result) {
   if (err) {
    console.log(err)
    return;
   }
   updateProduct(result.args);
  });
 })
}

Only solidityEvent1 will fire if both watch() functions are present. Thank you in advance for your assistance.

0

3 Answers 3

1

I'm still unaware what has caused the issue but I was able to resolve it.

Simply watch() for allEvents and then manually filter which event has been caught. So the above code now looks something like this:

...

function solidityEventListener() {
 let solidityEvent;
 Contract.deployed().then(function(i) {
  solidityEvent = i.allEvents({fromBlock: 0, toBlock: 'latest'});

  solidityEvent.watch(function(err, result) {
   if (err) {
    console.log(err)
    return;
   }
   // insert if statements to check which event has been caught
  });
 })
}
0

Try using this in when declaring the Event var UpdateEvent = Contract.eventToWatch({}, 'latest');

1
  • It still yields the same issue; only the first watch() function appears to be active.
    – SolidTee
    Mar 2, 2018 at 20:40
0

Using web3.js v0.2, you could use a filter object - if you what to filter for multiple events you must pass in a list within a list. For the following two events emitted in a Solidity contract:

event event1(uint256 indexed anumb)
event event2(uint256 indexed anothernumb)
event event3(uint256 indexed yetanothernumb)

Use the following to filter for events1 and event2 only:

const filter = web3.eth.filter({
  fromBlock: 0,
  toBlock: 'latest',
  address: contractAddress,
  topics: [[web3.sha3('event1(uint256)'),web3.sha3('event2(uint256)')]]
})

filter.get((error, result) => {
   // gets all prior results
})

or

filter.watch((error, result) => {
   // 
})

From the docs:

topics: Array of Strings - An array of values which must each appear in the log entries. The order is important, if you want to leave topics out use null, e.g. [null, '0x00...']. You can also pass another array for each topic with options for that topic e.g. [null, ['option1', 'option2']]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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