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

added single pool query #4549

Merged
merged 26 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e87f1ce
added single pool query
nicolaslara Mar 9, 2023
7278e48
added changelog
nicolaslara Mar 9, 2023
149c32f
remove empty tests as they are invalid
nicolaslara Mar 9, 2023
ee46ae9
removed sender and made reserved
nicolaslara Mar 9, 2023
12ba6cf
remove and reserve unused sender
nicolaslara Mar 9, 2023
9fd94a0
removed sender
nicolaslara Mar 9, 2023
01d7ba5
actually remove sender
nicolaslara Mar 9, 2023
44679b2
querygen
nicolaslara Mar 9, 2023
e85903d
move impl to correct file
nicolaslara Mar 9, 2023
0569a5b
more flexible querygen
nicolaslara Mar 9, 2023
a760155
Update proto/osmosis/poolmanager/v1beta1/query.proto
nicolaslara Mar 15, 2023
65a00e1
Update proto/osmosis/poolmanager/v1beta1/query.proto
nicolaslara Mar 15, 2023
909f440
Update proto/osmosis/poolmanager/v1beta1/query.proto
nicolaslara Mar 15, 2023
5662ab3
Update proto/osmosis/poolmanager/v1beta1/query.proto
nicolaslara Mar 15, 2023
4866d00
Merge branch 'main' into nicolas/simple-swap-endpoint
nicolaslara Mar 15, 2023
95de2fe
protos
nicolaslara Mar 15, 2023
27115c2
added cli
nicolaslara Mar 15, 2023
bf8b9d4
fixed command parsing
nicolaslara Mar 15, 2023
a3c9056
single line
nicolaslara Mar 15, 2023
76a6293
lint
nicolaslara Mar 15, 2023
bf00a07
fix cli
nicolaslara Mar 16, 2023
e6f7ecc
Merge branch 'main' into nicolas/simple-swap-endpoint
nicolaslara Mar 20, 2023
8408858
run proto gen
nicolaslara Mar 20, 2023
2755382
added whitelisted query
nicolaslara Mar 20, 2023
5a7bf12
fix test for v15
nicolaslara Mar 20, 2023
79b8fd2
Merge branch 'main' into nicolas/simple-swap-endpoint
nicolaslara Mar 24, 2023
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

