From 73316891f43545333704a372ccb58ef18c122950 Mon Sep 17 00:00:00 2001 From: doggystylez <98429202+doggystylez@users.noreply.github.com> Date: Tue, 1 Aug 2023 23:45:56 -0400 Subject: [PATCH] fix: cl spotprice query (#5863) * fix: cl spotprice * fix tests * Update CHANGELOG.md * swap base quote for twap e2e --------- Co-authored-by: Adam Tucker --- CHANGELOG.md | 1 + tests/e2e/e2e_test.go | 10 +++++----- x/concentrated-liquidity/model/pool.go | 2 +- x/concentrated-liquidity/model/pool_test.go | 2 +- x/concentrated-liquidity/pool_test.go | 9 +++++---- x/concentrated-liquidity/swaps_test.go | 4 ++-- x/poolmanager/client/cli/query.go | 2 +- x/poolmanager/router_test.go | 4 ++-- 8 files changed, 18 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ea2c07fa00..034411997db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### State Breaking * [#5532](https://github.com/osmosis-labs/osmosis/pull/5532) fix: Fix x/tokenfactory genesis import denoms reset x/bank existing denom metadata +* [#5863](https://github.com/osmosis-labs/osmosis/pull/5863) fix: swap base/quote asset for CL spot price query * [#5869](https://github.com/osmosis-labs/osmosis/pull/5869) fix negative interval accumulation with spread rewards * [#5872](https://github.com/osmosis-labs/osmosis/pull/5872) fix negative interval accumulation with incentive rewards * [#5883](https://github.com/osmosis-labs/osmosis/pull/5883) feat: Uninitialize empty ticks diff --git a/tests/e2e/e2e_test.go b/tests/e2e/e2e_test.go index 8afcd8e0408..b8dcebbd6ca 100644 --- a/tests/e2e/e2e_test.go +++ b/tests/e2e/e2e_test.go @@ -1071,7 +1071,7 @@ func (s *IntegrationTestSuite) CreateConcentratedLiquidityPoolVoting_And_TWAP() // Check initial TWAP // We expect this to error since there is no spot price yet. s.T().Log("initial twap check") - initialTwapBOverA, err := chainANode.QueryGeometricTwapToNow(concentratedPool.GetId(), concentratedPool.GetToken0(), concentratedPool.GetToken1(), timeBeforePositionCreationBeforeSwap) + initialTwapBOverA, err := chainANode.QueryGeometricTwapToNow(concentratedPool.GetId(), concentratedPool.GetToken1(), concentratedPool.GetToken0(), timeBeforePositionCreationBeforeSwap) s.Require().Error(err) s.Require().Equal(sdk.Dec{}, initialTwapBOverA) @@ -1080,7 +1080,7 @@ func (s *IntegrationTestSuite) CreateConcentratedLiquidityPoolVoting_And_TWAP() chainANode.CreateConcentratedPosition(address1, "[-120000]", "40000", fmt.Sprintf("10000000%s,20000000%s", concentratedPool.GetToken0(), concentratedPool.GetToken1()), 0, 0, concentratedPool.GetId()) timeAfterPositionCreationBeforeSwap := chainANode.QueryLatestBlockTime() chainA.WaitForNumHeights(2) - firstPositionTwapBOverA, err := chainANode.QueryGeometricTwapToNow(concentratedPool.GetId(), concentratedPool.GetToken0(), concentratedPool.GetToken1(), timeAfterPositionCreationBeforeSwap) + firstPositionTwapBOverA, err := chainANode.QueryGeometricTwapToNow(concentratedPool.GetId(), concentratedPool.GetToken1(), concentratedPool.GetToken0(), timeAfterPositionCreationBeforeSwap) s.Require().NoError(err) s.Require().Equal(sdk.MustNewDecFromStr("0.5"), firstPositionTwapBOverA) @@ -1094,7 +1094,7 @@ func (s *IntegrationTestSuite) CreateConcentratedLiquidityPoolVoting_And_TWAP() timeAfterSwapPlus1Height := chainANode.QueryLatestBlockTime() s.T().Log("querying for the TWAP after swap") - afterSwapTwapBOverA, err := chainANode.QueryGeometricTwap(concentratedPool.GetId(), concentratedPool.GetToken0(), concentratedPool.GetToken1(), timeAfterSwap, timeAfterSwapPlus1Height) + afterSwapTwapBOverA, err := chainANode.QueryGeometricTwap(concentratedPool.GetId(), concentratedPool.GetToken1(), concentratedPool.GetToken0(), timeAfterSwap, timeAfterSwapPlus1Height) s.Require().NoError(err) // We swap stake so uosmo's supply will decrease and stake will increase. @@ -1108,7 +1108,7 @@ func (s *IntegrationTestSuite) CreateConcentratedLiquidityPoolVoting_And_TWAP() chainA.WaitForNumHeights(1) s.T().Log("querying for the TWAP from after pool drained") - afterRemoveTwapBOverA, err := chainANode.QueryGeometricTwapToNow(concentratedPool.GetId(), concentratedPool.GetToken0(), concentratedPool.GetToken1(), timeAfterSwapPlus1Height) + afterRemoveTwapBOverA, err := chainANode.QueryGeometricTwapToNow(concentratedPool.GetId(), concentratedPool.GetToken1(), concentratedPool.GetToken0(), timeAfterSwapPlus1Height) s.Require().Error(err) s.Require().Equal(sdk.Dec{}, afterRemoveTwapBOverA) @@ -1118,7 +1118,7 @@ func (s *IntegrationTestSuite) CreateConcentratedLiquidityPoolVoting_And_TWAP() chainANode.CreateConcentratedPosition(address1, "[-120000]", "40000", fmt.Sprintf("10000000%s,10000000%s", concentratedPool.GetToken0(), concentratedPool.GetToken1()), 0, 0, concentratedPool.GetId()) chainA.WaitForNumHeights(1) timeAfterSwapRemoveAndCreatePlus1Height := chainANode.QueryLatestBlockTime() - secondTwapBOverA, err := chainANode.QueryGeometricTwapToNow(concentratedPool.GetId(), concentratedPool.GetToken0(), concentratedPool.GetToken1(), timeAfterSwapRemoveAndCreatePlus1Height) + secondTwapBOverA, err := chainANode.QueryGeometricTwapToNow(concentratedPool.GetId(), concentratedPool.GetToken1(), concentratedPool.GetToken0(), timeAfterSwapRemoveAndCreatePlus1Height) s.Require().NoError(err) s.Require().Equal(sdk.NewDec(1), secondTwapBOverA) } diff --git a/x/concentrated-liquidity/model/pool.go b/x/concentrated-liquidity/model/pool.go index f1ab7647984..8d0d825b26e 100644 --- a/x/concentrated-liquidity/model/pool.go +++ b/x/concentrated-liquidity/model/pool.go @@ -108,7 +108,7 @@ func (p Pool) IsActive(ctx sdk.Context) bool { // SpotPrice returns the spot price of the pool. // If base asset is the Token0 of the pool, we use the current sqrt price of the pool. // If not, we calculate the inverse of the current sqrt price of the pool. -func (p Pool) SpotPrice(ctx sdk.Context, baseAssetDenom string, quoteAssetDenom string) (sdk.Dec, error) { +func (p Pool) SpotPrice(ctx sdk.Context, quoteAssetDenom string, baseAssetDenom string) (sdk.Dec, error) { // validate base asset is in pool if baseAssetDenom != p.Token0 && baseAssetDenom != p.Token1 { return sdk.Dec{}, fmt.Errorf("base asset denom (%s) is not in pool with (%s, %s) pair", baseAssetDenom, p.Token0, p.Token1) diff --git a/x/concentrated-liquidity/model/pool_test.go b/x/concentrated-liquidity/model/pool_test.go index 7a740e51540..1bbab8baf51 100644 --- a/x/concentrated-liquidity/model/pool_test.go +++ b/x/concentrated-liquidity/model/pool_test.go @@ -206,7 +206,7 @@ func (s *ConcentratedPoolTestSuite) TestSpotPrice() { } // Check the spot price of the mock pool using the SpotPrice method. - spotPriceFromMethod, err := mock_pool.SpotPrice(sdk.Context{}, tc.param.baseDenom, tc.param.quoteDenom) + spotPriceFromMethod, err := mock_pool.SpotPrice(sdk.Context{}, tc.param.quoteDenom, tc.param.baseDenom) if tc.expectedErr != nil { s.Require().Error(err) diff --git a/x/concentrated-liquidity/pool_test.go b/x/concentrated-liquidity/pool_test.go index cda96fa6f0b..41c6ec3616c 100644 --- a/x/concentrated-liquidity/pool_test.go +++ b/x/concentrated-liquidity/pool_test.go @@ -250,14 +250,15 @@ func (s *KeeperTestSuite) TestCalculateSpotPrice() { // set up default position to have proper spot price s.SetupDefaultPosition(defaultPoolId) - spotPriceBaseUSDC, err := s.App.ConcentratedLiquidityKeeper.CalculateSpotPrice(s.Ctx, poolId, ETH, USDC) + // ETH is token0 so its price will be the DefaultCurrSqrtPrice squared + spotPriceBaseETH, err := s.App.ConcentratedLiquidityKeeper.CalculateSpotPrice(s.Ctx, poolId, USDC, ETH) s.Require().NoError(err) - s.Require().Equal(spotPriceBaseUSDC, DefaultCurrSqrtPrice.PowerInteger(2).SDKDec()) + s.Require().Equal(spotPriceBaseETH, DefaultCurrSqrtPrice.PowerInteger(2).SDKDec()) // test that we have correct values for reversed quote asset and base asset - spotPriceBaseETH, err := s.App.ConcentratedLiquidityKeeper.CalculateSpotPrice(s.Ctx, poolId, USDC, ETH) + spotPriceBaseUSDC, err := s.App.ConcentratedLiquidityKeeper.CalculateSpotPrice(s.Ctx, poolId, ETH, USDC) s.Require().NoError(err) - s.Require().Equal(spotPriceBaseETH, osmomath.OneDec().Quo(DefaultCurrSqrtPrice.PowerInteger(2)).SDKDec()) + s.Require().Equal(spotPriceBaseUSDC, osmomath.OneDec().Quo(DefaultCurrSqrtPrice.PowerInteger(2)).SDKDec()) // try getting spot price from a non-existent pool spotPrice, err = s.App.ConcentratedLiquidityKeeper.CalculateSpotPrice(s.Ctx, poolId+1, USDC, ETH) diff --git a/x/concentrated-liquidity/swaps_test.go b/x/concentrated-liquidity/swaps_test.go index ac2bf255352..a5a30236155 100644 --- a/x/concentrated-liquidity/swaps_test.go +++ b/x/concentrated-liquidity/swaps_test.go @@ -3081,9 +3081,9 @@ func (s *KeeperTestSuite) inverseRelationshipInvariants(firstTokenIn, firstToken s.Require().Equal(liquidityBefore, liquidityAfter) // Within a margin of error, the spot price should be the same before and after the swap - oldSpotPrice, err := poolBefore.SpotPrice(s.Ctx, pool.GetToken0(), pool.GetToken1()) + oldSpotPrice, err := poolBefore.SpotPrice(s.Ctx, pool.GetToken1(), pool.GetToken0()) s.Require().NoError(err) - newSpotPrice, err := poolAfter.SpotPrice(s.Ctx, pool.GetToken0(), pool.GetToken1()) + newSpotPrice, err := poolAfter.SpotPrice(s.Ctx, pool.GetToken1(), pool.GetToken0()) s.Require().NoError(err) multiplicativeTolerance = osmomath.ErrTolerance{ MultiplicativeTolerance: sdk.MustNewDecFromStr("0.001"), diff --git a/x/poolmanager/client/cli/query.go b/x/poolmanager/client/cli/query.go index 4421899ff6c..7d4eea54606 100644 --- a/x/poolmanager/client/cli/query.go +++ b/x/poolmanager/client/cli/query.go @@ -91,7 +91,7 @@ func GetCmdPool() (*osmocli.QueryDescriptor, *queryproto.PoolRequest) { func GetCmdSpotPrice() (*osmocli.QueryDescriptor, *queryproto.SpotPriceRequest) { return &osmocli.QueryDescriptor{ - Use: "spot-price [quote-asset-denom] [base-asset-denom]", + Use: "spot-price [base-asset-denom] [quote-asset-denom]", Short: "Query spot-price", Long: `Query spot-price {{.CommandPrefix}} spot-price 1 uosmo ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2 diff --git a/x/poolmanager/router_test.go b/x/poolmanager/router_test.go index 096b4091be6..b2904e3f7aa 100644 --- a/x/poolmanager/router_test.go +++ b/x/poolmanager/router_test.go @@ -270,8 +270,8 @@ func (s *KeeperTestSuite) TestRouteCalculateSpotPrice() { "valid concentrated liquidity pool with position": { preCreatePoolType: types.Concentrated, poolId: 1, - quoteAssetDenom: "eth", - baseAssetDenom: "usdc", + quoteAssetDenom: "usdc", + baseAssetDenom: "eth", setPositionForCLPool: true, // We generate this value using the scripts in x/concentrated-liquidity/python // Exact output: 5000.000000000000000129480272834995458481