-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic x/bank gRPC query service (#6343)
* Add basic x/bank gRPC query server * proto lint * Add pb.go file * cleanup * add separate grpc query tests * Add request validation * Use gRPC status errors Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
- Loading branch information
1 parent
6139c04
commit 72925fa
Showing
12 changed files
with
2,073 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package keeper | ||
|
||
import ( | ||
"context" | ||
|
||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/bank/types" | ||
) | ||
|
||
var _ types.QueryServer = BaseKeeper{} | ||
|
||
// Balance implements the Query/Balance gRPC method | ||
func (q BaseKeeper) Balance(c context.Context, req *types.QueryBalanceRequest) (*types.QueryBalanceResponse, error) { | ||
if req == nil { | ||
return nil, status.Errorf(codes.InvalidArgument, "empty request") | ||
} | ||
|
||
if len(req.Address) == 0 { | ||
return nil, status.Errorf(codes.InvalidArgument, "invalid address") | ||
} | ||
|
||
if req.Denom == "" { | ||
return nil, status.Errorf(codes.InvalidArgument, "invalid denom") | ||
} | ||
|
||
ctx := sdk.UnwrapSDKContext(c) | ||
balance := q.GetBalance(ctx, req.Address, req.Denom) | ||
|
||
return &types.QueryBalanceResponse{Balance: &balance}, nil | ||
} | ||
|
||
// AllBalances implements the Query/AllBalances gRPC method | ||
func (q BaseKeeper) AllBalances(c context.Context, req *types.QueryAllBalancesRequest) (*types.QueryAllBalancesResponse, error) { | ||
if req == nil { | ||
return nil, status.Errorf(codes.InvalidArgument, "empty request") | ||
} | ||
|
||
if len(req.Address) == 0 { | ||
return nil, status.Errorf(codes.InvalidArgument, "invalid address") | ||
} | ||
|
||
ctx := sdk.UnwrapSDKContext(c) | ||
balances := q.GetAllBalances(ctx, req.Address) | ||
|
||
return &types.QueryAllBalancesResponse{Balances: balances}, nil | ||
} | ||
|
||
// TotalSupply implements the Query/TotalSupply gRPC method | ||
func (q BaseKeeper) TotalSupply(c context.Context, _ *types.QueryTotalSupplyRequest) (*types.QueryTotalSupplyResponse, error) { | ||
ctx := sdk.UnwrapSDKContext(c) | ||
totalSupply := q.GetSupply(ctx).GetTotal() | ||
|
||
return &types.QueryTotalSupplyResponse{Supply: totalSupply}, nil | ||
} | ||
|
||
// SupplyOf implements the Query/SupplyOf gRPC method | ||
func (q BaseKeeper) SupplyOf(c context.Context, req *types.QuerySupplyOfRequest) (*types.QuerySupplyOfResponse, error) { | ||
if req == nil { | ||
return nil, status.Errorf(codes.InvalidArgument, "empty request") | ||
} | ||
|
||
if req.Denom == "" { | ||
return nil, status.Errorf(codes.InvalidArgument, "invalid denom") | ||
} | ||
|
||
ctx := sdk.UnwrapSDKContext(c) | ||
supply := q.GetSupply(ctx).GetTotal().AmountOf(req.Denom) | ||
|
||
return &types.QuerySupplyOfResponse{Amount: supply}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package keeper_test | ||
|
||
import ( | ||
gocontext "context" | ||
|
||
"github.com/cosmos/cosmos-sdk/baseapp" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
"github.com/cosmos/cosmos-sdk/x/bank" | ||
"github.com/cosmos/cosmos-sdk/x/bank/types" | ||
) | ||
|
||
func (suite *IntegrationTestSuite) TestQueryBalance() { | ||
app, ctx := suite.app, suite.ctx | ||
_, _, addr := authtypes.KeyTestPubAddr() | ||
|
||
queryHelper := baseapp.NewQueryServerTestHelper(ctx) | ||
types.RegisterQueryServer(queryHelper, app.BankKeeper) | ||
queryClient := types.NewQueryClient(queryHelper) | ||
|
||
_, err := queryClient.Balance(gocontext.Background(), &types.QueryBalanceRequest{}) | ||
suite.Require().Error(err) | ||
|
||
_, err = queryClient.Balance(gocontext.Background(), &types.QueryBalanceRequest{Address: addr}) | ||
suite.Require().Error(err) | ||
|
||
req := types.NewQueryBalanceRequest(addr, fooDenom) | ||
res, err := queryClient.Balance(gocontext.Background(), req) | ||
suite.Require().NoError(err) | ||
suite.Require().NotNil(res) | ||
suite.True(res.Balance.IsZero()) | ||
|
||
origCoins := sdk.NewCoins(newFooCoin(50), newBarCoin(30)) | ||
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr) | ||
|
||
app.AccountKeeper.SetAccount(ctx, acc) | ||
suite.Require().NoError(app.BankKeeper.SetBalances(ctx, acc.GetAddress(), origCoins)) | ||
|
||
res, err = queryClient.Balance(gocontext.Background(), req) | ||
suite.Require().NoError(err) | ||
suite.Require().NotNil(res) | ||
suite.True(res.Balance.IsEqual(newFooCoin(50))) | ||
} | ||
|
||
func (suite *IntegrationTestSuite) TestQueryAllBalances() { | ||
app, ctx := suite.app, suite.ctx | ||
_, _, addr := authtypes.KeyTestPubAddr() | ||
|
||
queryHelper := baseapp.NewQueryServerTestHelper(ctx) | ||
types.RegisterQueryServer(queryHelper, app.BankKeeper) | ||
queryClient := types.NewQueryClient(queryHelper) | ||
|
||
_, err := queryClient.AllBalances(gocontext.Background(), &types.QueryAllBalancesRequest{}) | ||
suite.Require().Error(err) | ||
|
||
req := types.NewQueryAllBalancesRequest(addr) | ||
res, err := queryClient.AllBalances(gocontext.Background(), req) | ||
suite.Require().NoError(err) | ||
suite.Require().NotNil(res) | ||
suite.True(res.Balances.IsZero()) | ||
|
||
origCoins := sdk.NewCoins(newFooCoin(50), newBarCoin(30)) | ||
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr) | ||
|
||
app.AccountKeeper.SetAccount(ctx, acc) | ||
suite.Require().NoError(app.BankKeeper.SetBalances(ctx, acc.GetAddress(), origCoins)) | ||
|
||
res, err = queryClient.AllBalances(gocontext.Background(), req) | ||
suite.Require().NoError(err) | ||
suite.Require().NotNil(res) | ||
suite.True(res.Balances.IsEqual(origCoins)) | ||
} | ||
|
||
func (suite *IntegrationTestSuite) TestQueryTotalSupply() { | ||
app, ctx := suite.app, suite.ctx | ||
expectedTotalSupply := bank.NewSupply(sdk.NewCoins(sdk.NewInt64Coin("test", 400000000))) | ||
app.BankKeeper.SetSupply(ctx, expectedTotalSupply) | ||
|
||
queryHelper := baseapp.NewQueryServerTestHelper(ctx) | ||
types.RegisterQueryServer(queryHelper, app.BankKeeper) | ||
queryClient := types.NewQueryClient(queryHelper) | ||
|
||
res, err := queryClient.TotalSupply(gocontext.Background(), &types.QueryTotalSupplyRequest{}) | ||
suite.Require().NoError(err) | ||
suite.Require().NotNil(res) | ||
|
||
suite.Require().Equal(expectedTotalSupply.Total, res.Supply) | ||
} | ||
|
||
func (suite *IntegrationTestSuite) TestQueryTotalSupplyOf() { | ||
app, ctx := suite.app, suite.ctx | ||
|
||
test1Supply := sdk.NewInt64Coin("test1", 4000000) | ||
test2Supply := sdk.NewInt64Coin("test2", 700000000) | ||
expectedTotalSupply := bank.NewSupply(sdk.NewCoins(test1Supply, test2Supply)) | ||
app.BankKeeper.SetSupply(ctx, expectedTotalSupply) | ||
|
||
queryHelper := baseapp.NewQueryServerTestHelper(ctx) | ||
types.RegisterQueryServer(queryHelper, app.BankKeeper) | ||
queryClient := types.NewQueryClient(queryHelper) | ||
|
||
_, err := queryClient.SupplyOf(gocontext.Background(), &types.QuerySupplyOfRequest{}) | ||
suite.Require().Error(err) | ||
|
||
res, err := queryClient.SupplyOf(gocontext.Background(), &types.QuerySupplyOfRequest{Denom: test1Supply.Denom}) | ||
suite.Require().NoError(err) | ||
suite.Require().NotNil(res) | ||
|
||
suite.Require().Equal(test1Supply.Amount, res.Amount) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.