@@ -684,12 +684,17 @@ contract LiquidityProtection is ILiquidityProtection, Utils, Owned, ReentrancyGu
684684 // get the pool deficit
685685 Fraction memory poolDeficit = _poolDeficit (poolToken);
686686
687- // calculate the missing portion
688- Fraction memory missingPortion = Fraction ({ n: poolDeficit.n - poolDeficit.d, d: poolDeficit.d});
687+ // no deficit
688+ if (poolDeficit.d == 0 ) {
689+ return (targetAmount, targetAmount);
690+ }
691+
692+ // calculate the available liquidity portion
693+ Fraction memory availablePortion = Fraction ({ n: poolDeficit.d - poolDeficit.n, d: poolDeficit.d});
689694
690695 // return the amount the provider will receive for removing liquidity
691- // as well as the specific position value (before deficit reduction
692- return (_mulDivF (targetAmount, missingPortion .n, missingPortion .d), targetAmount);
696+ // as well as the specific position value (before deficit reduction)
697+ return (_mulDivF (targetAmount, availablePortion .n, availablePortion .d), targetAmount);
693698 }
694699
695700 /**
@@ -702,11 +707,19 @@ contract LiquidityProtection is ILiquidityProtection, Utils, Owned, ReentrancyGu
702707 returns (uint256 )
703708 {
704709 Fraction memory poolDeficit = _poolDeficit (poolToken);
710+
711+ // no deficit
712+ if (poolDeficit.d == 0 ) {
713+ return 0 ;
714+ }
715+
705716 return _mulDivF (PPM_RESOLUTION, poolDeficit.n, poolDeficit.d);
706717 }
707718
708719 /**
709- * @dev returns the protected liquidity amount and the total positions value
720+ * @dev returns the pool deficit based on the total protected amount vs. total
721+ * positions value, as a fraction.
722+ * note that 0/0 is returned if the pool is not in deficit
710723 */
711724 function _poolDeficit (IDSToken poolToken )
712725 private
@@ -726,10 +739,15 @@ contract LiquidityProtection is ILiquidityProtection, Utils, Owned, ReentrancyGu
726739 // get the total positions value
727740 uint256 totalValue = totalPositionsValue (poolToken);
728741
729- // the pool is in deficit if and only if
730- // the protected liquidity amount is lower than the total positions value
742+ // if the protected liquidity is equal or greater than the total value,
743+ // the pool is not in deficit
744+ if (protectedLiquidity >= totalValue) {
745+ return Fraction ({ n: 0 , d: 0 });
746+ }
747+
748+ // the pool is in deficit
731749 return Fraction ({
732- n: totalValue - Math. min (protectedLiquidity, totalValue ),
750+ n: totalValue. sub (protectedLiquidity),
733751 d: totalValue
734752 });
735753 }
0 commit comments