Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set tick spacing range as constants #369

Merged
merged 9 commits into from
Nov 15, 2023
5 changes: 3 additions & 2 deletions contracts/PoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {FeeLibrary} from "./libraries/FeeLibrary.sol";
import {Currency, CurrencyLibrary} from "./types/Currency.sol";
import {PoolKey} from "./types/PoolKey.sol";
import {LockDataLibrary} from "./libraries/LockDataLibrary.sol";
import {TickMath} from "./libraries/TickMath.sol";
import {NoDelegateCall} from "./NoDelegateCall.sol";
import {Owned} from "./Owned.sol";
import {IHooks} from "./interfaces/IHooks.sol";
Expand All @@ -34,10 +35,10 @@ contract PoolManager is IPoolManager, Fees, NoDelegateCall, ERC1155, IERC1155Rec
using FeeLibrary for uint24;

/// @inheritdoc IPoolManager
int24 public constant override MAX_TICK_SPACING = type(int16).max;
int24 public constant override MAX_TICK_SPACING = TickMath.MAX_TICK_SPACING;
hyunchel marked this conversation as resolved.
Show resolved Hide resolved

/// @inheritdoc IPoolManager
int24 public constant override MIN_TICK_SPACING = 1;
int24 public constant override MIN_TICK_SPACING = TickMath.MIN_TICK_SPACING;

/// @inheritdoc IPoolManager
IPoolManager.LockData public override lockData;
Expand Down
5 changes: 5 additions & 0 deletions contracts/libraries/TickMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ library TickMath {
/// @dev The maximum tick that may be passed to #getSqrtRatioAtTick computed from log base 1.0001 of 2**128
int24 internal constant MAX_TICK = -MIN_TICK;

/// @dev The minimum tick spacing value drawn from the range of type int16 that is greater than 0, i.e. min from the range [1, 32767]
int24 internal constant MIN_TICK_SPACING = 1;
/// @dev The maximum tick spacing value drawn from the range of type int16, i.e. max from the range [1, 32767]
int24 internal constant MAX_TICK_SPACING = type(int16).max;

/// @dev The minimum value that can be returned from #getSqrtRatioAtTick. Equivalent to getSqrtRatioAtTick(MIN_TICK)
uint160 internal constant MIN_SQRT_RATIO = 4295128739;
/// @dev The maximum value that can be returned from #getSqrtRatioAtTick. Equivalent to getSqrtRatioAtTick(MAX_TICK)
Expand Down
8 changes: 4 additions & 4 deletions test/foundry-tests/Pool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ contract PoolTest is Test {

function testModifyPosition(uint160 sqrtPriceX96, Pool.ModifyPositionParams memory params) public {
// Assumptions tested in PoolManager.t.sol
vm.assume(params.tickSpacing > 0);
vm.assume(params.tickSpacing < 32768);
vm.assume(params.tickSpacing >= TickMath.MIN_TICK_SPACING);
vm.assume(params.tickSpacing <= TickMath.MAX_TICK_SPACING);

testPoolInitialize(sqrtPriceX96, 0, 0, 0);

Expand Down Expand Up @@ -75,8 +75,8 @@ contract PoolTest is Test {

function testSwap(uint160 sqrtPriceX96, uint24 swapFee, Pool.SwapParams memory params) public {
// Assumptions tested in PoolManager.t.sol
vm.assume(params.tickSpacing > 0);
vm.assume(params.tickSpacing < 32768);
vm.assume(params.tickSpacing >= TickMath.MIN_TICK_SPACING);
vm.assume(params.tickSpacing <= TickMath.MAX_TICK_SPACING);
vm.assume(swapFee < 1000000);

testPoolInitialize(sqrtPriceX96, 0, 0, 0);
Expand Down
33 changes: 19 additions & 14 deletions test/foundry-tests/Tick.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ contract TickTest is Test, GasSnapshot {
}

function getMinTick(int24 tickSpacing) internal pure returns (int256) {
return (Constants.MIN_TICK / tickSpacing) * tickSpacing;
return (TickMath.MIN_TICK / tickSpacing) * tickSpacing;
}

function getMaxTick(int24 tickSpacing) internal pure returns (int256) {
return (Constants.MAX_TICK / tickSpacing) * tickSpacing;
return (TickMath.MAX_TICK / tickSpacing) * tickSpacing;
}

function checkCantOverflow(int24 tickSpacing, uint128 maxLiquidityPerTick) internal {
Expand Down Expand Up @@ -105,18 +105,25 @@ 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(TickMath.MIN_TICK_SPACING);

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

function testTick_tickSpacingToMaxLiquidityPerTick_returnsTheCorrectValueForMaxTickSpacing() public {
uint128 maxLiquidityPerTick = tickSpacingToMaxLiquidityPerTick(TickMath.MAX_TICK_SPACING);

assertEq(maxLiquidityPerTick, 6169404334338910476561253576012511949);
checkCantOverflow(TickMath.MAX_TICK_SPACING, maxLiquidityPerTick);
}

function testTick_tickSpacingToMaxLiquidityPerTick_returnsTheCorrectValueForEntireRange() public {
uint128 maxLiquidityPerTick = tickSpacingToMaxLiquidityPerTick(887272);
uint128 maxLiquidityPerTick = tickSpacingToMaxLiquidityPerTick(TickMath.MAX_TICK);

assertEq(maxLiquidityPerTick, Constants.MAX_UINT128 / 3); // 126 bits
hyunchel marked this conversation as resolved.
Show resolved Hide resolved
checkCantOverflow(887272, maxLiquidityPerTick);
assertEq(maxLiquidityPerTick, type(uint128).max / 3);
checkCantOverflow(TickMath.MAX_TICK, maxLiquidityPerTick);
}

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

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

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

function testTick_tickSpacingToMaxLiquidityPerTick_gasCostMaxTickSpacing() public {
int24 MAX_TICK_SPACING = 32767;

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

Expand Down Expand Up @@ -455,8 +460,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 <= TickMath.MAX_TICK_SPACING);
vm.assume(tickSpacing >= TickMath.MIN_TICK_SPACING);

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

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