Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions rpc/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import (
"context"

internalutils "github.com/NethermindEth/starknet.go/internal/utils"
)

// Events retrieves events from the provider matching the given filter.
Expand All @@ -23,3 +25,19 @@

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.

feltKey, err := internalutils.HexToFelt(key)
if err != nil {
return nil
}

for i := range events {
for _, k := range events[i].Keys {
if k.Equal(feltKey) {
return &events[i]
}
}
}
return nil

Check failure on line 42 in rpc/events.go

View workflow job for this annotation

GitHub Actions / lint

return with no blank line before (nlreturn)
}
38 changes: 38 additions & 0 deletions rpc/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,41 @@ func TestEvents(t *testing.T) {
require.Exactly(t, test.expectedResp.Events[0], events.Events[0], "Events mismatch")
}
}

func TestEventWith(t *testing.T) {
key := "0xabc"
feltKey := internalUtils.TestHexToFelt(t, key)

events := []Event{
{
EventContent: EventContent{
Keys: []*felt.Felt{feltKey},
},
},
}

found := EventWith(events, key)
require.NotNil(t, found, "Expected to find event")
require.True(t, found.Keys[0].Equal(feltKey), "Expected matching key")
}

func TestTransactionReceiptWithBlockInfo_EventWith(t *testing.T) {
key := "0xdead"
feltKey := internalUtils.TestHexToFelt(t, key)

receipt := &TransactionReceiptWithBlockInfo{
TransactionReceipt: TransactionReceipt{
Events: []Event{
{
EventContent: EventContent{
Keys: []*felt.Felt{feltKey},
},
},
},
},
}

found := receipt.EventWith(key)
require.NotNil(t, found, "Expected to find event from receipt method")
require.True(t, found.Keys[0].Equal(feltKey), "Expected matching key in receipt method")
}
6 changes: 6 additions & 0 deletions rpc/types_transaction_receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,9 @@ func (tr *TransactionReceiptWithBlockInfo) UnmarshalJSON(data []byte) error {

return nil
}

// EventWith finds the first event in the transaction receipt that has a specific key.
// The key is provided as a hex string. If no event is found, it returns nil.
func (tr *TransactionReceiptWithBlockInfo) EventWith(key string) *Event {
return EventWith(tr.Events, key)
}
Loading