Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests minor fixes #431

Merged
merged 2 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ optimizer_runs = 4294967295
[fmt]
wrap_comments = true

# See more config options https://github.com/foundry-rs/foundry/tree/master/crates/config
# See more config options https://github.com/foundry-rs/foundry/tree/master/crates/config
6 changes: 3 additions & 3 deletions test/forge/InvariantBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ contract InvariantBaseTest is BaseTest {
}
}

/// @dev Apparently permanently setting block number and timestamp with cheatcodes in this function doesn't work,
/// @dev Permanently setting block number and timestamp with cheatcodes in this function doesn't work at the moment,
/// they get reset to the ones defined in the set up function after each function call.
/// The solution we choose is to store these in storage, and set them with roll and warp cheatcodes with the
/// setCorrectBlock function at the the begenning of each function.
/// The solution we choose is to save these in storage, and set them with roll and warp cheatcodes with the
/// setCorrectBlock function at the the beginning of each function.
/// The purpose of this function is to increment these variables to simulate a new block.
function newBlock(uint256 elapsed) public {
elapsed = bound(elapsed, 10, 1 days);
Expand Down
28 changes: 14 additions & 14 deletions test/forge/invariant/TestInvariantSingleMarket.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,37 @@ contract SingleMarketInvariantTest is InvariantBaseTest {
// High price because of the 1e36 price scale
oracle.setPrice(1e40);

_weightSelector(this.setMarketFee.selector, 5);
_weightSelector(this.supplyOnMorpho.selector, 20);
_weightSelector(this.borrowOnMorpho.selector, 15);
_weightSelector(this.repayOnMorpho.selector, 10);
_weightSelector(this.withdrawOnMorpho.selector, 15);
_weightSelector(this.supplyCollateralOnMorpho.selector, 20);
_weightSelector(this.withdrawCollateralOnMorpho.selector, 15);
_weightSelector(this.newBlock.selector, 20);
_weightSelector(this.setFeeNoRevert.selector, 5);
_weightSelector(this.supplyNoRevert.selector, 20);
_weightSelector(this.withdrawNoRevert.selector, 15);
_weightSelector(this.borrowNoRevert.selector, 15);
_weightSelector(this.repayNoRevert.selector, 10);
_weightSelector(this.supplyCollateralNoRevert.selector, 20);
_weightSelector(this.withdrawCollateralNoRevert.selector, 15);

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

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

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

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

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

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

function withdrawOnMorpho(uint256 amount) public setCorrectBlock {
function withdrawNoRevert(uint256 amount) public setCorrectBlock {
_accrueInterest(marketParams);

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

function borrowOnMorpho(uint256 amount) public setCorrectBlock {
function borrowNoRevert(uint256 amount) public setCorrectBlock {
_accrueInterest(marketParams);

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

function repayOnMorpho(uint256 amount) public setCorrectBlock {
function repayNoRevert(uint256 amount) public setCorrectBlock {
_accrueInterest(marketParams);

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

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

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

function withdrawCollateralOnMorpho(uint256 amount) public setCorrectBlock {
function withdrawCollateralNoRevert(uint256 amount) public setCorrectBlock {
_accrueInterest(marketParams);

amount = bound(amount, 1, MAX_TEST_AMOUNT);
Expand Down
46 changes: 23 additions & 23 deletions test/forge/invariant/TestInvariantSingleMarketChangingPrice.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ contract SingleMarketChangingPriceInvariantTest is InvariantBaseTest {

_approveSendersTransfers(targetSenders());

_weightSelector(this.supplyOnMorpho.selector, 20);
_weightSelector(this.withdrawOnMorpho.selector, 5);
_weightSelector(this.withdrawOnMorphoOnBehalf.selector, 5);
_weightSelector(this.borrowOnMorpho.selector, 5);
_weightSelector(this.borrowOnMorphoOnBehalf.selector, 5);
_weightSelector(this.repayOnMorpho.selector, 2);
_weightSelector(this.repayOnMorphoOnBehalf.selector, 2);
_weightSelector(this.supplyCollateralOnMorpho.selector, 20);
_weightSelector(this.withdrawCollateralOnMorpho.selector, 5);
_weightSelector(this.withdrawCollateralOnMorphoOnBehalf.selector, 5);
_weightSelector(this.newBlock.selector, 20);
_weightSelector(this.changePrice.selector, 5);
_weightSelector(this.setMarketFee.selector, 2);
_weightSelector(this.setFeeNoRevert.selector, 2);
_weightSelector(this.supplyNoRevert.selector, 20);
_weightSelector(this.withdrawNoRevert.selector, 5);
_weightSelector(this.withdrawOnBehalfNoRevert.selector, 5);
_weightSelector(this.borrowNoRevert.selector, 5);
_weightSelector(this.borrowOnBehalfNoRevert.selector, 5);
_weightSelector(this.repayNoRevert.selector, 2);
_weightSelector(this.repayOnBehalfNoRevert.selector, 2);
_weightSelector(this.supplyCollateralNoRevert.selector, 20);
_weightSelector(this.withdrawCollateralNoRevert.selector, 5);
_weightSelector(this.withdrawCollateralOnBehalfNoRevert.selector, 5);

blockNumber = block.number;
timestamp = block.timestamp;
Expand All @@ -46,22 +46,22 @@ contract SingleMarketChangingPriceInvariantTest is InvariantBaseTest {
oracle.setPrice(currentPrice.wMulDown(variation));
}

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

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

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

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

function withdrawOnMorpho(uint256 amount) public setCorrectBlock {
function withdrawNoRevert(uint256 amount) public setCorrectBlock {
_accrueInterest(marketParams);

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

function withdrawOnMorphoOnBehalf(uint256 amount, address seed) public setCorrectBlock {
function withdrawOnBehalfNoRevert(uint256 amount, address seed) public setCorrectBlock {
_accrueInterest(marketParams);

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

function borrowOnMorpho(uint256 amount) public setCorrectBlock {
function borrowNoRevert(uint256 amount) public setCorrectBlock {
_accrueInterest(marketParams);

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

function borrowOnMorphoOnBehalf(uint256 amount, address seed) public setCorrectBlock {
function borrowOnBehalfNoRevert(uint256 amount, address seed) public setCorrectBlock {
_accrueInterest(marketParams);

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

function repayOnMorpho(uint256 shares) public setCorrectBlock {
function repayNoRevert(uint256 shares) public setCorrectBlock {
_accrueInterest(marketParams);

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

function repayOnMorphoOnBehalf(uint256 shares, address seed) public setCorrectBlock {
function repayOnBehalfNoRevert(uint256 shares, address seed) public setCorrectBlock {
_accrueInterest(marketParams);

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

function supplyCollateralOnMorpho(uint256 amount) public setCorrectBlock {
function supplyCollateralNoRevert(uint256 amount) public setCorrectBlock {
_accrueInterest(marketParams);

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

function withdrawCollateralOnMorpho(uint256 amount) public setCorrectBlock {
function withdrawCollateralNoRevert(uint256 amount) public setCorrectBlock {
_accrueInterest(marketParams);

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

function withdrawCollateralOnMorphoOnBehalf(uint256 amount, address seed) public setCorrectBlock {
function withdrawCollateralOnBehalfNoRevert(uint256 amount, address seed) public setCorrectBlock {
_accrueInterest(marketParams);

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

function liquidateOnMorpho(uint256 seized, address seed) public setCorrectBlock {
function liquidateNoRevert(uint256 seized, address seed) public setCorrectBlock {
_accrueInterest(marketParams);

user = _randomSenderToLiquidate(targetSenders(), seed);
Expand Down
24 changes: 12 additions & 12 deletions test/forge/invariant/TestInvariantSinglePosition.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,25 @@ contract SinglePositionInvariantTest is InvariantBaseTest {
// High price because of the 1e36 price scale
oracle.setPrice(1e40);

_weightSelector(this.supplyOnMorpho.selector, 20);
_weightSelector(this.borrowOnMorpho.selector, 15);
_weightSelector(this.repayOnMorpho.selector, 10);
_weightSelector(this.withdrawOnMorpho.selector, 15);
_weightSelector(this.supplyCollateralOnMorpho.selector, 20);
_weightSelector(this.withdrawCollateralOnMorpho.selector, 15);
_weightSelector(this.supplyNoRevert.selector, 20);
_weightSelector(this.withdrawNoRevert.selector, 15);
_weightSelector(this.borrowNoRevert.selector, 15);
_weightSelector(this.repayNoRevert.selector, 10);
_weightSelector(this.supplyCollateralNoRevert.selector, 20);
_weightSelector(this.withdrawCollateralNoRevert.selector, 15);

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

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

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

function withdrawOnMorpho(uint256 amount) public {
function withdrawNoRevert(uint256 amount) public {
uint256 availableLiquidity = morpho.totalSupplyAssets(id) - morpho.totalBorrowAssets(id);
if (morpho.supplyShares(id, msg.sender) == 0) return;
if (availableLiquidity == 0) return;
Expand All @@ -58,7 +58,7 @@ contract SinglePositionInvariantTest is InvariantBaseTest {
morpho.withdraw(marketParams, amount, 0, msg.sender, msg.sender);
}

function borrowOnMorpho(uint256 amount) public {
function borrowNoRevert(uint256 amount) public {
uint256 availableLiquidity = morpho.totalSupplyAssets(id) - morpho.totalBorrowAssets(id);
if (availableLiquidity == 0) return;

Expand All @@ -68,7 +68,7 @@ contract SinglePositionInvariantTest is InvariantBaseTest {
morpho.borrow(marketParams, amount, 0, msg.sender, msg.sender);
}

function repayOnMorpho(uint256 amount) public {
function repayNoRevert(uint256 amount) public {
if (morpho.borrowShares(id, msg.sender) == 0) return;

uint256 borrowerBalance =
Expand All @@ -81,15 +81,15 @@ contract SinglePositionInvariantTest is InvariantBaseTest {
morpho.repay(marketParams, amount, 0, msg.sender, hex"");
}

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

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

function withdrawCollateralOnMorpho(uint256 amount) public {
function withdrawCollateralNoRevert(uint256 amount) public {
amount = bound(amount, 1, MAX_TEST_AMOUNT);

vm.prank(msg.sender);
Expand Down
28 changes: 14 additions & 14 deletions test/forge/invariant/TestInvariantTwoMarkets.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ contract TwoMarketsInvariantTest is InvariantBaseTest {
// High price because of the 1e36 price scale
oracle.setPrice(1e40);

_weightSelector(this.setMarketFee.selector, 5);
_weightSelector(this.supplyOnMorpho.selector, 20);
_weightSelector(this.borrowOnMorpho.selector, 15);
_weightSelector(this.repayOnMorpho.selector, 10);
_weightSelector(this.withdrawOnMorpho.selector, 15);
_weightSelector(this.supplyCollateralOnMorpho.selector, 20);
_weightSelector(this.withdrawCollateralOnMorpho.selector, 15);
_weightSelector(this.newBlock.selector, 20);
_weightSelector(this.setFeeNoRevert.selector, 5);
_weightSelector(this.supplyNoRevert.selector, 20);
_weightSelector(this.withdrawNoRevert.selector, 15);
_weightSelector(this.borrowNoRevert.selector, 15);
_weightSelector(this.repayNoRevert.selector, 10);
_weightSelector(this.supplyCollateralNoRevert.selector, 20);
_weightSelector(this.withdrawCollateralNoRevert.selector, 15);

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

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

newFee = bound(newFee, 0.1e18, MAX_FEE);
Expand All @@ -72,7 +72,7 @@ contract TwoMarketsInvariantTest is InvariantBaseTest {
morpho.setFee(chosenMarket, newFee);
}

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

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

function withdrawOnMorpho(uint256 amount, bool changeMarket) public setCorrectBlock {
function withdrawNoRevert(uint256 amount, bool changeMarket) public setCorrectBlock {
_accrueInterest(marketParams);

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

function borrowOnMorpho(uint256 amount, bool changeMarket) public setCorrectBlock {
function borrowNoRevert(uint256 amount, bool changeMarket) public setCorrectBlock {
_accrueInterest(marketParams);

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

function repayOnMorpho(uint256 amount, bool changeMarket) public setCorrectBlock {
function repayNoRevert(uint256 amount, bool changeMarket) public setCorrectBlock {
_accrueInterest(marketParams);

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

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

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

function withdrawCollateralOnMorpho(uint256 amount, bool changeMarket) public setCorrectBlock {
function withdrawCollateralNoRevert(uint256 amount, bool changeMarket) public setCorrectBlock {
_accrueInterest(marketParams);

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