Skip to content

Commit

Permalink
feat: Add Supply Offsets to bank module (cosmos#119)
Browse files Browse the repository at this point in the history
* add query supplies with offsets

* add functions to interface

* add tests for query

* add supplywithoutoffset queries
  • Loading branch information
sunnya97 authored and roysc committed Jul 10, 2022
1 parent e87cd84 commit 45d5757
Show file tree
Hide file tree
Showing 13 changed files with 3,845 additions and 362 deletions.
2,650 changes: 2,396 additions & 254 deletions api/cosmos/bank/v1beta1/query.pulsar.go

Large diffs are not rendered by default.

76 changes: 76 additions & 0 deletions api/cosmos/bank/v1beta1/query_grpc.pb.go

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

2 changes: 1 addition & 1 deletion api/cosmos/base/abci/v1beta1/abci.pulsar.go

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

47 changes: 47 additions & 0 deletions proto/cosmos/bank/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ service Query {
option (google.api.http).get = "/cosmos/bank/v1beta1/supply/by_denom";
}

// TotalSupplyWithoutOffset queries the total supply of all coins.
rpc TotalSupplyWithoutOffset(QueryTotalSupplyWithoutOffsetRequest) returns (QueryTotalSupplyWithoutOffsetResponse) {
option (google.api.http).get = "/cosmos/bank/v1beta1/supply_without_offset";
}

// SupplyOf queries the supply of a single coin.
rpc SupplyOfWithoutOffset(QuerySupplyOfWithoutOffsetRequest) returns (QuerySupplyOfWithoutOffsetResponse) {
option (google.api.http).get = "/cosmos/bank/v1beta1/supply_without_offset/{denom}";
}

// Params queries the parameters of x/bank module.
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/cosmos/bank/v1beta1/params";
Expand Down Expand Up @@ -171,6 +181,43 @@ message QuerySupplyOfResponse {
cosmos.base.v1beta1.Coin amount = 1 [(gogoproto.nullable) = false];
}

// QueryTotalSupplyWithoutOffsetRequest is the request type for the Query/TotalSupplyWithoutOffset RPC
// method.
message QueryTotalSupplyWithoutOffsetRequest {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

// pagination defines an optional pagination for the request.
//
// Since: cosmos-sdk 0.43
cosmos.base.query.v1beta1.PageRequest pagination = 1;
}

// QueryTotalSupplyWithoutOffsetResponse is the response type for the Query/TotalSupplyWithoutOffset RPC
// method
message QueryTotalSupplyWithoutOffsetResponse {
// supply is the supply of the coins
repeated cosmos.base.v1beta1.Coin supply = 1
[(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"];

// pagination defines the pagination in the response.
//
// Since: cosmos-sdk 0.43
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

// QuerySupplyOfWithoutOffsetRequest is the request type for the Query/SupplyOfWithoutOffset RPC method.
message QuerySupplyOfWithoutOffsetRequest {
// denom is the coin denom to query balances for.
string denom = 1;
}

// QuerySupplyOfWithoutOffsetResponse is the response type for the Query/SupplyOfWithoutOffset RPC method.
message QuerySupplyOfWithoutOffsetResponse {
// amount is the supply of the coin.
cosmos.base.v1beta1.Coin amount = 1 [(gogoproto.nullable) = false];
}

// QueryParamsRequest defines the request type for querying x/bank parameters.
message QueryParamsRequest {}

Expand Down
2 changes: 1 addition & 1 deletion types/abci.pb.go

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

31 changes: 29 additions & 2 deletions x/bank/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (k BaseKeeper) SpendableBalances(ctx context.Context, req *types.QuerySpend
// TotalSupply implements the Query/TotalSupply gRPC method
func (k BaseKeeper) TotalSupply(ctx context.Context, req *types.QueryTotalSupplyRequest) (*types.QueryTotalSupplyResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
totalSupply, pageRes, err := k.GetPaginatedTotalSupply(sdkCtx, req.Pagination)
totalSupply, pageRes, err := k.GetPaginatedTotalSupplyWithOffsets(sdkCtx, req.Pagination)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
Expand All @@ -132,11 +132,38 @@ func (k BaseKeeper) SupplyOf(c context.Context, req *types.QuerySupplyOfRequest)
}

ctx := sdk.UnwrapSDKContext(c)
supply := k.GetSupply(ctx, req.Denom)
supply := k.GetSupplyWithOffset(ctx, req.Denom)

return &types.QuerySupplyOfResponse{Amount: sdk.NewCoin(req.Denom, supply.Amount)}, nil
}

// TotalSupply implements the Query/TotalSupplyWithoutOffset gRPC method
func (k BaseKeeper) TotalSupplyWithoutOffset(ctx context.Context, req *types.QueryTotalSupplyWithoutOffsetRequest) (*types.QueryTotalSupplyWithoutOffsetResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
totalSupply, pageRes, err := k.GetPaginatedTotalSupply(sdkCtx, req.Pagination)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

return &types.QueryTotalSupplyWithoutOffsetResponse{Supply: totalSupply, Pagination: pageRes}, nil
}

// SupplyOf implements the Query/SupplyOf gRPC method
func (k BaseKeeper) SupplyOfWithoutOffset(c context.Context, req *types.QuerySupplyOfWithoutOffsetRequest) (*types.QuerySupplyOfWithoutOffsetResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

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

ctx := sdk.UnwrapSDKContext(c)
supply := k.GetSupply(ctx, req.Denom)

return &types.QuerySupplyOfWithoutOffsetResponse{Amount: sdk.NewCoin(req.Denom, supply.Amount)}, nil
}

// Params implements the gRPC service handler for querying x/bank parameters.
func (k BaseKeeper) Params(ctx context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
if req == nil {
Expand Down
33 changes: 32 additions & 1 deletion x/bank/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,23 @@ func (suite *IntegrationTestSuite) TestQueryTotalSupply() {

expectedTotalSupply := genesisSupply.Add(testCoins...)
suite.Require().Equal(2, len(res.Supply))
suite.Require().Equal(res.Supply, expectedTotalSupply)
suite.Require().Equal(expectedTotalSupply, res.Supply)

// test total supply query with supply offset
app.BankKeeper.AddSupplyOffset(ctx, "test", sdk.NewInt(-100000000))
res, err = queryClient.TotalSupply(gocontext.Background(), &types.QueryTotalSupplyRequest{})
suite.Require().NoError(err)
suite.Require().NotNil(res)

suite.Require().Equal(expectedTotalSupply.Sub(sdk.NewCoins(sdk.NewInt64Coin("test", 100000000))...), res.Supply)

// make sure query without offsets hasn't changed
res2, err := queryClient.TotalSupplyWithoutOffset(gocontext.Background(), &types.QueryTotalSupplyWithoutOffsetRequest{})
suite.Require().NoError(err)
suite.Require().NotNil(res2)

suite.Require().Equal(expectedTotalSupply, res2.Supply)

}

func (suite *IntegrationTestSuite) TestQueryTotalSupplyOf() {
Expand All @@ -177,6 +193,21 @@ func (suite *IntegrationTestSuite) TestQueryTotalSupplyOf() {
suite.Require().NotNil(res)

suite.Require().Equal(test1Supply, res.Amount)

// test total supply of query with supply offset
app.BankKeeper.AddSupplyOffset(ctx, "test1", sdk.NewInt(-1000000))
res, err = queryClient.SupplyOf(gocontext.Background(), &types.QuerySupplyOfRequest{Denom: test1Supply.Denom})
suite.Require().NoError(err)
suite.Require().NotNil(res)

suite.Require().Equal(test1Supply.Sub(sdk.NewInt64Coin("test1", 1000000)), res.Amount)

// make sure query without offsets hasn't changed
res2, err := queryClient.SupplyOfWithoutOffset(gocontext.Background(), &types.QuerySupplyOfWithoutOffsetRequest{Denom: test1Supply.Denom})
suite.Require().NoError(err)
suite.Require().NotNil(res2)

suite.Require().Equal(test1Supply, res2.Amount)
}

func (suite *IntegrationTestSuite) TestQueryParams() {
Expand Down
5 changes: 5 additions & 0 deletions x/bank/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ type Keeper interface {
HasSupply(ctx sdk.Context, denom string) bool
GetPaginatedTotalSupply(ctx sdk.Context, pagination *query.PageRequest) (sdk.Coins, *query.PageResponse, error)
IterateTotalSupply(ctx sdk.Context, cb func(sdk.Coin) bool)
GetSupplyOffset(ctx sdk.Context, denom string) sdk.Int
AddSupplyOffset(ctx sdk.Context, denom string, offsetAmount sdk.Int)
GetSupplyWithOffset(ctx sdk.Context, denom string) sdk.Coin
GetPaginatedTotalSupplyWithOffsets(ctx sdk.Context, pagination *query.PageRequest) (sdk.Coins, *query.PageResponse, error)
IterateTotalSupplyWithOffsets(ctx sdk.Context, cb func(sdk.Coin) bool)
GetDenomMetaData(ctx sdk.Context, denom string) (types.Metadata, bool)
HasDenomMetaData(ctx sdk.Context, denom string) bool
SetDenomMetaData(ctx sdk.Context, denomMetaData types.Metadata)
Expand Down
4 changes: 2 additions & 2 deletions x/bank/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ func (suite *IntegrationTestSuite) TestSendManyCoins() {
addr2 := sdk.AccAddress([]byte("addr2_______________"))
acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
app.AccountKeeper.SetAccount(ctx, acc2)
suite.Require().NoError(simapp.FundAccount(app.BankKeeper, ctx, addr2, balances))
suite.Require().NoError(testutil.FundAccount(app.BankKeeper, ctx, addr2, balances))

addr3 := sdk.AccAddress([]byte("addr3_______________"))
acc3 := app.AccountKeeper.NewAccountWithAddress(ctx, addr3)
Expand All @@ -500,7 +500,7 @@ func (suite *IntegrationTestSuite) TestSendManyCoins() {

suite.Require().Error(app.BankKeeper.SendManyCoins(ctx, addr1, toAddrs, sendAmts))

suite.Require().NoError(simapp.FundAccount(app.BankKeeper, ctx, addr1, balances))
suite.Require().NoError(testutil.FundAccount(app.BankKeeper, ctx, addr1, balances))
suite.Require().Error(app.BankKeeper.SendManyCoins(ctx, addr1, toAddrs, sendAmts))

toAddrs = append(toAddrs, addr3)
Expand Down
Loading

0 comments on commit 45d5757

Please sign in to comment.