Skip to content

Commit

Permalink
Fixed the issue with open short (#1199)
Browse files Browse the repository at this point in the history
* Added a fix for the open short insolvency

* Added some testing and modified the fix slightly

* Fixed the remaining tests
  • Loading branch information
jalextowle authored Oct 24, 2024
1 parent 0252ea4 commit a78e12c
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 11 deletions.
11 changes: 11 additions & 0 deletions contracts/src/internal/HyperdriveShort.sol
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ abstract contract HyperdriveShort is IHyperdriveEvents, HyperdriveLP {
block.timestamp,
spotPrice
);

// Adjust the base deposit to ensure that we are overestimating the
// amount of base that needs to be deposited to provide the required
// amount of vault shares for the pool to remain solvent.
//
// NOTE: We round up and add 1 wei to the base deposit to ensure
// that the deposit in shares is greater than or equal to the amount
// required to maintain solvency.
baseDeposit =
_convertToBase(baseDeposit.divUp(vaultSharePrice)) +
1;
}

// Take custody of the trader's deposit and ensure that the trader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ contract MorphoBlue_USDe_DAI_HyperdriveTest is
roundTripLongMaturityWithBaseUpperBoundTolerance: 1e3,
roundTripLongMaturityWithBaseTolerance: 1e5,
roundTripShortInstantaneousWithBaseUpperBoundTolerance: 1e3,
roundTripShortInstantaneousWithBaseTolerance: 1e5,
roundTripShortInstantaneousWithBaseTolerance: 1e7,
roundTripShortMaturityWithBaseTolerance: 1e10,
// NOTE: Share deposits and withdrawals are disabled, so these are
// 0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ contract MorphoBlue_sUSDe_DAI_HyperdriveTest is
roundTripLongMaturityWithBaseUpperBoundTolerance: 1e3,
roundTripLongMaturityWithBaseTolerance: 1e5,
roundTripShortInstantaneousWithBaseUpperBoundTolerance: 1e3,
roundTripShortInstantaneousWithBaseTolerance: 1e5,
roundTripShortInstantaneousWithBaseTolerance: 1e8,
roundTripShortMaturityWithBaseTolerance: 1e10,
// NOTE: Share deposits and withdrawals are disabled, so these are
// 0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ contract MorphoBlue_wstETH_USDA_HyperdriveTest is
roundTripLongMaturityWithBaseUpperBoundTolerance: 1e3,
roundTripLongMaturityWithBaseTolerance: 1e5,
roundTripShortInstantaneousWithBaseUpperBoundTolerance: 1e3,
roundTripShortInstantaneousWithBaseTolerance: 1e5,
roundTripShortInstantaneousWithBaseTolerance: 1e8,
roundTripShortMaturityWithBaseTolerance: 1e10,
// NOTE: Share deposits and withdrawals are disabled, so these are
// 0.
Expand Down
2 changes: 1 addition & 1 deletion test/integrations/hyperdrive/NonstandardDecimals.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ contract NonstandardDecimalsTest is HyperdriveTest {
(, uint256 shortBasePaid) = openShort(celine, bondAmount);

// Ensure that the long and short fixed interest are equal.
assertApproxEqAbs(bondAmount - longBasePaid, shortBasePaid, 3);
assertApproxEqAbs(bondAmount - longBasePaid, shortBasePaid, 4);
}

function test_nonstandard_decimals_longs_outstanding() external {
Expand Down
10 changes: 6 additions & 4 deletions test/units/hyperdrive/AddLiquidityTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,12 @@ contract AddLiquidityTest is HyperdriveTest {

// Ensure that all of the capital except for the minimum share reserves
// and the zero address's LP present value was removed from the system.
assertEq(
assertApproxEqAbs(
baseToken.balanceOf(address(hyperdrive)),
(ONE + hyperdrive.lpSharePrice()).mulDown(
hyperdrive.getPoolConfig().minimumShareReserves
)
),
1
);
}

Expand Down Expand Up @@ -502,11 +503,12 @@ contract AddLiquidityTest is HyperdriveTest {

// Ensure that all of the capital except for the minimum share reserves
// and the zero address's LP present value was removed from the system.
assertEq(
assertApproxEqAbs(
baseToken.balanceOf(address(hyperdrive)),
(ONE + hyperdrive.lpSharePrice()).mulDown(
hyperdrive.getPoolConfig().minimumShareReserves
)
),
1
);
}

Expand Down
7 changes: 4 additions & 3 deletions test/units/hyperdrive/OpenShortTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -488,10 +488,11 @@ contract OpenShortTest is HyperdriveTest {
IHyperdrive.PoolInfo memory poolInfoAfter = hyperdrive.getPoolInfo();

{
assertEq(
assertApproxEqAbs(
poolInfoAfter.shareReserves,
poolInfoBefore.shareReserves -
baseProceeds.divDown(poolInfoBefore.vaultSharePrice)
baseProceeds.divDown(poolInfoBefore.vaultSharePrice),
1
);
assertEq(
poolInfoAfter.vaultSharePrice,
Expand Down Expand Up @@ -573,7 +574,7 @@ contract OpenShortTest is HyperdriveTest {
hyperdrive.getPoolInfo().vaultSharePrice
);
assertEq(eventAsBase, true);
assertEq(eventBaseProceeds, shortAmount - basePaid);
assertApproxEqAbs(eventBaseProceeds, shortAmount - basePaid, 1);
assertEq(eventBondAmount, shortAmount);
}
}
14 changes: 14 additions & 0 deletions test/utils/InstanceTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2673,6 +2673,20 @@ abstract contract InstanceTest is HyperdriveTest {
);
(uint256 maturityTime, uint256 basePaid) = openShort(bob, _shortAmount);

// Ensure that the pool has more shares than expected. This verifies
// that the pool is solvent.
assertGe(
hyperdrive.totalShares(),
hyperdrive.getPoolInfo().shareReserves +
(hyperdrive.getPoolInfo().shortsOutstanding +
hyperdrive.getPoolInfo().shortsOutstanding.mulDown(
hyperdrive.getPoolConfig().fees.flat
)).divDown(hyperdrive.getPoolInfo().vaultSharePrice) +
hyperdrive.getUncollectedGovernanceFees() +
hyperdrive.getPoolInfo().withdrawalSharesProceeds +
hyperdrive.getPoolInfo().zombieShareReserves
);

// Get some balance information before the withdrawal.
(
uint256 totalSupplyAssetsBefore,
Expand Down

0 comments on commit a78e12c

Please sign in to comment.