Skip to content

Commit

Permalink
feat: update to add more invariants
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas-manuel committed Jul 3, 2024
1 parent cef1abf commit b813c19
Showing 1 changed file with 81 additions and 5 deletions.
86 changes: 81 additions & 5 deletions test/invariant/Invariants.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ abstract contract PSMInvariantTestBase is PSMTestBase {
expectedUsdcBalance += swapperHandler.swapsIn(swapper, address(usdc));
expectedDaiBalance += swapperHandler.swapsIn(swapper, address(dai));
expectedSDaiBalance += swapperHandler.swapsIn(swapper, address(sDai));
}

if (address(transferHandler) != address(0)) {
expectedUsdcBalance += transferHandler.transfersIn(address(usdc));
expectedDaiBalance += transferHandler.transfersIn(address(dai));
expectedSDaiBalance += transferHandler.transfersIn(address(sDai));
}
if (address(transferHandler) != address(0)) {
expectedUsdcBalance += transferHandler.transfersIn(address(usdc));
expectedDaiBalance += transferHandler.transfersIn(address(dai));
expectedSDaiBalance += transferHandler.transfersIn(address(sDai));
}

// Loop twice to avoid underflows between LPs
Expand All @@ -135,6 +135,32 @@ abstract contract PSMInvariantTestBase is PSMTestBase {
assertEq(sDai.balanceOf(address(psm)), expectedSDaiBalance);
}

function _checkInvariant_F() public view {
uint256 totalValueSwappedIn;
uint256 totalValueSwappedOut;

for(uint256 i; i < 3; i++) {
address swapper = swapperHandler.swappers(i);

totalValueSwappedIn +=
swapperHandler.swapsIn(swapper, address(usdc)) * 1e12 +
swapperHandler.swapsIn(swapper, address(dai)) +
swapperHandler.swapsIn(swapper, address(sDai)) * rateProvider.getConversionRate() / 1e27;
}

// Loop twice to avoid underflows between LPs
for(uint256 i; i < 3; i++) {
address swapper = swapperHandler.swappers(i);

totalValueSwappedOut +=
swapperHandler.swapsIn(swapper, address(usdc)) * 1e12 +
swapperHandler.swapsIn(swapper, address(dai)) +
swapperHandler.swapsIn(swapper, address(sDai)) * rateProvider.getConversionRate() / 1e27;
}

assertEq(totalValueSwappedIn, totalValueSwappedOut);
}

/**********************************************************************************************/
/*** Helper functions ***/
/**********************************************************************************************/
Expand Down Expand Up @@ -314,6 +340,10 @@ contract PSMInvariants_ConstantRate_NoTransfer is PSMInvariantTestBase {
_checkInvariant_E();
}

function invariant_F() public view {
_checkInvariant_F();
}

function afterInvariant() public {
_withdrawAllPositions();
}
Expand Down Expand Up @@ -346,6 +376,17 @@ contract PSMInvariants_ConstantRate_WithTransfers is PSMInvariantTestBase {
_checkInvariant_C();
}

// No invariant D because rate changes lead to large rounding errors when compared with
// ghost variables

function invariant_E() public view {
_checkInvariant_E();
}

function invariant_F() public view {
_checkInvariant_F();
}

function afterInvariant() public {
_withdrawAllPositions();
}
Expand Down Expand Up @@ -384,6 +425,14 @@ contract PSMInvariants_RateSetting_NoTransfer is PSMInvariantTestBase {
// No invariant D because rate changes lead to large rounding errors when compared with
// ghost variables

function invariant_E() public view {
_checkInvariant_E();
}

function invariant_F() public view {
_checkInvariant_F();
}

function afterInvariant() public {
_withdrawAllPositions();
}
Expand Down Expand Up @@ -424,6 +473,14 @@ contract PSMInvariants_RateSetting_WithTransfers is PSMInvariantTestBase {
// No invariant D because rate changes lead to large rounding errors when compared with
// ghost variables

function invariant_E() public view {
_checkInvariant_E();
}

function invariant_F() public view {
_checkInvariant_F();
}

function afterInvariant() public {
_withdrawAllPositions();
}
Expand Down Expand Up @@ -479,6 +536,14 @@ contract PSMInvariants_TimeBasedRateSetting_NoTransfer is PSMInvariantTestBase {
// No invariant D because rate changes lead to large rounding errors when compared with
// ghost variables

function invariant_E() public view {
_checkInvariant_E();
}

function invariant_F() public view {
_checkInvariant_F();
}

function afterInvariant() public {
_withdrawAllPositions();
}
Expand Down Expand Up @@ -533,6 +598,17 @@ contract PSMInvariants_TimeBasedRateSetting_WithTransfers is PSMInvariantTestBas
_checkInvariant_C();
}

// No invariant D because rate changes lead to large rounding errors when compared with
// ghost variables

function invariant_E() public view {
_checkInvariant_E();
}

function invariant_F() public view {
_checkInvariant_F();
}

function afterInvariant() public {
_withdrawAllPositions();
}
Expand Down

0 comments on commit b813c19

Please sign in to comment.