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

fix: cl spotprice query #5863

Merged
merged 5 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]",
Copy link
Member

Choose a reason for hiding this comment

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

note to other reviewers: This changed due to order of proto query client

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