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

refactor(CL): convert priceLimit API in swaps to BigDec #6368

Merged
merged 9 commits into from
Sep 14, 2023

Conversation

p0mvn
Copy link
Member

@p0mvn p0mvn commented Sep 11, 2023

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

  • 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

@p0mvn p0mvn added the V:state/compatible/backport State machine compatible PR, should be backported label Sep 11, 2023
@p0mvn p0mvn force-pushed the roman/price-limit-api branch from bc88430 to a6b868f Compare September 11, 2023 16:19
Comment on lines -3538 to +3541
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)
Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Contributor

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?

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member Author

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

@p0mvn p0mvn added the A:backport/v19.x backport patches to v19.x branch label Sep 11, 2023
@p0mvn p0mvn marked this pull request as ready for review September 11, 2023 17:31
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.

LGTM, just had question on comment vs actual code

Comment on lines -3538 to +3541
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)
Copy link
Member

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.

Comment on lines 124 to 129
if priceLimit.IsZero() {
if zeroForOne {
return osmomath.BigDecFromDec(types.MinSqrtPrice), nil
return types.MinSqrtPriceBigDec, nil
}
return osmomath.BigDecFromDec(types.MaxSqrtPrice), nil
}
Copy link
Member

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.

Copy link
Contributor

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?

Copy link
Contributor

@AlpinYukseloglu AlpinYukseloglu left a 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

Comment on lines -3538 to +3541
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)
Copy link
Contributor

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?

x/concentrated-liquidity/swapstrategy/swap_strategy.go Outdated Show resolved Hide resolved
Comment on lines 124 to 129
if priceLimit.IsZero() {
if zeroForOne {
return osmomath.BigDecFromDec(types.MinSqrtPrice), nil
return types.MinSqrtPriceBigDec, nil
}
return osmomath.BigDecFromDec(types.MaxSqrtPrice), nil
}
Copy link
Contributor

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) {
Copy link
Contributor

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?

Copy link
Member Author

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)
)
Copy link
Contributor

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

Copy link
Contributor

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)

Copy link
Member Author

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

@p0mvn p0mvn merged commit 3f15d3d into main Sep 14, 2023
1 check passed
@p0mvn p0mvn deleted the roman/price-limit-api branch September 14, 2023 11:17
mergify bot pushed a commit that referenced this pull request Sep 14, 2023
* 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
p0mvn added a commit that referenced this pull request Sep 20, 2023
…) (#6408)

* refactor(CL): convert priceLimit API in swaps to BigDec (backport #6368)

* lint

---------

Co-authored-by: roman <[email protected]>
@github-actions github-actions bot mentioned this pull request May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A:backport/v19.x backport patches to v19.x branch C:x/concentrated-liquidity V:state/compatible/backport State machine compatible PR, should be backported
Projects
None yet
Development

Successfully merging this pull request may close these issues.

refactor(CL): convert priceLimit from Dec to BigDec
3 participants