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

Skip interest accrual if already accrued in the same block #182

Merged
merged 6 commits into from
Jul 31, 2023
Merged
Changes from 5 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
36 changes: 20 additions & 16 deletions src/Blue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -288,24 +288,28 @@ contract Blue is IFlashLender {
// Interests management.

function _accrueInterests(Market memory market, Id id) internal {
uint256 marketTotalBorrow = totalBorrow[id];

if (marketTotalBorrow != 0) {
uint256 borrowRate = market.irm.borrowRate(market);
uint256 accruedInterests = marketTotalBorrow.mulWadDown(borrowRate * (block.timestamp - lastUpdate[id]));
totalBorrow[id] = marketTotalBorrow + accruedInterests;
totalSupply[id] += accruedInterests;

if (fee[id] != 0) {
uint256 feeAmount = accruedInterests.mulWadDown(fee[id]);
// The fee amount is subtracted from the total supply in this calculation to compensate for the fact that total supply is already updated.
uint256 feeShares = feeAmount.mulDivDown(totalSupplyShares[id], totalSupply[id] - feeAmount);
supplyShare[id][feeRecipient] += feeShares;
totalSupplyShares[id] += feeShares;
uint256 elapsed = block.timestamp - lastUpdate[id];

if (elapsed != 0) {
uint256 marketTotalBorrow = totalBorrow[id];

if (marketTotalBorrow != 0) {
uint256 borrowRate = market.irm.borrowRate(market);
uint256 accruedInterests = marketTotalBorrow.mulWadDown(borrowRate * elapsed);
totalBorrow[id] = marketTotalBorrow + accruedInterests;
totalSupply[id] += accruedInterests;

if (fee[id] != 0) {
uint256 feeAmount = accruedInterests.mulWadDown(fee[id]);
// The fee amount is subtracted from the total supply in this calculation to compensate for the fact that total supply is already updated.
uint256 feeShares = feeAmount.mulDivDown(totalSupplyShares[id], totalSupply[id] - feeAmount);
supplyShare[id][feeRecipient] += feeShares;
totalSupplyShares[id] += feeShares;
}
}
}

lastUpdate[id] = block.timestamp;
lastUpdate[id] = block.timestamp;
}
}

// Health check.
Expand Down