From e9823d7f5a1b0915e9b08b7785580b671c93b04d Mon Sep 17 00:00:00 2001 From: Oliver Townsend Date: Fri, 20 Sep 2024 15:09:04 -0700 Subject: [PATCH 1/3] paginate token admin registry get all tokens call --- .../ccip/view/v1_5/tokenadminregistry.go | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/integration-tests/deployment/ccip/view/v1_5/tokenadminregistry.go b/integration-tests/deployment/ccip/view/v1_5/tokenadminregistry.go index 1e704efae72..bc466f94462 100644 --- a/integration-tests/deployment/ccip/view/v1_5/tokenadminregistry.go +++ b/integration-tests/deployment/ccip/view/v1_5/tokenadminregistry.go @@ -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) } @@ -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 +} From 249487bfe391315631651a5e2f53c8a784a5385a Mon Sep 17 00:00:00 2001 From: Oliver Townsend Date: Fri, 20 Sep 2024 15:17:11 -0700 Subject: [PATCH 2/3] add nil check --- .../deployment/ccip/view/v1_5/tokenadminregistry.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/integration-tests/deployment/ccip/view/v1_5/tokenadminregistry.go b/integration-tests/deployment/ccip/view/v1_5/tokenadminregistry.go index bc466f94462..974a86d7cb5 100644 --- a/integration-tests/deployment/ccip/view/v1_5/tokenadminregistry.go +++ b/integration-tests/deployment/ccip/view/v1_5/tokenadminregistry.go @@ -19,6 +19,9 @@ type TokenAdminRegistryView struct { } func GenerateTokenAdminRegistryView(taContract *token_admin_registry.TokenAdminRegistry) (TokenAdminRegistryView, error) { + if taContract == nil { + 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) From bcf7e07af9f5241ade7ea7531a1f13383c689bbc Mon Sep 17 00:00:00 2001 From: Oliver Townsend Date: Fri, 20 Sep 2024 15:34:05 -0700 Subject: [PATCH 3/3] restructure loop condition --- .../deployment/ccip/view/v1_5/tokenadminregistry.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/integration-tests/deployment/ccip/view/v1_5/tokenadminregistry.go b/integration-tests/deployment/ccip/view/v1_5/tokenadminregistry.go index 974a86d7cb5..ebfff8c2feb 100644 --- a/integration-tests/deployment/ccip/view/v1_5/tokenadminregistry.go +++ b/integration-tests/deployment/ccip/view/v1_5/tokenadminregistry.go @@ -41,14 +41,16 @@ func GenerateTokenAdminRegistryView(taContract *token_admin_registry.TokenAdminR 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 { + 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 }