Skip to content

Commit

Permalink
paginate token admin registry get all tokens call
Browse files Browse the repository at this point in the history
  • Loading branch information
ogtownsend committed Sep 20, 2024
1 parent 360ecfb commit 2529ff1
Showing 1 changed file with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ 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)
tokens, err := getAllConfiguredTokensPaginated(taContract)
if err != nil {
return TokenAdminRegistryView{}, fmt.Errorf("view error for token admin registry: %w", err)
}
Expand All @@ -28,3 +32,20 @@ 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)
fetchedTokens := make([]common.Address, 0)
for len(fetchedTokens) < GetTokensPaginationSize {
fetchedTokens, err := taContract.GetAllConfiguredTokens(nil, startIndex, GetTokensPaginationSize)
if err != nil {
return nil, err
}
allTokens = append(allTokens, fetchedTokens...)
startIndex += GetTokensPaginationSize
}
return allTokens, nil
}

0 comments on commit 2529ff1

Please sign in to comment.