From 7bd815e3f58f6ac7642595099fb8c5e0636f1624 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 19 Sep 2023 21:27:26 -0400 Subject: [PATCH] refactor(CL): convert priceLimit API in swaps to BigDec (backport #6368) (#6408) * refactor(CL): convert priceLimit API in swaps to BigDec (backport #6368) * lint --------- Co-authored-by: roman --- CHANGELOG.md | 2 + x/concentrated-liquidity/export_test.go | 11 +- x/concentrated-liquidity/fuzz_test.go | 4 +- x/concentrated-liquidity/math/precompute.go | 1 - x/concentrated-liquidity/position_test.go | 2 +- x/concentrated-liquidity/query.go | 6 +- x/concentrated-liquidity/range_test.go | 4 +- .../spread_rewards_test.go | 16 +-- x/concentrated-liquidity/swaps.go | 16 +-- x/concentrated-liquidity/swaps_test.go | 110 +++++++++--------- .../swaps_tick_cross_test.go | 2 +- .../swapstrategy/swap_strategy.go | 38 ++++-- .../swapstrategy/swap_strategy_test.go | 9 +- .../swapstrategy/zero_for_one.go | 2 +- 14 files changed, 123 insertions(+), 100 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a6df571d9f..57b2d022d79 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,6 +58,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Note: with the update, the Dec and Int do not get initialized to zero values by default in proto marhaling/unmarshaling. Instead, they get set to nil values. * maxDecBitLen has changed by one bit so overflow panic can be triggerred sooner. +* [#6368](https://github.com/osmosis-labs/osmosis/pull/6368) Convert priceLimit API in CL swaps to BigDec + ## v19.0.0 diff --git a/x/concentrated-liquidity/export_test.go b/x/concentrated-liquidity/export_test.go index fbfa3e82dbc..4c5ee71065c 100644 --- a/x/concentrated-liquidity/export_test.go +++ b/x/concentrated-liquidity/export_test.go @@ -60,7 +60,7 @@ func (k Keeper) SwapOutAmtGivenIn( tokenIn sdk.Coin, tokenOutDenom string, spreadFactor osmomath.Dec, - priceLimit osmomath.Dec) (calcTokenIn, calcTokenOut sdk.Coin, poolUpdates PoolUpdates, err error) { + priceLimit osmomath.BigDec) (calcTokenIn, calcTokenOut sdk.Coin, poolUpdates PoolUpdates, err error) { return k.swapOutAmtGivenIn(ctx, sender, pool, tokenIn, tokenOutDenom, spreadFactor, priceLimit) } @@ -70,8 +70,7 @@ func (k Keeper) ComputeOutAmtGivenIn( tokenInMin sdk.Coin, tokenOutDenom string, spreadFactor osmomath.Dec, - priceLimit osmomath.Dec, - + priceLimit osmomath.BigDec, ) (swapResult SwapResult, poolUpdates PoolUpdates, err error) { return k.computeOutAmtGivenIn(ctx, poolId, tokenInMin, tokenOutDenom, spreadFactor, priceLimit) } @@ -83,7 +82,7 @@ func (k Keeper) SwapInAmtGivenOut( desiredTokenOut sdk.Coin, tokenInDenom string, spreadFactor osmomath.Dec, - priceLimit osmomath.Dec) (calcTokenIn, calcTokenOut sdk.Coin, poolUpdates PoolUpdates, err error) { + priceLimit osmomath.BigDec) (calcTokenIn, calcTokenOut sdk.Coin, poolUpdates PoolUpdates, err error) { return k.swapInAmtGivenOut(ctx, sender, pool, desiredTokenOut, tokenInDenom, spreadFactor, priceLimit) } @@ -92,7 +91,7 @@ func (k Keeper) ComputeInAmtGivenOut( desiredTokenOut sdk.Coin, tokenInDenom string, spreadFactor osmomath.Dec, - priceLimit osmomath.Dec, + priceLimit osmomath.BigDec, poolId uint64, ) (swapResult SwapResult, poolUpdates PoolUpdates, err error) { @@ -329,7 +328,7 @@ func (k Keeper) GetLargestSupportedUptimeDuration(ctx sdk.Context) time.Duration func (k Keeper) SetupSwapStrategy(ctx sdk.Context, p types.ConcentratedPoolExtension, spreadFactor osmomath.Dec, tokenInDenom string, - priceLimit osmomath.Dec) (strategy swapstrategy.SwapStrategy, sqrtPriceLimit osmomath.BigDec, err error) { + priceLimit osmomath.BigDec) (strategy swapstrategy.SwapStrategy, sqrtPriceLimit osmomath.BigDec, err error) { return k.setupSwapStrategy(p, spreadFactor, tokenInDenom, priceLimit) } diff --git a/x/concentrated-liquidity/fuzz_test.go b/x/concentrated-liquidity/fuzz_test.go index 091c8e1dfa4..acd3d821bb4 100644 --- a/x/concentrated-liquidity/fuzz_test.go +++ b/x/concentrated-liquidity/fuzz_test.go @@ -288,7 +288,7 @@ func (s *KeeperTestSuite) swap(pool types.ConcentratedPoolExtension, swapInFunde // // Execute swap fmt.Printf("swap in: %s\n", swapInFunded) cacheCtx, writeOutGivenIn := s.Ctx.CacheContext() - _, tokenOut, _, err := s.clk.SwapOutAmtGivenIn(cacheCtx, s.TestAccs[0], pool, swapInFunded, swapOutDenom, pool.GetSpreadFactor(s.Ctx), osmomath.ZeroDec()) + _, tokenOut, _, err := s.clk.SwapOutAmtGivenIn(cacheCtx, s.TestAccs[0], pool, swapInFunded, swapOutDenom, pool.GetSpreadFactor(s.Ctx), osmomath.ZeroBigDec()) if errors.As(err, &types.InvalidAmountCalculatedError{}) { // If the swap we're about to execute will not generate enough output, we skip the swap. // it would error for a real user though. This is good though, since that user would just be burning funds. @@ -307,7 +307,7 @@ func (s *KeeperTestSuite) swap(pool types.ConcentratedPoolExtension, swapInFunde // We expect the returned amountIn to be roughly equal to the original swapInFunded. cacheCtx, _ = s.Ctx.CacheContext() fmt.Printf("swap out: %s\n", tokenOut) - amountInSwapResult, _, _, err := s.clk.SwapInAmtGivenOut(cacheCtx, s.TestAccs[0], pool, tokenOut, swapInFunded.Denom, pool.GetSpreadFactor(s.Ctx), osmomath.ZeroDec()) + amountInSwapResult, _, _, err := s.clk.SwapInAmtGivenOut(cacheCtx, s.TestAccs[0], pool, tokenOut, swapInFunded.Denom, pool.GetSpreadFactor(s.Ctx), osmomath.ZeroBigDec()) if errors.As(err, &types.InvalidAmountCalculatedError{}) { // If the swap we're about to execute will not generate enough output, we skip the swap. // it would error for a real user though. This is good though, since that user would just be burning funds. diff --git a/x/concentrated-liquidity/math/precompute.go b/x/concentrated-liquidity/math/precompute.go index 7e1b71c2659..d942a6d4b53 100644 --- a/x/concentrated-liquidity/math/precompute.go +++ b/x/concentrated-liquidity/math/precompute.go @@ -7,7 +7,6 @@ import ( var ( sdkOneDec = osmomath.OneDec() - sdkNineDec = osmomath.NewDec(9) sdkTenDec = osmomath.NewDec(10) powersOfTen []osmomath.Dec negPowersOfTen []osmomath.Dec diff --git a/x/concentrated-liquidity/position_test.go b/x/concentrated-liquidity/position_test.go index 6c46b7d08db..d50f0929c5d 100644 --- a/x/concentrated-liquidity/position_test.go +++ b/x/concentrated-liquidity/position_test.go @@ -1796,7 +1796,7 @@ func (s *KeeperTestSuite) TestTickRoundingEdgeCase() { swapAddr := testAccs[2] desiredTokenOut := sdk.NewCoin(USDC, osmomath.NewInt(10000)) s.FundAcc(swapAddr, sdk.NewCoins(sdk.NewCoin(ETH, osmomath.NewInt(1000000000000000000)))) - _, _, _, err := s.clk.SwapInAmtGivenOut(s.Ctx, swapAddr, pool, desiredTokenOut, ETH, osmomath.ZeroDec(), osmomath.ZeroDec()) + _, _, _, err := s.clk.SwapInAmtGivenOut(s.Ctx, swapAddr, pool, desiredTokenOut, ETH, osmomath.ZeroDec(), osmomath.ZeroBigDec()) s.Require().NoError(err) // Both positions should be able to withdraw successfully diff --git a/x/concentrated-liquidity/query.go b/x/concentrated-liquidity/query.go index 9eecbd2ab5c..9291c350430 100644 --- a/x/concentrated-liquidity/query.go +++ b/x/concentrated-liquidity/query.go @@ -270,12 +270,12 @@ func (k Keeper) GetNumNextInitializedTicks(ctx sdk.Context, poolId, numberOfNext ctx.Logger().Debug(fmt.Sprintf("min_tick %d\n", types.MinInitializedTick)) ctx.Logger().Debug(fmt.Sprintf("max_tick %d\n", types.MaxTick)) - var boundTick sdk.Int + var boundTick osmomath.Int if boundTick.IsNil() { if zeroForOne { - boundTick = sdk.NewInt(types.MinInitializedTick) + boundTick = osmomath.NewInt(types.MinInitializedTick) } else { - boundTick = sdk.NewInt(types.MaxTick) + boundTick = osmomath.NewInt(types.MaxTick) } } diff --git a/x/concentrated-liquidity/range_test.go b/x/concentrated-liquidity/range_test.go index 06be6e088e8..2b25dff1e58 100644 --- a/x/concentrated-liquidity/range_test.go +++ b/x/concentrated-liquidity/range_test.go @@ -357,7 +357,7 @@ func (s *KeeperTestSuite) executeRandomizedSwap(pool types.ConcentratedPoolExten } // Note that we set the price limit to zero to ensure that the swap can execute in either direction (gets automatically set to correct limit) - swappedIn, swappedOut, _, err := s.clk.SwapInAmtGivenOut(s.Ctx, swapAddress, pool, swapOutCoin, swapInDenom, pool.GetSpreadFactor(s.Ctx), osmomath.ZeroDec()) + swappedIn, swappedOut, _, err := s.clk.SwapInAmtGivenOut(s.Ctx, swapAddress, pool, swapOutCoin, swapInDenom, pool.GetSpreadFactor(s.Ctx), osmomath.ZeroBigDec()) s.Require().NoError(err) return swappedIn, swappedOut @@ -439,7 +439,7 @@ func (s *KeeperTestSuite) getInitialPositionAssets(pool types.ConcentratedPoolEx // Calculate asset amounts that would be required to get the required spot price (rounding up on asset1 to ensure we stay in the intended tick) asset0Amount := osmomath.NewInt(100000000000000) - asset1Amount := osmomath.NewDecFromInt(asset0Amount).Mul(requiredPrice.Dec()).Ceil().TruncateInt() + asset1Amount := osmomath.BigDecFromDec(osmomath.NewDecFromInt(asset0Amount)).Mul(requiredPrice).Ceil().Dec().TruncateInt() assetCoins := sdk.NewCoins( sdk.NewCoin(pool.GetToken0(), asset0Amount), diff --git a/x/concentrated-liquidity/spread_rewards_test.go b/x/concentrated-liquidity/spread_rewards_test.go index 4f8c55184a1..4bfae0964ed 100644 --- a/x/concentrated-liquidity/spread_rewards_test.go +++ b/x/concentrated-liquidity/spread_rewards_test.go @@ -1370,16 +1370,16 @@ func (s *KeeperTestSuite) TestFunctional_SpreadRewards_Swaps() { } // Swap multiple times USDC for ETH, therefore increasing the spot price - ticksActivatedAfterEachSwap, totalSpreadRewardsExpected, _, _ := s.swapAndTrackXTimesInARow(clPool.GetId(), DefaultCoin1, ETH, types.MaxSpotPrice, positions.numSwaps) + ticksActivatedAfterEachSwap, totalSpreadRewardsExpected, _, _ := s.swapAndTrackXTimesInARow(clPool.GetId(), DefaultCoin1, ETH, types.MaxSpotPriceBigDec, positions.numSwaps) s.CollectAndAssertSpreadRewards(s.Ctx, clPool.GetId(), totalSpreadRewardsExpected, positionIds, [][]int64{ticksActivatedAfterEachSwap}, onlyUSDC, positions) // Swap multiple times ETH for USDC, therefore decreasing the spot price - ticksActivatedAfterEachSwap, totalSpreadRewardsExpected, _, _ = s.swapAndTrackXTimesInARow(clPool.GetId(), DefaultCoin0, USDC, types.MinSpotPrice, positions.numSwaps) + ticksActivatedAfterEachSwap, totalSpreadRewardsExpected, _, _ = s.swapAndTrackXTimesInARow(clPool.GetId(), DefaultCoin0, USDC, types.MinSpotPriceBigDec, positions.numSwaps) s.CollectAndAssertSpreadRewards(s.Ctx, clPool.GetId(), totalSpreadRewardsExpected, positionIds, [][]int64{ticksActivatedAfterEachSwap}, onlyETH, positions) // Do the same swaps as before, however this time we collect spread rewards after both swap directions are complete. - ticksActivatedAfterEachSwapUp, totalSpreadRewardsExpectedUp, _, _ := s.swapAndTrackXTimesInARow(clPool.GetId(), DefaultCoin1, ETH, types.MaxSpotPrice, positions.numSwaps) - ticksActivatedAfterEachSwapDown, totalSpreadRewardsExpectedDown, _, _ := s.swapAndTrackXTimesInARow(clPool.GetId(), DefaultCoin0, USDC, types.MinSpotPrice, positions.numSwaps) + ticksActivatedAfterEachSwapUp, totalSpreadRewardsExpectedUp, _, _ := s.swapAndTrackXTimesInARow(clPool.GetId(), DefaultCoin1, ETH, types.MaxSpotPriceBigDec, positions.numSwaps) + ticksActivatedAfterEachSwapDown, totalSpreadRewardsExpectedDown, _, _ := s.swapAndTrackXTimesInARow(clPool.GetId(), DefaultCoin0, USDC, types.MinSpotPriceBigDec, positions.numSwaps) totalSpreadRewardsExpected = totalSpreadRewardsExpectedUp.Add(totalSpreadRewardsExpectedDown...) // We expect all positions to have both denoms in their spread reward accumulators except USDC for the overlapping range position since @@ -1413,7 +1413,7 @@ func (s *KeeperTestSuite) TestFunctional_SpreadRewards_LP() { s.FundAcc(owner, fundCoins) // Errors since no position. - _, _, _, err := s.App.ConcentratedLiquidityKeeper.SwapOutAmtGivenIn(s.Ctx, owner, pool, sdk.NewCoin(ETH, osmomath.OneInt()), USDC, pool.GetSpreadFactor(s.Ctx), types.MaxSpotPrice) + _, _, _, err := s.App.ConcentratedLiquidityKeeper.SwapOutAmtGivenIn(s.Ctx, owner, pool, sdk.NewCoin(ETH, osmomath.OneInt()), USDC, pool.GetSpreadFactor(s.Ctx), types.MaxSpotPriceBigDec) s.Require().Error(err) // Create position in the default range 1. @@ -1421,7 +1421,7 @@ func (s *KeeperTestSuite) TestFunctional_SpreadRewards_LP() { s.Require().NoError(err) // Swap once. - ticksActivatedAfterEachSwap, totalSpreadRewardsExpected, _, _ := s.swapAndTrackXTimesInARow(pool.GetId(), DefaultCoin1, ETH, types.MaxSpotPrice, 1) + ticksActivatedAfterEachSwap, totalSpreadRewardsExpected, _, _ := s.swapAndTrackXTimesInARow(pool.GetId(), DefaultCoin1, ETH, types.MaxSpotPriceBigDec, 1) // Withdraw half. halfLiquidity := positionDataOne.Liquidity.Mul(osmomath.NewDecWithPrec(5, 1)) @@ -1446,7 +1446,7 @@ func (s *KeeperTestSuite) TestFunctional_SpreadRewards_LP() { fullLiquidity := positionDataTwo.Liquidity // Swap once in the other direction. - ticksActivatedAfterEachSwap, totalSpreadRewardsExpected, _, _ = s.swapAndTrackXTimesInARow(pool.GetId(), DefaultCoin0, USDC, types.MinSpotPrice, 1) + ticksActivatedAfterEachSwap, totalSpreadRewardsExpected, _, _ = s.swapAndTrackXTimesInARow(pool.GetId(), DefaultCoin0, USDC, types.MinSpotPriceBigDec, 1) // This should claim under the hood for position 2 since full liquidity is removed. balanceBeforeWithdraw := s.App.BankKeeper.GetBalance(ctx, owner, ETH) @@ -1542,7 +1542,7 @@ func (s *KeeperTestSuite) tickStatusInvariance(ticksActivatedAfterEachSwap [][]i // swapAndTrackXTimesInARow performs `numSwaps` swaps and tracks the tick activated after each swap. // It also returns the total spread rewards collected, the total token in, and the total token out. -func (s *KeeperTestSuite) swapAndTrackXTimesInARow(poolId uint64, coinIn sdk.Coin, coinOutDenom string, priceLimit osmomath.Dec, numSwaps int) (ticksActivatedAfterEachSwap []int64, totalSpreadRewards sdk.Coins, totalTokenIn sdk.Coin, totalTokenOut sdk.Coin) { +func (s *KeeperTestSuite) swapAndTrackXTimesInARow(poolId uint64, coinIn sdk.Coin, coinOutDenom string, priceLimit osmomath.BigDec, numSwaps int) (ticksActivatedAfterEachSwap []int64, totalSpreadRewards sdk.Coins, totalTokenIn sdk.Coin, totalTokenOut sdk.Coin) { // Retrieve pool clPool, err := s.App.ConcentratedLiquidityKeeper.GetPoolById(s.Ctx, poolId) s.Require().NoError(err) diff --git a/x/concentrated-liquidity/swaps.go b/x/concentrated-liquidity/swaps.go index 77edbb16651..8110400dc79 100644 --- a/x/concentrated-liquidity/swaps.go +++ b/x/concentrated-liquidity/swaps.go @@ -216,7 +216,7 @@ func (k Keeper) swapOutAmtGivenIn( tokenIn sdk.Coin, tokenOutDenom string, spreadFactor osmomath.Dec, - priceLimit osmomath.Dec, + priceLimit osmomath.BigDec, ) (calcTokenIn, calcTokenOut sdk.Coin, poolUpdates PoolUpdates, err error) { swapResult, poolUpdates, err := k.computeOutAmtGivenIn(ctx, pool.GetId(), tokenIn, tokenOutDenom, spreadFactor, priceLimit) if err != nil { @@ -247,7 +247,7 @@ func (k *Keeper) swapInAmtGivenOut( desiredTokenOut sdk.Coin, tokenInDenom string, spreadFactor osmomath.Dec, - priceLimit osmomath.Dec, + priceLimit osmomath.BigDec, ) (calcTokenIn, calcTokenOut sdk.Coin, poolUpdates PoolUpdates, err error) { swapResult, poolUpdates, err := k.computeInAmtGivenOut(ctx, desiredTokenOut, tokenInDenom, spreadFactor, priceLimit, pool.GetId()) if err != nil { @@ -278,7 +278,7 @@ func (k Keeper) CalcOutAmtGivenIn( spreadFactor osmomath.Dec, ) (tokenOut sdk.Coin, err error) { cacheCtx, _ := ctx.CacheContext() - swapResult, _, err := k.computeOutAmtGivenIn(cacheCtx, poolI.GetId(), tokenIn, tokenOutDenom, spreadFactor, osmomath.ZeroDec()) + swapResult, _, err := k.computeOutAmtGivenIn(cacheCtx, poolI.GetId(), tokenIn, tokenOutDenom, spreadFactor, osmomath.ZeroBigDec()) if err != nil { return sdk.Coin{}, err } @@ -293,7 +293,7 @@ func (k Keeper) CalcInAmtGivenOut( spreadFactor osmomath.Dec, ) (sdk.Coin, error) { cacheCtx, _ := ctx.CacheContext() - swapResult, _, err := k.computeInAmtGivenOut(cacheCtx, tokenOut, tokenInDenom, spreadFactor, osmomath.ZeroDec(), poolI.GetId()) + swapResult, _, err := k.computeInAmtGivenOut(cacheCtx, tokenOut, tokenInDenom, spreadFactor, osmomath.ZeroBigDec(), poolI.GetId()) if err != nil { return sdk.Coin{}, err } @@ -354,7 +354,7 @@ func (k Keeper) computeOutAmtGivenIn( tokenInMin sdk.Coin, tokenOutDenom string, spreadFactor osmomath.Dec, - priceLimit osmomath.Dec, + priceLimit osmomath.BigDec, ) (swapResult SwapResult, poolUpdates PoolUpdates, err error) { p, spreadRewardAccumulator, uptimeAccums, err := k.swapSetup(ctx, poolId, tokenInMin.Denom, tokenOutDenom) if err != nil { @@ -481,7 +481,7 @@ func (k Keeper) computeInAmtGivenOut( desiredTokenOut sdk.Coin, tokenInDenom string, spreadFactor osmomath.Dec, - priceLimit osmomath.Dec, + priceLimit osmomath.BigDec, poolId uint64, ) (swapResult SwapResult, poolUpdates PoolUpdates, err error) { p, spreadRewardAccumulator, uptimeAccums, err := k.swapSetup(ctx, poolId, tokenInDenom, desiredTokenOut.Denom) @@ -734,7 +734,7 @@ func checkDenomValidity(inDenom, outDenom, asset0, asset1 string) error { return nil } -func (k Keeper) setupSwapStrategy(p types.ConcentratedPoolExtension, spreadFactor osmomath.Dec, tokenInDenom string, priceLimit osmomath.Dec) (strategy swapstrategy.SwapStrategy, sqrtPriceLimit osmomath.BigDec, err error) { +func (k Keeper) setupSwapStrategy(p types.ConcentratedPoolExtension, spreadFactor osmomath.Dec, tokenInDenom string, priceLimit osmomath.BigDec) (strategy swapstrategy.SwapStrategy, sqrtPriceLimit osmomath.BigDec, err error) { zeroForOne := getZeroForOne(tokenInDenom, p.GetToken0()) // take provided price limit and turn this into a sqrt price limit since formulas use sqrtPrice @@ -838,7 +838,7 @@ func (k Keeper) ComputeMaxInAmtGivenMaxTicksCrossed( } // Setup the swap strategy - swapStrategy, _, err := k.setupSwapStrategy(p, p.GetSpreadFactor(cacheCtx), tokenInDenom, osmomath.ZeroDec()) + swapStrategy, _, err := k.setupSwapStrategy(p, p.GetSpreadFactor(cacheCtx), tokenInDenom, osmomath.ZeroBigDec()) if err != nil { return sdk.Coin{}, sdk.Coin{}, err } diff --git a/x/concentrated-liquidity/swaps_test.go b/x/concentrated-liquidity/swaps_test.go index b27d047ec9d..f3ccdde777d 100644 --- a/x/concentrated-liquidity/swaps_test.go +++ b/x/concentrated-liquidity/swaps_test.go @@ -32,7 +32,7 @@ type SwapTest struct { tokenInDenom string // Shared. - priceLimit osmomath.Dec + priceLimit osmomath.BigDec spreadFactor osmomath.Dec secondPositionLowerPrice osmomath.Dec secondPositionUpperPrice osmomath.Dec @@ -84,7 +84,7 @@ var ( "single position within one tick: usdc -> eth": { tokenIn: sdk.NewCoin("usdc", osmomath.NewInt(42000000)), tokenOutDenom: "eth", - priceLimit: osmomath.NewDec(5004), + priceLimit: osmomath.NewBigDec(5004), spreadFactor: osmomath.ZeroDec(), // from math import * // from decimal import * @@ -125,7 +125,7 @@ var ( "single position within one tick: usdc -> eth, with zero price limit": { tokenIn: sdk.NewCoin("usdc", osmomath.NewInt(42000000)), tokenOutDenom: "eth", - priceLimit: osmomath.ZeroDec(), + priceLimit: osmomath.ZeroBigDec(), spreadFactor: osmomath.ZeroDec(), // from math import * // from decimal import * @@ -166,7 +166,7 @@ var ( "single position within one tick: eth -> usdc": { tokenIn: sdk.NewCoin("eth", osmomath.NewInt(13370)), tokenOutDenom: "usdc", - priceLimit: osmomath.NewDec(4993), + priceLimit: osmomath.NewBigDec(4993), spreadFactor: osmomath.ZeroDec(), // from math import * // from decimal import * @@ -203,7 +203,7 @@ var ( "single position within one tick: eth -> usdc, with zero price limit": { tokenIn: sdk.NewCoin("eth", osmomath.NewInt(13370)), tokenOutDenom: "usdc", - priceLimit: osmomath.ZeroDec(), + priceLimit: osmomath.ZeroBigDec(), spreadFactor: osmomath.ZeroDec(), // from math import * // from decimal import * @@ -241,7 +241,7 @@ var ( "two positions within one tick: usdc -> eth": { tokenIn: sdk.NewCoin("usdc", osmomath.NewInt(42000000)), tokenOutDenom: "eth", - priceLimit: osmomath.NewDec(5002), + priceLimit: osmomath.NewBigDec(5002), spreadFactor: osmomath.ZeroDec(), secondPositionLowerPrice: DefaultLowerPrice, secondPositionUpperPrice: DefaultUpperPrice, @@ -277,7 +277,7 @@ var ( "two positions within one tick: eth -> usdc": { tokenIn: sdk.NewCoin("eth", osmomath.NewInt(13370)), tokenOutDenom: "usdc", - priceLimit: osmomath.NewDec(4996), + priceLimit: osmomath.NewBigDec(4996), spreadFactor: osmomath.ZeroDec(), secondPositionLowerPrice: DefaultLowerPrice, secondPositionUpperPrice: DefaultUpperPrice, @@ -321,7 +321,7 @@ var ( "two positions with consecutive price ranges: usdc -> eth": { tokenIn: sdk.NewCoin("usdc", osmomath.NewInt(10000000000)), tokenOutDenom: "eth", - priceLimit: osmomath.NewDec(6255), + priceLimit: osmomath.NewBigDec(6255), spreadFactor: osmomath.ZeroDec(), secondPositionLowerPrice: osmomath.NewDec(5500), secondPositionUpperPrice: osmomath.NewDec(6250), @@ -376,7 +376,7 @@ var ( "two positions with consecutive price ranges: eth -> usdc": { tokenIn: sdk.NewCoin("eth", osmomath.NewInt(2000000)), tokenOutDenom: "usdc", - priceLimit: osmomath.NewDec(3900), + priceLimit: osmomath.NewBigDec(3900), spreadFactor: osmomath.ZeroDec(), secondPositionLowerPrice: osmomath.NewDec(4000), secondPositionUpperPrice: osmomath.NewDec(4545), @@ -433,7 +433,7 @@ var ( "two positions with partially overlapping price ranges: usdc -> eth": { tokenIn: sdk.NewCoin("usdc", osmomath.NewInt(10000000000)), tokenOutDenom: "eth", - priceLimit: osmomath.NewDec(6056), + priceLimit: osmomath.NewBigDec(6056), spreadFactor: osmomath.ZeroDec(), secondPositionLowerPrice: osmomath.NewDec(5001), secondPositionUpperPrice: osmomath.NewDec(6250), @@ -487,7 +487,7 @@ var ( "two positions with partially overlapping price ranges, not utilizing full liquidity of second position: usdc -> eth": { tokenIn: sdk.NewCoin("usdc", osmomath.NewInt(8500000000)), tokenOutDenom: "eth", - priceLimit: osmomath.NewDec(6056), + priceLimit: osmomath.NewBigDec(6056), spreadFactor: osmomath.ZeroDec(), // from math import * // from decimal import * @@ -553,7 +553,7 @@ var ( "two positions with partially overlapping price ranges: eth -> usdc": { tokenIn: sdk.NewCoin("eth", osmomath.NewInt(2000000)), tokenOutDenom: "usdc", - priceLimit: osmomath.NewDec(4128), + priceLimit: osmomath.NewBigDec(4128), spreadFactor: osmomath.ZeroDec(), // from math import * // from decimal import * @@ -617,7 +617,7 @@ var ( "two positions with partially overlapping price ranges, not utilizing full liquidity of second position: eth -> usdc": { tokenIn: sdk.NewCoin("eth", osmomath.NewInt(1800000)), tokenOutDenom: "usdc", - priceLimit: osmomath.NewDec(4128), + priceLimit: osmomath.NewBigDec(4128), spreadFactor: osmomath.ZeroDec(), secondPositionLowerPrice: osmomath.NewDec(4000), secondPositionUpperPrice: osmomath.NewDec(4999), @@ -678,7 +678,7 @@ var ( "two sequential positions with a gap": { tokenIn: sdk.NewCoin("usdc", osmomath.NewInt(10000000000)), tokenOutDenom: "eth", - priceLimit: osmomath.NewDec(6106), + priceLimit: osmomath.NewBigDec(6106), spreadFactor: osmomath.ZeroDec(), secondPositionLowerPrice: osmomath.NewDec(5501), secondPositionUpperPrice: osmomath.NewDec(6250), @@ -734,7 +734,7 @@ var ( "single position within one tick, trade completes but slippage protection interrupts trade early: eth -> usdc": { tokenIn: sdk.NewCoin("eth", osmomath.NewInt(13370)), tokenOutDenom: "usdc", - priceLimit: osmomath.NewDec(4994), + priceLimit: osmomath.NewBigDec(4994), spreadFactor: osmomath.ZeroDec(), // from math import * // from decimal import * @@ -774,7 +774,7 @@ var ( // are estimated by utilizing scripts from scripts/cl/main.py tokenIn: sdk.NewCoin("usdc", osmomath.NewInt(42000000)), tokenOutDenom: "eth", - priceLimit: osmomath.NewDec(5004), + priceLimit: osmomath.NewBigDec(5004), spreadFactor: osmomath.MustNewDecFromStr("0.01"), expectedTokenIn: sdk.NewCoin("usdc", osmomath.NewInt(42000000)), expectedTokenOut: sdk.NewCoin("eth", osmomath.NewInt(8312)), @@ -790,7 +790,7 @@ var ( // are estimated by utilizing scripts from scripts/cl/main.py tokenIn: sdk.NewCoin("eth", osmomath.NewInt(13370)), tokenOutDenom: "usdc", - priceLimit: osmomath.NewDec(4990), + priceLimit: osmomath.NewBigDec(4990), spreadFactor: osmomath.MustNewDecFromStr("0.03"), secondPositionLowerPrice: DefaultLowerPrice, secondPositionUpperPrice: DefaultUpperPrice, @@ -811,7 +811,7 @@ var ( // are estimated by utilizing scripts from scripts/cl/main.py tokenIn: sdk.NewCoin("eth", osmomath.NewInt(2000000)), tokenOutDenom: "usdc", - priceLimit: osmomath.NewDec(4094), + priceLimit: osmomath.NewBigDec(4094), spreadFactor: osmomath.MustNewDecFromStr("0.05"), secondPositionLowerPrice: osmomath.NewDec(4000), secondPositionUpperPrice: osmomath.NewDec(4545), @@ -831,7 +831,7 @@ var ( // are estimated by utilizing scripts from scripts/cl/main.py tokenIn: sdk.NewCoin("usdc", osmomath.NewInt(10000000000)), tokenOutDenom: "eth", - priceLimit: osmomath.NewDec(6056), + priceLimit: osmomath.NewBigDec(6056), spreadFactor: osmomath.MustNewDecFromStr("0.1"), secondPositionLowerPrice: osmomath.NewDec(5001), secondPositionUpperPrice: osmomath.NewDec(6250), @@ -851,7 +851,7 @@ var ( // are estimated by utilizing scripts from scripts/cl/main.py tokenIn: sdk.NewCoin("eth", osmomath.NewInt(1800000)), tokenOutDenom: "usdc", - priceLimit: osmomath.NewDec(4128), + priceLimit: osmomath.NewBigDec(4128), spreadFactor: osmomath.MustNewDecFromStr("0.005"), secondPositionLowerPrice: osmomath.NewDec(4000), secondPositionUpperPrice: osmomath.NewDec(4999), @@ -871,7 +871,7 @@ var ( // are estimated by utilizing scripts from scripts/cl/main.py tokenIn: sdk.NewCoin("usdc", osmomath.NewInt(10000000000)), tokenOutDenom: "eth", - priceLimit: osmomath.NewDec(6106), + priceLimit: osmomath.NewBigDec(6106), secondPositionLowerPrice: osmomath.NewDec(5501), secondPositionUpperPrice: osmomath.NewDec(6250), spreadFactor: osmomath.MustNewDecFromStr("0.03"), @@ -890,7 +890,7 @@ var ( // are estimated by utilizing scripts from scripts/cl/main.py tokenIn: sdk.NewCoin("eth", osmomath.NewInt(13370)), tokenOutDenom: "usdc", - priceLimit: osmomath.NewDec(4994), + priceLimit: osmomath.NewBigDec(4994), spreadFactor: osmomath.MustNewDecFromStr("0.01"), expectedTokenIn: sdk.NewCoin("eth", osmomath.NewInt(13023)), expectedTokenOut: sdk.NewCoin("usdc", osmomath.NewInt(64417624)), @@ -907,14 +907,14 @@ var ( "single position within one tick, trade does not complete due to lack of liquidity: usdc -> eth": { tokenIn: sdk.NewCoin("usdc", osmomath.NewInt(5300000000)), tokenOutDenom: "eth", - priceLimit: osmomath.NewDec(6000), + priceLimit: osmomath.NewBigDec(6000), spreadFactor: osmomath.ZeroDec(), expectErr: true, }, "single position within one tick, trade does not complete due to lack of liquidity: eth -> usdc": { tokenIn: sdk.NewCoin("eth", osmomath.NewInt(1100000)), tokenOutDenom: "usdc", - priceLimit: osmomath.NewDec(4000), + priceLimit: osmomath.NewBigDec(4000), spreadFactor: osmomath.ZeroDec(), expectErr: true, }, @@ -929,7 +929,7 @@ var ( "single position within one tick: eth (in) -> usdc (out) | zfo": { tokenOut: sdk.NewCoin(USDC, osmomath.NewInt(42000000)), tokenInDenom: ETH, - priceLimit: osmomath.NewDec(4993), + priceLimit: osmomath.NewBigDec(4993), spreadFactor: osmomath.ZeroDec(), // from math import * // from decimal import * @@ -958,7 +958,7 @@ var ( "single position within one tick: usdc (in) -> eth (out) ofz": { tokenOut: sdk.NewCoin(ETH, osmomath.NewInt(13370)), tokenInDenom: USDC, - priceLimit: osmomath.NewDec(5010), + priceLimit: osmomath.NewBigDec(5010), spreadFactor: osmomath.ZeroDec(), // from math import * // from decimal import * @@ -987,7 +987,7 @@ var ( "two positions within one tick: eth (in) -> usdc (out) | zfo": { tokenOut: sdk.NewCoin("usdc", osmomath.NewInt(66829187)), tokenInDenom: "eth", - priceLimit: osmomath.NewDec(4990), + priceLimit: osmomath.NewBigDec(4990), spreadFactor: osmomath.ZeroDec(), secondPositionLowerPrice: DefaultLowerPrice, secondPositionUpperPrice: DefaultUpperPrice, @@ -1024,7 +1024,7 @@ var ( "two positions within one tick: usdc (in) -> eth (out) | ofz": { tokenOut: sdk.NewCoin("eth", osmomath.NewInt(8398)), tokenInDenom: "usdc", - priceLimit: osmomath.NewDec(5020), + priceLimit: osmomath.NewBigDec(5020), spreadFactor: osmomath.ZeroDec(), secondPositionLowerPrice: DefaultLowerPrice, secondPositionUpperPrice: DefaultUpperPrice, @@ -1058,7 +1058,7 @@ var ( "two positions with consecutive price ranges: eth (in) -> usdc (out) | zfo": { tokenOut: sdk.NewCoin("usdc", osmomath.NewInt(9103422788)), tokenInDenom: "eth", - priceLimit: osmomath.NewDec(3900), + priceLimit: osmomath.NewBigDec(3900), spreadFactor: osmomath.ZeroDec(), secondPositionLowerPrice: osmomath.NewDec(4000), secondPositionUpperPrice: osmomath.NewDec(4545), @@ -1119,7 +1119,7 @@ var ( "two positions with consecutive price ranges: usdc (in) -> eth (out) | ofz": { tokenOut: sdk.NewCoin(ETH, osmomath.NewInt(1820630)), tokenInDenom: USDC, - priceLimit: osmomath.NewDec(6106), + priceLimit: osmomath.NewBigDec(6106), spreadFactor: osmomath.ZeroDec(), secondPositionLowerPrice: osmomath.NewDec(5500), // 315000 secondPositionUpperPrice: osmomath.NewDec(6250), // 322500 @@ -1177,7 +1177,7 @@ var ( "two positions with partially overlapping price ranges: eth (in) -> usdc (out) | zfo": { tokenOut: sdk.NewCoin(USDC, osmomath.NewInt(9321276930)), tokenInDenom: ETH, - priceLimit: osmomath.NewDec(4128), + priceLimit: osmomath.NewBigDec(4128), spreadFactor: osmomath.ZeroDec(), secondPositionLowerPrice: osmomath.NewDec(4000), secondPositionUpperPrice: osmomath.NewDec(4999), @@ -1246,7 +1246,7 @@ var ( "two positions with partially overlapping price ranges, not utilizing full liquidity of second position: eth (in) -> usdc (out) | zfo": { tokenOut: sdk.NewCoin(USDC, osmomath.NewInt(8479320318)), tokenInDenom: ETH, - priceLimit: osmomath.NewDec(4128), + priceLimit: osmomath.NewBigDec(4128), spreadFactor: osmomath.ZeroDec(), secondPositionLowerPrice: osmomath.NewDec(4000), secondPositionUpperPrice: osmomath.NewDec(4999), @@ -1317,7 +1317,7 @@ var ( "two positions with partially overlapping price ranges: usdc (in) -> eth (out) | ofz": { tokenOut: sdk.NewCoin(ETH, osmomath.NewInt(1864161)), tokenInDenom: USDC, - priceLimit: osmomath.NewDec(6056), + priceLimit: osmomath.NewBigDec(6056), spreadFactor: osmomath.ZeroDec(), secondPositionLowerPrice: osmomath.NewDec(5001), secondPositionUpperPrice: osmomath.NewDec(6250), @@ -1378,7 +1378,7 @@ var ( "two positions with partially overlapping price ranges, not utilizing full liquidity of second position: usdc (in) -> eth (out) | ofz": { tokenOut: sdk.NewCoin(ETH, osmomath.NewInt(1609138)), tokenInDenom: USDC, - priceLimit: osmomath.NewDec(6056), + priceLimit: osmomath.NewBigDec(6056), spreadFactor: osmomath.ZeroDec(), secondPositionLowerPrice: osmomath.NewDec(5001), secondPositionUpperPrice: osmomath.NewDec(6250), @@ -1451,7 +1451,7 @@ var ( "two sequential positions with a gap usdc (in) -> eth (out) | ofz": { tokenOut: sdk.NewCoin(ETH, osmomath.NewInt(1820545)), tokenInDenom: USDC, - priceLimit: osmomath.NewDec(6106), + priceLimit: osmomath.NewBigDec(6106), spreadFactor: osmomath.ZeroDec(), secondPositionLowerPrice: osmomath.NewDec(5501), // 315010 secondPositionUpperPrice: osmomath.NewDec(6250), // 322500 @@ -1504,7 +1504,7 @@ var ( "single position within one tick, trade completes but slippage protection interrupts trade early: usdc (in) -> eth (out) | ofz": { tokenOut: sdk.NewCoin(ETH, osmomath.NewInt(1820545)), tokenInDenom: USDC, - priceLimit: osmomath.NewDec(5002), + priceLimit: osmomath.NewBigDec(5002), spreadFactor: osmomath.ZeroDec(), // from math import * // from decimal import * @@ -1532,7 +1532,7 @@ var ( "spread factor 1: single position within one tick: eth (in) -> usdc (out) (1% spread factor) | zfo": { tokenOut: sdk.NewCoin(USDC, osmomath.NewInt(42000000)), tokenInDenom: ETH, - priceLimit: osmomath.NewDec(4993), + priceLimit: osmomath.NewBigDec(4993), spreadFactor: osmomath.MustNewDecFromStr("0.01"), // from math import * // from decimal import * @@ -1569,7 +1569,7 @@ var ( "spread factor 2: two positions within one tick: usdc (in) -> eth (out) (3% spread factor) | ofz": { tokenOut: sdk.NewCoin(ETH, osmomath.NewInt(8398)), tokenInDenom: USDC, - priceLimit: osmomath.NewDec(5020), + priceLimit: osmomath.NewBigDec(5020), spreadFactor: osmomath.MustNewDecFromStr("0.03"), secondPositionLowerPrice: DefaultLowerPrice, secondPositionUpperPrice: DefaultUpperPrice, @@ -1612,7 +1612,7 @@ var ( "spread factor 3: two positions with consecutive price ranges: usdc (in) -> eth (out) (0.1% spread factor) | ofz": { tokenOut: sdk.NewCoin(ETH, osmomath.NewInt(1820630)), tokenInDenom: USDC, - priceLimit: osmomath.NewDec(6106), + priceLimit: osmomath.NewBigDec(6106), spreadFactor: osmomath.MustNewDecFromStr("0.001"), secondPositionLowerPrice: osmomath.NewDec(5500), // 315000 secondPositionUpperPrice: osmomath.NewDec(6250), // 322500 @@ -1671,7 +1671,7 @@ var ( "spread factor 4: two positions with partially overlapping price ranges: eth (in) -> usdc (out) (10% spread factor) | zfo": { tokenOut: sdk.NewCoin(USDC, osmomath.NewInt(9321276930)), tokenInDenom: ETH, - priceLimit: osmomath.NewDec(4128), + priceLimit: osmomath.NewBigDec(4128), spreadFactor: osmomath.MustNewDecFromStr("0.1"), secondPositionLowerPrice: osmomath.NewDec(4000), secondPositionUpperPrice: osmomath.NewDec(4999), @@ -1743,7 +1743,7 @@ var ( "spread factor 5: two positions with partially overlapping price ranges, not utilizing full liquidity of second position: usdc (in) -> eth (out) (5% spread factor) | ofz": { tokenOut: sdk.NewCoin(ETH, osmomath.NewInt(1609138)), tokenInDenom: USDC, - priceLimit: osmomath.NewDec(6056), + priceLimit: osmomath.NewBigDec(6056), spreadFactor: osmomath.MustNewDecFromStr("0.05"), secondPositionLowerPrice: osmomath.NewDec(5001), secondPositionUpperPrice: osmomath.NewDec(6250), @@ -1811,7 +1811,7 @@ var ( "spread factor 6: two sequential positions with a gap usdc (in) -> eth (out) (0.03% spread factor) | ofz": { tokenOut: sdk.NewCoin(ETH, osmomath.NewInt(1820545)), tokenInDenom: USDC, - priceLimit: osmomath.NewDec(6106), + priceLimit: osmomath.NewBigDec(6106), spreadFactor: osmomath.MustNewDecFromStr("0.0003"), secondPositionLowerPrice: osmomath.NewDec(5501), // 315010 secondPositionUpperPrice: osmomath.NewDec(6250), // 322500 @@ -1869,7 +1869,7 @@ var ( "spread factor 7: single position within one tick, trade completes but slippage protection interrupts trade early: usdc (in) -> eth (out) (1% spread factor) | ofz": { tokenOut: sdk.NewCoin(ETH, osmomath.NewInt(1820545)), tokenInDenom: USDC, - priceLimit: osmomath.NewDec(5002), + priceLimit: osmomath.NewBigDec(5002), spreadFactor: osmomath.MustNewDecFromStr("0.01"), // from math import * // from decimal import * @@ -1902,14 +1902,14 @@ var ( "single position within one tick, trade does not complete due to lack of liquidity: usdc -> eth ": { tokenOut: sdk.NewCoin("usdc", osmomath.NewInt(5300000000)), tokenInDenom: "eth", - priceLimit: osmomath.NewDec(6000), + priceLimit: osmomath.NewBigDec(6000), spreadFactor: osmomath.ZeroDec(), expectErr: true, }, "single position within one tick, trade does not complete due to lack of liquidity: eth -> usdc ": { tokenOut: sdk.NewCoin("eth", osmomath.NewInt(1100000)), tokenInDenom: "usdc", - priceLimit: osmomath.NewDec(4000), + priceLimit: osmomath.NewBigDec(4000), spreadFactor: osmomath.ZeroDec(), expectErr: true, }, @@ -2115,7 +2115,7 @@ func (s *KeeperTestSuite) TestSwap_NoPositions() { _, _, _, err := s.App.ConcentratedLiquidityKeeper.SwapInAmtGivenOut( s.Ctx, s.TestAccs[0], pool, DefaultCoin0, DefaultCoin1.Denom, - osmomath.ZeroDec(), osmomath.ZeroDec(), + osmomath.ZeroDec(), osmomath.ZeroBigDec(), ) s.Require().Error(err) s.Require().ErrorIs(err, types.NoSpotPriceWhenNoLiquidityError{PoolId: pool.GetId()}) @@ -2123,7 +2123,7 @@ func (s *KeeperTestSuite) TestSwap_NoPositions() { _, _, _, err = s.App.ConcentratedLiquidityKeeper.SwapOutAmtGivenIn( s.Ctx, s.TestAccs[0], pool, DefaultCoin0, DefaultCoin1.Denom, - osmomath.ZeroDec(), osmomath.ZeroDec(), + osmomath.ZeroDec(), osmomath.ZeroBigDec(), ) s.Require().Error(err) @@ -2837,7 +2837,7 @@ func (s *KeeperTestSuite) TestInverseRelationshipSwapOutAmtGivenIn() { secondTokenIn, secondTokenOut, _, err := s.App.ConcentratedLiquidityKeeper.SwapOutAmtGivenIn( s.Ctx, s.TestAccs[0], poolBefore, firstTokenOut, firstTokenIn.Denom, - DefaultZeroSpreadFactor, osmomath.ZeroDec(), + DefaultZeroSpreadFactor, osmomath.ZeroBigDec(), ) s.Require().NoError(err) @@ -2914,7 +2914,7 @@ func (s *KeeperTestSuite) TestInverseRelationshipSwapInAmtGivenOut() { secondTokenIn, secondTokenOut, _, err := s.App.ConcentratedLiquidityKeeper.SwapInAmtGivenOut( s.Ctx, s.TestAccs[0], poolBefore, firstTokenIn, firstTokenOut.Denom, - DefaultZeroSpreadFactor, osmomath.ZeroDec(), + DefaultZeroSpreadFactor, osmomath.ZeroBigDec(), ) s.Require().NoError(err) @@ -3180,7 +3180,7 @@ func (s *KeeperTestSuite) TestFunctionalSwaps() { // 5000 // Swap multiple times USDC for ETH, therefore increasing the spot price - _, _, totalTokenIn, totalTokenOut := s.swapAndTrackXTimesInARow(clPool.GetId(), swapCoin1, ETH, types.MaxSpotPrice, positions.numSwaps) + _, _, totalTokenIn, totalTokenOut := s.swapAndTrackXTimesInARow(clPool.GetId(), swapCoin1, ETH, types.MaxSpotPriceBigDec, positions.numSwaps) clPool, err := s.App.ConcentratedLiquidityKeeper.GetPoolById(s.Ctx, clPool.GetId()) s.Require().NoError(err) @@ -3229,7 +3229,7 @@ func (s *KeeperTestSuite) TestFunctionalSwaps() { } // Swap multiple times ETH for USDC, therefore decreasing the spot price - _, _, totalTokenIn, totalTokenOut = s.swapAndTrackXTimesInARow(clPool.GetId(), swapCoin0, USDC, types.MinSpotPrice, positions.numSwaps) + _, _, totalTokenIn, totalTokenOut = s.swapAndTrackXTimesInARow(clPool.GetId(), swapCoin0, USDC, types.MinSpotPriceBigDec, positions.numSwaps) clPool, err = s.App.ConcentratedLiquidityKeeper.GetPoolById(s.Ctx, clPool.GetId()) s.Require().NoError(err) @@ -3289,7 +3289,7 @@ func (s *KeeperTestSuite) TestFunctionalSwaps() { } // Swap multiple times USDC for ETH, therefore increasing the spot price - _, _, totalTokenIn, totalTokenOut = s.swapAndTrackXTimesInARow(clPool.GetId(), swapCoin1, ETH, types.MaxSpotPrice, positions.numSwaps) + _, _, totalTokenIn, totalTokenOut = s.swapAndTrackXTimesInARow(clPool.GetId(), swapCoin1, ETH, types.MaxSpotPriceBigDec, positions.numSwaps) clPool, err = s.App.ConcentratedLiquidityKeeper.GetPoolById(s.Ctx, clPool.GetId()) s.Require().NoError(err) @@ -3349,7 +3349,7 @@ func (s *KeeperTestSuite) TestFunctionalSwaps() { } // Swap multiple times ETH for USDC, therefore decreasing the spot price - _, _, totalTokenIn, totalTokenOut = s.swapAndTrackXTimesInARow(clPool.GetId(), swapCoin0, USDC, types.MinSpotPrice, positions.numSwaps) + _, _, totalTokenIn, totalTokenOut = s.swapAndTrackXTimesInARow(clPool.GetId(), swapCoin0, USDC, types.MinSpotPriceBigDec, positions.numSwaps) clPool, err = s.App.ConcentratedLiquidityKeeper.GetPoolById(s.Ctx, clPool.GetId()) s.Require().NoError(err) @@ -3404,11 +3404,11 @@ func (s *KeeperTestSuite) TestInfiniteSwapLoop_OutGivenIn() { swapEthFunded := sdk.NewCoin(ETH, osmomath.Int(osmomath.MustNewDecFromStr("10000000000000000000000000000000000000000"))) swapUSDCFunded := sdk.NewCoin(USDC, osmomath.Int(osmomath.MustNewDecFromStr("10000"))) s.FundAcc(swapAddress, sdk.NewCoins(swapEthFunded, swapUSDCFunded)) - _, tokenOut, _, err := s.clk.SwapInAmtGivenOut(s.Ctx, swapAddress, pool, sdk.NewCoin(USDC, osmomath.NewInt(10000)), ETH, pool.GetSpreadFactor(s.Ctx), osmomath.ZeroDec()) + _, tokenOut, _, err := s.clk.SwapInAmtGivenOut(s.Ctx, swapAddress, pool, sdk.NewCoin(USDC, osmomath.NewInt(10000)), ETH, pool.GetSpreadFactor(s.Ctx), osmomath.ZeroBigDec()) s.Require().NoError(err) // Swap back in the amount that was swapped out to test the inverse relationship - _, _, _, err = s.clk.SwapOutAmtGivenIn(s.Ctx, swapAddress, pool, tokenOut, ETH, pool.GetSpreadFactor(s.Ctx), osmomath.ZeroDec()) + _, _, _, err = s.clk.SwapOutAmtGivenIn(s.Ctx, swapAddress, pool, tokenOut, ETH, pool.GetSpreadFactor(s.Ctx), osmomath.ZeroBigDec()) s.Require().NoError(err) } diff --git a/x/concentrated-liquidity/swaps_tick_cross_test.go b/x/concentrated-liquidity/swaps_tick_cross_test.go index c6e2b71291f..38122af99c2 100644 --- a/x/concentrated-liquidity/swaps_tick_cross_test.go +++ b/x/concentrated-liquidity/swaps_tick_cross_test.go @@ -74,7 +74,7 @@ func (s *KeeperTestSuite) validateIteratorLeftZeroForOne(poolId uint64, expected pool, err := s.App.ConcentratedLiquidityKeeper.GetPoolById(s.Ctx, poolId) s.Require().NoError(err) - zeroForOneSwapStrategy, _, err := s.App.ConcentratedLiquidityKeeper.SetupSwapStrategy(s.Ctx, pool, osmomath.ZeroDec(), pool.GetToken0(), types.MinSqrtPrice) + zeroForOneSwapStrategy, _, err := s.App.ConcentratedLiquidityKeeper.SetupSwapStrategy(s.Ctx, pool, osmomath.ZeroDec(), pool.GetToken0(), types.MinSqrtPriceBigDec) s.Require().NoError(err) initializedTickValue := pool.GetCurrentTick() iter := zeroForOneSwapStrategy.InitializeNextTickIterator(s.Ctx, pool.GetId(), initializedTickValue) diff --git a/x/concentrated-liquidity/swapstrategy/swap_strategy.go b/x/concentrated-liquidity/swapstrategy/swap_strategy.go index fa2a02ec0f3..cd7d346334d 100644 --- a/x/concentrated-liquidity/swapstrategy/swap_strategy.go +++ b/x/concentrated-liquidity/swapstrategy/swap_strategy.go @@ -104,25 +104,47 @@ func New(zeroForOne bool, sqrtPriceLimit osmomath.BigDec, storeKey sdk.StoreKey, // GetPriceLimit returns the price limit based on which token is being swapped in. // If zero in for one out, the price is decreasing. Therefore, min spot price is the limit. // If one in for zero out, the price is increasing. Therefore, max spot price is the limit. -func GetPriceLimit(zeroForOne bool) osmomath.Dec { +func GetPriceLimit(zeroForOne bool) osmomath.BigDec { if zeroForOne { - return types.MinSpotPrice + return types.MinSpotPriceBigDec } - return types.MaxSpotPrice + return types.MaxSpotPriceBigDec } -func GetSqrtPriceLimit(priceLimit osmomath.Dec, zeroForOne bool) (osmomath.BigDec, error) { +func GetSqrtPriceLimit(priceLimit osmomath.BigDec, zeroForOne bool) (osmomath.BigDec, error) { if priceLimit.IsZero() { if zeroForOne { - return osmomath.BigDecFromDec(types.MinSqrtPrice), nil + return types.MinSqrtPriceBigDec, nil } - return osmomath.BigDecFromDec(types.MaxSqrtPrice), nil + + return types.MaxSqrtPriceBigDec, nil + } + + if priceLimit.LT(types.MinSpotPriceV2) || priceLimit.GT(types.MaxSpotPriceBigDec) { + return osmomath.BigDec{}, types.PriceBoundError{ProvidedPrice: priceLimit, MinSpotPrice: types.MinSpotPriceV2, MaxSpotPrice: types.MaxSpotPrice} + } + + // To keep state-compatibility with the original at-launch price range + // we utilize the same sqrt price function. + if priceLimit.GTE(types.MinSpotPriceBigDec) { + // Truncation is fine since previous Osmosis version only supported + // 18 decimal price ranges. + sqrtPriceLimit, err := osmomath.MonotonicSqrt(priceLimit.Dec()) + if err != nil { + return osmomath.BigDec{}, err + } + if err != nil { + return osmomath.BigDec{}, err + } + return osmomath.BigDecFromDec(sqrtPriceLimit), nil } - sqrtPriceLimit, err := osmomath.MonotonicSqrt(priceLimit) + // On the newly extended lower price range, utilize the 36 decimal + // sqrt. + sqrtPriceLimit, err := osmomath.MonotonicSqrtBigDec(priceLimit) if err != nil { return osmomath.BigDec{}, err } - return osmomath.BigDecFromDec(sqrtPriceLimit), nil + return sqrtPriceLimit, nil } diff --git a/x/concentrated-liquidity/swapstrategy/swap_strategy_test.go b/x/concentrated-liquidity/swapstrategy/swap_strategy_test.go index 7b5133e408f..3b3fae53c02 100644 --- a/x/concentrated-liquidity/swapstrategy/swap_strategy_test.go +++ b/x/concentrated-liquidity/swapstrategy/swap_strategy_test.go @@ -37,6 +37,7 @@ var ( three = osmomath.NewDec(3) four = osmomath.NewDec(4) five = osmomath.NewDec(5) + zeroBigDec = osmomath.ZeroBigDec() sqrt5000 = osmomath.MustNewDecFromStr("70.710678118654752440") // 5000 defaultSqrtPriceLower = osmomath.MustNewDecFromStr("70.688664163408836321") // approx 4996.89 defaultSqrtPriceUpper = sqrt5000 @@ -49,7 +50,7 @@ var ( defaultAmountReserves = osmomath.NewInt(1_000_000_000) DefaultCoins = sdk.NewCoins(sdk.NewCoin(ETH, defaultAmountReserves), sdk.NewCoin(USDC, defaultAmountReserves)) oneULPDec = osmomath.SmallestDec() - oneULPBigDec = osmomath.SmallestDec() + oneULPBigDec = osmomath.SmallestBigDec() ) func TestStrategyTestSuite(t *testing.T) { @@ -251,15 +252,15 @@ func (suite *StrategyTestSuite) TestComputeSwapState_Inverse() { func (suite *StrategyTestSuite) TestGetPriceLimit() { tests := map[string]struct { zeroForOne bool - expected osmomath.Dec + expected osmomath.BigDec }{ "zero for one -> min": { zeroForOne: true, - expected: types.MinSpotPrice, + expected: types.MinSpotPriceBigDec, }, "one for zero -> max": { zeroForOne: false, - expected: types.MaxSpotPrice, + expected: types.MaxSpotPriceBigDec, }, } diff --git a/x/concentrated-liquidity/swapstrategy/zero_for_one.go b/x/concentrated-liquidity/swapstrategy/zero_for_one.go index 92f7358c9d5..8cd120e590d 100644 --- a/x/concentrated-liquidity/swapstrategy/zero_for_one.go +++ b/x/concentrated-liquidity/swapstrategy/zero_for_one.go @@ -252,7 +252,7 @@ func (s zeroForOneStrategy) UpdateTickAfterCrossing(nextTick int64) int64 { // types.MinSqrtRatio <= sqrtPrice <= current square root price func (s zeroForOneStrategy) ValidateSqrtPrice(sqrtPrice osmomath.BigDec, currentSqrtPrice osmomath.BigDec) error { // check that the price limit is below the current sqrt price but not lower than the minimum sqrt price if we are swapping asset0 for asset1 - if sqrtPrice.GT(currentSqrtPrice) || sqrtPrice.LT(osmomath.BigDecFromDec(types.MinSqrtPrice)) { + if sqrtPrice.GT(currentSqrtPrice) || sqrtPrice.LT(types.MinSqrtPriceBigDec) { return types.SqrtPriceValidationError{SqrtPriceLimit: sqrtPrice, LowerBound: types.MinSqrtPriceBigDec, UpperBound: currentSqrtPrice} } return nil