feat: add EventWith utility and method to filter events (issue #765)#767
feat: add EventWith utility and method to filter events (issue #765)#767raymax0x wants to merge 2 commits into
Conversation
This commit introduces a new utility function, `EventWith`, that allows searching for events within a transaction receipt or a slice of events based on a specific key. The `EventWith` function iterates through the events and compares the provided key against the event keys. If a match is found, the corresponding event is returned. This functionality simplifies event filtering and retrieval, making it easier to work with transaction receipts and event data. A new method `EventWith` is added to `TransactionReceiptWithBlockInfo` to use the new utility function. Additionally, comprehensive unit tests are included to ensure the correctness and reliability of the new function.
thiagodeev
left a comment
There was a problem hiding this comment.
Thanks @raymax0x!
I want to expand the idea.
In utils, a new file called events.go.
Inside, this new type:
type EmittedEventOpts {
BlockHash felt,
BlockNumber felt,
BlockNumberEnd felt,
FromAddress felt,
KeysSelectors [][]felt,
TransactionHash felt,
}
The following utilities:
- utils.EmittedEventsWith(
emittedEvents []EmittedEvent,
opts EmittedEventOpts
) []EmittedEvent - utils.EmittedEventWith(
emittedEvents []EmittedEvent,
opts EmittedEventOpts
) EmittedEvent - utils.EventsWith(
events []EmittedEvent,
fromAddress felt,
keysSelectors [][]felt
) []Event - utils.EventWith(
events []EmittedEvent,
fromAddress felt,
keysSelectors [][]felt
) Event
The behaviour of keysSelectors [][]felt would be the same as the keys field demonstrated in the examples/readEvent example (ref)
You are free to suggest adjustments to the field names.
WDYT about it? Ask me any questions or give suggestions
| return &result, nil | ||
| } | ||
|
|
||
| func EventWith(events []Event, key string) *Event { |
There was a problem hiding this comment.
It should accept a slice of keys instead of a single one, since an event can have multiple keys.
Also, let's make it accept a param called fromAddress and include it in the filter logic.
There was a problem hiding this comment.
Understood. Will expand this.
Thanks for the quick feedback! Expanding this feature into centralising event filter helpers in It's good to have one event toolbox, which will make much easier to discover and maintain. First I need to solve this issue, before expanding this feature : BackgroundWhile adding the utility function, I ran into a cyclic-import issue. Current Package Layout
The problem: if we add new helpers to Options
Next StepWhich option should i choose before I proceed? |
|
Thanks @raymax0x! Regarding your question, I understand. Maybe now is a good time to create the |
Thanks for the confirmation, @thiagodeev ! I'll go ahead and create the types package and move all event-related types there to resolve the cyclic import issues. |
|
Hey @raymax0x! |
|
Feat: Add Utility for Filtering Events by Key
This pull request introduces a utility function and an associated method for filtering transaction events by key, addressing #765.
What’s New
EventWith(events []Event, key string) *Event:A new function added to
rpc/events.gothat scans a slice of events and returns the first event containing a matching key.(*TransactionReceiptWithBlockInfo).EventWith(key string) *Event:A method added to the
TransactionReceiptWithBlockInfostruct that internally usesEventWith(...)to return the first matching event in itsEventsfield.Changes Made
EventWithfunction torpc/events.gowith key-to-feltconversion usinginternal/utils.HexToFelt.EventWithmethod toTransactionReceiptWithBlockInfoinrpc/types_transaction_receipt.go.rpc/events_test.goto verify both function and method correctness.✅ Verification
To verify:
go test ./...