-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add API to iterate through all events in a contract #961
Comments
+1 for implementing |
Maybe we could publish a recipe on combining filters into a single object that can be polled. |
The key difference is that they want the automatic argument decoding, which they don't get with the generic filter on an address. I like the recipe idea. |
|
Yeah, it's only returning the name ( |
Wow, this ticket is still open! Here is a snippet that emits all decode-able events for a specific contract. I too found this was an annoyance @carver from web3.contract import Contract
from web3._utils.events import get_event_data
from eth_utils import event_abi_to_log_topic
def fetch_events(contract: Contract, fromBlock, toBlock=None):
topic2abi = {event_abi_to_log_topic(_): _
for _ in contract.abi if _['type'] == 'event'}
logs = w3.eth.getLogs(dict(
address=contract.address,
fromBlock=fromBlock,
toBlock=toBlock
))
for entry in logs:
topic0 = entry['topics'][0]
if topic0 in topic2abi:
yield get_event_data(w3.codec, topic2abi[topic0], entry) |
Ping @kclowes for an old issue that could use some love. |
Getting all the events sorted by their log index would be really helpful. Sometimes, two different events are emitted on the same block. With the current solution, sometimes I can find the latest emitted event first instead of finding the oldest emitted event first. |
Resurrecting this issue as something needs to be done to make web3.py more usable, I keep running into this issue and a few others where all the straightforward intuitive things that you'd expect to just work... don't... |
@fselmo @kclowes @pacrob Did some sleuthing and found an iterator over contract events has been implemented as of 2019, where each event is returned within the iterator. Remaining:
|
What was wrong?
No easy way to get all events for a contract (while still parsing the results). See this StackExchange question. One option is to iterate over all the events, but it's a bit awkward right now. I think the easiest way is:
How can it be fixed?
Some options:
__iter__
onContract.events
to iterate through all events in the ABI (my favorite option, except that it's inconsistent withcontract.functions
, which is doing the wrong thing IMO)Contract.all_events()
equivalent toContract.all_functions()
Then the example changes to:
Of course, we could also implement
contract.create_filter()
like web3.js'scontract.events.allEvents
. I kind of like that the filters are event specific right now, though. I don't think it's too big a deal to require callers to write a filter loop on events.The text was updated successfully, but these errors were encountered: