From d05af338207f786affe876698e14a9ab60a1c055 Mon Sep 17 00:00:00 2001 From: defistar Date: Thu, 11 Jul 2024 16:47:03 +0530 Subject: [PATCH] feat: CCIP-2594 optimise pagination function --- contracts/src/v0.8/ccip/capability/CCIPConfig.sol | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/contracts/src/v0.8/ccip/capability/CCIPConfig.sol b/contracts/src/v0.8/ccip/capability/CCIPConfig.sol index 1c0ac14808..4e52a28f94 100644 --- a/contracts/src/v0.8/ccip/capability/CCIPConfig.sol +++ b/contracts/src/v0.8/ccip/capability/CCIPConfig.sol @@ -102,12 +102,16 @@ contract CCIPConfig is ITypeAndVersion, ICapabilityConfiguration, OwnerIsCreator uint256 pageSize ) external view returns (CCIPConfigTypes.ChainConfigInfo[] memory) { uint256 totalItems = s_remoteChainSelectors.length(); // Total number of chain selectors - if (pageSize == 0 || pageIndex * pageSize >= totalItems) { + uint256 startIndex = pageIndex * pageSize; + + if (pageSize == 0 || startIndex >= totalItems) { return new CCIPConfigTypes.ChainConfigInfo[](0); // Return an empty array if pageSize is 0 or pageIndex is out of bounds } - uint256 startIndex = pageIndex * pageSize; - uint256 endIndex = startIndex + pageSize > totalItems ? totalItems : startIndex + pageSize; + uint256 endIndex = startIndex + pageSize; + if (endIndex > totalItems) { + endIndex = totalItems; + } CCIPConfigTypes.ChainConfigInfo[] memory paginatedChainConfigs = new CCIPConfigTypes.ChainConfigInfo[](endIndex - startIndex);