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

fix/test: swap over contiguously initialized ticks with spacing of one #5582

Merged
merged 11 commits into from
Jun 22, 2023

Conversation

p0mvn
Copy link
Member

@p0mvn p0mvn commented Jun 19, 2023

Closes: #5558, #5571, 5574

What is the purpose of the change

Adding swap test vectors for out given in where contiguous ticks are initialized with spacing of one. Both zero for one and one for zero are covered

This test helped uncover 2 problems that were separated into the following PRs:

This PR also addresses removing the hack described here: #5571

It also addresses #5574 with investigations done in TestSwapOutGivenIn_PriceLimit_TickCross_ZeroForOne to swap with sqrt price limit at tick boundaries and asserting that ticks are crossed as expected. I was unable to construct a test case that would reproduce the behavior defined in #5574

Testing and Verifying

This change adds a test.

Documentation and Release Note

  • Does this pull request introduce a new feature or user-facing behavior changes?
  • Changelog entry added to Unreleased section of CHANGELOG.md?

Where is the change documented?

  • Specification (x/{module}/README.md)
  • Osmosis documentation site
  • Code comments?
  • N/A

Comment on lines 936 to 954
// computeExpectedValuesForTestOneForZero returns the tick to swap to during estimate computation and amountIn multiplier.
// It most cases, the tick to swap to is the same as the expected tick to reach after the swap and the multiplier is 1.
// The only exception is when performing a second swap in the same direction within the same tick.
// In such a case, we need to run our estimate logic one tick further to ensure that our estimate is non-zero.
// However, we discount the amountIn by half to ensure that the tick is not crossed.
computeNextTickToReachAndMultiplier := func(isZeroForOne bool, expectedSwapEndTick int64, shouldStayWithinTheSameTickInCompute bool) (int64, sdk.Dec) {
if shouldStayWithinTheSameTickInCompute {
nextTickToReachInCompute := expectedSwapEndTick
if isZeroForOne {
nextTickToReachInCompute = nextTickToReachInCompute - 1
} else {
nextTickToReachInCompute = nextTickToReachInCompute + 1
}

return nextTickToReachInCompute, sdk.NewDecWithPrec(5, 1)
}

return expectedSwapEndTick, sdk.OneDec()
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain a little more about the multiplier here? I later realized we used 50 percent to stay within the same tick, but it was not clear when initially reading the code here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct. We use a 50% discount to stay within the same bucket. 50% picked arbitrarily. If we don't discount, the next tick will be crossed as we are running the token in estimation logic between ticks. For some test cases, we specifically want to test staying within the same bucket.

Let me know if there is anything else that I should elaborate on

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add that the 50 percent is arbitrary in the comments to make it easier for other reviewers?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@p0mvn p0mvn requested a review from czarcas7ic June 21, 2023 17:14
Base automatically changed from roman/min-tick-rename to main June 21, 2023 22:43
@p0mvn p0mvn marked this pull request as draft June 22, 2023 01:47
@p0mvn p0mvn added V:state/compatible/no_backport State machine compatible PR, depends on prior breaks A:no-changelog labels Jun 22, 2023
@p0mvn p0mvn marked this pull request as ready for review June 22, 2023 01:55
@p0mvn p0mvn added V:state/breaking State machine breaking PR and removed V:state/compatible/no_backport State machine compatible PR, depends on prior breaks labels Jun 22, 2023
Copy link
Member

@czarcas7ic czarcas7ic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While the I don't have the most in depth understanding of the tick issues we have been facing, this PR logically follows for me

x/concentrated-liquidity/swaps_tick_cross_test.go Outdated Show resolved Hide resolved
@p0mvn p0mvn merged commit df4c956 into main Jun 22, 2023
@p0mvn p0mvn deleted the roman/conting-tick-tests branch June 22, 2023 03:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

test(CL): multiple swaps over contiguous initialized ticks with tick spacing of 1
2 participants