[Ethereum] How to get event logs in web3.py v3.*


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:

The most promising is combination of Eth.filter with either:

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 to v4.

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/490

If you're feeling really motivated, you could cherry-pick the changes from this PR back to the v3 branch. Then you could write the v4-compatible filter code in v3.