0

I have set up a JavaScript file that takes in my contract and executes the various functions and then returns the correct events. The file runs through 'truffle exec'.

I am using the following line to get my events to return:

      let myEvent = contract.ThisEvent({}, {fromBlock: 0, toBlock: 'latest'});
  myEvent.get((error, logs) => {
    logs.forEach(log => console.log(
      'Message: ' + log.args.message
    ))
  });

The problem I am facing is that I have a variety of events with the same name but that return different messages. The idea is for each event to be returned in a specific order (as set up in the function). But when I try to execute them in the JS file they end up being returned in the terminal in the wrong order.

I'm not sure how to get it to return the events in the correct order. Can I maybe change the fromBlock/toBlock to make this happen? And if yes is there a way to figure out in advance in which block which event is going to be in?

Thanks!

1 Answer 1

0

Same way you do log.args.message, you can do log.blockNumber to see in which block that event was emitted and order them that way, that said, you might be getting some of them out of order because transactions are not always mined in the same order as they are sent.

Note 1: With that function you are going through the whole chain, which can be really slow, usually events are used to throw notifications when something happens, if you are using it for different reasons, consider storing it in a data structure in the smart contract.

If you are using it for notifications, you don't need to read the whole chain, just check blocks as they are mined.

var event = myContractInstance.myEvent([{valueA: 23}] [, additionalFilterObject] , function(error, result){
 if (!error)
   console.log(result);
});

Note 2: You seem to be using web3 0.2x (Careful when upgrading to version 1.x things change a lot)

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.