Skip to content

Conversation

raymax0x
Copy link
Contributor

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 uses EventWith(...) to return the first matching event in its Events field.


Changes Made

  • Added EventWith function to rpc/events.go with key-to-felt conversion using internal/utils.HexToFelt.
  • Added EventWith method to TransactionReceiptWithBlockInfo in rpc/types_transaction_receipt.go.
  • Wrote unit tests in rpc/events_test.go to verify both function and method correctness.

✅ Verification

  • ✅ All tests passed successfully
  • ✅ Unit tests written for both the utility function and method.
  • ✅ Compatible with existing receipt logic, no breaking changes.

To verify:

 go test ./...

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.
Copy link
Collaborator

@thiagodeev thiagodeev left a 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 {
Copy link
Collaborator

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Understood. Will expand this.

@raymax0x
Copy link
Contributor Author

raymax0x commented Jul 1, 2025

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

Thanks for the quick feedback! Expanding this feature into centralising event filter helpers in utils/events.go sounds great.

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 :

Background

While adding the utility function, I ran into a cyclic-import issue.

Current Package Layout

  • rpc – core types (Event, EmittedEvent, …)
  • utils – helper functions

The problem: if we add new helpers to utils that rely on rpc types and rpc in turn needs those helpers, we create an import loop.


Options

Option What we do ✅ Pros ⚠️ Cons
1. Keep everything in rpc Add rpc/events_utils.go Easiest change, guarantees no cyclic imports rpc package keeps ballooning
2. Introduce an events package Move all event-related types and helpers into a new package Clear separation; both rpc and utils can import events Requires a one-time refactor and updated imports
3. Interface-based approach Define small interfaces in utils; have rpc types implement them Decoupled and future-proof Adds complexity and boilerplate

Next Step

Which option should i choose before I proceed?

@thiagodeev
Copy link
Collaborator

Thanks @raymax0x!
Unfortunately, due to the proximity of Starknet v0.14.0 + RPC v0.9.0 update in Sepolia, I'll pause the reviews for a few days to release a compliant Starknet.go version ASAP.
Hope you understand 🙌

Regarding your question, I understand. Maybe now is a good time to create the types pkg (ref issue). Create it, please, and move all event types that are triggering 'cyclic-import' issues

@raymax0x
Copy link
Contributor Author

raymax0x commented Jul 2, 2025

Thanks @raymax0x! Unfortunately, due to the proximity of Starknet v0.14.0 + RPC v0.9.0 update in Sepolia, I'll pause the reviews for a few days to release a compliant Starknet.go version ASAP. Hope you understand 🙌

Regarding your question, I understand. Maybe now is a good time to create the types pkg (ref issue). Create it, please, and move all event types that are triggering 'cyclic-import' issues

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.
Appreciate the direction, I’ll keep the changes scoped and aligned with the internal structure. Will update the PR soon. 🙌

@thiagodeev
Copy link
Collaborator

Hey @raymax0x!
What's the status of this PR? Is it ready for review?
Thanks!

@raymax0x
Copy link
Contributor Author

Hey @raymax0x! What's the status of this PR? Is it ready for review? Thanks!
Thanks for the ping, @thiagodeev. I’m tied up with work and will resume this next week. I’ll update the eventWith utility + tests and ping you when it’s ready for review.

@thiagodeev thiagodeev moved this from 🔖 To do to 📋 Backlog in [Nubia] Starknet.go Sep 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: 📋 Backlog

Development

Successfully merging this pull request may close these issues.

2 participants