From e7c806040161ab7e221fcffa0e3d82e351e64e65 Mon Sep 17 00:00:00 2001 From: Lucas Manuel Date: Wed, 2 Oct 2024 09:23:01 -0400 Subject: [PATCH] fix: Update tolerances to pass on longer invariants campaigns (SC-709) (#42) * feat: optimize for gas * fix: remove comment * fix: increase invariant tolerances --- test/invariant/Invariants.t.sol | 7 ++++--- test/invariant/handlers/SwapperHandler.sol | 16 ++++++++-------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/test/invariant/Invariants.t.sol b/test/invariant/Invariants.t.sol index e675f89..885ad9e 100644 --- a/test/invariant/Invariants.t.sol +++ b/test/invariant/Invariants.t.sol @@ -303,16 +303,17 @@ abstract contract PSMInvariantTestBase is PSMTestBase { // When all funds are completely withdrawn, the sum of all funds withdrawn is equal to the // sum of value of all LPs including the burn address. All rounding errors get reduced to - // a few wei. + // a few wei. Using 20 as a low tolerance that still allows for high rounding errors with + // large rate changes in long campaigns. assertApproxEqAbs( sumLpValue + _getLpTokenValue(BURN_ADDRESS), sumStartingValue + startingSeedValue, - 6 + 20 ); // All funds can always be withdrawn completely (rounding in withdrawal against users). assertEq(psm.totalShares(), 0); - assertLe(psm.totalAssets(), 5); + assertLe(psm.totalAssets(), 20); } function _warpAndAssertConsistentValueAccrual() public { diff --git a/test/invariant/handlers/SwapperHandler.sol b/test/invariant/handlers/SwapperHandler.sol index 46ec9d7..b1074c1 100644 --- a/test/invariant/handlers/SwapperHandler.sol +++ b/test/invariant/handlers/SwapperHandler.sol @@ -140,7 +140,7 @@ contract SwapperHandler is HandlerBase { assertApproxEqAbs( psm.convertToAssetValue(1e18), startingConversion, - 2e12, + 3e12, "SwapperHandler/swapExactIn/conversion-rate-change" ); @@ -148,7 +148,7 @@ contract SwapperHandler is HandlerBase { assertApproxEqAbs( psm.convertToAssetValue(1_000_000e18), startingConversionMillion, - 2_000_000e12, // 2e18 of value + 3_000_000e12, // 2e18 of value "SwapperHandler/swapExactIn/conversion-rate-change-million" ); @@ -284,7 +284,7 @@ contract SwapperHandler is HandlerBase { assertApproxEqAbs( psm.convertToAssetValue(1e18), startingConversion, - 2e12, + 3e12, "SwapperHandler/swapExactOut/conversion-rate-change" ); @@ -292,7 +292,7 @@ contract SwapperHandler is HandlerBase { assertApproxEqAbs( psm.convertToAssetValue(1_000_000e18), startingConversionMillion, - 2_000_000e12, // 2e18 of value + 3_000_000e12, // 2e18 of value "SwapperHandler/swapExactOut/conversion-rate-change-million" ); @@ -305,11 +305,11 @@ contract SwapperHandler is HandlerBase { // Disregard this assertion if the LP has less than a dollar of value if (startingConversionLp0 > 1e18) { - // Position values can fluctuate by up to 0.00000002% on swaps + // Position values can fluctuate by up to 0.00000003% on swaps assertApproxEqRel( psm.convertToAssetValue(psm.shares(lp0)), startingConversionLp0, - 0.000002e18, + 0.000003e18, "SwapperHandler/swapExactOut/conversion-rate-change-lp" ); } @@ -321,11 +321,11 @@ contract SwapperHandler is HandlerBase { "SwapperHandler/swapExactOut/conversion-rate-lp-decrease" ); - // PSM value can fluctuate by up to 0.00000002% on swaps because of USDC rounding + // PSM value can fluctuate by up to 0.00000003% on swaps because of USDC rounding assertApproxEqRel( psm.totalAssets(), startingValue, - 0.000002e18, + 0.000003e18, "SwapperHandler/swapExactOut/psm-total-value-change" );