From 814f426e0a4f8f6400ed8a1e8ca8f42958f16d66 Mon Sep 17 00:00:00 2001 From: Alexandru Niculae <43644109+avniculae@users.noreply.github.com> Date: Mon, 9 Feb 2026 13:38:53 +0200 Subject: [PATCH] test: fix liquidation tests --- tests/Base.t.sol | 43 +------------------------------- tests/mocks/MockSpoke.sol | 5 ++++ tests/unit/Spoke/SpokeBase.t.sol | 7 +++++- 3 files changed, 12 insertions(+), 43 deletions(-) diff --git a/tests/Base.t.sol b/tests/Base.t.sol index 3594759fd..4781c93b1 100644 --- a/tests/Base.t.sol +++ b/tests/Base.t.sol @@ -2066,7 +2066,7 @@ abstract contract Base is Test { ); uint256 targetTotalDebtValue = totalAdjustedCollateralValue.wadDivUp(desiredHf); require( - userAccountData.totalDebtValueRay / WadRayMath.RAY < targetTotalDebtValue, + userAccountData.totalDebtValueRay / WadRayMath.RAY <= targetTotalDebtValue, 'User has enough debt' ); return targetTotalDebtValue - userAccountData.totalDebtValueRay / WadRayMath.RAY; @@ -2579,47 +2579,6 @@ abstract contract Base is Test { return spoke.getLiquidationBonus(reserveId, user, healthFactor); } - /** - * @notice Returns the required debt amount in value terms to ensure user position is above a certain health factor. - * @return requiredDebt The required additional debt amount in value terms. - */ - function _getRequiredDebtForGtHf( - ISpoke spoke, - address user, - uint256 desiredHf - ) internal view returns (uint256) { - ISpoke.UserAccountData memory userAccountData = spoke.getUserAccountData(user); - - return - userAccountData - .totalCollateralValue - .percentMulDown(userAccountData.avgCollateralFactor.fromWadDown()) - .percentMulDown(99_00) - .wadDivDown(desiredHf) - userAccountData.totalDebtValueRay.fromRayUp(); - // buffer to force debt lower (ie making sure resultant debt creates HF that is gt desired) - } - - /// @dev Borrow to be below a certain healthy health factor - /// @dev This function validates HF and does not mock price, thus it will cache user RP properly - function _borrowToBeAboveHealthyHf( - ISpoke spoke, - address user, - uint256 reserveId, - uint256 desiredHf - ) internal returns (uint256, uint256) { - uint256 requiredDebtInBase = _getRequiredDebtForGtHf(spoke, user, desiredHf); - uint256 requiredDebtAmount = _convertValueToAmount(spoke, reserveId, requiredDebtInBase) - 1; - - vm.assume(requiredDebtAmount < MAX_SUPPLY_AMOUNT); - - vm.prank(user); - spoke.borrow(reserveId, requiredDebtAmount, user); - - uint256 finalHf = _getUserHealthFactor(spoke, user); - assertGt(finalHf, desiredHf, 'should borrow so that HF is above desiredHf'); - return (finalHf, requiredDebtAmount); - } - function _mockDecimals(address underlying, uint8 decimals) internal { vm.mockCall( underlying, diff --git a/tests/mocks/MockSpoke.sol b/tests/mocks/MockSpoke.sol index 452f7e706..b0cb2e9c0 100644 --- a/tests/mocks/MockSpoke.sol +++ b/tests/mocks/MockSpoke.sol @@ -48,6 +48,11 @@ contract MockSpoke is Spoke, Test { uint256 drawnShares = hub.draw(reserve.assetId, amount, msg.sender); userPosition.drawnShares += drawnShares.toUint120(); if (!positionStatus.isBorrowing(reserveId)) { + require( + MAX_USER_RESERVES_LIMIT == MAX_ALLOWED_USER_RESERVES_LIMIT || + positionStatus.borrowCount(_reserveCount) < MAX_USER_RESERVES_LIMIT, + MaximumUserReservesExceeded() + ); positionStatus.setBorrowing(reserveId, true); } diff --git a/tests/unit/Spoke/SpokeBase.t.sol b/tests/unit/Spoke/SpokeBase.t.sol index 866600390..9863b4b8d 100644 --- a/tests/unit/Spoke/SpokeBase.t.sol +++ b/tests/unit/Spoke/SpokeBase.t.sol @@ -1109,7 +1109,12 @@ contract SpokeBase is Base { _borrowWithoutHfCheck(spoke, user, reserveId, requiredDebtAmount); uint256 finalHf = _getUserHealthFactor(spoke, user); - assertApproxEqAbs(finalHf, desiredHf, 0.001e18); + assertApproxEqRel( + finalHf, + desiredHf, + _approxRelFromBps(100), + 'final health factor should be close to desired health factor' + ); return (finalHf, requiredDebtAmount); }