ethereumweb3jsgo-ethereum

What is the difference between getPastEvents() vs events.MyEvent({ fromBlock: 0})?


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?


Solution

  • 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.