-
Notifications
You must be signed in to change notification settings - Fork 606
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
refactor(CL): convert priceLimit API in swaps to BigDec #6368
Conversation
bc88430
to
a6b868f
Compare
amountOneOut, _, _ := s.computeSwapAmountsInGivenOut(poolId, pool.GetCurrentSqrtPrice(), types.MinInitializedTickV2, true, false) | ||
amountOneOut, _, _ := s.computeSwapAmountsInGivenOut(poolId, pool.GetCurrentSqrtPrice(), types.MinInitializedTick, true, false) | ||
|
||
// estimate the amount in to fund | ||
amountZeroIn, _, _ := s.computeSwapAmounts(poolId, pool.GetCurrentSqrtPrice(), types.MinInitializedTickV2, true, false) | ||
amountZeroIn, _, _ := s.computeSwapAmounts(poolId, pool.GetCurrentSqrtPrice(), types.MinInitializedTick, true, false) |
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.
Note: swaps to v2 min tick are not enabled anymore.
Previously the estimation would stop the current min tick anyways, so this request to estimate till MinInitializedTickV2
is invalid at this time. To be re-added in a future PR.
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.
The definition for min initialized tick will be the same across all pools at this time though right? So we shouldn't need to name it something different if I am understanding correctly.
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.
Is this tracked in an issue?
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.
Already addressed here:
https://github.com/osmosis-labs/osmosis/pull/6380/files#r1325756769
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.
The definition for min initialized tick will be the same across all pools at this time though right? So we shouldn't need to name it something different if I am understanding correctly.
See: #6318
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.
LGTM, just had question on comment vs actual code
amountOneOut, _, _ := s.computeSwapAmountsInGivenOut(poolId, pool.GetCurrentSqrtPrice(), types.MinInitializedTickV2, true, false) | ||
amountOneOut, _, _ := s.computeSwapAmountsInGivenOut(poolId, pool.GetCurrentSqrtPrice(), types.MinInitializedTick, true, false) | ||
|
||
// estimate the amount in to fund | ||
amountZeroIn, _, _ := s.computeSwapAmounts(poolId, pool.GetCurrentSqrtPrice(), types.MinInitializedTickV2, true, false) | ||
amountZeroIn, _, _ := s.computeSwapAmounts(poolId, pool.GetCurrentSqrtPrice(), types.MinInitializedTick, true, false) |
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.
The definition for min initialized tick will be the same across all pools at this time though right? So we shouldn't need to name it something different if I am understanding correctly.
if priceLimit.IsZero() { | ||
if zeroForOne { | ||
return osmomath.BigDecFromDec(types.MinSqrtPrice), nil | ||
return types.MinSqrtPriceBigDec, nil | ||
} | ||
return osmomath.BigDecFromDec(types.MaxSqrtPrice), nil | ||
} |
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.
Comment says if price limit is zero and strat is one for zero, max is returned. But I don't see that logic here.
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.
Hmm don't the first few lines of the function do just that?
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.
Nice work, LGTM. Left a couple non blocking comments about capturing our offline discussion on monotonicity in comments and clarifying a point about MinSpotPriceV2
amountOneOut, _, _ := s.computeSwapAmountsInGivenOut(poolId, pool.GetCurrentSqrtPrice(), types.MinInitializedTickV2, true, false) | ||
amountOneOut, _, _ := s.computeSwapAmountsInGivenOut(poolId, pool.GetCurrentSqrtPrice(), types.MinInitializedTick, true, false) | ||
|
||
// estimate the amount in to fund | ||
amountZeroIn, _, _ := s.computeSwapAmounts(poolId, pool.GetCurrentSqrtPrice(), types.MinInitializedTickV2, true, false) | ||
amountZeroIn, _, _ := s.computeSwapAmounts(poolId, pool.GetCurrentSqrtPrice(), types.MinInitializedTick, true, false) |
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.
Is this tracked in an issue?
if priceLimit.IsZero() { | ||
if zeroForOne { | ||
return osmomath.BigDecFromDec(types.MinSqrtPrice), nil | ||
return types.MinSqrtPriceBigDec, nil | ||
} | ||
return osmomath.BigDecFromDec(types.MaxSqrtPrice), nil | ||
} |
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.
Hmm don't the first few lines of the function do just that?
} | ||
return osmomath.BigDecFromDec(types.MaxSqrtPrice), nil | ||
} | ||
|
||
sqrtPriceLimit, err := osmomath.MonotonicSqrt(priceLimit) | ||
if priceLimit.LT(types.MinSpotPriceV2) || priceLimit.GT(types.MaxSpotPriceBigDec) { |
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.
Trying to reconcile this with the "swaps to MinInitializedTickV2 are not enabled anymore" comment above in swap tests section – should this then be checking against regular min spot price (MinSpotPriceBigDec
) or have I misunderstood the issue?
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.
This was more of an added guard check for the future. It is guaranteed by the caller that priceLimit
is above types.MinSpotPriceBigDec
. As a result, state-compatibility is ensured
I made the check to adhere to v2 minimum right away to avoid refactoring the tests in the future PRs in the sequence.
Realize now that should have avoided doing that for clarity in the review. Will take a step by step approach in the future
oneULPUnderThreshold = types.MinSpotPriceBigDec.Sub(oneULPBigDec) | ||
atThreshold = types.MinSpotPriceBigDec | ||
oneULPAboveThreshold = types.MinSpotPriceBigDec.Add(oneULPBigDec) | ||
) |
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.
Note to capture an offline discussion we had for posterity: this threshold does not break monotonicity because it is at a value that is equivalent in regular Dec
and BigDec
domains.
Specifically, calling the conversion of the threshold spot price from BigDec
to Dec
is a misnomer because nothing is actually being "truncated" (which would break monotonicity) – the padded 18 zeroes are simply being trimmed
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.
It might be worthwhile to capture this in comments somewhere if it isn't already (sorry if it is and I missed it)
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.
This is unrelated to testing the monotonicity of tick-to-sqrt price conversions that can be found in the rest of the test suite. It simply tests the execution flow of GetSqrtPriceLimit
.
Added clarifying comment: e46ef8a
* refactor(CL): convert priceLimit API in swaps to BigDec * changelog * comment updates * remove unused constant * fix test * tests for GetSqrtPriceLimit * Update x/concentrated-liquidity/swapstrategy/swap_strategy.go Co-authored-by: Alpo <[email protected]> * clarifying comment --------- Co-authored-by: Alpo <[email protected]> (cherry picked from commit 3f15d3d) # Conflicts: # CHANGELOG.md # x/concentrated-liquidity/export_test.go # x/concentrated-liquidity/fuzz_test.go # x/concentrated-liquidity/keeper_test.go # x/concentrated-liquidity/position_test.go # x/concentrated-liquidity/range_test.go # x/concentrated-liquidity/spread_rewards_test.go # x/concentrated-liquidity/swaps.go # x/concentrated-liquidity/swaps_test.go # x/concentrated-liquidity/swaps_tick_cross_test.go # x/concentrated-liquidity/swapstrategy/swap_strategy.go # x/concentrated-liquidity/swapstrategy/swap_strategy_test.go # x/concentrated-liquidity/swapstrategy/zero_for_one.go # x/concentrated-liquidity/types/constants.go
…) (#6408) * refactor(CL): convert priceLimit API in swaps to BigDec (backport #6368) * lint --------- Co-authored-by: roman <[email protected]>
Closes: #6355
What is the purpose of the change
State-compatible API change to the priceLimit swaps API needed to support the extended price range.
To be backported and validated on a v19.x node.
Testing and Verifying
This change is already covered by existing tests, such as (please describe tests).
Documentation and Release Note
Unreleased
section ofCHANGELOG.md
?Where is the change documented?
x/{module}/README.md
)