From c2ea676764dffc508fef2f608d7dbad4a1b487de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Tue, 15 Oct 2024 15:22:38 +0200 Subject: [PATCH] test[invariant]: add invariant for approx up-only. --- test/invariants/BaseInvariants.sol | 4 ++-- test/invariants/BasicInvariants.sol | 7 ++----- test/invariants/handlers/SwapHandler.sol | 6 +++--- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/test/invariants/BaseInvariants.sol b/test/invariants/BaseInvariants.sol index 0511cc0..b914ffc 100644 --- a/test/invariants/BaseInvariants.sol +++ b/test/invariants/BaseInvariants.sol @@ -160,7 +160,7 @@ abstract contract Invariant_Base_Test_ is Invariant_Shared_Test_ { } } - function assert_lp_invariant_L(uint256 initialBalance) public { + function assert_lp_invariant_L(uint256 initialBalance, uint256 maxError) public { // As we will manipulate state here, we will snapshot the state and revert it after uint256 snapshotId = vm.snapshot(); @@ -181,7 +181,7 @@ abstract contract Invariant_Base_Test_ is Invariant_Shared_Test_ { if (sum < initialBalance) { // In this situation user have lost a bit of asset, ensure this is not too much - assertApproxEqRel(sum, initialBalance, 1e14, "lpHandler.invariant_L_a"); + assertApproxEqRel(sum, initialBalance, maxError, "lpHandler.invariant_L_a"); } else { // In this case user have gained asset. assertGe(sum, initialBalance, "lpHandler.invariant_L_b"); diff --git a/test/invariants/BasicInvariants.sol b/test/invariants/BasicInvariants.sol index 09b3c6f..c641bd6 100644 --- a/test/invariants/BasicInvariants.sol +++ b/test/invariants/BasicInvariants.sol @@ -23,6 +23,7 @@ contract Invariant_Basic_Test_ is Invariant_Base_Test_ { uint256 private constant MAX_SELL_T1 = 1.02 * 1e36; // We could have use type(uint256).max, but this is non-sense uint256 private constant MAX_WETH_PER_USERS = 10_000 ether; // 10M uint256 private constant MAX_STETH_PER_USERS = 10_000 ether; // 10M, actual total supply + uint256 private constant MAX_LOSS_IN_PCT = 1e12; // 0.0001% ////////////////////////////////////////////////////// /// --- SETUP @@ -56,10 +57,6 @@ contract Invariant_Basic_Test_ is Invariant_Base_Test_ { vm.prank(capManager.owner()); capManager.setTotalAssetsCap(type(uint248).max); - // Disable account cap, unlimited capacity for user to provide liquidity - vm.prank(capManager.owner()); - capManager.setAccountCapEnabled(false); - // Set prices, start with almost 1:1 vm.prank(lidoARM.owner()); lidoARM.setPrices(1e36 - 1, 1e36); @@ -121,7 +118,7 @@ contract Invariant_Basic_Test_ is Invariant_Base_Test_ { assert_lp_invariant_I(); assert_lp_invariant_J(); assert_lp_invariant_K(); - assert_lp_invariant_L(MAX_WETH_PER_USERS); + assert_lp_invariant_L(MAX_WETH_PER_USERS, MAX_LOSS_IN_PCT); assert_lp_invariant_M(); } diff --git a/test/invariants/handlers/SwapHandler.sol b/test/invariants/handlers/SwapHandler.sol index e09390e..5987b99 100644 --- a/test/invariants/handlers/SwapHandler.sol +++ b/test/invariants/handlers/SwapHandler.sol @@ -222,9 +222,9 @@ contract SwapHandler is BaseHandler { IERC20 tokenIn = tokenOut == weth ? steth : weth; uint256 reserveUser = tokenIn.balanceOf(user); - if (reserveUser == 0) return 0; + if (reserveUser < 3) return 0; - uint256 amount = ((reserveUser - 1) * price(tokenIn)) / arm.PRICE_SCALE(); + uint256 amount = ((reserveUser - 3) * price(tokenIn)) / arm.PRICE_SCALE(); // Emit event to see it directly in logs emit GetAmountOutMax(amount); @@ -236,7 +236,7 @@ contract SwapHandler is BaseHandler { function estimateAmountIn(IERC20 tokenOut, uint256 amountOut) public returns (uint256) { IERC20 tokenIn = tokenOut == weth ? steth : weth; - uint256 amountIn = (amountOut * arm.PRICE_SCALE()) / price(tokenIn) + 1; + uint256 amountIn = (amountOut * arm.PRICE_SCALE()) / price(tokenIn) + 3; // Emit event to see it directly in logs emit EstimateAmountIn(amountIn);