-
Notifications
You must be signed in to change notification settings - Fork 609
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: inconsistencies in tick to price and price to tick conversions #4925
Comments
Conclusion on this from the call to pursue in #4965:
|
Added a variety of test cases at our default exponent of price 6, going from the smallest possible price upwards. The steps followed:
Consider these 3 cases:
Note from test case 3 that the magnitude of the difference is quite large. However, this is only because of our tick spacing model. The core of the problem is that when we compute the inverse, we end up getting a smaller price and a lower tick due to losing precision. To be continued / draft sectionFor test case 3, when we compute
|
The scope of this work keeps changing and increasing. There are too many suspicious behaviors that keep having me go in circles. As a result, I'm going to start splitting the investigations into separate PRs and document them here. My initial investigations that lead to the discovery of this problem: #4965 After #4965, Additionally, there were off-by-one errors stemming from rounding that got fixed in: #4998 As a drive-by change, I removed the use of a broken Next, I attempted to mitigate the risk of incorrect rounding when squaring during swaps: #5002 |
Latest state on this. Price to tick conversion summary. We have: price to tick conversion and dynamic tick spacing at human readable increments.
Uniswap:
The core of our problem is that the conversion occurs between price and tick, and not square root price and tick. For context, the reason why we track square root price and not price is because it allows us to have a simple math relationship Since we want to support dynamic price granularity, we ended up breaking To show the consequences, let's take an extreme example where at price 10_000_000, our price increments are in the order of magnitude of 1. That is, the only possible prices are 10_000_000, 10_000_010, 10_000_020 etc. So, say that I end up on current square root price As a result, I must truncate the price to the nearest one that can be mapped to a tick. Using banker's rounding, However, what do I do with the excess amounts? Do I swap them back? There are a few solutions:
Pros:
Risks:
Pros
Risks:
Pros:
Problems:
|
Had some more discussions after writing up the above. To summarize the next 3 actions:
|
This is closed by the latest change: #5019 |
Background
If we have tick X and call
TickTiSqrtPrice
and get Y. Then, by callingPriceToTick(Y.Power(2))
, we should be able to get X back.Currently, this is not the case. Probably because of the
ApproxSqrt
function here:osmosis/x/concentrated-liquidity/math/tick.go
Lines 86 to 90 in 97c9be1
Suggested Design
Add the call to
PriceToTick
at the end of this test:osmosis/x/concentrated-liquidity/math/tick_test.go
Lines 162 to 171 in 97c9be1
Then, you will be able to reproduce the problem
Acceptance Criteria
TestTickToSqrtPricePriceToTick_InverseRelationship
to callPriceToTick
second timeThe text was updated successfully, but these errors were encountered: