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

Add Pool Type Query #2832

Merged
merged 5 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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: 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

### Features
* [#2739](https://github.com/osmosis-labs/osmosis/pull/2739) Add pool type query
### Bug Fixes
* [#2803](https://github.com/osmosis-labs/osmosis/pull/2803) Fix total pool liquidity CLI query.

Expand Down
15 changes: 15 additions & 0 deletions proto/osmosis/gamm/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ service Query {
option (google.api.http).get = "/osmosis/gamm/v1beta1/pools/{pool_id}";
}

// PoolType returns the type of the pool.
// Returns "Balancer" as a string literal when the pool is a balancer pool.
// Errors if the pool is failed to be type caseted.
rpc PoolType(QueryPoolTypeRequest) returns (QueryPoolTypeResponse) {
option (google.api.http).get = "/osmosis/gamm/v1beta1/pool_type/{pool_id}";
}

rpc PoolParams(QueryPoolParamsRequest) returns (QueryPoolParamsResponse) {
option (google.api.http).get =
"/osmosis/gamm/v1beta1/pools/{pool_id}/params";
Expand Down Expand Up @@ -95,6 +102,14 @@ message QueryNumPoolsResponse {
uint64 num_pools = 1 [ (gogoproto.moretags) = "yaml:\"num_pools\"" ];
}

//=============================== PoolType
message QueryPoolTypeRequest {
uint64 pool_id = 1 [ (gogoproto.moretags) = "yaml:\"pool_id\"" ];
}
message QueryPoolTypeResponse {
string pool_type = 1 [ (gogoproto.moretags) = "yaml:\"pool_type\"" ];
}

//=============================== PoolParams
message QueryPoolParamsRequest {
uint64 pool_id = 1 [ (gogoproto.moretags) = "yaml:\"pool_id\"" ];
Expand Down
13 changes: 13 additions & 0 deletions x/gamm/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ func (q Querier) NumPools(ctx context.Context, _ *types.QueryNumPoolsRequest) (*
}, nil
}

func (q Querier) PoolType(ctx context.Context, req *types.QueryPoolTypeRequest) (*types.QueryPoolTypeResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

sdkCtx := sdk.UnwrapSDKContext(ctx)
poolType, err := q.Keeper.GetPoolType(sdkCtx, req.PoolId)

return &types.QueryPoolTypeResponse{
PoolType: poolType,
}, err
}

// PoolParams queries a specified pool for its params.
func (q Querier) PoolParams(ctx context.Context, req *types.QueryPoolParamsRequest) (*types.QueryPoolParamsResponse, error) {
if req == nil {
Expand Down
12 changes: 12 additions & 0 deletions x/gamm/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ func (suite *KeeperTestSuite) TestQueryPools() {
}
}

func (suite *KeeperTestSuite) TestPoolType() {
poolId := suite.PrepareBalancerPool()

// error when querying invalid pool ID
_, err := suite.queryClient.PoolType(gocontext.Background(), &types.QueryPoolTypeRequest{PoolId: poolId + 1})
suite.Require().Error(err)

res, err := suite.queryClient.PoolType(gocontext.Background(), &types.QueryPoolTypeRequest{PoolId: poolId})
suite.Require().NoError(err)
suite.Require().Equal("Balancer", res.PoolType)
}

func (suite *KeeperTestSuite) TestQueryNumPools1() {
res, err := suite.queryClient.NumPools(gocontext.Background(), &types.QueryNumPoolsRequest{})
suite.Require().NoError(err)
Expand Down
15 changes: 15 additions & 0 deletions x/gamm/keeper/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,21 @@ func (k Keeper) GetNextPoolId(ctx sdk.Context) uint64 {
return nextPoolId
}

func (k Keeper) GetPoolType(ctx sdk.Context, poolId uint64) (string, error) {
pool, err := k.GetPoolAndPoke(ctx, poolId)
if err != nil {
return "", err
}

switch pool := pool.(type) {
case *balancer.Pool:
return "Balancer", nil
default:
errMsg := fmt.Sprintf("unrecognized %s pool type: %T", types.ModuleName, pool)
return "", sdkerrors.Wrap(sdkerrors.ErrUnpackAny, errMsg)
}
}

// getNextPoolIdAndIncrement returns the next pool Id, and increments the corresponding state entry.
func (k Keeper) getNextPoolIdAndIncrement(ctx sdk.Context) uint64 {
nextPoolId := k.GetNextPoolId(ctx)
Expand Down
Loading