Skip to content

Commit

Permalink
fix: apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Grimal committed Aug 22, 2023
1 parent d0fd553 commit b45366c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 73 deletions.
3 changes: 2 additions & 1 deletion test/forge/InvariantBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ contract InvariantBaseTest is BaseTest {
timestamp += elapsed;
}

function setCorrectBlock() internal {
modifier setCorrectBlock() {
vm.roll(blockNumber);
vm.warp(timestamp);
_;
}

function _randomSenderToWithdrawOnBehalf(address[] memory addresses, address seed, address sender)
Expand Down
26 changes: 8 additions & 18 deletions test/forge/invariant/TestInvariantSingleMarket.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,30 @@ contract SingleMarketInvariantTest is InvariantBaseTest {
_weightSelector(this.withdrawOnMorpho.selector, 15);
_weightSelector(this.supplyCollateralOnMorpho.selector, 20);
_weightSelector(this.withdrawCollateralOnMorpho.selector, 15);
_weightSelector(this.newBlock.selector, 10);
_weightSelector(this.newBlock.selector, 20);

blockNumber = block.number;
timestamp = block.timestamp;

targetSelector(FuzzSelector({addr: address(this), selectors: selectors}));
}

function setMarketFee(uint256 newFee) public {
setCorrectBlock();

function setMarketFee(uint256 newFee) public setCorrectBlock {
newFee = bound(newFee, 0.1e18, MAX_FEE);

vm.prank(OWNER);
morpho.setFee(market, newFee);
}

function supplyOnMorpho(uint256 amount) public {
setCorrectBlock();

function supplyOnMorpho(uint256 amount) public setCorrectBlock {
amount = bound(amount, 1, MAX_TEST_AMOUNT);

borrowableToken.setBalance(msg.sender, amount);
vm.prank(msg.sender);
morpho.supply(market, amount, 0, msg.sender, hex"");
}

function withdrawOnMorpho(uint256 amount) public {
setCorrectBlock();
function withdrawOnMorpho(uint256 amount) public setCorrectBlock {
morpho.accrueInterest(market);

uint256 availableLiquidity = morpho.totalSupplyAssets(id) - morpho.totalBorrowAssets(id);
Expand All @@ -70,8 +65,7 @@ contract SingleMarketInvariantTest is InvariantBaseTest {
morpho.withdraw(market, amount, 0, msg.sender, msg.sender);
}

function borrowOnMorpho(uint256 amount) public {
setCorrectBlock();
function borrowOnMorpho(uint256 amount) public setCorrectBlock {
morpho.accrueInterest(market);

uint256 availableLiquidity = morpho.totalSupplyAssets(id) - morpho.totalBorrowAssets(id);
Expand All @@ -84,8 +78,7 @@ contract SingleMarketInvariantTest is InvariantBaseTest {
morpho.borrow(market, amount, 0, msg.sender, msg.sender);
}

function repayOnMorpho(uint256 amount) public {
setCorrectBlock();
function repayOnMorpho(uint256 amount) public setCorrectBlock {
morpho.accrueInterest(market);

if (morpho.borrowShares(id, msg.sender) == 0) return;
Expand All @@ -101,18 +94,15 @@ contract SingleMarketInvariantTest is InvariantBaseTest {
morpho.repay(market, amount, 0, msg.sender, hex"");
}

function supplyCollateralOnMorpho(uint256 amount) public {
setCorrectBlock();

function supplyCollateralOnMorpho(uint256 amount) public setCorrectBlock {
amount = bound(amount, 1, MAX_TEST_AMOUNT);

collateralToken.setBalance(msg.sender, amount);
vm.prank(msg.sender);
morpho.supplyCollateral(market, amount, msg.sender, hex"");
}

function withdrawCollateralOnMorpho(uint256 amount) public {
setCorrectBlock();
function withdrawCollateralOnMorpho(uint256 amount) public setCorrectBlock {
morpho.accrueInterest(market);

amount = bound(amount, 1, MAX_TEST_AMOUNT);
Expand Down
53 changes: 17 additions & 36 deletions test/forge/invariant/TestInvariantSingleMarketChangingPrice.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ contract SingleMarketChangingPriceInvariantTest is InvariantBaseTest {
_weightSelector(this.supplyCollateralOnMorpho.selector, 20);
_weightSelector(this.withdrawCollateralOnMorpho.selector, 5);
_weightSelector(this.withdrawCollateralOnMorphoOnBehalf.selector, 5);
_weightSelector(this.newBlock.selector, 5);
_weightSelector(this.newBlock.selector, 20);
_weightSelector(this.changePrice.selector, 5);
_weightSelector(this.setMarketFee.selector, 2);

Expand All @@ -39,39 +39,29 @@ contract SingleMarketChangingPriceInvariantTest is InvariantBaseTest {
oracle.setPrice(1e36);
}

function changePrice(uint256 variation, bool add) public {
// price variation bounded between 2% and 20%
variation = bound(variation, 2e16, 2e17);
function changePrice(uint256 variation) public {
// price variation bounded between -20% and +20%
variation = bound(variation, 0.8e18, 1.2e18);
uint256 currentPrice = IOracle(market.oracle).price();
uint256 priceVariation = currentPrice.wMulDown(variation);
if (add) {
oracle.setPrice(currentPrice + priceVariation);
} else {
oracle.setPrice(currentPrice - priceVariation);
}
oracle.setPrice(currentPrice.wMulDown(variation));
}

function setMarketFee(uint256 newFee) public {
setCorrectBlock();

function setMarketFee(uint256 newFee) public setCorrectBlock {
newFee = bound(newFee, 0, MAX_FEE);

vm.prank(OWNER);
morpho.setFee(market, newFee);
}

function supplyOnMorpho(uint256 amount) public {
setCorrectBlock();

function supplyOnMorpho(uint256 amount) public setCorrectBlock {
amount = bound(amount, 1, MAX_TEST_AMOUNT);
borrowableToken.setBalance(msg.sender, amount);

vm.prank(msg.sender);
morpho.supply(market, amount, 0, msg.sender, hex"");
}

function withdrawOnMorpho(uint256 amount) public {
setCorrectBlock();
function withdrawOnMorpho(uint256 amount) public setCorrectBlock {
morpho.accrueInterest(market);

uint256 availableLiquidity = morpho.totalSupplyAssets(id) - morpho.totalBorrowAssets(id);
Expand All @@ -88,8 +78,7 @@ contract SingleMarketChangingPriceInvariantTest is InvariantBaseTest {
morpho.withdraw(market, amount, 0, msg.sender, msg.sender);
}

function withdrawOnMorphoOnBehalf(uint256 amount, address seed) public {
setCorrectBlock();
function withdrawOnMorphoOnBehalf(uint256 amount, address seed) public setCorrectBlock {
morpho.accrueInterest(market);

uint256 availableLiquidity = morpho.totalSupplyAssets(id) - morpho.totalBorrowAssets(id);
Expand All @@ -108,8 +97,7 @@ contract SingleMarketChangingPriceInvariantTest is InvariantBaseTest {
morpho.withdraw(market, amount, 0, onBehalf, msg.sender);
}

function borrowOnMorpho(uint256 amount) public {
setCorrectBlock();
function borrowOnMorpho(uint256 amount) public setCorrectBlock {
morpho.accrueInterest(market);

uint256 availableLiquidity = morpho.totalSupplyAssets(id) - morpho.totalBorrowAssets(id);
Expand All @@ -131,8 +119,7 @@ contract SingleMarketChangingPriceInvariantTest is InvariantBaseTest {
morpho.borrow(market, amount, 0, msg.sender, msg.sender);
}

function borrowOnMorphoOnBehalf(uint256 amount, address seed) public {
setCorrectBlock();
function borrowOnMorphoOnBehalf(uint256 amount, address seed) public setCorrectBlock {
morpho.accrueInterest(market);

uint256 availableLiquidity = morpho.totalSupplyAssets(id) - morpho.totalBorrowAssets(id);
Expand All @@ -156,8 +143,7 @@ contract SingleMarketChangingPriceInvariantTest is InvariantBaseTest {
morpho.borrow(market, amount, 0, onBehalf, msg.sender);
}

function repayOnMorpho(uint256 shares) public {
setCorrectBlock();
function repayOnMorpho(uint256 shares) public setCorrectBlock {
morpho.accrueInterest(market);

uint256 borrowShares = morpho.borrowShares(id, msg.sender);
Expand All @@ -173,8 +159,7 @@ contract SingleMarketChangingPriceInvariantTest is InvariantBaseTest {
morpho.repay(market, 0, shares, msg.sender, hex"");
}

function repayOnMorphoOnBehalf(uint256 shares, address seed) public {
setCorrectBlock();
function repayOnMorphoOnBehalf(uint256 shares, address seed) public setCorrectBlock {
morpho.accrueInterest(market);

address onBehalf = _randomSenderToRepayOnBehalf(targetSenders(), seed);
Expand All @@ -190,8 +175,7 @@ contract SingleMarketChangingPriceInvariantTest is InvariantBaseTest {
morpho.repay(market, 0, shares, onBehalf, hex"");
}

function supplyCollateralOnMorpho(uint256 amount) public {
setCorrectBlock();
function supplyCollateralOnMorpho(uint256 amount) public setCorrectBlock {
morpho.accrueInterest(market);

amount = bound(amount, 1, MAX_TEST_AMOUNT);
Expand All @@ -201,8 +185,7 @@ contract SingleMarketChangingPriceInvariantTest is InvariantBaseTest {
morpho.supplyCollateral(market, amount, msg.sender, hex"");
}

function withdrawCollateralOnMorpho(uint256 amount) public {
setCorrectBlock();
function withdrawCollateralOnMorpho(uint256 amount) public setCorrectBlock {
morpho.accrueInterest(market);

if (morpho.collateral(id, msg.sender) == 0 || !isHealthy(id, msg.sender)) return;
Expand All @@ -223,8 +206,7 @@ contract SingleMarketChangingPriceInvariantTest is InvariantBaseTest {
morpho.withdrawCollateral(market, amount, msg.sender, msg.sender);
}

function withdrawCollateralOnMorphoOnBehalf(uint256 amount, address seed) public {
setCorrectBlock();
function withdrawCollateralOnMorphoOnBehalf(uint256 amount, address seed) public setCorrectBlock {
morpho.accrueInterest(market);

address onBehalf = _randomSenderToWithdrawCollateralOnBehalf(targetSenders(), seed, msg.sender);
Expand All @@ -246,8 +228,7 @@ contract SingleMarketChangingPriceInvariantTest is InvariantBaseTest {
morpho.withdrawCollateral(market, amount, onBehalf, msg.sender);
}

function liquidateOnMorpho(uint256 seized, address seed) public {
setCorrectBlock();
function liquidateOnMorpho(uint256 seized, address seed) public setCorrectBlock {
morpho.accrueInterest(market);

user = _randomSenderToLiquidate(targetSenders(), seed);
Expand Down
28 changes: 10 additions & 18 deletions test/forge/invariant/TestInvariantTwoMarkets.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ contract TwoMarketsInvariantTest is InvariantBaseTest {
_weightSelector(this.withdrawOnMorpho.selector, 15);
_weightSelector(this.supplyCollateralOnMorpho.selector, 20);
_weightSelector(this.withdrawCollateralOnMorpho.selector, 15);
_weightSelector(this.newBlock.selector, 10);
_weightSelector(this.newBlock.selector, 20);

blockNumber = block.number;
timestamp = block.timestamp;
Expand All @@ -63,18 +63,16 @@ contract TwoMarketsInvariantTest is InvariantBaseTest {
}
}

function setMarketFee(uint256 newFee) public {
setCorrectBlock();
function setMarketFee(uint256 newFee, bool changeMarket) public setCorrectBlock {
(MarketParams memory chosenMarket,) = chooseMarket(changeMarket);

newFee = bound(newFee, 0.1e18, MAX_FEE);

vm.prank(OWNER);
morpho.setFee(market, newFee);
morpho.setFee(chosenMarket, newFee);
}

function supplyOnMorpho(uint256 amount, bool changeMarket) public {
setCorrectBlock();

function supplyOnMorpho(uint256 amount, bool changeMarket) public setCorrectBlock {
(MarketParams memory chosenMarket,) = chooseMarket(changeMarket);

amount = bound(amount, 1, MAX_TEST_AMOUNT);
Expand All @@ -84,8 +82,7 @@ contract TwoMarketsInvariantTest is InvariantBaseTest {
morpho.supply(chosenMarket, amount, 0, msg.sender, hex"");
}

function withdrawOnMorpho(uint256 amount, bool changeMarket) public {
setCorrectBlock();
function withdrawOnMorpho(uint256 amount, bool changeMarket) public setCorrectBlock {
morpho.accrueInterest(market);

(MarketParams memory chosenMarket, Id chosenId) = chooseMarket(changeMarket);
Expand All @@ -104,8 +101,7 @@ contract TwoMarketsInvariantTest is InvariantBaseTest {
morpho.withdraw(chosenMarket, amount, 0, msg.sender, msg.sender);
}

function borrowOnMorpho(uint256 amount, bool changeMarket) public {
setCorrectBlock();
function borrowOnMorpho(uint256 amount, bool changeMarket) public setCorrectBlock {
morpho.accrueInterest(market);

(MarketParams memory chosenMarket, Id chosenId) = chooseMarket(changeMarket);
Expand All @@ -120,8 +116,7 @@ contract TwoMarketsInvariantTest is InvariantBaseTest {
morpho.borrow(chosenMarket, amount, 0, msg.sender, msg.sender);
}

function repayOnMorpho(uint256 amount, bool changeMarket) public {
setCorrectBlock();
function repayOnMorpho(uint256 amount, bool changeMarket) public setCorrectBlock {
morpho.accrueInterest(market);

(MarketParams memory chosenMarket, Id chosenId) = chooseMarket(changeMarket);
Expand All @@ -142,9 +137,7 @@ contract TwoMarketsInvariantTest is InvariantBaseTest {
morpho.repay(chosenMarket, amount, 0, msg.sender, hex"");
}

function supplyCollateralOnMorpho(uint256 amount, bool changeMarket) public {
setCorrectBlock();

function supplyCollateralOnMorpho(uint256 amount, bool changeMarket) public setCorrectBlock {
(MarketParams memory chosenMarket,) = chooseMarket(changeMarket);

amount = bound(amount, 1, MAX_TEST_AMOUNT);
Expand All @@ -154,8 +147,7 @@ contract TwoMarketsInvariantTest is InvariantBaseTest {
morpho.supplyCollateral(chosenMarket, amount, msg.sender, hex"");
}

function withdrawCollateralOnMorpho(uint256 amount, bool changeMarket) public {
setCorrectBlock();
function withdrawCollateralOnMorpho(uint256 amount, bool changeMarket) public setCorrectBlock {
morpho.accrueInterest(market);

(MarketParams memory chosenMarket,) = chooseMarket(changeMarket);
Expand Down

0 comments on commit b45366c

Please sign in to comment.