Skip to content

Commit

Permalink
Add Pool Type Query (osmosis-labs#2832)
Browse files Browse the repository at this point in the history
Closes: osmosis-labs#2739 

## What is the purpose of the change

Adds pool type query to query the type of pool. The query would return a string of the type of pool in a string, depending on the type of pool, would return an error otherwise.


## Brief Changelog
- Add pool type query 


## Testing and Verifying
Added new test cases 
## Documentation and Release Note

  - Does this pull request introduce a new feature or user-facing behavior changes? yes 
  - Is a relevant changelog entry added to the `Unreleased` section in `CHANGELOG.md`?yes
  - How is the feature or change documented? (not applicable   /   specification (`x/<module>/spec/`)  /  [Osmosis docs repo](https://github.com/osmosis-labs/docs)   /   not documented)
  • Loading branch information
mattverse authored and Ruslan Akhtariev committed Sep 28, 2022
1 parent 1c0ca1c commit c8e9ef3
Show file tree
Hide file tree
Showing 7 changed files with 629 additions and 97 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ 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
* [#2803](https://github.com/osmosis-labs/osmosis/pull/2803) Fix total pool liquidity CLI query.

## v12.0.0

This release includes several cosmwasm-developer and appchain-ecosystem affecting upgrades:
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

0 comments on commit c8e9ef3

Please sign in to comment.