From 59e965bca8b750fe541f9282311a576f0b92f636 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Fingen?= Date: Wed, 1 May 2024 12:41:37 +0100 Subject: [PATCH 1/2] test: Fix multicollateral fuzz test --- contracts/src/CollateralRegistry.sol | 2 +- contracts/src/test/multicollateral.t.sol | 74 ++++++++++++++++++------ 2 files changed, 57 insertions(+), 19 deletions(-) diff --git a/contracts/src/CollateralRegistry.sol b/contracts/src/CollateralRegistry.sol index 40b1a4e0..13504e18 100644 --- a/contracts/src/CollateralRegistry.sol +++ b/contracts/src/CollateralRegistry.sol @@ -10,7 +10,7 @@ import "./Dependencies/LiquityBase.sol"; import "./Interfaces/ICollateralRegistry.sol"; -// import "forge-std/console.sol"; +// import "forge-std/console2.sol"; contract CollateralRegistry is LiquityBase, ICollateralRegistry { // mapping from Collateral token address to the corresponding TroveManagers diff --git a/contracts/src/test/multicollateral.t.sol b/contracts/src/test/multicollateral.t.sol index c6d7fc6e..10906f6e 100644 --- a/contracts/src/test/multicollateral.t.sol +++ b/contracts/src/test/multicollateral.t.sol @@ -163,6 +163,52 @@ contract MulticollateralTest is DevTestSetup { uint256 _spBoldAmount4, uint256 _redemptionFraction ) public { + uint256 boldAmount = 10000e18; + uint256 minBoldBalance = 1; + // TODO: remove gas compensation + _spBoldAmount1 = bound(_spBoldAmount1, 0, boldAmount - 200e18); + _spBoldAmount2 = bound(_spBoldAmount2, 0, boldAmount - 200e18); + _spBoldAmount3 = bound(_spBoldAmount3, 0, boldAmount - 200e18); + _spBoldAmount4 = bound(_spBoldAmount4, 0, boldAmount - 200e18 - minBoldBalance); + _redemptionFraction = bound( + _redemptionFraction, + DECIMAL_PRECISION / minBoldBalance, + DECIMAL_PRECISION + ); + + _testMultiCollateralRedemption(boldAmount, _spBoldAmount1, _spBoldAmount2, _spBoldAmount3, _spBoldAmount4, _redemptionFraction); + } + + function testMultiCollateralRedemptionCrazy() public { + uint256 boldAmount = 10000e18; + uint256 minBoldBalance = 1; + + _testMultiCollateralRedemption( + boldAmount, + /* + 115792089237316195423570985008687907853269984665640564039457584007913129639932, + 115792089237316195423570985008687907853269984665640564039457584007913129639932, + 115792089237316195423570985008687907853269984665640564039457584007913129639932, + 115792089237316195423570985008687907853269984665640564039457584007913129639932, + 0 + */ + // TODO: remove gas compensation + boldAmount - 200e18, + boldAmount - 200e18, + boldAmount - 200e18, + boldAmount - 200e18 - minBoldBalance, + DECIMAL_PRECISION / minBoldBalance + ); + } + + function _testMultiCollateralRedemption( + uint256 _boldAmount, + uint256 _spBoldAmount1, + uint256 _spBoldAmount2, + uint256 _spBoldAmount3, + uint256 _spBoldAmount4, + uint256 _redemptionFraction + ) internal { TestValues memory testValues1; TestValues memory testValues2; TestValues memory testValues3; @@ -173,37 +219,29 @@ contract MulticollateralTest is DevTestSetup { testValues3.price = contractsArray[2].priceFeed.getPrice(); testValues4.price = contractsArray[3].priceFeed.getPrice(); - uint256 boldAmount = 10000e18; - // TODO: remove gas compensation - _spBoldAmount1 = bound(_spBoldAmount1, 0, boldAmount - 200e18); - _spBoldAmount2 = bound(_spBoldAmount2, 0, boldAmount - 200e18); - _spBoldAmount3 = bound(_spBoldAmount3, 0, boldAmount - 200e18); - _spBoldAmount4 = bound(_spBoldAmount4, 0, boldAmount - 200e18); - // With too low redemption fractions, it reverts due to `newBaseRate` rounding down to zero, so we put a min of 0.01% - _redemptionFraction = bound(_redemptionFraction, 1e14, DECIMAL_PRECISION); // First collateral - openMulticollateralTroveNoHints100pctMaxFeeWithIndex(0, A, 0, 10e18, boldAmount, 5e16); + openMulticollateralTroveNoHints100pctMaxFeeWithIndex(0, A, 0, 10e18, _boldAmount, 5e16); if (_spBoldAmount1 > 0) makeMulticollateralSPDeposit(0, A, _spBoldAmount1); // Second collateral - openMulticollateralTroveNoHints100pctMaxFeeWithIndex(1, A, 0, 10e18, boldAmount, 5e16); + openMulticollateralTroveNoHints100pctMaxFeeWithIndex(1, A, 0, 10e18, _boldAmount, 5e16); if (_spBoldAmount2 > 0) makeMulticollateralSPDeposit(1, A, _spBoldAmount2); // Third collateral - openMulticollateralTroveNoHints100pctMaxFeeWithIndex(2, A, 0, 10e18, boldAmount, 5e16); + openMulticollateralTroveNoHints100pctMaxFeeWithIndex(2, A, 0, 10e18, _boldAmount, 5e16); if (_spBoldAmount3 > 0) makeMulticollateralSPDeposit(2, A, _spBoldAmount3); // Fourth collateral - openMulticollateralTroveNoHints100pctMaxFeeWithIndex(3, A, 0, 10e18, boldAmount, 5e16); + openMulticollateralTroveNoHints100pctMaxFeeWithIndex(3, A, 0, 10e18, _boldAmount, 5e16); if (_spBoldAmount4 > 0) makeMulticollateralSPDeposit(3, A, _spBoldAmount4); uint256 boldBalance = boldToken.balanceOf(A); // Check A’s final bal // TODO: change when we switch to new gas compensation - //assertEq(boldToken.balanceOf(A), boldAmount * 4 - _spBoldAmount1 - _spBoldAmount2 - _spBoldAmount3 - _spBoldAmount4, "Wrong Bold balance before redemption"); + //assertEq(boldToken.balanceOf(A), _boldAmount * 4 - _spBoldAmount1 - _spBoldAmount2 - _spBoldAmount3 - _spBoldAmount4, "Wrong Bold balance before redemption"); // Stack too deep - //assertEq(boldBalance, boldAmount * 4 - _spBoldAmount1 - _spBoldAmount2 - _spBoldAmount3 - _spBoldAmount4 - 800e18, "Wrong Bold balance before redemption"); + //assertEq(boldBalance, _boldAmount * 4 - _spBoldAmount1 - _spBoldAmount2 - _spBoldAmount3 - _spBoldAmount4 - 800e18, "Wrong Bold balance before redemption"); uint256 redeemAmount = boldBalance * _redemptionFraction / DECIMAL_PRECISION; @@ -213,10 +251,10 @@ contract MulticollateralTest is DevTestSetup { testValues3.collInitialBalance = contractsArray[2].WETH.balanceOf(A); testValues4.collInitialBalance = contractsArray[3].WETH.balanceOf(A); - testValues1.unbackedPortion = boldAmount - _spBoldAmount1; - testValues2.unbackedPortion = boldAmount - _spBoldAmount2; - testValues3.unbackedPortion = boldAmount - _spBoldAmount3; - testValues4.unbackedPortion = boldAmount - _spBoldAmount4; + testValues1.unbackedPortion = _boldAmount - _spBoldAmount1; + testValues2.unbackedPortion = _boldAmount - _spBoldAmount2; + testValues3.unbackedPortion = _boldAmount - _spBoldAmount3; + testValues4.unbackedPortion = _boldAmount - _spBoldAmount4; uint256 totalUnbacked = testValues1.unbackedPortion + testValues2.unbackedPortion + testValues3.unbackedPortion + testValues4.unbackedPortion; From f5acb8fb82cdb34d597715664ec447ab3d632e6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Fingen?= Date: Fri, 3 May 2024 09:46:04 +0100 Subject: [PATCH 2/2] test: Rename test function name --- contracts/src/test/multicollateral.t.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/src/test/multicollateral.t.sol b/contracts/src/test/multicollateral.t.sol index 10906f6e..e9f129c7 100644 --- a/contracts/src/test/multicollateral.t.sol +++ b/contracts/src/test/multicollateral.t.sol @@ -179,7 +179,7 @@ contract MulticollateralTest is DevTestSetup { _testMultiCollateralRedemption(boldAmount, _spBoldAmount1, _spBoldAmount2, _spBoldAmount3, _spBoldAmount4, _redemptionFraction); } - function testMultiCollateralRedemptionCrazy() public { + function testMultiCollateralRedemptionMaxSPAmount() public { uint256 boldAmount = 10000e18; uint256 minBoldBalance = 1;