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

Apply and mint interest #76

Closed
wants to merge 15 commits into from
Closed

Apply and mint interest #76

wants to merge 15 commits into from

Conversation

RickGriff
Copy link
Collaborator

@RickGriff RickGriff commented Feb 29, 2024

We now have 3 aggregate trackers on the ActivePool, as per the simple compounding specification:

aggRecordedDebt (D)
recordedDebtSum (G)
weightedRecordedDebtSum (S)

G is the original ActivePool debt tracker from Liquity v1. We keep it for now as a) it may be useful for computing a size-weighted average interest rate on the front end, and b) would be needed if we decide to add an emergency base interest rate to every position.

Pseudocode for aggregate trackers in different operations

As in v1, we always apply any redistribution debt gain before handling interest accounting.
The redistribution gain is already included in G before we alter G from the specific operation.

The prefix “old” means the respective debt with no redistribution gains or interest applied, and “new” implies all debt changes (redist. gains, interest, debt drawn/repaid) are included.

openTrove:

D += agg_accrued_interest + trove_debt
S += weighted_trove_debt 
G += trove_debt

closeTrove:

D += (agg_accrued_interest - old_recorded_trove_debt - accrued_trove_interest) // Don’t remove redist. gain here since it was not already applied to D
S -= old_weighted_trove_debt
G -= (old_recorded_trove_debt + redistribution_gain)  // remove redist.gain here since it was already applied to G.

adjustTrove:

D+= agg_accrued_interest +trove_debt_change + redist_debt_gain
S+= (new_weighted_trove_debt - old_weighted_trove_debt)
G+= (new_recorded_trove_debt - old_recorded_trove_debt)

And for the individual Trove:
new_recorded_trove_debt = old_recorded_trove_debt + accrued_trove_interest + trove_debt_change

applyTroveInterestPermissionless:

D += agg_accrued_interest
S += (new_weighted_trove_debt - old_weighted_trove_debt)
G += (new_recorded_trove_debt - old_recorded_trove_debt)

adjustTroveInterestRate:

D+= agg_accrued_interest
S += (new_weighted_trove_debt - old_weighted_trove_debt)
G += (new_recorded_trove_debt - old_recorded_trove_debt)

SP deposit/withdrawal:
D += agg_accrued_interest

Liquidation - Normal Mode, offset

This is effectively the same as closing the Trove:

D+= agg_accrued_interest - old_recorded_trove_debt - accrued_trove_interest
S -= old_weighted_trove_debt
G -= (old_recorded_trove_debt + redist_debt_gain)

Liquidation - Normal Mode, redistribution:

This is effectively the same as closing the Trove, except we add the entire debt to the DefaultPool:

D+= (agg_accrued_interest - old_recorded_trove_debt - accrued_trove_interest)
S -= old_weighted_trove_debt
G -= (old_recorded_trove_debt + redist_debt_gain) 

Also:

DefaultPool.debt += (old_recorded_trove_debt + redistribution_gain + accrued_trove_interest)

TODOs:

** Liquidations in Recovery Mode**

Minting/applying interest has been implemented for RM liquidations to make tests pass, but if we keep RM we should double-check that minting/applying interest is correct for for every logical case of RM.

Redemption:

This is effectively a downward debt adjustment (since we will no longer close Troves on redemption):

D+= agg_accrued_interest - redeemed_debt + redist_debt_gain
S+= (new_weighted_recorded_debt - old_weighted_recorded_debt)
G+= (new_recorded_trove_debt - old_recorded_trove_debt)

This will be implemented in combination with #55 and #48, since these significantly change redemption logic anyway.

Testing TODOs:

  • more thorough invariant test (i.e. D + S*delta_t == sum_individual_entire_trove_debts)

@RickGriff RickGriff marked this pull request as ready for review March 1, 2024 08:12
@liquity liquity deleted a comment from vercel bot Mar 11, 2024
@RickGriff RickGriff marked this pull request as draft March 15, 2024 15:05
@RickGriff RickGriff marked this pull request as ready for review March 27, 2024 08:38
@bingen bingen mentioned this pull request Mar 27, 2024
@bingen
Copy link
Collaborator

bingen commented Apr 3, 2024

Closed in favour of #85

@bingen bingen closed this Apr 3, 2024
@danielattilasimon danielattilasimon deleted the apply_and_mint_interest branch April 12, 2024 10:06
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.

2 participants