Skip to content

Commit

Permalink
fix: cl spotprice query (#5863)
Browse files Browse the repository at this point in the history
* fix: cl spotprice

* fix tests

* Update CHANGELOG.md

* swap base quote for twap e2e

---------

Co-authored-by: Adam Tucker <[email protected]>
  • Loading branch information
doggystylez and czarcas7ic authored Aug 2, 2023
1 parent 97f4901 commit 7331689
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions tests/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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.
Expand All @@ -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)

Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion x/concentrated-liquidity/model/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion x/concentrated-liquidity/model/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions x/concentrated-liquidity/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions x/concentrated-liquidity/swaps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
2 changes: 1 addition & 1 deletion x/poolmanager/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func GetCmdPool() (*osmocli.QueryDescriptor, *queryproto.PoolRequest) {

func GetCmdSpotPrice() (*osmocli.QueryDescriptor, *queryproto.SpotPriceRequest) {
return &osmocli.QueryDescriptor{
Use: "spot-price <pool-ID> [quote-asset-denom] [base-asset-denom]",
Use: "spot-price <pool-ID> [base-asset-denom] [quote-asset-denom]",
Short: "Query spot-price",
Long: `Query spot-price
{{.CommandPrefix}} spot-price 1 uosmo ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2
Expand Down
4 changes: 2 additions & 2 deletions x/poolmanager/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7331689

Please sign in to comment.