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

refactor(interests): expose accrued interests and fee #138

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
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
55 changes: 37 additions & 18 deletions src/Blue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ contract Blue {
require(isLltvEnabled[market.lltv], "LLTV not enabled");
require(lastUpdate[id] == 0, "market already exists");

accrueInterests(market, id);
_accrueInterests(market, id);
}

// Supply management.
Expand All @@ -105,7 +105,7 @@ contract Blue {
require(lastUpdate[id] != 0, "unknown market");
require(amount != 0, "zero amount");

accrueInterests(market, id);
_accrueInterests(market, id);

if (totalSupply[id] == 0) {
supplyShare[id][onBehalf] = WAD;
Expand All @@ -127,7 +127,7 @@ contract Blue {
require(amount != 0, "zero amount");
require(isSenderOrIsApproved(onBehalf), "not approved");

accrueInterests(market, id);
_accrueInterests(market, id);

uint256 shares = amount.wMul(totalSupplyShares[id]).wDiv(totalSupply[id]);
supplyShare[id][onBehalf] -= shares;
Expand All @@ -148,7 +148,7 @@ contract Blue {
require(amount != 0, "zero amount");
require(isSenderOrIsApproved(onBehalf), "not approved");

accrueInterests(market, id);
_accrueInterests(market, id);

if (totalBorrow[id] == 0) {
borrowShare[id][onBehalf] = WAD;
Expand All @@ -172,7 +172,7 @@ contract Blue {
require(lastUpdate[id] != 0, "unknown market");
require(amount != 0, "zero amount");

accrueInterests(market, id);
_accrueInterests(market, id);

uint256 shares = amount.wMul(totalBorrowShares[id]).wDiv(totalBorrow[id]);
borrowShare[id][onBehalf] -= shares;
Expand Down Expand Up @@ -204,7 +204,7 @@ contract Blue {
require(amount != 0, "zero amount");
require(isSenderOrIsApproved(onBehalf), "not approved");

accrueInterests(market, id);
_accrueInterests(market, id);

collateral[id][onBehalf] -= amount;

Expand All @@ -220,7 +220,7 @@ contract Blue {
require(lastUpdate[id] != 0, "unknown market");
require(seized != 0, "zero amount");

accrueInterests(market, id);
_accrueInterests(market, id);

require(!isHealthy(market, id, borrower), "cannot liquidate a healthy position");

Expand Down Expand Up @@ -261,25 +261,44 @@ contract Blue {

// Interests management.

function accrueInterests(Market calldata market, Id id) private {
uint256 marketTotalBorrow = totalBorrow[id];
function accrued(Market calldata market) external view returns (uint256 accruedInterests, uint256 feeShares) {
Id id = market.toId();
require(lastUpdate[id] != 0, "unknown market");

if (marketTotalBorrow != 0) {
uint256 borrowRate = market.irm.borrowRate(market);
uint256 accruedInterests = marketTotalBorrow.wMul(borrowRate).wMul(block.timestamp - lastUpdate[id]);
totalBorrow[id] = marketTotalBorrow + accruedInterests;
(accruedInterests, feeShares) = _accruedInterests(market, id);
}

function _accrueInterests(Market calldata market, Id id) internal {
(uint256 accruedInterests, uint256 feeShares) = _accruedInterests(market, id);

if (accruedInterests > 0) {
totalBorrow[id] += accruedInterests;
totalSupply[id] += accruedInterests;
}

if (feeShares > 0) {
supplyShare[id][feeRecipient] += feeShares;
totalSupplyShares[id] += feeShares;
}

lastUpdate[id] = block.timestamp;
}

function _accruedInterests(Market calldata market, Id id)
internal
view
returns (uint256 accruedInterests, uint256 feeShares)
{
if (totalBorrow[id] != 0) {
uint256 borrowRate = market.irm.borrowRate(market);
accruedInterests = totalBorrow[id].wMul(borrowRate).wMul(block.timestamp - lastUpdate[id]);

if (fee[id] != 0) {
uint256 feeAmount = accruedInterests.wMul(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.wMul(totalSupplyShares[id]).wDiv(totalSupply[id] - feeAmount);
supplyShare[id][feeRecipient] += feeShares;
totalSupplyShares[id] += feeShares;
feeShares = feeAmount.wMul(totalSupplyShares[id]).wDiv(totalSupply[id] - feeAmount);
}
}

lastUpdate[id] = block.timestamp;
}

// Health check.
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IIrm.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ pragma solidity >=0.5.0;
import {Market} from "src/libraries/MarketLib.sol";

interface IIrm {
function borrowRate(Market calldata market) external returns (uint256);
function borrowRate(Market calldata market) external view returns (uint256);
Rubilmax marked this conversation as resolved.
Show resolved Hide resolved
}