-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #706 from morpho-org/ci/fix-invariant
Fix invariants in CI
- Loading branch information
Showing
7 changed files
with
147 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
pragma solidity ^0.8.0; | ||
|
||
import "./BaseInvariantTest.sol"; | ||
|
||
contract DynamicInvariantTest is BaseInvariantTest { | ||
using MorphoLib for IMorpho; | ||
using MarketParamsLib for MarketParams; | ||
|
||
uint256 internal immutable MIN_PRICE = ORACLE_PRICE_SCALE / 10; | ||
uint256 internal immutable MAX_PRICE = ORACLE_PRICE_SCALE * 10; | ||
|
||
function setUp() public virtual override { | ||
selectors.push(this.liquidateSeizedAssetsNoRevert.selector); | ||
selectors.push(this.liquidateRepaidSharesNoRevert.selector); | ||
selectors.push(this.setFeeNoRevert.selector); | ||
selectors.push(this.setPrice.selector); | ||
selectors.push(this.mine.selector); | ||
|
||
super.setUp(); | ||
} | ||
|
||
/* HANDLERS */ | ||
|
||
function setPrice(uint256 price) external { | ||
price = bound(price, MIN_PRICE, MAX_PRICE); | ||
|
||
oracle.setPrice(price); | ||
} | ||
|
||
/* INVARIANTS */ | ||
|
||
function invariantSupplyShares() public { | ||
address[] memory users = targetSenders(); | ||
|
||
for (uint256 i; i < allMarketParams.length; ++i) { | ||
MarketParams memory _marketParams = allMarketParams[i]; | ||
Id _id = _marketParams.id(); | ||
|
||
uint256 sumSupplyShares = morpho.supplyShares(_id, FEE_RECIPIENT); | ||
for (uint256 j; j < users.length; ++j) { | ||
sumSupplyShares += morpho.supplyShares(_id, users[j]); | ||
} | ||
|
||
assertEq(sumSupplyShares, morpho.totalSupplyShares(_id), vm.toString(_marketParams.lltv)); | ||
} | ||
} | ||
|
||
function invariantBorrowShares() public { | ||
address[] memory users = targetSenders(); | ||
|
||
for (uint256 i; i < allMarketParams.length; ++i) { | ||
MarketParams memory _marketParams = allMarketParams[i]; | ||
Id _id = _marketParams.id(); | ||
|
||
uint256 sumBorrowShares; | ||
for (uint256 j; j < users.length; ++j) { | ||
sumBorrowShares += morpho.borrowShares(_id, users[j]); | ||
} | ||
|
||
assertEq(sumBorrowShares, morpho.totalBorrowShares(_id), vm.toString(_marketParams.lltv)); | ||
} | ||
} | ||
|
||
function invariantTotalSupplyGeTotalBorrow() public { | ||
for (uint256 i; i < allMarketParams.length; ++i) { | ||
MarketParams memory _marketParams = allMarketParams[i]; | ||
Id _id = _marketParams.id(); | ||
|
||
assertGe(morpho.totalSupplyAssets(_id), morpho.totalBorrowAssets(_id)); | ||
} | ||
} | ||
|
||
function invariantMorphoBalance() public { | ||
for (uint256 i; i < allMarketParams.length; ++i) { | ||
MarketParams memory _marketParams = allMarketParams[i]; | ||
Id _id = _marketParams.id(); | ||
|
||
assertGe( | ||
loanToken.balanceOf(address(morpho)) + morpho.totalBorrowAssets(_id), morpho.totalSupplyAssets(_id) | ||
); | ||
} | ||
} | ||
|
||
function invariantBadDebt() public { | ||
address[] memory users = targetSenders(); | ||
|
||
for (uint256 i; i < allMarketParams.length; ++i) { | ||
MarketParams memory _marketParams = allMarketParams[i]; | ||
Id _id = _marketParams.id(); | ||
|
||
for (uint256 j; j < users.length; ++j) { | ||
address user = users[j]; | ||
|
||
if (morpho.collateral(_id, user) == 0) { | ||
assertEq( | ||
morpho.borrowShares(_id, user), | ||
0, | ||
string.concat(vm.toString(_marketParams.lltv), ":", vm.toString(user)) | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
pragma solidity ^0.8.0; | ||
|
||
import "./BaseInvariantTest.sol"; | ||
|
||
contract StaticInvariantTest is BaseInvariantTest { | ||
/* INVARIANTS */ | ||
|
||
function invariantHealthy() public { | ||
address[] memory users = targetSenders(); | ||
|
||
for (uint256 i; i < allMarketParams.length; ++i) { | ||
MarketParams memory _marketParams = allMarketParams[i]; | ||
|
||
for (uint256 j; j < users.length; ++j) { | ||
assertTrue(_isHealthy(_marketParams, users[j])); | ||
} | ||
} | ||
} | ||
} |