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

[CL]: Fix low level tick rounding to mitigate fund drain bug #5510

Closed
wants to merge 12 commits into from

Conversation

AlpinYukseloglu
Copy link
Contributor

@AlpinYukseloglu AlpinYukseloglu commented Jun 13, 2023

Closes: #5516

What is the purpose of the change

This PR implements the discussed fix to the vector outlined in #5493. It currently breaks tick invariant tests, which are skipped in this PR until we converge on how to handle tick <> sqrt price conversions in #5519.

This PR also renames PriceToTickRoundDown to PriceToTickRoundDownSpacing, as the original naming caused some confusion around how this bug could be possible in the first place.

Testing and Verifying

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

@@ -411,14 +411,14 @@ var (
spreadFactor: sdk.ZeroDec(),
// params
// liquidity: 1517882343.751510418088349649
// sqrtPriceNext: 70.668238976219012613 which is 4994 https://www.wolframalpha.com/input?i=70.710678118654752440+%2B+42000000+%2F+1517882343.751510418088349649
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note to reviewer: this test vector seemed to have been incorrectly set up (previous wolfram link went to an unrelated equation)

tickIndex := CalculatePriceToTick(price)
tickIndex, err := CalculatePriceToTick(price)
if err != nil {
return 0, err
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note to reviewer: returning 0 here isn't ideal, but it seems to be what we do in all other tick logic upon error. If there's a better value to return here that's more clearly invalid (e.g. an sdk.Int{} equivalent) please lmk

@AlpinYukseloglu AlpinYukseloglu marked this pull request as ready for review June 14, 2023 06:47
Comment on lines +234 to +243
// If price is lower than the price in the tick below, our error assumption is violated so we panic.
if price.LT(tickBelowPrice) {
panic(fmt.Sprintf("price %s is outside of error bounds for tick %d", price, tickIndex))
}

// If price falls in the bucket below `tickIndex`, return that bucket index.
// Otherwise, return the current tick index.
if price.GTE(tickBelowPrice) && price.LT(curPrice) {
return tickIndex - 1, nil
}
Copy link
Member

Choose a reason for hiding this comment

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

I think we should make a new SqrtPriceToTick function, that does the squaring internally, then does this comparison on the derived sqrt prices. As the problem is our price definition doesn't match due to errors.

(I don't think this on its own will save us from anything aside from the erroneous bucket definition due to not truncating)

@AlpinYukseloglu
Copy link
Contributor Author

Closing in favor of #5541

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.

[CL][bug]: Bankers rounding in price to tick conversions allows for LP fund drain
2 participants