-
Notifications
You must be signed in to change notification settings - Fork 1k
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
partylikeits1983
wants to merge
7
commits into
Uniswap:main
Choose a base branch
from
partylikeits1983:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3c2292e
feat: add tests for TickMath
partylikeits1983 a6f68fb
rm console.log
partylikeits1983 4b28109
fix: rename fuzzing tests
partylikeits1983 ce1ab96
Merge remote-tracking branch 'upstream/main'
partylikeits1983 e3edabd
feat: update TickMath tests
partylikeits1983 089503f
feat: add TickMath fuzz test
partylikeits1983 9eb5041
Merge branch 'main' into main
partylikeits1983 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,11 @@ contract TickMathTestTest is Test, JavascriptFfi { | |
uint160[] getSqrtRatioAtTickFuzzResults; | ||
int24[] getTickAtSqrtRatioFuzzResults; | ||
|
||
struct FeeTier { | ||
uint24 fee; | ||
int24 tickSpacing; | ||
} | ||
|
||
TickMathTest tickMath; | ||
|
||
function setUp() public { | ||
|
@@ -44,6 +49,77 @@ contract TickMathTestTest is Test, JavascriptFfi { | |
assertEq(maxTick, MAX_TICK); | ||
} | ||
|
||
function test_maxUsableTick_checkTypicalFeeTiers() 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 | ||
feeTiers[1] = FeeTier({fee: 5, tickSpacing: 10}); // 0.05% fee | ||
feeTiers[2] = FeeTier({fee: 30, tickSpacing: 60}); // 0.30% fee | ||
feeTiers[3] = FeeTier({fee: 100, tickSpacing: 200}); // 1% fee | ||
feeTiers[4] = FeeTier({fee: 1000000, tickSpacing: 2000000}); // max fee | ||
|
||
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_checkTypicalFeeTiers() 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 | ||
feeTiers[1] = FeeTier({fee: 5, tickSpacing: 10}); // 0.05% fee | ||
feeTiers[2] = FeeTier({fee: 30, tickSpacing: 60}); // 0.30% fee | ||
feeTiers[3] = FeeTier({fee: 100, tickSpacing: 200}); // 1% fee | ||
feeTiers[4] = FeeTier({fee: 1000000, tickSpacing: 2000000}); // max fee | ||
|
||
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 test_maxUsableTick_shouldMatchExpectedMaxTickGivenSpacing_fuzz(int24 tickSpacing) public { | ||
tickSpacing = int24(bound(tickSpacing, TickMath.MIN_TICK_SPACING, TickMath.MAX_TICK_SPACING)); | ||
vm.assume(tickSpacing != 0); | ||
|
||
int24 expectedMaxTick = (MAX_TICK / tickSpacing) * tickSpacing; | ||
assertEq(tickMath.maxUsableTick(tickSpacing), expectedMaxTick); | ||
} | ||
|
||
function test_minUsableTick_shouldMatchExpectedMinTickGivenSpacing_fuzz(int24 tickSpacing) public { | ||
tickSpacing = int24(bound(tickSpacing, TickMath.MIN_TICK_SPACING, TickMath.MAX_TICK_SPACING)); | ||
vm.assume(tickSpacing != 0); | ||
|
||
int24 expectedMinTick = (MIN_TICK / tickSpacing) * tickSpacing; | ||
assertEq(tickMath.minUsableTick(tickSpacing), expectedMinTick); | ||
} | ||
|
||
function test_maxUsableTick_equalsMinUsableTick_fuzz(int24 tickSpacing) public { | ||
tickSpacing = int24(bound(tickSpacing, TickMath.MIN_TICK_SPACING, TickMath.MAX_TICK_SPACING)); | ||
vm.assume(tickSpacing != 0); | ||
|
||
assertEq(-tickMath.minUsableTick(tickSpacing), tickMath.maxUsableTick(tickSpacing)); | ||
} | ||
Comment on lines
+106
to
+111
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hensha256 resolving this suggestion from @aadams on my previous PR |
||
|
||
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); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hensha256 Using
bound
instead of vm.assume and usingTickMath.MIN_TICK_SPACING
andTickMath.MAX_TICK_SPACING
instead ofMIN_TICK
andMAX_TICK