Skip to content

Commit

Permalink
test: stage
Browse files Browse the repository at this point in the history
  • Loading branch information
dyedm1 committed Nov 27, 2023
1 parent c622124 commit 7222205
Showing 1 changed file with 171 additions and 0 deletions.
171 changes: 171 additions & 0 deletions test/foundry/core/CollateralTracker.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3762,6 +3762,7 @@ contract CollateralTrackerTest is Test, PositionUtils {
int24 atTick,
uint256 swapSizeSeed
) public {
vm.assume(pool != USDC_WETH_100);
vm.assume(strikeSeed != strikeSeed2);

{
Expand Down Expand Up @@ -3921,6 +3922,176 @@ contract CollateralTrackerTest is Test, PositionUtils {
}
}

function test_Success_collateralCheck_ITMcallSpread_assetTT0(
uint256 x,
uint128 positionSizeSeed,
uint256 widthSeed,
uint256 widthSeed2,
int256 strikeSeed,
int256 strikeSeed2,
int24 atTick,
uint256 swapSizeSeed
) public {
vm.assume(strikeSeed != strikeSeed2);

uint128 required;

{
_initWorld(x);
vm.assume(pool != USDC_WETH_100);

// initalize a custom Panoptic pool
_deployCustomPanopticPool(token0, token1, pool);

// Invoke all interactions with the Collateral Tracker from user Bob
vm.startPrank(Bob);

// give Bob the max amount of tokens
_grantTokens(Bob);

// approve collateral tracker to move tokens on Bob's behalf
IERC20Partial(token0).approve(address(collateralToken0), type(uint128).max);
IERC20Partial(token1).approve(address(collateralToken1), type(uint128).max);

// award corresponding shares
_mockMaxDeposit(Bob);

// have Bob sell
(width, strike) = PositionUtils.getITMSW(
widthSeed,
strikeSeed,
uint24(tickSpacing),
currentTick,
0
);

(width1, strike1) = PositionUtils.getITMSW(
widthSeed2,
strikeSeed2,
uint24(tickSpacing),
currentTick,
0
);

tokenId = uint256(0).addUniv3pool(poolId).addLeg(0, 1, 0, 0, 0, 0, strike, width);
tokenId = tokenId.addLeg(1, 1, 0, 0, 0, 1, strike1, width1);
positionIdList.push(tokenId);

positionSize0 = uint128(bound(positionSizeSeed, 2, 2 ** 104));
_assumePositionValidity(Bob, tokenId, positionSize0);
_spreadTokensRequired(tokenId1, positionSize0);

panopticPool.mintOptions(
positionIdList,
positionSize0,
type(uint64).max,
TickMath.MIN_TICK,
TickMath.MAX_TICK
);
}

{
// Alice buys
changePrank(Alice);

// give Bob the max amount of tokens
_grantTokens(Alice);

// approve collateral tracker to move tokens on Bob's behalf
IERC20Partial(token0).approve(address(collateralToken0), type(uint128).max);
IERC20Partial(token1).approve(address(collateralToken1), type(uint128).max);

// award corresponding shares
_mockMaxDeposit(Alice);

tokenId1 = uint256(0).addUniv3pool(poolId).addLeg(0, 1, 0, 1, 0, 1, strike, width);
tokenId1 = tokenId1.addLeg(1, 1, 0, 0, 0, 0, strike1, width1);
positionIdList1.push(tokenId1);

_assumePositionValidity(Alice, tokenId1, positionSize0 / 2);
required = _spreadTokensRequired(tokenId1, positionSize0 / 2);

panopticPool.mintOptions(
positionIdList1,
positionSize0 / 2,
type(uint64).max,
TickMath.MIN_TICK,
TickMath.MAX_TICK
);
}

// mimic pool activity
twoWaySwap(swapSizeSeed);

// check requirement at fuzzed tick
{
atTick = int24(bound(atTick, TickMath.MIN_TICK, TickMath.MAX_TICK));
atTick = (atTick / tickSpacing) * tickSpacing;

(int128 premium0, int128 premium1, uint256[2][] memory posBalanceArray) = panopticPool
.calculateAccumulatedFeesBatch(Alice, positionIdList1);

uint256 tokenData0 = collateralToken0.getAccountMarginDetails(
Alice,
atTick,
posBalanceArray,
premium0
);
uint256 tokenData1 = collateralToken1.getAccountMarginDetails(
Alice,
atTick,
posBalanceArray,
premium1
);

(, uint64 poolUtilization0, uint64 poolUtilization1) = panopticPool
.optionPositionBalance(Alice, tokenId1);

uint128 poolUtilizations = uint128(poolUtilization0) +
(uint128(poolUtilization1) << 64);

// only add premium requirement if there is net premia owed
required += premium0 < 0 ? uint128(-premium0) : 0;
premium1 = premium1 < 0 ? -premium1 : int128(0);
assertEq(required, tokenData0.leftSlot(), "required token0");
assertEq(premium1, int128(tokenData1.leftSlot()), "required token1");
}

{
(, currentTick, , , , , ) = pool.slot0();

(int128 premium0, int128 premium1, uint256[2][] memory posBalanceArray) = panopticPool
.calculateAccumulatedFeesBatch(Alice, positionIdList1);

uint256 tokenData0 = collateralToken0.getAccountMarginDetails(
Alice,
currentTick,
posBalanceArray,
premium0
);
uint256 tokenData1 = collateralToken1.getAccountMarginDetails(
Alice,
currentTick,
posBalanceArray,
premium1
);

(uint256 calcBalanceCross, uint256 calcThresholdCross) = PanopticMath
.convertCollateralData(tokenData0, tokenData1, 1, currentTick);

(tokenData0, tokenData1) = panopticHelper.checkCollateral(
panopticPool,
Alice,
currentTick,
1,
positionIdList1
);

assertEq(tokenData0, calcBalanceCross, "0");
assertEq(tokenData1, calcThresholdCross, "1");
}
}

/* identical leg spreads */

function test_Success_collateralCheck_OTMCallIdenticalSpread(
Expand Down

0 comments on commit 7222205

Please sign in to comment.