Skip to content

Commit

Permalink
feat(CL): x/poolmanager spot price query for concentrated liquidity (#…
Browse files Browse the repository at this point in the history
…4682)

* feat(CL): x/poolmanager spot price query

* changelog

* Update x/poolmanager/client/query_proto_wrap.go

* Update x/poolmanager/client/cli/query.go

* Update x/poolmanager/client/cli/query.go

* Update go.mod

---------

Co-authored-by: p0mvn <[email protected]>
  • Loading branch information
p0mvn and p0mvn authored Mar 22, 2023
1 parent d61c0ed commit 9303268
Show file tree
Hide file tree
Showing 14 changed files with 786 additions and 113 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* [#4336](https://github.com/osmosis-labs/osmosis/pull/4336) Move epochs module into its own go.mod
* [#4658](https://github.com/osmosis-labs/osmosis/pull/4658) Deprecate x/gamm Pool query. The new one is located in x/poolmanager.
* [#4682](https://github.com/osmosis-labs/osmosis/pull/4682) Deprecate x/gamm SpotPrice v2 query. The new one is located in x/poolmanager.

## v15.0.0

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
github.com/ory/dockertest/v3 v3.9.1
github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3
github.com/osmosis-labs/osmosis/osmomath v0.0.3-dev.0.20230316203321-2edd49875f80
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230317232332-81bd3c5ef0c2
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230320210812-4f37fbb5d1ef
github.com/osmosis-labs/osmosis/x/epochs v0.0.0-20230313084619-8d291c1d9813
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.0-20230316203321-2edd49875f80
github.com/pkg/errors v0.9.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -935,8 +935,8 @@ github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3 h1:Ylmch
github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3/go.mod h1:lV6KnqXYD/ayTe7310MHtM3I2q8Z6bBfMAi+bhwPYtI=
github.com/osmosis-labs/osmosis/osmomath v0.0.3-dev.0.20230316203321-2edd49875f80 h1:MqSb3Q5Uqb2r3PcyijgFL0lcIe57PqncqyfkRtyyn6E=
github.com/osmosis-labs/osmosis/osmomath v0.0.3-dev.0.20230316203321-2edd49875f80/go.mod h1:PT796t7vS3STUWYikgwzY9e7jhLo0N/hwZf4R6haj0o=
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230317232332-81bd3c5ef0c2 h1:syLNakfGKE9SjReHzcE8B0vWaykxKeno10uOyHi+0aI=
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230317232332-81bd3c5ef0c2/go.mod h1:spcwVLt51T9NAUpFb+W0UKO7e9mwF5K/hc3nTrmGoP4=
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230320210812-4f37fbb5d1ef h1:4V0HxAtcOJSavt8F3F8wuwpUaBAp9QYkI1FKqziDAXo=
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230320210812-4f37fbb5d1ef/go.mod h1:spcwVLt51T9NAUpFb+W0UKO7e9mwF5K/hc3nTrmGoP4=
github.com/osmosis-labs/osmosis/x/epochs v0.0.0-20230313084619-8d291c1d9813 h1:OVT8hqFET8DkAeiSUs5h2nTg1uZFU6kPRP2sDWzVm/o=
github.com/osmosis-labs/osmosis/x/epochs v0.0.0-20230313084619-8d291c1d9813/go.mod h1:lJebPe8cmSTwXXuWmgSNc1S4Bobz9g3x05ZL4igbAAI=
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.0-20230316203321-2edd49875f80 h1:8gZdQsLkU0XOJ43xlzr1ig9fTVqgdZcoGbLL0ZnUzZs=
Expand Down
12 changes: 6 additions & 6 deletions proto/osmosis/gamm/v2/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@ import "cosmos_proto/cosmos.proto";
option go_package = "github.com/osmosis-labs/osmosis/v15/x/gamm/v2types";

service Query {
// SpotPrice defines a gRPC query handler that returns the spot price given
// a base denomination and a quote denomination.
// Deprecated: please use alternate in x/poolmanager
rpc SpotPrice(QuerySpotPriceRequest) returns (QuerySpotPriceResponse) {
option deprecated = true;
option (google.api.http).get = "/osmosis/gamm/v2/pools/{pool_id}/prices";
}
}

// QuerySpotPriceRequest defines the gRPC request structure for a SpotPrice
// query.
// Deprecated: please use alternate in x/poolmanager
message QuerySpotPriceRequest {
option deprecated = true;
uint64 pool_id = 1 [ (gogoproto.moretags) = "yaml:\"pool_id\"" ];
string base_asset_denom = 2
[ (gogoproto.moretags) = "yaml:\"base_asset_denom\"" ];
string quote_asset_denom = 3
[ (gogoproto.moretags) = "yaml:\"quote_asset_denom\"" ];
}

// QuerySpotPriceResponse defines the gRPC response structure for a SpotPrice
// query.
// Depreacted: please use alternate in x/poolmanager
message QuerySpotPriceResponse {
option deprecated = true;
// String of the Dec. Ex) 10.203uatom
string spot_price = 1 [ (gogoproto.moretags) = "yaml:\"spot_price\"" ];
}
24 changes: 24 additions & 0 deletions proto/osmosis/poolmanager/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ service Query {
rpc AllPools(AllPoolsRequest) returns (AllPoolsResponse) {
option (google.api.http).get = "/osmosis/poolmanager/v1beta1/all-pools";
}

// SpotPrice defines a gRPC query handler that returns the spot price given
// a base denomination and a quote denomination.
rpc SpotPrice(SpotPriceRequest) returns (SpotPriceResponse) {
option (google.api.http).get =
"/osmosis/poolmanager/pools/{pool_id}/prices";
}
}

//=============================== Params
Expand Down Expand Up @@ -117,3 +124,20 @@ message AllPoolsResponse {
repeated google.protobuf.Any pools = 1
[ (cosmos_proto.accepts_interface) = "PoolI" ];
}

// SpotPriceRequest defines the gRPC request structure for a SpotPrice
// query.
message SpotPriceRequest {
uint64 pool_id = 1 [ (gogoproto.moretags) = "yaml:\"pool_id\"" ];
string base_asset_denom = 2
[ (gogoproto.moretags) = "yaml:\"base_asset_denom\"" ];
string quote_asset_denom = 3
[ (gogoproto.moretags) = "yaml:\"quote_asset_denom\"" ];
}

// SpotPriceResponse defines the gRPC response structure for a SpotPrice
// query.
message SpotPriceResponse {
// String of the Dec. Ex) 10.203uatom
string spot_price = 1 [ (gogoproto.moretags) = "yaml:\"spot_price\"" ];
}
5 changes: 5 additions & 0 deletions proto/osmosis/poolmanager/v1beta1/query.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ queries:
query_func: "k.AllPools"
cli:
cmd: "AllPools"
SpotPrice:
proto_wrapper:
query_func: "k.RouteCalculateSpotPrice"
cli:
cmd: "SpotPrice"
8 changes: 3 additions & 5 deletions x/gamm/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ Example:
)
}

//nolint:staticcheck
// Deprecated: use alternate in x/poolmanager.
func GetCmdSpotPrice() (*osmocli.QueryDescriptor, *types.QuerySpotPriceRequest) {
return &osmocli.QueryDescriptor{
Use: "spot-price <pool-ID> [quote-asset-denom] [base-asset-denom]",
Expand All @@ -204,8 +204,7 @@ func GetCmdSpotPrice() (*osmocli.QueryDescriptor, *types.QuerySpotPriceRequest)
`}, &types.QuerySpotPriceRequest{}
}

// GetCmdEstimateSwapExactAmountIn returns estimation of output coin when amount of x token input.
// nolint: staticcheck
// Deprecated: use alternate in x/poolmanager.
func GetCmdEstimateSwapExactAmountIn() (*osmocli.QueryDescriptor, *types.QuerySwapExactAmountInRequest) {
return &osmocli.QueryDescriptor{
Use: "estimate-swap-exact-amount-in <poolID> <sender> <tokenIn>",
Expand All @@ -219,8 +218,7 @@ func GetCmdEstimateSwapExactAmountIn() (*osmocli.QueryDescriptor, *types.QuerySw
}, &types.QuerySwapExactAmountInRequest{}
}

// GetCmdEstimateSwapExactAmountOut returns estimation of input coin to get exact amount of x token output.
// nolint: staticcheck
// Deprecated: use alternate in x/poolmanager.
func GetCmdEstimateSwapExactAmountOut() (*osmocli.QueryDescriptor, *types.QuerySwapExactAmountOutRequest) {
return &osmocli.QueryDescriptor{
Use: "estimate-swap-exact-amount-out <poolID> <sender> <tokenOut>",
Expand Down
4 changes: 4 additions & 0 deletions x/gamm/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ func (q Querier) SpotPrice(ctx context.Context, req *types.QuerySpotPriceRequest
}, nil
}

// Deeprecated: use alternate in x/poolmanager
// nolint: staticcheck
func (q QuerierV2) SpotPrice(ctx context.Context, req *v2types.QuerySpotPriceRequest) (*v2types.QuerySpotPriceResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
Expand All @@ -402,6 +404,8 @@ func (q QuerierV2) SpotPrice(ctx context.Context, req *v2types.QuerySpotPriceReq
return nil, status.Error(codes.Internal, err.Error())
}

// Deeprecated: use alternate in x/poolmanager
// nolint: staticcheck
return &v2types.QuerySpotPriceResponse{
SpotPrice: sp.String(),
}, nil
Expand Down
78 changes: 40 additions & 38 deletions x/gamm/v2types/query.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions x/poolmanager/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func GetQueryCmd() *cobra.Command {
osmocli.AddQueryCmd(cmd, queryproto.NewQueryClient, GetCmdNumPools)
osmocli.AddQueryCmd(cmd, queryproto.NewQueryClient, GetCmdEstimateSwapExactAmountIn)
osmocli.AddQueryCmd(cmd, queryproto.NewQueryClient, GetCmdEstimateSwapExactAmountOut)
osmocli.AddQueryCmd(cmd, queryproto.NewQueryClient, GetCmdSpotPrice)

return cmd
}
Expand Down Expand Up @@ -73,6 +74,15 @@ func GetCmdPool() (*osmocli.QueryDescriptor, *queryproto.PoolRequest) {
{{.CommandPrefix}} pool 1`}, &queryproto.PoolRequest{}
}

func GetCmdSpotPrice() (*osmocli.QueryDescriptor, *queryproto.SpotPriceRequest) {
return &osmocli.QueryDescriptor{
Use: "spot-price <pool-ID> [quote-asset-denom] [base-asset-denom]",
Short: "Query spot-price",
Long: `Query spot-price
{{.CommandPrefix}} spot-price 1 uosmo ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2
`}, &queryproto.SpotPriceRequest{}
}

func EstimateSwapExactAmountInParseArgs(args []string, fs *flag.FlagSet) (proto.Message, error) {
poolID, err := strconv.Atoi(args[0])
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions x/poolmanager/client/grpc/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ type Querier struct {

var _ queryproto.QueryServer = Querier{}

func (q Querier) SpotPrice(grpcCtx context.Context,
req *queryproto.SpotPriceRequest,
) (*queryproto.SpotPriceResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(grpcCtx)
return q.Q.SpotPrice(ctx, *req)
}

func (q Querier) Pool(grpcCtx context.Context,
req *queryproto.PoolRequest,
) (*queryproto.PoolResponse, error) {
Expand Down
20 changes: 20 additions & 0 deletions x/poolmanager/client/query_proto_wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,23 @@ func (q Querier) AllPools(ctx sdk.Context, req queryproto.AllPoolsRequest) (*que
Pools: anyPools,
}, nil
}

// SpotPrice returns the spot price of the pool with the given quote and base asset denoms.
func (q Querier) SpotPrice(ctx sdk.Context, req queryproto.SpotPriceRequest) (*queryproto.SpotPriceResponse, error) {
if req.BaseAssetDenom == "" {
return nil, status.Error(codes.InvalidArgument, "invalid base asset denom")
}

if req.QuoteAssetDenom == "" {
return nil, status.Error(codes.InvalidArgument, "invalid quote asset denom")
}

sp, err := q.K.RouteCalculateSpotPrice(ctx, req.PoolId, req.QuoteAssetDenom, req.BaseAssetDenom)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

return &queryproto.SpotPriceResponse{
SpotPrice: sp.String(),
}, err
}
Loading

0 comments on commit 9303268

Please sign in to comment.