Skip to content

Commit

Permalink
fix: Update SP pending yields if deposit makes it reach threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
bingen committed Sep 12, 2024
1 parent e2615c7 commit c5be546
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions contracts/src/StabilityPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,16 @@ contract StabilityPool is LiquityBase, IStabilityPool, IStabilityPoolEvents {

_updateDepositAndSnapshots(msg.sender, newDeposit, newStashedColl);
boldToken.sendToPool(msg.sender, address(this), _topUp);
_updateTotalBoldDeposits(_topUp + keptYieldGain, 0);
uint256 totalBoldDepositsCached = _updateTotalBoldDeposits(_topUp + keptYieldGain, 0);
_decreaseYieldGainsOwed(currentYieldGain);
_sendBoldtoDepositor(msg.sender, yieldGainToSend);
_sendCollGainToDepositor(collToSend);

// If there were pending yields and with the new deposit we are reaching the threshold, let’s move the yield to owed
uint256 yieldGainsPendingCached = yieldGainsPending;
if (yieldGainsPendingCached > 0 && totalBoldDepositsCached >= DECIMAL_PRECISION) {
_updateYieldRewardsSum(yieldGainsPendingCached, totalBoldDepositsCached);
}
}

function _getYieldToKeepOrSend(uint256 _currentYieldGain, bool _doClaim) internal pure returns (uint256, uint256) {
Expand Down Expand Up @@ -365,22 +371,27 @@ contract StabilityPool is LiquityBase, IStabilityPool, IStabilityPoolEvents {

function triggerBoldRewards(uint256 _boldYield) external {
_requireCallerIsActivePool();
assert(_boldYield > 0); // TODO: remove before deploying

uint256 totalBoldDepositsCached = totalBoldDeposits; // cached to save an SLOAD

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 += yieldGainsPending + _boldYield;
_updateYieldRewardsSum(yieldGainsPending + _boldYield, totalBoldDepositsCached);
}

function _updateYieldRewardsSum(uint256 _accumulatedYield, uint256 _totalBoldDeposits) internal {
assert(_accumulatedYield > 0); // TODO: remove before deploying

yieldGainsOwed += _accumulatedYield;
yieldGainsPending = 0;

uint256 yieldPerUnitStaked = _computeYieldPerUnitStaked(_boldYield, totalBoldDepositsCached);
uint256 yieldPerUnitStaked = _computeYieldPerUnitStaked(_accumulatedYield, _totalBoldDeposits);

uint256 marginalYieldGain = yieldPerUnitStaked * P;
epochToScaleToB[currentEpoch][currentScale] = epochToScaleToB[currentEpoch][currentScale] + marginalYieldGain;
Expand Down Expand Up @@ -544,11 +555,13 @@ contract StabilityPool is LiquityBase, IStabilityPool, IStabilityPoolEvents {
emit StabilityPoolCollBalanceUpdated(newCollBalance);
}

function _updateTotalBoldDeposits(uint256 _depositIncrease, uint256 _depositDecrease) internal {
if (_depositIncrease == 0 && _depositDecrease == 0) return;
function _updateTotalBoldDeposits(uint256 _depositIncrease, uint256 _depositDecrease) internal returns (uint256) {
if (_depositIncrease == 0 && _depositDecrease == 0) return totalBoldDeposits;
uint256 newTotalBoldDeposits = totalBoldDeposits + _depositIncrease - _depositDecrease;
totalBoldDeposits = newTotalBoldDeposits;
emit StabilityPoolBoldBalanceUpdated(newTotalBoldDeposits);

return newTotalBoldDeposits;
}

function _decreaseYieldGainsOwed(uint256 _amount) internal {
Expand Down

0 comments on commit c5be546

Please sign in to comment.