feat: add event filter creation and log fetching to ContractEvent#30
feat: add event filter creation and log fetching to ContractEvent#30
Conversation
Co-authored-by: yihuang <236070+yihuang@users.noreply.github.com> Agent-Logs-Url: https://github.com/yihuang/eth-contract/sessions/d670f30b-84e1-4112-8a0b-c67037184b18
There was a problem hiding this comment.
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 generateFilterParamsviaconstruct_event_filter_params. - Add async
ContractEvent.get_logs()to fetch logs withw3.eth.get_logs()and decode viaparse_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. |
eth_contract/contract.py
Outdated
| from web3.exceptions import MismatchedABI | ||
| from web3.types import ( | ||
| BlockIdentifier, | ||
| ChecksumAddress, |
There was a problem hiding this comment.
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.
| ChecksumAddress, |
eth_contract/contract.py
Outdated
| return [ | ||
| decoded | ||
| for log in logs | ||
| for decoded in [self.parse_log(log)] | ||
| if decoded is not None | ||
| ] |
There was a problem hiding this comment.
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.
| 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 |
Co-authored-by: yihuang <236070+yihuang@users.noreply.github.com> Agent-Logs-Url: https://github.com/yihuang/eth-contract/sessions/dda142b9-70b7-4d13-ada8-07748b55c8a2
Fixed in 643d2cc — removed the duplicate |
ContractEventhad no way to buildeth_getLogsfilter parameters or fetch/decode logs from the chain, making event-driven workflows awkward.Changes
ContractEvent.build_filter()— returns a ready-to-useFilterParamsdict by delegating to web3.py'sconstruct_event_filter_params. Supportsaddress,argument_filters(indexed param filtering encoded as topics),from_block, andto_block.ContractEvent.get_logs()— async method that callseth_getLogswith the built filter and returns a decodedlist[EventData]via the existingparse_log().Usage
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.