* [#4549](https://github.com/osmosis-labs/osmosis/pull/4549) Add single pool price estimate queries

### API Breaks

* [#4489](https://github.com/osmosis-labs/osmosis/pull/4489) Add unlockingLockId to BeginUnlocking response
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,205 changes: 2,035 additions & 170 deletions go.work.sum

Large diffs are not rendered by default.

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";
}

rpc NumPools(NumPoolsRequest) returns (NumPoolsResponse) {
Expand All @@ -45,8 +59,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";
Comment on lines 80 to +82
Copy link
Member

@ValarDragon ValarDragon Mar 13, 2023

Choose a reason for hiding this comment

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

Is this query in the stargate whitelist? If so, then seems important to reason about if this would cause a state break on mainnet. My first pass thought is it wouldn't, and should be safe. This is because the only gas difference would come from the rust side. But the Rust side is fixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is. It's also potentially used by contracts.

One place that may be state breaking is https://github.com/osmosis-labs/osmosis/pull/4549/files#diff-417b3334c7fd0aefd80aafa70e8feb77a81586dc63b266105aa25bad2ab09a2fL53. It was (unnecessarily) checking that the sender wasn't empty. So a call with an empty sender would've failed before and would pass now.

Copy link
Member

@mattverse mattverse Mar 15, 2023

Choose a reason for hiding this comment

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

good catch, this leads me to think that we might want to add queries included in stargate queries to e2e testing, since we can mistakenly merge a query PR and have it state breaking. Going to create an issue to track this and further discuss this

uint64 pool_id = 2 [ (gogoproto.moretags) = "yaml:\"pool_id\"" ];
string token_in = 3 [ (gogoproto.moretags) = "yaml:\"token_in\"" ];
repeated SwapAmountInRoute routes = 4 [
Expand All @@ -55,6 +69,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 @@ -65,8 +86,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 @@ -75,6 +96,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: 0 additions & 2 deletions x/poolmanager/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ func TestGetCmdEstimateSwapExactAmountIn(t *testing.T) {
"basic test": {
Cmd: "1 osm11vmx8jtggpd9u7qr0t8vxclycz85u925sazglr7 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 @@ -216,7 +215,6 @@ func TestGetCmdEstimateSwapExactAmountOut(t *testing.T) {
"basic test": {
Cmd: "1 osm11vmx8jtggpd9u7qr0t8vxclycz85u925sazglr7 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 Down
2 changes: 0 additions & 2 deletions x/poolmanager/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ func EstimateSwapExactAmountInParseArgs(args []string, fs *flag.FlagSet) (proto.
}

return &queryproto.EstimateSwapExactAmountInRequest{
mattverse marked this conversation as resolved.
Show resolved Hide resolved
Sender: args[1], // TODO: where sender is used?
PoolId: uint64(poolID), // TODO: is this poolId used?
TokenIn: args[2],
Routes: routes,
Expand All @@ -95,7 +94,6 @@ func EstimateSwapExactAmountOutParseArgs(args []string, fs *flag.FlagSet) (proto
}

mattverse marked this conversation as resolved.
Show resolved Hide resolved
return &queryproto.EstimateSwapExactAmountOutRequest{
Sender: args[1], // TODO: where sender is used?
PoolId: uint64(poolID), // TODO: is this poolId used?
Routes: routes,
TokenOut: args[2],
Expand Down
58 changes: 53 additions & 5 deletions x/poolmanager/client/cli/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli_test

import (
gocontext "context"
"github.com/osmosis-labs/osmosis/v15/x/poolmanager/types"
"testing"

"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -39,13 +40,21 @@ func (s *QueryTestSuite) TestQueriesNeverAlterState() {
{
"Query estimate swap in",
"/osmosis.poolmanager.v1beta1.Query/EstimateSwapExactAmountIn",
&poolmanagerqueryproto.EstimateSwapExactAmountInRequest{},
&poolmanagerqueryproto.EstimateSwapExactAmountInRequest{
PoolId: 1,
TokenIn: "10bar",
Routes: types.SwapAmountInRoutes{{PoolId: 1, TokenOutDenom: "baz"}},
},
&poolmanagerqueryproto.EstimateSwapExactAmountInResponse{},
},
{
"Query estimate swap out",
"/osmosis.poolmanager.v1beta1.Query/EstimateSwapExactAmountOut",
&poolmanagerqueryproto.EstimateSwapExactAmountOutRequest{},
&poolmanagerqueryproto.EstimateSwapExactAmountOutRequest{
PoolId: 1,
TokenOut: "6baz",
Routes: types.SwapAmountOutRoutes{{PoolId: 1, TokenInDenom: "bar"}},
},
&poolmanagerqueryproto.EstimateSwapExactAmountOutResponse{},
},
}
Expand All @@ -61,10 +70,49 @@ func (s *QueryTestSuite) TestQueriesNeverAlterState() {
}
}

func TestQueryTestSuite(t *testing.T) {
func (s *QueryTestSuite) TestSimplifiedQueries() {
swapIn := &poolmanagerqueryproto.EstimateSwapExactAmountInRequest{
PoolId: 1,
TokenIn: "10bar",
Routes: types.SwapAmountInRoutes{{PoolId: 1, TokenOutDenom: "baz"}},
}
swapOut := &poolmanagerqueryproto.EstimateSwapExactAmountOutRequest{
PoolId: 1,
TokenOut: "6baz",
Routes: types.SwapAmountOutRoutes{{PoolId: 1, TokenInDenom: "bar"}},
}
simplifiedSwapIn := &poolmanagerqueryproto.EstimateSinglePoolSwapExactAmountInRequest{
PoolId: 1,
TokenIn: "10bar",
TokenOutDenom: "baz",
}
simplifiedSwapOut := &poolmanagerqueryproto.EstimateSinglePoolSwapExactAmountOutRequest{
PoolId: 1,
TokenOut: "6baz",
TokenInDenom: "bar",
}
s.SetupSuite()
output1 := &poolmanagerqueryproto.EstimateSwapExactAmountInResponse{}
output2 := &poolmanagerqueryproto.EstimateSwapExactAmountInResponse{}
err := s.QueryHelper.Invoke(gocontext.Background(),
"/osmosis.poolmanager.v1beta1.Query/EstimateSwapExactAmountIn", swapIn, output1)
s.Require().NoError(err)
err = s.QueryHelper.Invoke(gocontext.Background(),
"/osmosis.poolmanager.v1beta1.Query/EstimateSinglePoolSwapExactAmountIn", simplifiedSwapIn, output2)
s.Require().NoError(err)
s.Require().Equal(output1, output2)

// TODO: re-enable this once poolmanager is fully merged.
t.SkipNow()
output3 := &poolmanagerqueryproto.EstimateSwapExactAmountOutResponse{}
output4 := &poolmanagerqueryproto.EstimateSwapExactAmountOutResponse{}
err = s.QueryHelper.Invoke(gocontext.Background(),
"/osmosis.poolmanager.v1beta1.Query/EstimateSwapExactAmountOut", swapOut, output3)
s.Require().NoError(err)
err = s.QueryHelper.Invoke(gocontext.Background(),
"/osmosis.poolmanager.v1beta1.Query/EstimateSinglePoolSwapExactAmountOut", simplifiedSwapOut, output4)
s.Require().NoError(err)
s.Require().Equal(output3, output4)
}

func TestQueryTestSuite(t *testing.T) {
suite.Run(t, new(QueryTestSuite))
}
20 changes: 20 additions & 0 deletions x/poolmanager/client/grpc/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,23 @@ func (q Querier) EstimateSwapExactAmountIn(grpcCtx context.Context,
return q.Q.EstimateSwapExactAmountIn(ctx, *req)
}

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

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

22 changes: 18 additions & 4 deletions x/poolmanager/client/query_proto_wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ func (q Querier) EstimateSwapExactAmountIn(ctx sdk.Context, req queryproto.Estim

// EstimateSwapExactAmountOut estimates token output amount for a swap.
func (q Querier) EstimateSwapExactAmountOut(ctx sdk.Context, req queryproto.EstimateSwapExactAmountOutRequest) (*queryproto.EstimateSwapExactAmountOutResponse, error) {
if req.Sender == "" {
return nil, status.Error(codes.InvalidArgument, "address cannot be empty")
}

if req.TokenOut == "" {
return nil, status.Error(codes.InvalidArgument, "invalid token")
}
Expand Down Expand Up @@ -83,3 +79,21 @@ func (q Querier) NumPools(ctx sdk.Context, _ queryproto.NumPoolsRequest) (*query
NumPools: q.K.GetNextPoolId(ctx) - 1,
}, nil
}

func (q Querier) EstimateSinglePoolSwapExactAmountOut(ctx sdk.Context, req queryproto.EstimateSinglePoolSwapExactAmountOutRequest) (*queryproto.EstimateSwapExactAmountOutResponse, error) {
routeReq := &queryproto.EstimateSwapExactAmountOutRequest{
PoolId: req.PoolId,
TokenOut: req.TokenOut,
Routes: types.SwapAmountOutRoutes{{PoolId: req.PoolId, TokenInDenom: req.TokenInDenom}},
}
return q.EstimateSwapExactAmountOut(ctx, *routeReq)
}

func (q Querier) EstimateSinglePoolSwapExactAmountIn(ctx sdk.Context, req queryproto.EstimateSinglePoolSwapExactAmountInRequest) (*queryproto.EstimateSwapExactAmountInResponse, error) {
routeReq := &queryproto.EstimateSwapExactAmountInRequest{
PoolId: req.PoolId,
TokenIn: req.TokenIn,
Routes: types.SwapAmountInRoutes{{PoolId: req.PoolId, TokenOutDenom: req.TokenOutDenom}},
}
return q.EstimateSwapExactAmountIn(ctx, *routeReq)
}
Loading