Skip to content

Commit 19a5017

Browse files
committed
liquidity protection - added remove liqudiity test
1 parent 0a6c27b commit 19a5017

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

contracts/liquidity-protection/LiquidityProtection.sol

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

test/liquidity-protection/LiquidityProtection.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,21 @@ describe('LiquidityProtection', () => {
10951095
expect(amount).to.equal(reserveAmount);
10961096
});
10971097

1098+
it('verifies that removeLiquidityReturn returns the correct amount when the pool is in deficit', async () => {
1099+
const reserveAmount = BigNumber.from(1000);
1100+
await addProtectedLiquidity(poolToken.address, baseToken, baseTokenAddress, reserveAmount);
1101+
const protectionIds = await liquidityProtectionStore.protectedLiquidityIds(owner.address);
1102+
const protectionId = protectionIds[0];
1103+
1104+
await liquidityProtection.setTotalPositionsValue(poolToken.address, reserveAmount.mul(100).div(80));
1105+
1106+
const amount = (
1107+
await liquidityProtection.removeLiquidityReturn(protectionId, PPM_RESOLUTION, now)
1108+
)[0];
1109+
1110+
expect(amount).to.equal(reserveAmount.mul(80).div(100));
1111+
});
1112+
10981113
it('verifies that removeLiquidityReturn returns the correct amount for removing a portion of a protection', async () => {
10991114
const reserveAmount = BigNumber.from(1000);
11001115
await addProtectedLiquidity(poolToken.address, baseToken, baseTokenAddress, reserveAmount);

0 commit comments

Comments
 (0)