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

Add public accrue interest function #565

Merged
merged 2 commits into from
Nov 5, 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
8 changes: 8 additions & 0 deletions src/Morpho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,14 @@ contract Morpho is IMorpho {

/* INTEREST MANAGEMENT */

/// @inheritdoc IMorpho
function accrueInterest(MarketParams memory marketParams) external {
Id id = marketParams.id();
require(market[id].lastUpdate != 0, ErrorsLib.MARKET_NOT_CREATED);

_accrueInterest(marketParams, id);
}

/// @dev Accrues interest for the given market `marketParams`.
/// @dev Assumes that the inputs `marketParams` and `id` match.
function _accrueInterest(MarketParams memory marketParams, Id id) internal {
Expand Down
3 changes: 3 additions & 0 deletions src/interfaces/IMorpho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,9 @@ interface IMorpho {
/// @param signature The signature.
function setAuthorizationWithSig(Authorization calldata authorization, Signature calldata signature) external;

/// @notice Accrues interest for the given market `marketParams`.
function accrueInterest(MarketParams memory marketParams) external;
MathisGD marked this conversation as resolved.
Show resolved Hide resolved

/// @notice Returns the data stored on the different `slots`.
function extSloads(bytes32[] memory slots) external view returns (bytes32[] memory);
}
6 changes: 0 additions & 6 deletions test/forge/BaseTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,6 @@ contract BaseTest is Test {
return bound(lltv, 0, WAD - 1);
}

function _accrueInterest(MarketParams memory market) internal {
collateralToken.setBalance(address(this), 1);
morpho.supplyCollateral(market, 1, address(this), hex"");
morpho.withdrawCollateral(market, 1, address(this), address(10));
}

function neq(MarketParams memory a, MarketParams memory b) internal pure returns (bool) {
return (Id.unwrap(a.id()) != Id.unwrap(b.id()));
}
Expand Down
21 changes: 11 additions & 10 deletions test/forge/integration/AccrueInterestIntegrationTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ contract AccrueInterestIntegrationTest is BaseTest {
using MorphoLib for IMorpho;
using SharesMathLib for uint256;

function testAccrueInterestMarketNotCreated(MarketParams memory marketParamsFuzz) public {
vm.assume(neq(marketParamsFuzz, marketParams));

vm.expectRevert(bytes(ErrorsLib.MARKET_NOT_CREATED));
morpho.accrueInterest(marketParamsFuzz);
}

function testAccrueInterestNoTimeElapsed(uint256 amountSupplied, uint256 amountBorrowed) public {
uint256 collateralPrice = oracle.price();
uint256 amountCollateral;
Expand All @@ -28,7 +35,7 @@ contract AccrueInterestIntegrationTest is BaseTest {
uint256 totalSupplyBeforeAccrued = morpho.totalSupplyAssets(id);
uint256 totalSupplySharesBeforeAccrued = morpho.totalSupplyShares(id);

_accrueInterest(marketParams);
morpho.accrueInterest(marketParams);

assertEq(morpho.totalBorrowAssets(id), totalBorrowBeforeAccrued, "total borrow");
assertEq(morpho.totalSupplyAssets(id), totalSupplyBeforeAccrued, "total supply");
Expand All @@ -49,7 +56,7 @@ contract AccrueInterestIntegrationTest is BaseTest {
uint256 totalSupplyBeforeAccrued = morpho.totalSupplyAssets(id);
uint256 totalSupplySharesBeforeAccrued = morpho.totalSupplyShares(id);

_accrueInterest(marketParams);
morpho.accrueInterest(marketParams);

assertEq(morpho.totalBorrowAssets(id), totalBorrowBeforeAccrued, "total borrow");
assertEq(morpho.totalSupplyAssets(id), totalSupplyBeforeAccrued, "total supply");
Expand Down Expand Up @@ -86,12 +93,9 @@ contract AccrueInterestIntegrationTest is BaseTest {
uint256 expectedAccruedInterest =
totalBorrowBeforeAccrued.wMulDown(borrowRate.wTaylorCompounded(blocks * BLOCK_TIME));

collateralToken.setBalance(address(this), 1);
morpho.supplyCollateral(marketParams, 1, address(this), hex"");
vm.expectEmit(true, true, true, true, address(morpho));
emit EventsLib.AccrueInterest(id, borrowRate, expectedAccruedInterest, 0);
// Accrues interest.
morpho.withdrawCollateral(marketParams, 1, address(this), address(this));
morpho.accrueInterest(marketParams);
MerlinEgalite marked this conversation as resolved.
Show resolved Hide resolved

assertEq(morpho.totalBorrowAssets(id), totalBorrowBeforeAccrued + expectedAccruedInterest, "total borrow");
assertEq(morpho.totalSupplyAssets(id), totalSupplyBeforeAccrued + expectedAccruedInterest, "total supply");
Expand Down Expand Up @@ -151,12 +155,9 @@ contract AccrueInterestIntegrationTest is BaseTest {
params.totalSupplySharesBeforeAccrued
);

collateralToken.setBalance(address(this), 1);
morpho.supplyCollateral(marketParams, 1, address(this), hex"");
vm.expectEmit(true, true, true, true, address(morpho));
emit EventsLib.AccrueInterest(id, params.borrowRate, params.expectedAccruedInterest, params.feeShares);
// Accrues interest.
morpho.withdrawCollateral(marketParams, 1, address(this), address(this));
morpho.accrueInterest(marketParams);
MerlinEgalite marked this conversation as resolved.
Show resolved Hide resolved

assertEq(
morpho.totalSupplyAssets(id),
Expand Down
12 changes: 6 additions & 6 deletions test/forge/libraries/periphery/MorphoBalancesLibTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ contract MorphoBalancesLibTest is BaseTest {
uint256 virtualTotalBorrowShares
) = morpho.expectedMarketBalances(marketParams);

_accrueInterest(marketParams);
morpho.accrueInterest(marketParams);

assertEq(virtualTotalSupplyAssets, morpho.totalSupplyAssets(id), "total supply assets");
assertEq(virtualTotalBorrowAssets, morpho.totalBorrowAssets(id), "total borrow assets");
Expand All @@ -36,7 +36,7 @@ contract MorphoBalancesLibTest is BaseTest {

uint256 expectedTotalSupply = morpho.expectedTotalSupply(marketParams);

_accrueInterest(marketParams);
morpho.accrueInterest(marketParams);

assertEq(expectedTotalSupply, morpho.totalSupplyAssets(id));
}
Expand All @@ -48,7 +48,7 @@ contract MorphoBalancesLibTest is BaseTest {

uint256 expectedTotalBorrow = morpho.expectedTotalBorrow(marketParams);

_accrueInterest(marketParams);
morpho.accrueInterest(marketParams);

assertEq(expectedTotalBorrow, morpho.totalBorrowAssets(id));
}
Expand All @@ -63,7 +63,7 @@ contract MorphoBalancesLibTest is BaseTest {

uint256 expectedTotalSupplyShares = morpho.expectedTotalSupplyShares(marketParams);

_accrueInterest(marketParams);
morpho.accrueInterest(marketParams);

assertEq(expectedTotalSupplyShares, morpho.totalSupplyShares(id));
}
Expand All @@ -75,7 +75,7 @@ contract MorphoBalancesLibTest is BaseTest {

uint256 expectedSupplyBalance = morpho.expectedSupplyBalance(marketParams, address(this));

_accrueInterest(marketParams);
morpho.accrueInterest(marketParams);

uint256 actualSupplyBalance = morpho.supplyShares(id, address(this)).toAssetsDown(
morpho.totalSupplyAssets(id), morpho.totalSupplyShares(id)
Expand All @@ -91,7 +91,7 @@ contract MorphoBalancesLibTest is BaseTest {

uint256 expectedBorrowBalance = morpho.expectedBorrowBalance(marketParams, address(this));

_accrueInterest(marketParams);
morpho.accrueInterest(marketParams);

uint256 actualBorrowBalance = morpho.borrowShares(id, address(this)).toAssetsUp(
morpho.totalBorrowAssets(id), morpho.totalBorrowShares(id)
Expand Down