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

Hold received SP yield when total deposited amount is low #419

Merged
merged 10 commits into from
Sep 13, 2024
Merged

Conversation

bingen
Copy link
Collaborator

@bingen bingen commented Sep 12, 2024

We track those yields in a new state var yieldGainsPending, and we assimilate them to yieldGainsOwed once the totalBoldDeposits reaches 1 BOLD (1e18).

Closes #317

It also prevents losses of yield when the SP is empty.

To make it easy to copy paste output from invariant tests and debug.
Also to be able to fix specific test cases without relying on
randomness, and make sure those ones pass.
uint256 totalBoldDepositsAfter = amount + totalBoldDepositsBefore;
if (totalBoldDepositsBefore < DECIMAL_PRECISION && totalBoldDepositsAfter >= DECIMAL_PRECISION) {
v.depositorPendingYield =
(v.pendingYield + SP_YIELD_SPLIT * v.pendingInterest / 1e18) * amount / totalBoldDepositsAfter;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we take into account the user's existing deposit, i.e. add v.boldDeposit to amount? Also v.boldYield if they're not claiming.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course we'd also have to add v.boldYield to totalBoldDepositsAfter.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this too!

uint256 yieldGainsPendingCached = yieldGainsPending;
if (yieldGainsPendingCached > 0 && totalBoldDepositsCached >= DECIMAL_PRECISION) {
_updateYieldRewardsSum(yieldGainsPendingCached, totalBoldDepositsCached);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We must do the same in withdrawFromSP(). Reason: a depositor could have enough pending yield gains to put total deposits over the threshold of DECIMAL_PRECISION. Remember that yield gains are deposited if they're not claimed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see you already fixed it, thanks!

if (totalBoldDepositsCached == 0 || _boldYield == 0) {

// 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.
Copy link
Collaborator

@danielattilasimon danielattilasimon Sep 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment is outdated now. By that, I mean that the BOLD is not missed out on, and can be obtained by later depositor.

@bingen bingen marked this pull request as ready for review September 13, 2024 09:57
@bingen bingen merged commit 70bf6d9 into main Sep 13, 2024
7 checks passed
Copy link
Collaborator

@RickGriff RickGriff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finally reviewing this - really nice, looks good!

@@ -584,11 +612,11 @@ contract StabilityPool is LiquityBase, IStabilityPool, IStabilityPoolEvents {

Snapshots memory snapshots = depositSnapshots[_depositor];

uint256 pendingSPYield = activePool.calcPendingSPYield();
uint256 pendingSPYield = activePool.calcPendingSPYield() + yieldGainsPending;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When there are pending yield gains, how are they awarded to depositors?

This function seems to calculate as if all pending gains were immediately awarded pro-rata, including pending yield gains.

But in reality I guess some pending yield gains might not be, right (depending on the SP size). We don't really know when they'll be rewarded. Still, it's fine as an estimate IMO.

Also, I guess and IIRC if there are pending yield gains and a depositor makes a large enough deposit, he receives all those gains, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Discrepancy between yieldGainsOwed and sum of individual yield gains in StabilityPool
3 participants