@@ -524,7 +524,7 @@ contract LiquidityProtection is ILiquidityProtection, Utils, Owned, ReentrancyGu
524524 // calculate the amount of pool tokens required for liquidation
525525 // note that the amount is doubled since it's not possible to liquidate one reserve only
526526 Fraction memory poolRate = _poolTokenRate (pos.poolToken, pos.reserveToken);
527- uint256 poolAmount = _liquidationAmount (targetAmount, poolRate, pos.poolToken, pos.poolAmount );
527+ uint256 poolAmount = _liquidationAmount (targetAmount, poolRate, pos.poolToken);
528528
529529 // calculate the base token amount received by liquidating the pool tokens
530530 // note that the amount is divided by 2 since the pool amount represents both reserves
@@ -607,7 +607,7 @@ contract LiquidityProtection is ILiquidityProtection, Utils, Owned, ReentrancyGu
607607 // calculate the amount of pool tokens required for liquidation
608608 // note that the amount is doubled since it's not possible to liquidate one reserve only
609609 Fraction memory poolRate = _poolTokenRate (removedPos.poolToken, removedPos.reserveToken);
610- uint256 poolAmount = _liquidationAmount (targetAmount, poolRate, removedPos.poolToken, 0 );
610+ uint256 poolAmount = _liquidationAmount (targetAmount, poolRate, removedPos.poolToken);
611611
612612 // withdraw the pool tokens from the wallet
613613 _withdrawPoolTokens (removedPos.poolToken, poolAmount);
@@ -1327,20 +1327,20 @@ contract LiquidityProtection is ILiquidityProtection, Utils, Owned, ReentrancyGu
13271327 function _liquidationAmount (
13281328 uint256 targetAmount ,
13291329 Fraction memory poolRate ,
1330- IDSToken poolToken ,
1331- uint256 additionalAmount
1330+ IDSToken poolToken
13321331 ) private view returns (uint256 ) {
13331332 // note that the amount is doubled since it's not possible to liquidate one reserve only
13341333 uint256 poolAmount = _mulDivF (targetAmount, poolRate.d.mul (2 ), poolRate.n);
13351334 // limit the amount of pool tokens by the amount the system/caller holds
1336- return Math.min (poolAmount, _systemStore. systemBalance ( poolToken). add (additionalAmount ));
1335+ return Math.min (poolAmount, poolToken. balanceOf ( address (_wallet) ));
13371336 }
13381337
13391338 /**
13401339 * @dev withdraw pool tokens from the wallet
13411340 */
13421341 function _withdrawPoolTokens (IDSToken poolToken , uint256 poolAmount ) private {
1343- _systemStore.decSystemBalance (poolToken, poolAmount);
1342+ uint256 systemBalance = _systemStore.systemBalance (poolToken);
1343+ _systemStore.decSystemBalance (poolToken, Math.min (poolAmount, systemBalance));
13441344 _wallet.withdrawTokens (IReserveToken (address (poolToken)), address (this ), poolAmount);
13451345 }
13461346
0 commit comments