4

As discussed before I managed to get past events via

myContract.myEvent({}, { fromBlock: 0, toBlock: 'latest' }).get((error, results) => {

But it seems that for future events I need to watch for them via

myContract.myEvent().watch((error, result) => {

That is real odd in that I need to implement two event handlers - one for past and one for future events. Is there currently a cleaner way of handling this?

2
  • Which Ethereum node software are you using? When using Parity v1.5 I only need to make the first call you mention, and I get historical events and, later, new events.
    – pyggie
    Feb 14, 2017 at 5:12
  • I am using the latest released geth with the latest web3.js
    – SCBuergel
    Feb 14, 2017 at 13:26

1 Answer 1

4

Semantically, past events are a finite list, while future events are an infinite list (i.e. stream). The latter can encompass the former, but not the other way around, i.e. you could represent all past and future events as a stream. This is not built in, but it wouldn’t be hard to implement if you really wanted to:

function allEvents(ev, cb) {   
  ev({}, { fromBlock: 0, toBlock: 'latest' }).get((error, results) => {
    if(error) return cb(error)
    results.forEach(result => cb(null, result))
    ev().watch(cb)   
  }) 
}

...

allEvents(myContract.myEvent, (error, e) => {
  ...
})

Your Answer

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

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