-
Notifications
You must be signed in to change notification settings - Fork 137
feat: add EventWith utility and method to filter events (issue #765) #767
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
base: main
Are you sure you want to change the base?
feat: add EventWith utility and method to filter events (issue #765) #767
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.go
that scans a slice of events and returns the first event containing a matching key.(*TransactionReceiptWithBlockInfo).EventWith(key string) *Event
:A method added to the
TransactionReceiptWithBlockInfo
struct that internally usesEventWith(...)
to return the first matching event in itsEvents
field.Changes Made
EventWith
function torpc/events.go
with key-to-felt
conversion usinginternal/utils.HexToFelt
.EventWith
method toTransactionReceiptWithBlockInfo
inrpc/types_transaction_receipt.go
.rpc/events_test.go
to verify both function and method correctness.✅ Verification
To verify:
go test ./...