Skip to content
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

starknet monitoring: paginate events #506

Merged
merged 1 commit into from
Aug 6, 2024
Merged
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
55 changes: 37 additions & 18 deletions relayer/pkg/chainlink/ocr2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,42 @@ func (c *Client) LinkAvailableForPayment(ctx context.Context, address *felt.Felt
return ans, nil
}

func (c *Client) collectAllEvents(ctx context.Context, block starknetrpc.BlockID, address *felt.Felt, eventKey *felt.Felt, pageSize int) (events []starknetrpc.EmittedEvent, err error) {
input := starknetrpc.EventsInput{
EventFilter: starknetrpc.EventFilter{
FromBlock: block,
ToBlock: block,
Address: address,
Keys: [][]*felt.Felt{{eventKey}}, // skip other event types
},
ResultPageRequest: starknetrpc.ResultPageRequest{
ChunkSize: pageSize,
},
}

chunk, err := c.r.Events(ctx, input)

if err != nil {
return nil, err
}

events = chunk.Events

for chunk.ContinuationToken != "" {
input.ResultPageRequest.ContinuationToken = chunk.ContinuationToken

chunk, err = c.r.Events(ctx, input)

if err != nil {
return nil, err
}

events = append(events, chunk.Events...)
}

return events, nil
}

func (c *Client) fetchEventsFromBlock(ctx context.Context, address *felt.Felt, eventType string, blockNum uint64) (events []starknetrpc.EmittedEvent, err error) {
latestBlockHeight, err := c.r.LatestBlockHeight(ctx)
if err != nil {
Expand All @@ -200,24 +236,7 @@ func (c *Client) fetchEventsFromBlock(ctx context.Context, address *felt.Felt, e

eventKey := starknetutils.GetSelectorFromNameFelt(eventType)

input := starknetrpc.EventsInput{
EventFilter: starknetrpc.EventFilter{
FromBlock: block,
ToBlock: block,
Address: address,
Keys: [][]*felt.Felt{{eventKey}}, // skip other event types
// PageSize: 0,
// PageNumber: 0,
},
ResultPageRequest: starknetrpc.ResultPageRequest{
// ContinuationToken: ,
ChunkSize: 10,
},
}
chunk, err := c.r.Events(ctx, input)
events = chunk.Events

// TODO: check events.isLastPage, query more if needed
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this todo was never implemented, so this PR is doing it (basically)

events, err = c.collectAllEvents(ctx, block, address, eventKey, 10)

if err != nil {
return events, fmt.Errorf("couldn't fetch events for block: %w", err)
Expand Down
Loading
Loading