Skip to content

Commit

Permalink
fix: Hold yield in SP when total deposits amount is low
Browse files Browse the repository at this point in the history
  • Loading branch information
bingen committed Sep 11, 2024
1 parent 57e2cf8 commit e2615c7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
6 changes: 4 additions & 2 deletions contracts/src/ActivePool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,11 @@ contract ActivePool is IActivePool {
uint256 remainderToLPs = mintedAmount - spYield;

_boldToken.mint(address(interestRouter), remainderToLPs);
_boldToken.mint(address(stabilityPool), spYield);

stabilityPool.triggerBoldRewards(spYield);
if (spYield > 0) {
_boldToken.mint(address(stabilityPool), spYield);
stabilityPool.triggerBoldRewards(spYield);
}
}

lastAggUpdateTime = block.timestamp;
Expand Down
1 change: 1 addition & 0 deletions contracts/src/Interfaces/IStabilityPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ interface IStabilityPool is ILiquityBase, IBoldRewardsReceiver {
function getTotalBoldDeposits() external view returns (uint256);

function getYieldGainsOwed() external view returns (uint256);
function getYieldGainsPending() external view returns (uint256);

/*
* Calculates the Coll gain earned by the deposit since its last snapshots were taken.
Expand Down
23 changes: 16 additions & 7 deletions contracts/src/StabilityPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ contract StabilityPool is LiquityBase, IStabilityPool, IStabilityPoolEvents {
// TODO: from the contract's perspective, this is a write-only variable. It is only ever read in tests, so it would
// be better to keep it outside the core contract.
uint256 internal yieldGainsOwed;
// Total remaining Bold yield gains (from Trove interest mints) held by SP, not yet paid out to depositors,
// and not accounted for because they were received when the total deposits were too small
uint256 internal yieldGainsPending;

// --- Data structures ---

Expand Down Expand Up @@ -227,6 +230,10 @@ contract StabilityPool is LiquityBase, IStabilityPool, IStabilityPoolEvents {
return yieldGainsOwed;
}

function getYieldGainsPending() external view override returns (uint256) {
return yieldGainsPending;
}

// --- External Depositor Functions ---

/* provideToSP():
Expand Down Expand Up @@ -360,16 +367,18 @@ contract StabilityPool is LiquityBase, IStabilityPool, IStabilityPoolEvents {
_requireCallerIsActivePool();

uint256 totalBoldDepositsCached = totalBoldDeposits; // cached to save an SLOAD
/*
* When total deposits is 0, B is not updated. In this case, the BOLD issued can not be obtained by later
* depositors - it is missed out on, and remains in the balance of the SP.
*
*/
if (totalBoldDepositsCached == 0 || _boldYield == 0) {

assert(_boldYield > 0); // TODO: remove before deploying

// When total deposits is very small, B is not updated. In this case, the BOLD issued can not be obtained by later
// depositors - it is missed out on, and remains in the balance of the SP.
if (totalBoldDepositsCached < DECIMAL_PRECISION) {
yieldGainsPending += _boldYield;
return;
}

yieldGainsOwed += _boldYield;
yieldGainsOwed += yieldGainsPending + _boldYield;
yieldGainsPending = 0;

uint256 yieldPerUnitStaked = _computeYieldPerUnitStaked(_boldYield, totalBoldDepositsCached);

Expand Down

0 comments on commit e2615c7

Please sign in to comment.