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

Add Tests for maxUsableTick and minUsableTick Functions #526

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/test/TickMathTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,12 @@ contract TickMathTest {
function MAX_TICK() external pure returns (int24) {
return TickMath.MAX_TICK;
}

function maxUsableTick(int24 tickSpacing) external pure returns (int24) {
return TickMath.maxUsableTick(tickSpacing);
}

function minUsableTick(int24 tickSpacing) external pure returns (int24) {
return TickMath.minUsableTick(tickSpacing);
}
}
67 changes: 67 additions & 0 deletions test/TickMath.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ contract TickMathTestTest is Test, JavascriptFfi {
uint160[] getSqrtRatioAtTickFuzzResults;
int24[] getTickAtSqrtRatioFuzzResults;

struct FeeTier {
uint256 fee;
int24 tickSpacing;
}

TickMathTest tickMath;

function setUp() public {
Expand All @@ -44,6 +49,68 @@ contract TickMathTestTest is Test, JavascriptFfi {
assertEq(maxTick, MAX_TICK);
}

partylikeits1983 marked this conversation as resolved.
Show resolved Hide resolved
function test_maxUsableTick_SpecificFeeTiers() public {
// The size of tick spacing is relative to the fee tier.
// The tick spacing is double the size of the fee tier.
// For example, a tick for 0.30% fee tier pool will be 0.60% in size.
// The 0.01% fee tier has a size of 0.01%.
FeeTier[] memory feeTiers = new FeeTier[](5);
feeTiers[0] = FeeTier({fee: 1, tickSpacing: 1}); // 0.01% fee tier
feeTiers[1] = FeeTier({fee: 5, tickSpacing: 10}); // 0.05% fee tier
feeTiers[2] = FeeTier({fee: 30, tickSpacing: 60}); // 0.30% fee tier
feeTiers[3] = FeeTier({fee: 100, tickSpacing: 200}); // 1% fee tier
feeTiers[4] = FeeTier({fee: 500, tickSpacing: 1000}); // very high fee tier

for (uint256 i = 0; i < feeTiers.length; i++) {
FeeTier memory tier = feeTiers[i];
int24 expectedMaxTick = (MAX_TICK / tier.tickSpacing) * tier.tickSpacing;
assertEq(tickMath.maxUsableTick(tier.tickSpacing), expectedMaxTick);
}
}

function test_minUsableTick_SpecificFeeTiers() public {
// The size of tick spacing is relative to the fee tier.
// The tick spacing is double the size of the fee tier.
// For example, a tick for 0.30% fee tier pool will be 0.60% in size.
// The 0.01% fee tier has a size of 0.01%.
FeeTier[] memory feeTiers = new FeeTier[](5);
feeTiers[0] = FeeTier({fee: 1, tickSpacing: 1}); // 0.01% fee tier
feeTiers[1] = FeeTier({fee: 5, tickSpacing: 10}); // 0.05% fee tier
feeTiers[2] = FeeTier({fee: 30, tickSpacing: 60}); // 0.30% fee tier
feeTiers[3] = FeeTier({fee: 100, tickSpacing: 200}); // 1% fee tier
feeTiers[4] = FeeTier({fee: 500, tickSpacing: 1000}); // very high fee tier

for (uint256 i = 0; i < feeTiers.length; i++) {
FeeTier memory tier = feeTiers[i];
int24 expectedMinTick = (MIN_TICK / tier.tickSpacing) * tier.tickSpacing;
assertEq(tickMath.minUsableTick(tier.tickSpacing), expectedMinTick);
}
}

function testFuzz_maxUsableTick(int24 tickSpacing) public {
vm.assume(tickSpacing > 0 && tickSpacing <= MAX_TICK && tickSpacing >= MIN_TICK);
partylikeits1983 marked this conversation as resolved.
Show resolved Hide resolved

int24 expectedMaxTick = (MAX_TICK / tickSpacing) * tickSpacing;
assertEq(tickMath.maxUsableTick(tickSpacing), expectedMaxTick);
}

function testFuzz_minUsableTick(int24 tickSpacing) public {
vm.assume(tickSpacing > 0 && tickSpacing <= MAX_TICK && tickSpacing >= MIN_TICK);

int24 expectedMinTick = (MIN_TICK / tickSpacing) * tickSpacing;
assertEq(tickMath.minUsableTick(tickSpacing), expectedMinTick);
}

function test_maxUsableTick_revertsForZero() public {
vm.expectRevert();
tickMath.maxUsableTick(0);
}

function test_minUsableTick_revertsForZero() public {
vm.expectRevert();
tickMath.minUsableTick(0);
}

function test_getSqrtRatioAtTick_throwsForTooLow() public {
vm.expectRevert(TickMath.InvalidTick.selector);
tickMath.getSqrtRatioAtTick(MIN_TICK - 1);
Expand Down
Loading