Skip to content

Commit

Permalink
refactor: less confusing accounting in removeFromBatch()
Browse files Browse the repository at this point in the history
  • Loading branch information
danielattilasimon committed Sep 12, 2024
1 parent a349602 commit 99a446c
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions contracts/src/BorrowerOperations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1059,14 +1059,16 @@ contract BorrowerOperations is LiquityBase, AddRemoveManagers, IBorrowerOperatio
vars.trove = vars.troveManager.getLatestTroveData(_troveId);
vars.batch = vars.troveManager.getLatestBatchData(vars.batchManager);

uint256 batchFutureDebt =
vars.batch.entireDebtWithoutRedistribution - (vars.trove.entireDebt - vars.trove.redistBoldDebtGain);

TroveChange memory batchChange;
batchChange.appliedRedistBoldDebtGain = vars.trove.redistBoldDebtGain;
batchChange.appliedRedistCollGain = vars.trove.redistCollGain;
batchChange.batchAccruedManagementFee = vars.batch.accruedManagementFee;
batchChange.oldWeightedRecordedDebt = vars.batch.weightedRecordedDebt
+ (vars.trove.entireDebt - vars.trove.redistBoldDebtGain) * vars.batch.annualInterestRate;
batchChange.newWeightedRecordedDebt = vars.batch.entireDebtWithoutRedistribution * vars.batch.annualInterestRate
+ vars.trove.entireDebt * _newAnnualInterestRate;
batchChange.oldWeightedRecordedDebt = vars.batch.weightedRecordedDebt;
batchChange.newWeightedRecordedDebt =
batchFutureDebt * vars.batch.annualInterestRate + vars.trove.entireDebt * _newAnnualInterestRate;

// Apply upfront fee on premature adjustments
if (
Expand All @@ -1078,14 +1080,11 @@ contract BorrowerOperations is LiquityBase, AddRemoveManagers, IBorrowerOperatio
}

// Recalculate newWeightedRecordedDebt, now taking into account the upfront fee
batchChange.newWeightedRecordedDebt = vars.batch.entireDebtWithoutRedistribution * vars.batch.annualInterestRate
+ vars.trove.entireDebt * _newAnnualInterestRate;
batchChange.newWeightedRecordedDebt =
batchFutureDebt * vars.batch.annualInterestRate + vars.trove.entireDebt * _newAnnualInterestRate;
// Add batch fees
batchChange.oldWeightedRecordedBatchManagementFee = vars.batch.weightedRecordedBatchManagementFee
+ (vars.trove.entireDebt - batchChange.upfrontFee - vars.trove.redistBoldDebtGain)
* vars.batch.annualManagementFee;
batchChange.newWeightedRecordedBatchManagementFee =
vars.batch.entireDebtWithoutRedistribution * vars.batch.annualManagementFee;
batchChange.oldWeightedRecordedBatchManagementFee = vars.batch.weightedRecordedBatchManagementFee;
batchChange.newWeightedRecordedBatchManagementFee = batchFutureDebt * vars.batch.annualManagementFee;

activePool.mintAggInterestAndAccountForTroveChange(batchChange, vars.batchManager);

Expand Down

0 comments on commit 99a446c

Please sign in to comment.