Skip to content

Commit 26e4627

Browse files
Merge pull request #574 from morpho-org/refactor/remove-skip-irm-call
Remove IRM call skip
2 parents a3d23a4 + 2dcc01b commit 26e4627

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

src/Morpho.sol

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -474,26 +474,23 @@ contract Morpho is IMorphoStaticTyping {
474474

475475
if (elapsed == 0) return;
476476

477-
if (market[id].totalBorrowAssets != 0) {
478-
uint256 borrowRate = IIrm(marketParams.irm).borrowRate(marketParams, market[id]);
479-
uint256 interest = market[id].totalBorrowAssets.wMulDown(borrowRate.wTaylorCompounded(elapsed));
480-
market[id].totalBorrowAssets += interest.toUint128();
481-
market[id].totalSupplyAssets += interest.toUint128();
482-
483-
uint256 feeShares;
484-
if (market[id].fee != 0) {
485-
uint256 feeAmount = interest.wMulDown(market[id].fee);
486-
// The fee amount is subtracted from the total supply in this calculation to compensate for the fact
487-
// that total supply is already increased by the full interest (including the fee amount).
488-
feeShares =
489-
feeAmount.toSharesDown(market[id].totalSupplyAssets - feeAmount, market[id].totalSupplyShares);
490-
position[id][feeRecipient].supplyShares += feeShares;
491-
market[id].totalSupplyShares += feeShares.toUint128();
492-
}
493-
494-
emit EventsLib.AccrueInterest(id, borrowRate, interest, feeShares);
477+
uint256 borrowRate = IIrm(marketParams.irm).borrowRate(marketParams, market[id]);
478+
uint256 interest = market[id].totalBorrowAssets.wMulDown(borrowRate.wTaylorCompounded(elapsed));
479+
market[id].totalBorrowAssets += interest.toUint128();
480+
market[id].totalSupplyAssets += interest.toUint128();
481+
482+
uint256 feeShares;
483+
if (market[id].fee != 0) {
484+
uint256 feeAmount = interest.wMulDown(market[id].fee);
485+
// The fee amount is subtracted from the total supply in this calculation to compensate for the fact
486+
// that total supply is already increased by the full interest (including the fee amount).
487+
feeShares = feeAmount.toSharesDown(market[id].totalSupplyAssets - feeAmount, market[id].totalSupplyShares);
488+
position[id][feeRecipient].supplyShares += feeShares;
489+
market[id].totalSupplyShares += feeShares.toUint128();
495490
}
496491

492+
emit EventsLib.AccrueInterest(id, borrowRate, interest, feeShares);
493+
497494
// Safe "unchecked" cast.
498495
market[id].lastUpdate = uint128(block.timestamp);
499496
}

src/libraries/periphery/MorphoBalancesLib.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ library MorphoBalancesLib {
4141

4242
uint256 elapsed = block.timestamp - market.lastUpdate;
4343

44+
// Skipped if elapsed == 0 of if totalBorrowAssets == 0 because interest would be null.
4445
if (elapsed != 0 && market.totalBorrowAssets != 0) {
4546
uint256 borrowRate = IIrm(marketParams.irm).borrowRateView(marketParams, market);
4647
uint256 interest = market.totalBorrowAssets.wMulDown(borrowRate.wTaylorCompounded(elapsed));

src/mocks/IrmMock.sol

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ contract IrmMock is IIrm {
1010
using MathLib for uint128;
1111

1212
function borrowRateView(MarketParams memory, Market memory market) public pure returns (uint256) {
13+
if (market.totalSupplyAssets == 0) return 0;
14+
1315
uint256 utilization = market.totalBorrowAssets.wDivDown(market.totalSupplyAssets);
1416

1517
// Divide by the number of seconds in a year.

0 commit comments

Comments
 (0)