From 13abd601ab161c5fb532b104fb542f7702e696ec Mon Sep 17 00:00:00 2001 From: Daniel Simon Date: Sun, 25 Aug 2024 11:40:49 +0700 Subject: [PATCH] fix: underflow when closing Trove that has pending batch management fees The issue occured when all the remaining Troves had debt from nothing but pending redistributions. This can happen if there's a liquidation by redistribution following a redemption that turns many Troves into zero-debt zombies. Fixes #361. --- contracts/src/ActivePool.sol | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/contracts/src/ActivePool.sol b/contracts/src/ActivePool.sol index ae71b3798..2d6658780 100644 --- a/contracts/src/ActivePool.sol +++ b/contracts/src/ActivePool.sol @@ -222,6 +222,11 @@ contract ActivePool is IActivePool { { _requireCallerIsBOorTroveM(); + // Batch management fees + if (_batchAddress != address(0)) { + _mintBatchManagementFeeAndAccountForChange(boldToken, _troveChange, _batchAddress); + } + // Do the arithmetic in 2 steps here to avoid overflow from the decrease uint256 newAggRecordedDebt = aggRecordedDebt; // 1 SLOAD newAggRecordedDebt += _mintAggInterest(boldToken, _troveChange.upfrontFee); // adds minted agg. interest + upfront fee @@ -238,11 +243,6 @@ contract ActivePool is IActivePool { newAggWeightedDebtSum += _troveChange.newWeightedRecordedDebt; newAggWeightedDebtSum -= _troveChange.oldWeightedRecordedDebt; aggWeightedDebtSum = newAggWeightedDebtSum; // 1 SSTORE - - // Batch management fees - if (_batchAddress != address(0)) { - _mintBatchManagementFeeAndAccountForChange(boldToken, _troveChange, _batchAddress); - } } function mintAggInterest() external override {