From 2e0e0e8cedcd22ec5163ac0c40aad402075c6567 Mon Sep 17 00:00:00 2001 From: dapp-whisperer Date: Mon, 4 Mar 2024 17:22:42 -0500 Subject: [PATCH 1/3] EtoF changes --- .../foundry_test/EchidnaToFoundry.t.sol | 38 ++++++++++++++++++- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/packages/contracts/foundry_test/EchidnaToFoundry.t.sol b/packages/contracts/foundry_test/EchidnaToFoundry.t.sol index 05a53a78a..9701d3fe7 100644 --- a/packages/contracts/foundry_test/EchidnaToFoundry.t.sol +++ b/packages/contracts/foundry_test/EchidnaToFoundry.t.sol @@ -25,13 +25,16 @@ contract EToFoundry is { modifier setup() override { _; + address sender = uint160(msg.sender) % 3 == 0 ? address(USER1) : uint160(msg.sender) % 3 == 1 + ? address(USER2) + : address(USER3); + actor = actors[sender]; } function setUp() public { _setUp(); _setUpActors(); - actor = actors[USER1]; - vm.startPrank(address(actor)); + actor = actors[address(USER1)]; } function _checkTotals() internal { @@ -221,9 +224,17 @@ contract EToFoundry is function _logStakes() internal { bytes32 currentCdp = sortedCdps.getFirst(); + console2.log("=== LogStakes ==="); + + uint256 currentPrice = priceFeedMock.fetchPrice(); + uint256 currentPricePerShare = collateral.getPooledEthByShares(1 ether); + console2.log("currentPrice", currentPrice); + console2.log("currentPricePerShare", currentPricePerShare); + while (currentCdp != bytes32(0)) { emit DebugBytes32(currentCdp); console2.log("CdpId", vm.toString(currentCdp)); + console2.log("==============================="); console2.log("cdpManager.getCdpStake(currentCdp)", cdpManager.getCdpStake(currentCdp)); console2.log( "cdpManager.getSyncedCdpCollShares(currentCdp)", @@ -239,7 +250,16 @@ contract EToFoundry is "cdpManager.getSyncedNominalICR(currentCdp)", cdpManager.getSyncedNominalICR(currentCdp) ); + console2.log( + "cdpManager.getCachedICR(currentCdp, currentPrice)", + cdpManager.getCachedICR(currentCdp, currentPrice) + ); + console2.log( + "cdpManager.getSyncedICR(currentCdp, currentPrice)", + cdpManager.getSyncedICR(currentCdp, currentPrice) + ); currentCdp = sortedCdps.getNext(currentCdp); + console2.log(""); } console2.log( @@ -1655,6 +1675,20 @@ contract EToFoundry is assertTrue(invariant_CSP_01(collateral, collSurplusPool), CSP_01); } + function test_debugTheLiquidation() public { + openCdp(3979204251130340497718654781931317513776851992409413887935202910391326,1000); + openCdp(3306424426048366109580062000503759874070670921097371866772152350788399154439,1069959377727045012); + _logStakes(); + setEthPerShare(0); + _logStakes(); + liquidateCdps(0); + vm.warp(block.timestamp + 902); + liquidateCdps(0); + _logStakes(); + + _checkTotals(); + } + function testGeneral17() public { setPrice(113290725923451524724356926138082459205154590681450821768273750342902011457932); openCdp( From 12a473e77a2478b5f65ae605020d99f945c26875 Mon Sep 17 00:00:00 2001 From: dapp-whisperer Date: Mon, 4 Mar 2024 17:29:22 -0500 Subject: [PATCH 2/3] surplus rounding check only with CDP < MCR --- .../contracts/TestContracts/invariants/TargetFunctions.sol | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/contracts/contracts/TestContracts/invariants/TargetFunctions.sol b/packages/contracts/contracts/TestContracts/invariants/TargetFunctions.sol index db1f2f890..c31fa553f 100644 --- a/packages/contracts/contracts/TestContracts/invariants/TargetFunctions.sol +++ b/packages/contracts/contracts/TestContracts/invariants/TargetFunctions.sol @@ -237,8 +237,10 @@ abstract contract TargetFunctions is Properties { if (success) { // SURPLUS-CHECK-1 | The surplus is capped at 4 wei | NOTE: Proxy of growth, storage var would further refine - gte(vars.collSurplusPoolBefore + 4, vars.collSurplusPoolAfter, "SURPLUS-CHECK-1"); - gte(vars.userSurplusBefore + 4, vars.userSurplusAfter, "SURPLUS-CHECK-2"); + if (_icrToLiq <= cdpManager.MCR()) { + gte(vars.collSurplusPoolBefore + 4, vars.collSurplusPoolAfter, "SURPLUS-CHECK-1"); + gte(vars.userSurplusBefore + 4, vars.userSurplusAfter, "SURPLUS-CHECK-2"); + } // if ICR >= TCR then we ignore // We could check that Liquidated is not above TCR From 6990b1475168e4da82d9e7e9419220b8595752e7 Mon Sep 17 00:00:00 2001 From: dapp-whisperer Date: Wed, 6 Mar 2024 13:13:03 -0500 Subject: [PATCH 3/3] lint --- .../TestContracts/invariants/TargetFunctions.sol | 3 +-- packages/contracts/foundry_test/EchidnaToFoundry.t.sol | 9 ++++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/contracts/contracts/TestContracts/invariants/TargetFunctions.sol b/packages/contracts/contracts/TestContracts/invariants/TargetFunctions.sol index c31fa553f..827daec45 100644 --- a/packages/contracts/contracts/TestContracts/invariants/TargetFunctions.sol +++ b/packages/contracts/contracts/TestContracts/invariants/TargetFunctions.sol @@ -226,7 +226,7 @@ abstract contract TargetFunctions is Properties { _before(_cdpId); uint256 _icrToLiq = cdpManager.getSyncedICR(_cdpId, priceFeedMock.getPrice()); - + (success, returnData) = actor.proxy( address(cdpManager), abi.encodeWithSelector(CdpManager.liquidate.selector, _cdpId) @@ -235,7 +235,6 @@ abstract contract TargetFunctions is Properties { _after(_cdpId); if (success) { - // SURPLUS-CHECK-1 | The surplus is capped at 4 wei | NOTE: Proxy of growth, storage var would further refine if (_icrToLiq <= cdpManager.MCR()) { gte(vars.collSurplusPoolBefore + 4, vars.collSurplusPoolAfter, "SURPLUS-CHECK-1"); diff --git a/packages/contracts/foundry_test/EchidnaToFoundry.t.sol b/packages/contracts/foundry_test/EchidnaToFoundry.t.sol index 9701d3fe7..ff898e284 100644 --- a/packages/contracts/foundry_test/EchidnaToFoundry.t.sol +++ b/packages/contracts/foundry_test/EchidnaToFoundry.t.sol @@ -225,7 +225,7 @@ contract EToFoundry is bytes32 currentCdp = sortedCdps.getFirst(); console2.log("=== LogStakes ==="); - + uint256 currentPrice = priceFeedMock.fetchPrice(); uint256 currentPricePerShare = collateral.getPooledEthByShares(1 ether); console2.log("currentPrice", currentPrice); @@ -1676,8 +1676,11 @@ contract EToFoundry is } function test_debugTheLiquidation() public { - openCdp(3979204251130340497718654781931317513776851992409413887935202910391326,1000); - openCdp(3306424426048366109580062000503759874070670921097371866772152350788399154439,1069959377727045012); + openCdp(3979204251130340497718654781931317513776851992409413887935202910391326, 1000); + openCdp( + 3306424426048366109580062000503759874070670921097371866772152350788399154439, + 1069959377727045012 + ); _logStakes(); setEthPerShare(0); _logStakes();