Skip to content

Commit

Permalink
fix: do not overwrite premia 1 before passing to ct1.exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
dyedm1 committed Nov 22, 2023
1 parent e8813e8 commit 8acdd38
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 31 deletions.
42 changes: 20 additions & 22 deletions contracts/PanopticPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/*//////////////////////////////////////////////////////////////
Expand Down
23 changes: 14 additions & 9 deletions test/foundry/core/PanopticPool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand All @@ -5005,7 +5004,8 @@ contract PanopticPoolTest is PositionUtils {
assertApproxEqAbs(
inAMM,
uint128(longAmounts.rightSlot() + shortAmounts.rightSlot()) * 2,
10
10,
"Incorrect token0 inAMM amount"
);
}

Expand All @@ -5014,7 +5014,8 @@ contract PanopticPoolTest is PositionUtils {
assertApproxEqAbs(
inAMM,
uint128(longAmounts.leftSlot() + shortAmounts.leftSlot()) * 2,
10
10,
"Incorrect token1 inAMM amount"
);
}
{
Expand Down Expand Up @@ -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"
);
}
}
Expand Down

0 comments on commit 8acdd38

Please sign in to comment.