Skip to content

Commit 12e2927

Browse files
author
barak manos
committed
Improve coding
1 parent 83cf455 commit 12e2927

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

contracts/liquidity-protection/LiquidityProtection.sol

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -718,12 +718,15 @@ contract LiquidityProtection is ILiquidityProtection, Utils, Owned, ReentrancyGu
718718
// deduct the position IL from the target amount
719719
targetAmount = _deductIL(Math.max(reserveAmount, targetAmount), loss);
720720

721-
// get the pool deficit state
722-
(uint256 protectedLiquidity, uint256 totalValue) = _poolDeficitState(poolToken);
721+
// get the pool deficit
722+
Fraction memory poolDeficit = _poolDeficit(poolToken);
723+
724+
// calculate the missing portion
725+
Fraction memory missingPortion = Fraction({ n: poolDeficit.n - poolDeficit.d, d: poolDeficit.d});
723726

724727
// return the amount the provider will receive for removing liquidity
725728
// as well as the specific position value (before deficit reduction
726-
return (_mulDivF(targetAmount, protectedLiquidity, totalValue), targetAmount);
729+
return (_mulDivF(targetAmount, missingPortion.n, missingPortion.d), targetAmount);
727730
}
728731

729732
/**
@@ -735,17 +738,17 @@ contract LiquidityProtection is ILiquidityProtection, Utils, Owned, ReentrancyGu
735738
view
736739
returns (uint256)
737740
{
738-
(uint256 protectedLiquidity, uint256 totalValue) = _poolDeficitState(poolToken);
739-
return _mulDivF(PPM_RESOLUTION, totalValue - protectedLiquidity, totalValue);
741+
Fraction memory poolDeficit = _poolDeficit(poolToken);
742+
return _mulDivF(PPM_RESOLUTION, poolDeficit.n, poolDeficit.d);
740743
}
741744

742745
/**
743746
* @dev returns the protected liquidity amount and the total positions value
744747
*/
745-
function _poolDeficitState(IDSToken poolToken)
748+
function _poolDeficit(IDSToken poolToken)
746749
private
747750
view
748-
returns (uint256, uint256)
751+
returns (Fraction memory)
749752
{
750753
// get the converter balance
751754
IConverter converter = IConverter(payable(_ownedBy(poolToken)));
@@ -762,7 +765,10 @@ contract LiquidityProtection is ILiquidityProtection, Utils, Owned, ReentrancyGu
762765

763766
// the pool is in deficit if and only if
764767
// the protected liquidity amount is lower than the total positions value
765-
return (Math.min(protectedLiquidity, totalValue), totalValue);
768+
return Fraction({
769+
n: totalValue - Math.min(protectedLiquidity, totalValue),
770+
d: totalValue
771+
});
766772
}
767773

768774
/**

0 commit comments

Comments
 (0)