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 d5368f3 commit c622124
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 175 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
env:
FOUNDRY_PROFILE: ci_test
run: |
forge test --fork-url http://localhost:8545 -vvv --fuzz-runs 1000
forge test --fork-url http://localhost:8545 -vvv --fuzz-runs 1000 --match-test test_Success_collateralCheck_ITMcallSpread_assetTT0
id: forge-test

contract_sizes:
Expand Down
179 changes: 5 additions & 174 deletions test/foundry/core/CollateralTracker.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3752,175 +3752,6 @@ contract CollateralTrackerTest is Test, PositionUtils {
}
}

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

{
_initWorld(x);

// 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, 1, 0, 0, 0, strike, width);
tokenId = tokenId.addLeg(1, 1, 1, 0, 0, 1, strike1, width1);
positionIdList.push(tokenId);

/// calculate position size
positionSize0 = uint128(bound(positionSizeSeed, 2, 2 ** 104));
_assumePositionValidity(Bob, tokenId, 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, 1, 1, 0, 1, strike, width);
tokenId1 = tokenId1.addLeg(1, 1, 1, 0, 0, 0, strike1, width1);
positionIdList1.push(tokenId1);

_assumePositionValidity(Alice, 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);

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

// 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(address(panopticPool)),
Alice,
currentTick,
1,
positionIdList1
);

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

function test_Success_collateralCheck_ITMcallSpread_assetTT0(
uint256 x,
uint128 positionSizeSeed,
Expand All @@ -3933,8 +3764,6 @@ contract CollateralTrackerTest is Test, PositionUtils {
) public {
vm.assume(strikeSeed != strikeSeed2);

uint128 required;

{
_initWorld(x);

Expand Down Expand Up @@ -3975,9 +3804,9 @@ contract CollateralTrackerTest is Test, PositionUtils {
tokenId = tokenId.addLeg(1, 1, 0, 0, 0, 1, strike1, width1);
positionIdList.push(tokenId);

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

panopticPool.mintOptions(
positionIdList,
Expand Down Expand Up @@ -4007,7 +3836,6 @@ contract CollateralTrackerTest is Test, PositionUtils {
positionIdList1.push(tokenId1);

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

panopticPool.mintOptions(
positionIdList1,
Expand Down Expand Up @@ -4048,6 +3876,9 @@ contract CollateralTrackerTest is Test, PositionUtils {
uint128 poolUtilizations = uint128(poolUtilization0) +
(uint128(poolUtilization1) << 64);

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

// only add premium requirement if there is net premia owed
required += premium0 < 0 ? uint128(-premium0) : 0;
premium1 = premium1 < 0 ? -premium1 : int128(0);
Expand Down Expand Up @@ -4078,7 +3909,7 @@ contract CollateralTrackerTest is Test, PositionUtils {
.convertCollateralData(tokenData0, tokenData1, 1, currentTick);

(tokenData0, tokenData1) = panopticHelper.checkCollateral(
panopticPool,
PanopticPool(address(panopticPool)),
Alice,
currentTick,
1,
Expand Down

0 comments on commit c622124

Please sign in to comment.