Skip to content

feat: add event filter creation and log fetching to ContractEvent#30

Open
Copilot wants to merge 4 commits intomainfrom
copilot/support-event-filter-creation
Open

feat: add event filter creation and log fetching to ContractEvent#30
Copilot wants to merge 4 commits intomainfrom
copilot/support-event-filter-creation

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 21, 2026

ContractEvent had no way to build eth_getLogs filter parameters or fetch/decode logs from the chain, making event-driven workflows awkward.

Changes

  • ContractEvent.build_filter() — returns a ready-to-use FilterParams dict by delegating to web3.py's construct_event_filter_params. Supports address, argument_filters (indexed param filtering encoded as topics), from_block, and to_block.
  • ContractEvent.get_logs() — async method that calls eth_getLogs with the built filter and returns a decoded list[EventData] via the existing parse_log().

Usage

transfer = ERC20.events.Transfer

# Build a filter (e.g. to pass to w3.eth.get_logs manually)
params = transfer.build_filter(
    address=token_addr,
    argument_filters={"from": owner},
    from_block=deploy_block,
)

# Or fetch + decode in one call
logs = await transfer.get_logs(
    w3,
    address=token_addr,
    argument_filters={"from": owner},
    from_block=deploy_block,
)
for log in logs:
    print(log["args"]["from"], log["args"]["to"], log["args"]["amount"])
Original prompt

This section details on the original issue you should resolve

<issue_title>Problem: event filter creation is not well supported</issue_title>
<issue_description>find a elegant way to support events use cases:

  • filter creation
  • logs/data encoding/decoding</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Add elegant support for event filter creation and data encoding feat: add event filter creation and log fetching to ContractEvent Mar 21, 2026
Copilot AI requested a review from yihuang March 21, 2026 10:45
@yihuang yihuang marked this pull request as ready for review March 21, 2026 14:46
Copilot AI review requested due to automatic review settings March 21, 2026 14:46
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds first-class event log querying support to ContractEvent, enabling consumers of eth_contract to build eth_getLogs filter parameters and fetch/decode logs directly via the existing event decoding flow.

Changes:

  • Add ContractEvent.build_filter() to generate FilterParams via construct_event_filter_params.
  • Add async ContractEvent.get_logs() to fetch logs with w3.eth.get_logs() and decode via parse_log().
  • Add unit/integration tests covering filter construction and end-to-end log fetching/decoding; remove the corresponding README TODO item.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
eth_contract/contract.py Implements ContractEvent.build_filter() and ContractEvent.get_logs() and adds required imports.
tests/test_abi.py Adds unit tests validating filter param construction (topics/address/block range/indexed arg filtering).
tests/test_erc20.py Adds async integration test that deploys a mock ERC20, emits Transfer logs, and verifies get_logs() decoding/filtering.
README.md Removes TODO entry now addressed by this PR.

from web3.exceptions import MismatchedABI
from web3.types import (
BlockIdentifier,
ChecksumAddress,
Copy link

Copilot AI Mar 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ChecksumAddress is imported twice (from eth_typing and again from web3.types), which redefines the name and will trip flake8/pyflakes (and also changes which type is referenced elsewhere in this module). Remove the web3.types.ChecksumAddress import or alias one of them to avoid the collision.

Suggested change
ChecksumAddress,

Copilot uses AI. Check for mistakes.
Comment on lines +265 to +270
return [
decoded
for log in logs
for decoded in [self.parse_log(log)]
if decoded is not None
]
Copy link

Copilot AI Mar 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nested list-comprehension pattern (for decoded in [self.parse_log(log)]) makes the control flow harder to read/maintain. Consider using an explicit loop (or filter(None, ...)) to decode logs and skip None results more clearly.

Suggested change
return [
decoded
for log in logs
for decoded in [self.parse_log(log)]
if decoded is not None
]
results: list[EventData] = []
for log in logs:
decoded = self.parse_log(log)
if decoded is not None:
results.append(decoded)
return results

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Owner

@yihuang yihuang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix ci failure

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 21, 2026

Fix ci failure

Fixed in 643d2cc — removed the duplicate ChecksumAddress import from web3.types (keeping the one from eth_typing that was already present), and simplified the get_logs return to use an explicit loop as suggested.

Copilot AI requested a review from yihuang March 21, 2026 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Problem: event filter creation is not well supported

3 participants