Skip to content

Commit

Permalink
Set tick spacing range as constants
Browse files Browse the repository at this point in the history
While tick and tick spacing both use int24 as their type, each has a
different range. Tick spacing has a range of [1, 32767].
This commit updates Tick test cases to use proper tick spacing range
instead that of tick.

Resolves issue Uniswap#371
  • Loading branch information
hyunchel committed Oct 15, 2023
1 parent d5fa3b6 commit 0fdc109
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
24 changes: 11 additions & 13 deletions test/foundry-tests/Tick.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,18 @@ contract TickTest is Test, GasSnapshot {
checkCantOverflow(HIGH_TICK_SPACING, maxLiquidityPerTick);
}

function testTick_tickSpacingToMaxLiquidityPerTick_returnsTheCorrectValueFor1() public {
uint128 maxLiquidityPerTick = tickSpacingToMaxLiquidityPerTick(1);
function testTick_tickSpacingToMaxLiquidityPerTick_returnsTheCorrectValueForMinTickSpacing() public {
uint128 maxLiquidityPerTick = tickSpacingToMaxLiquidityPerTick(Constants.MIN_TICK_SPACING);

assertEq(maxLiquidityPerTick, 191757530477355301479181766273477); // 126 bits
checkCantOverflow(1, maxLiquidityPerTick);
checkCantOverflow(Constants.MIN_TICK_SPACING, maxLiquidityPerTick);
}

function testTick_tickSpacingToMaxLiquidityPerTick_returnsTheCorrectValueForEntireRange() public {
uint128 maxLiquidityPerTick = tickSpacingToMaxLiquidityPerTick(887272);
function testTick_tickSpacingToMaxLiquidityPerTick_returnsTheCorrectValueForMaxTickSpacing() public {
uint128 maxLiquidityPerTick = tickSpacingToMaxLiquidityPerTick(Constants.MAX_TICK_SPACING);

assertEq(maxLiquidityPerTick, Constants.MAX_UINT128 / 3); // 126 bits
checkCantOverflow(887272, maxLiquidityPerTick);
assertEq(maxLiquidityPerTick, 6169404334338910476561253576012511949);
checkCantOverflow(Constants.MAX_TICK_SPACING, maxLiquidityPerTick);
}

function testTick_tickSpacingToMaxLiquidityPerTick_returnsTheCorrectValueFor2302() public {
Expand All @@ -128,7 +128,7 @@ contract TickTest is Test, GasSnapshot {

function testTick_tickSpacingToMaxLiquidityPerTick_gasCostMinTickSpacing() public {
snapStart("tickSpacingToMaxLiquidityPerTick_gasCostMinTickSpacing");
tickSpacingToMaxLiquidityPerTick(1);
tickSpacingToMaxLiquidityPerTick(Constants.MIN_TICK_SPACING);
snapEnd();
}

Expand All @@ -139,10 +139,8 @@ contract TickTest is Test, GasSnapshot {
}

function testTick_tickSpacingToMaxLiquidityPerTick_gasCostMaxTickSpacing() public {
int24 MAX_TICK_SPACING = 32767;

snapStart("tickSpacingToMaxLiquidityPerTick_gasCostMaxTickSpacing");
tickSpacingToMaxLiquidityPerTick(MAX_TICK_SPACING);
tickSpacingToMaxLiquidityPerTick(Constants.MAX_TICK_SPACING);
snapEnd();
}

Expand Down Expand Up @@ -455,8 +453,8 @@ contract TickTest is Test, GasSnapshot {
}

function testTick_tickSpacingToParametersInvariants_fuzz(int24 tickSpacing) public {
vm.assume(tickSpacing <= TickMath.MAX_TICK);
vm.assume(tickSpacing > 0);
vm.assume(tickSpacing <= Constants.MAX_TICK_SPACING);
vm.assume(tickSpacing >= Constants.MIN_TICK_SPACING);

int24 minTick = (TickMath.MIN_TICK / tickSpacing) * tickSpacing;
int24 maxTick = (TickMath.MAX_TICK / tickSpacing) * tickSpacing;
Expand Down
3 changes: 3 additions & 0 deletions test/foundry-tests/utils/Constants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ library Constants {

int24 constant MIN_TICK = -887272;
int24 constant MAX_TICK = 887272;

int24 constant MIN_TICK_SPACING = 1;
int24 constant MAX_TICK_SPACING = type(int16).max;
}

0 comments on commit 0fdc109

Please sign in to comment.