I would like to listen to events on a contract, but I'm having trouble figuring out which of the interfaces is the correct one and finding an example on how to use it.
The way I see it:
- Contract.eventFilter is
v4
only - Contract.pastEvents is
v3
only - LogFilter is
v3
only and marked as deprecated, pointing to: - LogFilter.get_all_entries –
v4
only - LogFilter.get is only in
v3
- and there are many many more…
The most promising is combination of Eth.filter with either:
- Eth.getFilterChanges for listening to new events or
- Eth.getFilterLogs for fetching them from history.
Which method should I use so that it will keep working once v4
comes out?
Best Answer
You've got it. The best way to be compatible with both is to use either of:
w3.eth.getFilterChanges()
w3.eth.getFilterLogs()
The good news is that the work is reusable. You'll probably want some kind of polling thread to get the filter changes in
v3
, and a similar thread will be useful in a port tov4
.The bad news is that it's one of the least convenient ways to write filters. I would suggest to go straight to
v4
now, but filters in particular need a lot of love in the beta. Block and transaction filters are simply broken at the moment: https://github.com/ethereum/web3.py/pull/490If you're feeling really motivated, you could cherry-pick the changes from this PR back to the
v3
branch. Then you could write thev4
-compatible filter code inv3
.