From 8acdd38f7cc26f6be6bdf9f8a4491b4ace783340 Mon Sep 17 00:00:00 2001 From: dyedm1 Date: Wed, 22 Nov 2023 14:56:31 -0500 Subject: [PATCH] fix: do not overwrite premia 1 before passing to ct1.exercise --- contracts/PanopticPool.sol | 42 +++++++++++++--------------- test/foundry/core/PanopticPool.t.sol | 23 +++++++++------ 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/contracts/PanopticPool.sol b/contracts/PanopticPool.sol index d4e0fd7..e2b0e7f 100644 --- a/contracts/PanopticPool.sol +++ b/contracts/PanopticPool.sol @@ -1171,28 +1171,26 @@ contract PanopticPool is ERC1155Holder, Multicall { ); // exercise the option and take the commission and addData - { - (int256 paidAmount0, int128 realizedPremium0) = s_collateralToken0.exercise( - owner, - longAmounts.rightSlot(), - shortAmounts.rightSlot(), - totalSwapped.rightSlot(), - currentPositionPremia.rightSlot() - ); - currentPositionPremia = int256(realizedPremium0); - paidAmounts = paidAmount0.toInt128(); - } - { - (int256 paidAmount1, int128 realizedPremium1) = s_collateralToken1.exercise( - owner, - longAmounts.leftSlot(), - shortAmounts.leftSlot(), - totalSwapped.leftSlot(), - currentPositionPremia.leftSlot() - ); - currentPositionPremia = currentPositionPremia.toLeftSlot(realizedPremium1); - paidAmounts = paidAmounts.toLeftSlot(paidAmount1.toInt128()); - } + int256 realizedPremium0; + + (paidAmounts, realizedPremium0) = s_collateralToken0.exercise( + owner, + longAmounts.rightSlot(), + shortAmounts.rightSlot(), + totalSwapped.rightSlot(), + currentPositionPremia.rightSlot() + ); + + (int256 paidAmount1, int128 realizedPremium1) = s_collateralToken1.exercise( + owner, + longAmounts.leftSlot(), + shortAmounts.leftSlot(), + totalSwapped.leftSlot(), + currentPositionPremia.leftSlot() + ); + + currentPositionPremia = realizedPremium0.toLeftSlot(realizedPremium1); + paidAmounts = paidAmounts.toLeftSlot(paidAmount1.toInt128()); } /*////////////////////////////////////////////////////////////// diff --git a/test/foundry/core/PanopticPool.t.sol b/test/foundry/core/PanopticPool.t.sol index 0ef7642..cbc2a0d 100644 --- a/test/foundry/core/PanopticPool.t.sol +++ b/test/foundry/core/PanopticPool.t.sol @@ -3596,9 +3596,6 @@ contract PanopticPoolTest is PositionUtils { ? (notionalVals[0] * tickSpacing) / 10_000 : -((notionalVals[0] * tickSpacing) / 10_000); - console2.log(tokensOwed0, tokensOwed1); - console2.log(expectedSwaps[0], expectedSwaps[1]); - assertApproxEqAbs( balanceBefores[0], uint256( @@ -4988,14 +4985,16 @@ contract PanopticPoolTest is PositionUtils { int256(uint256(type(uint104).max)) - (exerciseFeeAmounts[0] < 0 ? -1 : int8(1)) * int256(ct0.convertToShares(uint256(Math.abs(exerciseFeeAmounts[0])))), - 10 + 10, + "Incorrect Exercise Fees 0 for Bob" ); assertApproxEqAbs( int256(ct1.balanceOf(Bob)), int256(uint256(type(uint104).max)) - (exerciseFeeAmounts[1] < 0 ? -1 : int8(1)) * int256(ct1.convertToShares(uint256(Math.abs(exerciseFeeAmounts[1])))), - 10 + 10, + "Incorrect Exercise Fees 1 for Bob" ); assertEq(sfpm.balanceOf(address(pp), tokenId), 0); @@ -5005,7 +5004,8 @@ contract PanopticPoolTest is PositionUtils { assertApproxEqAbs( inAMM, uint128(longAmounts.rightSlot() + shortAmounts.rightSlot()) * 2, - 10 + 10, + "Incorrect token0 inAMM amount" ); } @@ -5014,7 +5014,8 @@ contract PanopticPoolTest is PositionUtils { assertApproxEqAbs( inAMM, uint128(longAmounts.leftSlot() + shortAmounts.leftSlot()) * 2, - 10 + 10, + "Incorrect token1 inAMM amount" ); } { @@ -5048,12 +5049,16 @@ contract PanopticPoolTest is PositionUtils { uint256(int256(lastCollateralBalance0[Alice]) + $balanceDelta0), uint256( int256((longAmounts.rightSlot() + shortAmounts.rightSlot()) / 1_000_000 + 10) - ) + ), + "Incorrect Collateral 0 Balance for Alice" ); assertApproxEqAbs( ct1.convertToAssets(ct1.balanceOf(Alice)), uint256(int256(lastCollateralBalance1[Alice]) + $balanceDelta1), - uint256(int256((longAmounts.leftSlot() + shortAmounts.leftSlot()) / 1_000_000 + 10)) + uint256( + int256((longAmounts.leftSlot() + shortAmounts.leftSlot()) / 1_000_000 + 10) + ), + "Incorrect Collateral 1 Balance for Alice" ); } }