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

fix: double interest on pending redistribution #151

Merged
merged 2 commits into from
May 6, 2024

Conversation

danielattilasimon
Copy link
Collaborator

@danielattilasimon danielattilasimon commented May 3, 2024

Addresses a bug that was found while reading the code.

The failing scenario was (implemented as test case testNoDoubleInterestOnPendingRedistribution):

  1. A, B, C, D each open Troves having 10'000 BOLD debt, 100% interest rate p.a.
  2. 1 year passes.
  3. A, B, C get liquidated through redistribution.
  4. D changes their interest rate to 10%.

The logs without the fix:

Logs:
  Trove D debt (initial)  : 10000.000000000000000000
  Trove D debt (post-1y)  : 20000.000000000000000000
  Trove D debt (post-liq) : 80000.000000000000000000
  Trove D debt (post-adj) : 140000.000000000000000000

Everything looks good up until immediately post-liquidation. Each Trove's debt doubles as expected, 3 of them get redistributed to D, thus D's debt is 80'000 BOLD.

But then, when D adjusts their interest rate, their debt immediately increases by a whopping 60K! This is an invariant violation; a Trove's debt should not be changed by adjusting the interest rate, unless time also passes inbetween.

The root cause is that we apply the redistribution first — which involves calculating the interest on the debt that's being redistributed — and then perform interest rate calculation on the new debt — which includes debt to which interest has already been added.

uint256 accruedTroveInterest = _troveManager.calcTroveAccruedInterest(_troveId);
(, uint256 redistDebtGain) = _troveManager.getAndApplyRedistributionGains(_troveId);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is the actual fix. Every other change is just auto-formatting.

Copy link
Collaborator

@bingen bingen left a comment

Choose a reason for hiding this comment

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

LGTM! Good find, thanks!

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.

Looks good!

@danielattilasimon danielattilasimon merged commit 96cc851 into main May 6, 2024
7 checks passed
@danielattilasimon danielattilasimon deleted the bug-double-interest branch May 6, 2024 08:04
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.

3 participants