3

What is the difference between using myContract.getPastEvents() to get past events and using myContract.events.MyEvent({ fromBlock: 0, }) ?

Also, if I pass to fromBlock a block number that has not been mined yet, does it work as expected?

  • Maybe MyEvent does not handle old events. – Nic Szerman Aug 2 '18 at 22:56
  • Answered in stack overflow, I copied the answer here. – Nic Szerman Aug 4 '18 at 3:22
1

Actually, both will return the events fired in the past. But MyEvent can additionally subscribe to an event. So it could be fired for every event emitted in the future.

However, MyEvent will return the events one-by-one. And for every event fired in the past and/or in the future, the callback will be called.

And, getPastEvents will return the events all-in-one-shot. It will return an array of the events fired in the past.


Note: do not use { fromBlock: 0, } for options on the main-net. You will wait for a very long time and the script will most likely terminate before any value is returned. To be able to get a result specify a resonable number of blocks between fromBlock and toBlock.

0

In general, MyEvent lets you specify filters within a specific event type (you can filter by parameter values), whereas getPastEvents returns all events for an event type.

The syntax you posted is nearly identical. The main difference is getPastEvents will return all events that have occurred up until the block number the node you’re connected to is synced to. MyEvent will return those same events, plus will call the callback on any new events that occur as new blocks are added to the chain.


Source: https://stackoverflow.com/a/51664434/4428008

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.