function test_WhenUpperBoundExceedsMaxAccountsToCheck2() external {
createAccounts(150);
uint256 lowerBound = 0;
uint256 upperBound = 150;
uint128[] memory liquidatableAccountIds = perpsEngine.checkLiquidatableAccounts(lowerBound, upperBound);
assertEq(liquidatableAccountIds.length, MAX_ACCOUNTS_TO_CHECK);
}
function createAccounts(uint256 amountOfTradingAccounts) internal {
MarketConfig memory fuzzMarketConfig = getFuzzMarketConfig(1);
uint256 initialMarginRate = fuzzMarketConfig.imr;
for (uint256 i; i < amountOfTradingAccounts; i++) {
uint256 accountMarginValueUsd = 10_000e18;
uint128 tradingAccountId = createAccountAndDeposit(accountMarginValueUsd, address(usdz));
openPosition(fuzzMarketConfig, tradingAccountId, initialMarginRate, accountMarginValueUsd, true);
}
}
uint256 constant MAX_ACCOUNTS_TO_CHECK = 100;
function checkLiquidatableAccounts(
uint256 lowerBound,
uint256 upperBound
)
external
view
returns (uint128[] memory liquidatableAccountsIds)
{
GlobalConfiguration.Data storage globalConfiguration = GlobalConfiguration.load();
uint256 cachedAccountsIdsWithActivePositionsLength =
globalConfiguration.accountsIdsWithActivePositions.length();
if (lowerBound >= cachedAccountsIdsWithActivePositionsLength) {
return new uint128 ;
}
if (upperBound > cachedAccountsIdsWithActivePositionsLength) {
upperBound = cachedAccountsIdsWithActivePositionsLength;
}
if (upperBound - lowerBound > MAX_ACCOUNTS_TO_CHECK) {
upperBound = lowerBound + MAX_ACCOUNTS_TO_CHECK;
}
liquidatableAccountsIds = new uint128[](upperBound - lowerBound);
for (uint256 i = lowerBound; i < upperBound; i++) {
uint128 tradingAccountId = uint128(globalConfiguration.accountsIdsWithActivePositions.at(i));
TradingAccount.Data storage tradingAccount = TradingAccount.loadExisting(tradingAccountId);
(, UD60x18 requiredMaintenanceMarginUsdX18, SD59x18 accountTotalUnrealizedPnlUsdX18) =
tradingAccount.getAccountMarginRequirementUsdAndUnrealizedPnlUsd(0, SD59x18_ZERO);
SD59x18 marginBalanceUsdX18 = tradingAccount.getMarginBalanceUsd(accountTotalUnrealizedPnlUsdX18);
if (TradingAccount.isLiquidatable(requiredMaintenanceMarginUsdX18, marginBalanceUsdX18)) {
liquidatableAccountsIds[i - lowerBound] = tradingAccountId;
}
}
}