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
Changes from 2 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
54 changes: 36 additions & 18 deletions src/Blue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,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 @@ -121,7 +121,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][msg.sender] = WAD;
Expand All @@ -142,7 +142,7 @@ contract Blue {
require(lastUpdate[id] != 0, "unknown market");
require(amount != 0, "zero amount");

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

uint256 shares = amount.wMul(totalSupplyShares[id]).wDiv(totalSupply[id]);
supplyShare[id][msg.sender] -= shares;
Expand All @@ -162,7 +162,7 @@ contract Blue {
require(lastUpdate[id] != 0, "unknown market");
require(amount != 0, "zero amount");

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

if (totalBorrow[id] == 0) {
borrowShare[id][msg.sender] = WAD;
Expand All @@ -186,7 +186,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][msg.sender] -= shares;
Expand Down Expand Up @@ -217,7 +217,7 @@ contract Blue {
require(lastUpdate[id] != 0, "unknown market");
require(amount != 0, "zero amount");

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

collateral[id][msg.sender] -= amount;

Expand All @@ -233,7 +233,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 @@ -264,25 +264,43 @@ contract Blue {

// Interests management.

function accrueInterests(Market calldata market, Id id) private {
uint256 marketTotalBorrow = totalBorrow[id];
function accrued(Market calldata market) external returns (uint256 accruedInterests, uint256 feeShares) {
Id id = market.toId();
require(lastUpdate[id] != 0, "unknown market");
Rubilmax marked this conversation as resolved.
Show resolved Hide resolved

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
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