Skip to content

Commit

Permalink
test[invariant]: add approx up-only invariant [WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
clement-ux committed Oct 15, 2024
1 parent 5df7718 commit 7e74fca
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
31 changes: 31 additions & 0 deletions test/invariants/BaseInvariants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,37 @@ abstract contract Invariant_Base_Test_ is Invariant_Shared_Test_ {
}
}

function assert_lp_invariant_L(uint256 initialBalance) public {
// As we will manipulate state here, we will snapshot the state and revert it after
uint256 snapshotId = vm.snapshot();

// 1. Finalize all claims on Lido
llmHandler.finalizeAllClaims();

// 2. Swap all stETH to WETH
_sweepAllStETH();

// 3. Finalize all claim redeem on ARM.
lpHandler.finalizeAllClaims();

for (uint256 i; i < lps.length; i++) {
address user = lps[i];
uint256 userShares = lidoARM.balanceOf(user);
uint256 assets = lidoARM.previewRedeem(userShares);
uint256 sum = assets + weth.balanceOf(user);

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");
} else {
// In this case user have gained asset.
assertGe(sum, initialBalance, "lpHandler.invariant_L_b");
}
}

vm.revertToAndDelete(snapshotId);
}

function assert_lp_invariant_M() public view {
address feeCollector = lidoARM.feeCollector();
assertEq(weth.balanceOf(feeCollector), ownerHandler.sum_of_fees(), "lpHandler.invariant_M");
Expand Down
3 changes: 2 additions & 1 deletion test/invariants/BasicInvariants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ contract Invariant_Basic_Test_ is Invariant_Base_Test_ {
//////////////////////////////////////////////////////
/// --- INVARIANTS
//////////////////////////////////////////////////////
function invariant_lp() external view {
function invariant_lp() external {
assert_lp_invariant_A();
assert_lp_invariant_B();
assert_lp_invariant_C();
Expand All @@ -121,6 +121,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_M();
}

Expand Down

0 comments on commit 7e74fca

Please sign in to comment.