2020-06-05 11:10:04 -07:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
2020-06-30 13:59:21 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/store/prefix"
|
2020-06-05 11:10:04 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-07-06 12:35:35 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/query"
|
2020-06-05 11:10:04 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ types.QueryServer = BaseKeeper{}
|
|
|
|
|
|
|
|
// Balance implements the Query/Balance gRPC method
|
2020-08-17 06:55:04 -07:00
|
|
|
func (k BaseKeeper) Balance(ctx context.Context, req *types.QueryBalanceRequest) (*types.QueryBalanceResponse, error) {
|
2020-06-05 11:10:04 -07:00
|
|
|
if req == nil {
|
2020-07-28 05:53:35 -07:00
|
|
|
return nil, status.Error(codes.InvalidArgument, "empty request")
|
2020-06-05 11:10:04 -07:00
|
|
|
}
|
|
|
|
|
2020-09-25 03:25:37 -07:00
|
|
|
if req.Address == "" {
|
2020-07-28 05:53:35 -07:00
|
|
|
return nil, status.Error(codes.InvalidArgument, "address cannot be empty")
|
2020-06-05 11:10:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.Denom == "" {
|
2020-07-28 05:53:35 -07:00
|
|
|
return nil, status.Error(codes.InvalidArgument, "invalid denom")
|
2020-06-05 11:10:04 -07:00
|
|
|
}
|
|
|
|
|
2020-08-17 06:55:04 -07:00
|
|
|
sdkCtx := sdk.UnwrapSDKContext(ctx)
|
2020-09-25 03:25:37 -07:00
|
|
|
address, err := sdk.AccAddressFromBech32(req.Address)
|
|
|
|
if err != nil {
|
2020-12-04 04:19:29 -08:00
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "invalid address: %s", err.Error())
|
2020-09-25 03:25:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
balance := k.GetBalance(sdkCtx, address, req.Denom)
|
2020-06-05 11:10:04 -07:00
|
|
|
|
|
|
|
return &types.QueryBalanceResponse{Balance: &balance}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllBalances implements the Query/AllBalances gRPC method
|
2020-08-17 06:55:04 -07:00
|
|
|
func (k BaseKeeper) AllBalances(ctx context.Context, req *types.QueryAllBalancesRequest) (*types.QueryAllBalancesResponse, error) {
|
2020-06-05 11:10:04 -07:00
|
|
|
if req == nil {
|
2020-07-28 05:53:35 -07:00
|
|
|
return nil, status.Error(codes.InvalidArgument, "empty request")
|
2020-06-05 11:10:04 -07:00
|
|
|
}
|
|
|
|
|
2020-12-04 04:19:29 -08:00
|
|
|
if req.Address == "" {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "address cannot be empty")
|
|
|
|
}
|
|
|
|
|
2020-09-25 03:25:37 -07:00
|
|
|
addr, err := sdk.AccAddressFromBech32(req.Address)
|
|
|
|
if err != nil {
|
2020-12-04 04:19:29 -08:00
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "invalid address: %s", err.Error())
|
2020-06-05 11:10:04 -07:00
|
|
|
}
|
|
|
|
|
2020-08-17 06:55:04 -07:00
|
|
|
sdkCtx := sdk.UnwrapSDKContext(ctx)
|
2020-06-05 11:10:04 -07:00
|
|
|
|
2020-06-20 16:44:04 -07:00
|
|
|
balances := sdk.NewCoins()
|
2021-02-01 05:17:44 -08:00
|
|
|
accountStore := k.getAccountStore(sdkCtx, addr)
|
2020-06-20 16:44:04 -07:00
|
|
|
|
2021-08-05 10:00:28 -07:00
|
|
|
pageRes, err := query.Paginate(accountStore, req.Pagination, func(key, value []byte) error {
|
|
|
|
var amount sdk.Int
|
|
|
|
if err := amount.Unmarshal(value); err != nil {
|
2020-06-20 16:44:04 -07:00
|
|
|
return err
|
|
|
|
}
|
2021-08-05 10:00:28 -07:00
|
|
|
balances = append(balances, sdk.NewCoin(string(key), amount))
|
2020-06-20 16:44:04 -07:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2020-12-07 01:32:59 -08:00
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "paginate: %v", err)
|
2020-06-20 16:44:04 -07:00
|
|
|
}
|
|
|
|
|
2020-07-20 09:51:16 -07:00
|
|
|
return &types.QueryAllBalancesResponse{Balances: balances, Pagination: pageRes}, nil
|
2020-06-05 11:10:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// TotalSupply implements the Query/TotalSupply gRPC method
|
2021-04-06 07:43:08 -07:00
|
|
|
func (k BaseKeeper) TotalSupply(ctx context.Context, req *types.QueryTotalSupplyRequest) (*types.QueryTotalSupplyResponse, error) {
|
2020-08-17 06:55:04 -07:00
|
|
|
sdkCtx := sdk.UnwrapSDKContext(ctx)
|
2021-04-06 07:43:08 -07:00
|
|
|
totalSupply, pageRes, err := k.GetPaginatedTotalSupply(sdkCtx, req.Pagination)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
2020-06-05 11:10:04 -07:00
|
|
|
|
2021-04-06 07:43:08 -07:00
|
|
|
return &types.QueryTotalSupplyResponse{Supply: totalSupply, Pagination: pageRes}, nil
|
2020-06-05 11:10:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// SupplyOf implements the Query/SupplyOf gRPC method
|
2020-08-17 06:55:04 -07:00
|
|
|
func (k BaseKeeper) SupplyOf(c context.Context, req *types.QuerySupplyOfRequest) (*types.QuerySupplyOfResponse, error) {
|
2020-06-05 11:10:04 -07:00
|
|
|
if req == nil {
|
2020-07-28 05:53:35 -07:00
|
|
|
return nil, status.Error(codes.InvalidArgument, "empty request")
|
2020-06-05 11:10:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.Denom == "" {
|
2020-07-28 05:53:35 -07:00
|
|
|
return nil, status.Error(codes.InvalidArgument, "invalid denom")
|
2020-06-05 11:10:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
2021-03-03 01:58:16 -08:00
|
|
|
supply := k.GetSupply(ctx, req.Denom)
|
2020-06-05 11:10:04 -07:00
|
|
|
|
2021-03-03 01:58:16 -08:00
|
|
|
return &types.QuerySupplyOfResponse{Amount: sdk.NewCoin(req.Denom, supply.Amount)}, nil
|
2020-06-05 11:10:04 -07:00
|
|
|
}
|
2020-08-17 06:55:04 -07:00
|
|
|
|
|
|
|
// 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 {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "empty request")
|
|
|
|
}
|
|
|
|
|
|
|
|
sdkCtx := sdk.UnwrapSDKContext(ctx)
|
|
|
|
params := k.GetParams(sdkCtx)
|
|
|
|
|
|
|
|
return &types.QueryParamsResponse{Params: params}, nil
|
|
|
|
}
|
2021-01-18 10:15:16 -08:00
|
|
|
|
|
|
|
// DenomsMetadata implements Query/DenomsMetadata gRPC method.
|
|
|
|
func (k BaseKeeper) DenomsMetadata(c context.Context, req *types.QueryDenomsMetadataRequest) (*types.QueryDenomsMetadataResponse, error) {
|
|
|
|
if req == nil {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "empty request")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.DenomMetadataPrefix)
|
|
|
|
|
|
|
|
metadatas := []types.Metadata{}
|
|
|
|
pageRes, err := query.Paginate(store, req.Pagination, func(_, value []byte) error {
|
|
|
|
var metadata types.Metadata
|
2021-04-29 03:46:22 -07:00
|
|
|
k.cdc.MustUnmarshal(value, &metadata)
|
2021-01-18 10:15:16 -08:00
|
|
|
|
|
|
|
metadatas = append(metadatas, metadata)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return &types.QueryDenomsMetadataResponse{
|
|
|
|
Metadatas: metadatas,
|
|
|
|
Pagination: pageRes,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DenomMetadata implements Query/DenomMetadata gRPC method.
|
|
|
|
func (k BaseKeeper) DenomMetadata(c context.Context, req *types.QueryDenomMetadataRequest) (*types.QueryDenomMetadataResponse, error) {
|
|
|
|
if req == nil {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "empty request")
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Denom == "" {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "invalid denom")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
|
|
|
|
metadata, found := k.GetDenomMetaData(ctx, req.Denom)
|
|
|
|
if !found {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "client metadata for denom %s", req.Denom)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &types.QueryDenomMetadataResponse{
|
|
|
|
Metadata: metadata,
|
|
|
|
}, nil
|
|
|
|
}
|
2021-06-28 07:40:44 -07:00
|
|
|
|
|
|
|
func (k BaseKeeper) DenomOwners(
|
|
|
|
goCtx context.Context,
|
|
|
|
req *types.QueryDenomOwnersRequest,
|
|
|
|
) (*types.QueryDenomOwnersResponse, error) {
|
|
|
|
|
|
|
|
if req == nil {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "empty request")
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Denom == "" {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "empty denom")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
2021-07-26 10:51:04 -07:00
|
|
|
denomPrefixStore := k.getDenomAddressPrefixStore(ctx, req.Denom)
|
2021-06-28 07:40:44 -07:00
|
|
|
|
|
|
|
var denomOwners []*types.DenomOwner
|
|
|
|
pageRes, err := query.FilteredPaginate(
|
2021-07-26 10:51:04 -07:00
|
|
|
denomPrefixStore,
|
2021-06-28 07:40:44 -07:00
|
|
|
req.Pagination,
|
|
|
|
func(key []byte, value []byte, accumulate bool) (bool, error) {
|
|
|
|
if accumulate {
|
2021-08-05 10:00:28 -07:00
|
|
|
address, _, err := types.AddressAndDenomFromBalancesStore(key)
|
2021-06-28 07:40:44 -07:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
denomOwners = append(
|
|
|
|
denomOwners,
|
|
|
|
&types.DenomOwner{
|
|
|
|
Address: address.String(),
|
2021-07-26 10:51:04 -07:00
|
|
|
Balance: k.GetBalance(ctx, address, req.Denom),
|
2021-06-28 07:40:44 -07:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return &types.QueryDenomOwnersResponse{DenomOwners: denomOwners, Pagination: pageRes}, nil
|
|
|
|
}
|