Skip to content

Commit

Permalink
test: draft
Browse files Browse the repository at this point in the history
  • Loading branch information
MerlinEgalite committed Sep 10, 2023
1 parent b860e60 commit bf387ca
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 16 deletions.
12 changes: 9 additions & 3 deletions contracts/mocks/IrmMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@ import {MathLib} from "@morpho-blue/libraries/MathLib.sol";
contract IrmMock is IIrm {
using MathLib for uint128;

function borrowRateView(MarketParams memory, Market memory market) public pure returns (uint256) {
uint256 public rate;

function setRate(uint256 newRate) external {
rate = newRate;
}

function borrowRateView(MarketParams memory, Market memory market) public view returns (uint256) {
uint256 utilization = market.totalBorrowAssets.wDivDown(market.totalSupplyAssets);

// Divide by the number of seconds in a year.
// This is a very simple model where x% utilization corresponds to x% APR.
return utilization / 365 days;
return rate == 0 ? utilization / 365 days : rate / 365 days;
}

function borrowRate(MarketParams memory marketParams, Market memory market) external pure returns (uint256) {
function borrowRate(MarketParams memory marketParams, Market memory market) external view returns (uint256) {
return borrowRateView(marketParams, market);
}
}
17 changes: 14 additions & 3 deletions test/forge/BaseTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ contract BaseTest is Test {
address internal ONBEHALF = _addrFromHashedString("Morpho On Behalf");
address internal RECEIVER = _addrFromHashedString("Morpho Receiver");
address internal LIQUIDATOR = _addrFromHashedString("Morpho Liquidator");
address internal OWNER = _addrFromHashedString("Morpho Owner");
address internal RISK_MANAGER = _addrFromHashedString("Morpho Risk Manager");
address internal ALLOCATOR = _addrFromHashedString("Morpho Allocator");
address internal OWNER = _addrFromHashedString("Owner");
address internal RISK_MANAGER = _addrFromHashedString("Risk Manager");
address internal ALLOCATOR = _addrFromHashedString("Allocator");
address internal FEE_RECIPIENT = _addrFromHashedString("Fee Recipient");

uint256 internal constant LLTV = 0.8 ether;
uint256 internal constant TIMELOCK = 0;
Expand Down Expand Up @@ -131,4 +132,14 @@ contract BaseTest is Test {
vault.enableMarket(params.id());
vm.stopPrank();
}

function _borrow(MarketParams memory params, uint256 amount) internal {
deal(address(collateralToken), BORROWER, type(uint256).max);

vm.startPrank(BORROWER);
collateralToken.approve(address(morpho), type(uint256).max);
morpho.supplyCollateral(params, type(uint128).max, BORROWER, hex"");
morpho.borrow(params, amount, 0, BORROWER, BORROWER);
vm.stopPrank();
}
}
44 changes: 44 additions & 0 deletions test/forge/FeeTest.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;

import "./BaseTest.sol";

contract FeeTest is BaseTest {
using MarketParamsLib for MarketParams;

uint256 internal constant FEE = 0.1 ether; // 10%

function setUp() public override {
super.setUp();

_submitAndEnableMarket(allMarkets[0], CAP);

irm.setRate(0.1 ether); // 10% APY.
}

function _setFee() internal {
vm.startPrank(OWNER);
vault.submitPendingFee(FEE);
vault.setFee();
vault.setFeeRecipient(FEE_RECIPIENT);
vm.stopPrank();
}

function testShouldMintSharesToFeeRecipient(uint256 amount) public {
_setFee();

amount = bound(amount, MIN_TEST_AMOUNT, MAX_TEST_AMOUNT);

vm.prank(SUPPLIER);
uint256 shares = vault.deposit(amount, SUPPLIER);

_borrow(allMarkets[0], amount / 2);

vm.warp(block.timestamp + 365 days);

vm.prank(SUPPLIER);
vault.redeem(shares / 3, SUPPLIER, SUPPLIER);

assertGt(vault.balanceOf(FEE_RECIPIENT), 0, "fee recipient balance is zero");
}
}
10 changes: 0 additions & 10 deletions test/forge/VaultSeveralMarketsTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,6 @@ contract VaultSeveralMarketsTest is BaseTest {
_assertBalances(cap, amount - toWithdraw);
}

function _borrow(MarketParams memory marketParams, uint256 amount) internal {
deal(address(collateralToken), BORROWER, type(uint256).max);

vm.startPrank(BORROWER);
collateralToken.approve(address(morpho), type(uint256).max);
morpho.supplyCollateral(marketParams, type(uint128).max, BORROWER, hex"");
morpho.borrow(marketParams, amount, 0, BORROWER, BORROWER);
vm.stopPrank();
}

function testWithdrawSeveralMarketsWithLessLiquidity(uint128 cap, uint256 toWithdraw, uint256 borrowed) public {
cap = uint128(bound(cap, MIN_TEST_AMOUNT, MAX_TEST_AMOUNT));
uint256 amount = 3 * cap;
Expand Down

0 comments on commit bf387ca

Please sign in to comment.