Skip to content

Add API to iterate through all events in a contract #961

@carver

Description

@carver

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:

    from web3.contract import ContractEvent
    
    filters = [
      event.createFilter(fromBlock='latest')
      for event in myContract.events
      if isinstance(event, ContractEvent)
    ]

How can it be fixed?

Some options:

  • Implement __iter__ on Contract.events to iterate through all events in the ABI (my favorite option, except that it's inconsistent with contract.functions, which is doing the wrong thing IMO)
  • Add a new Contract.all_events() equivalent to Contract.all_functions()

Then the example changes to:

    filters = [
      event.createFilter(fromBlock='latest')
      for event in myContract.events
    ]

Of course, we could also implement contract.create_filter() like web3.js's contract.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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions