Skip to content

Commit

Permalink
added single pool query (#4549)
Browse files Browse the repository at this point in the history
* added single pool query

* added changelog

* remove empty tests as they are invalid

* removed sender and made reserved

* remove and reserve unused sender

* removed sender

* actually remove sender

* querygen

* move impl to correct file

* more flexible querygen

* Update proto/osmosis/poolmanager/v1beta1/query.proto

Co-authored-by: Matt, Park <[email protected]>

* Update proto/osmosis/poolmanager/v1beta1/query.proto

Co-authored-by: Matt, Park <[email protected]>

* Update proto/osmosis/poolmanager/v1beta1/query.proto

Co-authored-by: Matt, Park <[email protected]>

* Update proto/osmosis/poolmanager/v1beta1/query.proto

Co-authored-by: Matt, Park <[email protected]>

* protos

* added cli

* fixed command parsing

* single line

* lint

* fix cli

* run proto gen

* added whitelisted query

* fix test for v15

---------

Co-authored-by: Matt, Park <[email protected]>
  • Loading branch information
nicolaslara and mattverse authored Mar 24, 2023
1 parent 1d76f4f commit 695c3c9
Show file tree
Hide file tree
Showing 18 changed files with 1,112 additions and 198 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Misc Improvements

* [#4582](https://github.com/osmosis-labs/osmosis/pull/4582) Consistently generate build tags metadata, to return a comma-separated list without stray quotes. This affects the output from `version` CLI subcommand and server info API calls.
* [#4549](https://github.com/osmosis-labs/osmosis/pull/4549) Add single pool price estimate queries

### API Breaks

### API breaks

Expand Down
2 changes: 1 addition & 1 deletion app/upgrades/v15/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (suite *UpgradeTestSuite) TestSetICQParams() {
v15.SetICQParams(suite.Ctx, suite.App.ICQKeeper)

suite.Require().True(suite.App.ICQKeeper.IsHostEnabled(suite.Ctx))
suite.Require().Len(suite.App.ICQKeeper.GetAllowQueries(suite.Ctx), 65)
suite.Require().Len(suite.App.ICQKeeper.GetAllowQueries(suite.Ctx), 67)
}

func (suite *UpgradeTestSuite) TestSetRateLimits() {
Expand Down
3 changes: 2 additions & 1 deletion cmd/querygen/templates/grpcTemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ type GrpcTemplate struct {

type GrpcQuery struct {
QueryName string
Response string
}

func GrpcTemplateFromQueryYml(queryYml QueryYml) GrpcTemplate {
GrpcQueries := []GrpcQuery{}
for queryName := range queryYml.Queries {
GrpcQueries = append(GrpcQueries, GrpcQuery{QueryName: queryName})
GrpcQueries = append(GrpcQueries, GrpcQuery{QueryName: queryName, Response: queryYml.Queries[queryName].ProtoWrapper.Response})
}
sort.Slice(GrpcQueries, func(i, j int) bool {
return GrpcQueries[i].QueryName > GrpcQueries[j].QueryName
Expand Down
2 changes: 1 addition & 1 deletion cmd/querygen/templates/grpc_template.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var _ queryproto.QueryServer = Querier{}

func (q Querier) {{.QueryName}}(grpcCtx context.Context,
req *queryproto.{{.QueryName}}Request,
) (*queryproto.{{.QueryName}}Response, error) {
) ({{ if .Response }}{{.Response}}{{else}}*queryproto.{{.QueryName}}Response{{end}}, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/querygen/templates/queryyml.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type YmlQueryDescriptor struct {
type ProtoWrapperDescriptor struct {
DefaultValues map[string]string `yaml:"default_values"`
QueryFunc string `yaml:"query_func"`
Response string
Response string `yaml:"response"`
}

type CliDescriptor struct{}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.19
require (
github.com/CosmWasm/wasmd v0.30.0
github.com/cosmos/cosmos-proto v1.0.0-alpha8
github.com/cosmos/cosmos-sdk v0.47.0
github.com/cosmos/cosmos-sdk v0.47.1
github.com/cosmos/go-bip39 v1.0.0
github.com/cosmos/ibc-go/v4 v4.3.0
github.com/gogo/protobuf v1.3.3
Expand Down
40 changes: 34 additions & 6 deletions proto/osmosis/poolmanager/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,28 @@ service Query {
rpc EstimateSwapExactAmountIn(EstimateSwapExactAmountInRequest)
returns (EstimateSwapExactAmountInResponse) {
option (google.api.http).get =
"/osmosis/gamm/v1beta1/{pool_id}/estimate/swap_exact_amount_in";
"/osmosis/poolmanager/v1beta1/{pool_id}/estimate/swap_exact_amount_in";
}

rpc EstimateSinglePoolSwapExactAmountIn(
EstimateSinglePoolSwapExactAmountInRequest)
returns (EstimateSwapExactAmountInResponse) {
option (google.api.http).get =
"/osmosis/poolmanager/v1beta1/{pool_id}/estimate/single_pool_swap_exact_amount_in";
}

// Estimates swap amount in given out.
rpc EstimateSwapExactAmountOut(EstimateSwapExactAmountOutRequest)
returns (EstimateSwapExactAmountOutResponse) {
option (google.api.http).get =
"/osmosis/gamm/v1beta1/{pool_id}/estimate/swap_exact_amount_out";
"/osmosis/poolmanager/v1beta1/{pool_id}/estimate/swap_exact_amount_out";
}

rpc EstimateSinglePoolSwapExactAmountOut(
EstimateSinglePoolSwapExactAmountOutRequest)
returns (EstimateSwapExactAmountOutResponse) {
option (google.api.http).get =
"/osmosis/poolmanager/v1beta1/{pool_id}/estimate_out/single_pool_swap_exact_amount_out";
}

// Returns the total number of pools existing in Osmosis.
Expand Down Expand Up @@ -64,8 +78,8 @@ message ParamsResponse { Params params = 1 [ (gogoproto.nullable) = false ]; }

//=============================== EstimateSwapExactAmountIn
message EstimateSwapExactAmountInRequest {
// TODO: CHANGE THIS TO RESERVED IN A PATCH RELEASE
string sender = 1 [ (gogoproto.moretags) = "yaml:\"sender\"" ];
reserved 1;
reserved "sender";
uint64 pool_id = 2 [ (gogoproto.moretags) = "yaml:\"pool_id\"" ];
string token_in = 3 [ (gogoproto.moretags) = "yaml:\"token_in\"" ];
repeated SwapAmountInRoute routes = 4 [
Expand All @@ -74,6 +88,13 @@ message EstimateSwapExactAmountInRequest {
];
}

message EstimateSinglePoolSwapExactAmountInRequest {
uint64 pool_id = 1 [ (gogoproto.moretags) = "yaml:\"pool_id\"" ];
string token_in = 2 [ (gogoproto.moretags) = "yaml:\"token_in\"" ];
string token_out_denom = 3
[ (gogoproto.moretags) = "yaml:\"token_out_denom\"" ];
}

message EstimateSwapExactAmountInResponse {
string token_out_amount = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
Expand All @@ -84,8 +105,8 @@ message EstimateSwapExactAmountInResponse {

//=============================== EstimateSwapExactAmountOut
message EstimateSwapExactAmountOutRequest {
// TODO: CHANGE THIS TO RESERVED IN A PATCH RELEASE
string sender = 1 [ (gogoproto.moretags) = "yaml:\"sender\"" ];
reserved 1;
reserved "sender";
uint64 pool_id = 2 [ (gogoproto.moretags) = "yaml:\"pool_id\"" ];
repeated SwapAmountOutRoute routes = 3 [
(gogoproto.moretags) = "yaml:\"routes\"",
Expand All @@ -94,6 +115,13 @@ message EstimateSwapExactAmountOutRequest {
string token_out = 4 [ (gogoproto.moretags) = "yaml:\"token_out\"" ];
}

message EstimateSinglePoolSwapExactAmountOutRequest {
uint64 pool_id = 1 [ (gogoproto.moretags) = "yaml:\"pool_id\"" ];
string token_in_denom = 2
[ (gogoproto.moretags) = "yaml:\"token_in_denom\"" ];
string token_out = 3 [ (gogoproto.moretags) = "yaml:\"token_out\"" ];
}

message EstimateSwapExactAmountOutResponse {
string token_in_amount = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
Expand Down
12 changes: 12 additions & 0 deletions proto/osmosis/poolmanager/v1beta1/query.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ queries:
query_func: "k.EstimateSwapExactAmountOut"
cli:
cmd: "EstimateSwapExactAmountOut"
EstimateSinglePoolSwapExactAmountIn:
proto_wrapper:
query_func: "k.EstimateSinglePoolSwapExactAmountIn"
response: "*queryproto.EstimateSwapExactAmountInResponse"
cli:
cmd: "EstimateSinglePoolSwapExactAmountIn"
EstimateSinglePoolSwapExactAmountOut:
proto_wrapper:
query_func: "k.EstimateSinglePoolSwapExactAmountOutTEST"
response: "*queryproto.EstimateSwapExactAmountOutResponse"
cli:
cmd: "EstimateSinglePoolSwapExactAmountOut"
NumPools:
proto_wrapper:
query_func: "k.NumPools"
Expand Down
2 changes: 1 addition & 1 deletion proto/osmosis/poolmanager/v1beta1/swap_route.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ message SwapAmountInRoute {
message SwapAmountOutRoute {
uint64 pool_id = 1 [ (gogoproto.moretags) = "yaml:\"pool_id\"" ];
string token_in_denom = 2
[ (gogoproto.moretags) = "yaml:\"token_out_denom\"" ];
[ (gogoproto.moretags) = "yaml:\"token_in_denom\"" ];
}
2 changes: 2 additions & 0 deletions wasmbinding/stargate_whitelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ func init() {
setWhitelistedQuery("/osmosis.poolmanager.v1beta1.Query/NumPools", &poolmanagerqueryproto.NumPoolsResponse{})
setWhitelistedQuery("/osmosis.poolmanager.v1beta1.Query/EstimateSwapExactAmountIn", &poolmanagerqueryproto.EstimateSwapExactAmountInResponse{})
setWhitelistedQuery("/osmosis.poolmanager.v1beta1.Query/EstimateSwapExactAmountOut", &poolmanagerqueryproto.EstimateSwapExactAmountOutRequest{})
setWhitelistedQuery("/osmosis.poolmanager.v1beta1.Query/EstimateSinglePoolSwapExactAmountIn", &poolmanagerqueryproto.EstimateSwapExactAmountInResponse{})
setWhitelistedQuery("/osmosis.poolmanager.v1beta1.Query/EstimateSinglePoolSwapExactAmountOut", &poolmanagerqueryproto.EstimateSwapExactAmountOutRequest{})

// txfees
setWhitelistedQuery("/osmosis.txfees.v1beta1.Query/FeeTokens", &txfeestypes.QueryFeeTokensResponse{})
Expand Down
38 changes: 34 additions & 4 deletions x/poolmanager/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,8 @@ func TestGetCmdEstimateSwapExactAmountIn(t *testing.T) {
desc, _ := cli.GetCmdEstimateSwapExactAmountIn()
tcs := map[string]osmocli.QueryCliTestCase[*queryproto.EstimateSwapExactAmountInRequest]{
"basic test": {
Cmd: "1 osm11vmx8jtggpd9u7qr0t8vxclycz85u925sazglr7 10stake --swap-route-pool-ids=2 --swap-route-denoms=node0token",
Cmd: "1 10stake --swap-route-pool-ids=2 --swap-route-denoms=node0token",
ExpectedQuery: &queryproto.EstimateSwapExactAmountInRequest{
Sender: "osm11vmx8jtggpd9u7qr0t8vxclycz85u925sazglr7",
PoolId: 1,
TokenIn: "10stake",
Routes: []types.SwapAmountInRoute{{PoolId: 2, TokenOutDenom: "node0token"}},
Expand All @@ -210,13 +209,13 @@ func TestGetCmdEstimateSwapExactAmountIn(t *testing.T) {
}
osmocli.RunQueryTestCases(t, desc, tcs)
}

func TestGetCmdEstimateSwapExactAmountOut(t *testing.T) {
desc, _ := cli.GetCmdEstimateSwapExactAmountOut()
tcs := map[string]osmocli.QueryCliTestCase[*queryproto.EstimateSwapExactAmountOutRequest]{
"basic test": {
Cmd: "1 osm11vmx8jtggpd9u7qr0t8vxclycz85u925sazglr7 10stake --swap-route-pool-ids=2 --swap-route-denoms=node0token",
Cmd: "1 10stake --swap-route-pool-ids=2 --swap-route-denoms=node0token",
ExpectedQuery: &queryproto.EstimateSwapExactAmountOutRequest{
Sender: "osm11vmx8jtggpd9u7qr0t8vxclycz85u925sazglr7",
PoolId: 1,
TokenOut: "10stake",
Routes: []types.SwapAmountOutRoute{{PoolId: 2, TokenInDenom: "node0token"}},
Expand All @@ -225,6 +224,37 @@ func TestGetCmdEstimateSwapExactAmountOut(t *testing.T) {
}
osmocli.RunQueryTestCases(t, desc, tcs)
}

func TestGetCmdEstimateSinglePoolSwapExactAmountIn(t *testing.T) {
desc, _ := cli.GetCmdEstimateSinglePoolSwapExactAmountIn()
tcs := map[string]osmocli.QueryCliTestCase[*queryproto.EstimateSinglePoolSwapExactAmountInRequest]{
"basic test": {
Cmd: "1 10stake node0token",
ExpectedQuery: &queryproto.EstimateSinglePoolSwapExactAmountInRequest{
PoolId: 1,
TokenIn: "10stake",
TokenOutDenom: "node0token",
},
},
}
osmocli.RunQueryTestCases(t, desc, tcs)
}

func TestGetCmdEstimateSinglePoolSwapExactAmountOut(t *testing.T) {
desc, _ := cli.GetCmdEstimateSinglePoolSwapExactAmountOut()
tcs := map[string]osmocli.QueryCliTestCase[*queryproto.EstimateSinglePoolSwapExactAmountOutRequest]{
"basic test": {
Cmd: "1 node0token 10stake",
ExpectedQuery: &queryproto.EstimateSinglePoolSwapExactAmountOutRequest{
PoolId: 1,
TokenInDenom: "node0token",
TokenOut: "10stake",
},
},
}
osmocli.RunQueryTestCases(t, desc, tcs)
}

func (s *IntegrationTestSuite) TestNewCreatePoolCmd() {
val := s.network.Validators[0]

Expand Down
38 changes: 30 additions & 8 deletions x/poolmanager/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ 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, GetCmdEstimateSinglePoolSwapExactAmountIn)
osmocli.AddQueryCmd(cmd, queryproto.NewQueryClient, GetCmdEstimateSinglePoolSwapExactAmountOut)
osmocli.AddQueryCmd(cmd, queryproto.NewQueryClient, GetCmdSpotPrice)

return cmd
Expand All @@ -31,10 +33,10 @@ func GetQueryCmd() *cobra.Command {
// GetCmdEstimateSwapExactAmountIn returns estimation of output coin when amount of x token input.
func GetCmdEstimateSwapExactAmountIn() (*osmocli.QueryDescriptor, *queryproto.EstimateSwapExactAmountInRequest) {
return &osmocli.QueryDescriptor{
Use: "estimate-swap-exact-amount-in <poolID> <sender> <tokenIn>",
Use: "estimate-swap-exact-amount-in <poolID> <tokenIn>",
Short: "Query estimate-swap-exact-amount-in",
Long: `Query estimate-swap-exact-amount-in.{{.ExampleHeader}}
{{.CommandPrefix}} estimate-swap-exact-amount-in 1 osm11vmx8jtggpd9u7qr0t8vxclycz85u925sazglr7 1000stake --swap-route-pool-ids=2 --swap-route-pool-ids=3`,
{{.CommandPrefix}} estimate-swap-exact-amount-in 1 1000stake --swap-route-pool-ids=2 --swap-route-pool-ids=3`,
ParseQuery: EstimateSwapExactAmountInParseArgs,
Flags: osmocli.FlagDesc{RequiredFlags: []*flag.FlagSet{FlagSetMultihopSwapRoutes()}},
QueryFnName: "EstimateSwapExactAmountIn",
Expand All @@ -45,10 +47,10 @@ func GetCmdEstimateSwapExactAmountIn() (*osmocli.QueryDescriptor, *queryproto.Es
// GetCmdEstimateSwapExactAmountOut returns estimation of input coin to get exact amount of x token output.
func GetCmdEstimateSwapExactAmountOut() (*osmocli.QueryDescriptor, *queryproto.EstimateSwapExactAmountOutRequest) {
return &osmocli.QueryDescriptor{
Use: "estimate-swap-exact-amount-out <poolID> <sender> <tokenOut>",
Use: "estimate-swap-exact-amount-out <poolID> <tokenOut>",
Short: "Query estimate-swap-exact-amount-out",
Long: `Query estimate-swap-exact-amount-out.{{.ExampleHeader}}
{{.CommandPrefix}} estimate-swap-exact-amount-out 1 osm11vmx8jtggpd9u7qr0t8vxclycz85u925sazglr7 1000stake --swap-route-pool-ids=2 --swap-route-pool-ids=3`,
{{.CommandPrefix}} estimate-swap-exact-amount-out 1 1000stake --swap-route-pool-ids=2 --swap-route-pool-ids=3`,
ParseQuery: EstimateSwapExactAmountOutParseArgs,
Flags: osmocli.FlagDesc{RequiredFlags: []*flag.FlagSet{FlagSetMultihopSwapRoutes()}},
QueryFnName: "EstimateSwapExactAmountOut",
Expand Down Expand Up @@ -95,9 +97,8 @@ func EstimateSwapExactAmountInParseArgs(args []string, fs *flag.FlagSet) (proto.
}

return &queryproto.EstimateSwapExactAmountInRequest{
Sender: args[1], // TODO: where sender is used?
PoolId: uint64(poolID), // TODO: is this poolId used?
TokenIn: args[2],
TokenIn: args[1],
Routes: routes,
}, nil
}
Expand All @@ -114,9 +115,30 @@ func EstimateSwapExactAmountOutParseArgs(args []string, fs *flag.FlagSet) (proto
}

return &queryproto.EstimateSwapExactAmountOutRequest{
Sender: args[1], // TODO: where sender is used?
PoolId: uint64(poolID), // TODO: is this poolId used?
Routes: routes,
TokenOut: args[2],
TokenOut: args[1],
}, nil
}

// GetCmdEstimateSinglePoolSwapExactAmountIn returns estimation of output coin when amount of x token input.
func GetCmdEstimateSinglePoolSwapExactAmountIn() (*osmocli.QueryDescriptor, *queryproto.EstimateSinglePoolSwapExactAmountInRequest) {
return &osmocli.QueryDescriptor{
Use: "estimate-single-pool-swap-exact-amount-in <poolID> <tokenIn> <tokenOutDenom>",
Short: "Query estimate-single-pool-swap-exact-amount-in",
Long: `Query estimate-single-pool-swap-exact-amount-in.{{.ExampleHeader}}
{{.CommandPrefix}} estimate-single-pool-swap-exact-amount-in 1 1000stake uosmo`,
QueryFnName: "EstimateSinglePoolSwapExactAmountIn",
}, &queryproto.EstimateSinglePoolSwapExactAmountInRequest{}
}

// GetCmdEstimateSinglePoolSwapExactAmountOut returns estimation of input coin to get exact amount of x token output.
func GetCmdEstimateSinglePoolSwapExactAmountOut() (*osmocli.QueryDescriptor, *queryproto.EstimateSinglePoolSwapExactAmountOutRequest) {
return &osmocli.QueryDescriptor{
Use: "estimate-single-pool-swap-exact-amount-out <poolID> <tokenInDenom> <tokenOut>",
Short: "Query estimate-single-pool-swap-exact-amount-out",
Long: `Query estimate-single-pool-swap-exact-amount-out.{{.ExampleHeader}}
{{.CommandPrefix}} estimate-single-pool-swap-exact-amount-out 1 uosmo 1000stake`,
QueryFnName: "EstimateSinglePoolSwapExactAmountOut",
}, &queryproto.EstimateSinglePoolSwapExactAmountOutRequest{}
}
Loading

0 comments on commit 695c3c9

Please sign in to comment.