Skip to content

Commit

Permalink
test[invariant]: add invariant for approx up-only.
Browse files Browse the repository at this point in the history
  • Loading branch information
clement-ux committed Oct 15, 2024
1 parent fd029b6 commit c2ea676
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 10 deletions.
4 changes: 2 additions & 2 deletions test/invariants/BaseInvariants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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");
Expand Down
7 changes: 2 additions & 5 deletions test/invariants/BasicInvariants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}

Expand Down
6 changes: 3 additions & 3 deletions test/invariants/handlers/SwapHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit c2ea676

Please sign in to comment.