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

CCIP-3416 paginate token admin registry get all tokens call #14514

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/token_admin_registry"
)

const (
GetTokensPaginationSize = 20
)

type TokenAdminRegistryView struct {
types.ContractMetaData
Tokens []common.Address `json:"tokens"`
}

func GenerateTokenAdminRegistryView(taContract *token_admin_registry.TokenAdminRegistry) (TokenAdminRegistryView, error) {
tokens, err := taContract.GetAllConfiguredTokens(nil, 0, 10)
if taContract == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

nice! should have this in every view

return TokenAdminRegistryView{}, fmt.Errorf("token admin registry contract is nil")
}
tokens, err := getAllConfiguredTokensPaginated(taContract)
if err != nil {
return TokenAdminRegistryView{}, fmt.Errorf("view error for token admin registry: %w", err)
}
Expand All @@ -28,3 +35,22 @@ func GenerateTokenAdminRegistryView(taContract *token_admin_registry.TokenAdminR
Tokens: tokens,
}, nil
}

// getAllConfiguredTokensPaginated fetches all configured tokens from the TokenAdminRegistry contract in paginated
// manner to avoid RPC timeouts since the list of configured tokens can grow to be very large over time.
func getAllConfiguredTokensPaginated(taContract *token_admin_registry.TokenAdminRegistry) ([]common.Address, error) {
startIndex := uint64(0)
allTokens := make([]common.Address, 0)
for {
fetchedTokens, err := taContract.GetAllConfiguredTokens(nil, startIndex, GetTokensPaginationSize)
if err != nil {
return nil, err
}
allTokens = append(allTokens, fetchedTokens...)
startIndex += GetTokensPaginationSize
if len(fetchedTokens) < GetTokensPaginationSize {
break
}
}
return allTokens, nil
}
Loading