github.com/Finschia/finschia-sdk@v0.48.1/x/token/keeper/grpc_query.go (about)

     1  package keeper
     2  
     3  import (
     4  	"context"
     5  
     6  	"google.golang.org/grpc/codes"
     7  	"google.golang.org/grpc/status"
     8  
     9  	"github.com/Finschia/finschia-sdk/store/prefix"
    10  	sdk "github.com/Finschia/finschia-sdk/types"
    11  	sdkerrors "github.com/Finschia/finschia-sdk/types/errors"
    12  	"github.com/Finschia/finschia-sdk/types/query"
    13  	"github.com/Finschia/finschia-sdk/x/token"
    14  )
    15  
    16  type queryServer struct {
    17  	keeper Keeper
    18  }
    19  
    20  // NewQueryServer returns an implementation of the token QueryServer interface
    21  // for the provided Keeper.
    22  func NewQueryServer(keeper Keeper) token.QueryServer {
    23  	return &queryServer{
    24  		keeper: keeper,
    25  	}
    26  }
    27  
    28  var _ token.QueryServer = queryServer{}
    29  
    30  func (s queryServer) addressFromBech32GRPC(address string, context string) (sdk.AccAddress, error) {
    31  	addr, err := sdk.AccAddressFromBech32(address)
    32  	if err != nil {
    33  		return nil, status.Error(codes.InvalidArgument, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress.Wrap(address), context).Error())
    34  	}
    35  
    36  	return addr, nil
    37  }
    38  
    39  // Balance queries the number of tokens of a given class owned by the owner.
    40  func (s queryServer) Balance(c context.Context, req *token.QueryBalanceRequest) (*token.QueryBalanceResponse, error) {
    41  	if req == nil {
    42  		return nil, status.Error(codes.InvalidArgument, "empty request")
    43  	}
    44  
    45  	if err := token.ValidateContractID(req.ContractId); err != nil {
    46  		return nil, status.Error(codes.InvalidArgument, err.Error())
    47  	}
    48  	addr, err := s.addressFromBech32GRPC(req.Address, "address")
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  
    53  	ctx := sdk.UnwrapSDKContext(c)
    54  	balance := s.keeper.GetBalance(ctx, req.ContractId, addr)
    55  
    56  	return &token.QueryBalanceResponse{Amount: balance}, nil
    57  }
    58  
    59  // Supply queries the number of tokens from the given contract id.
    60  func (s queryServer) Supply(c context.Context, req *token.QuerySupplyRequest) (*token.QuerySupplyResponse, error) {
    61  	if req == nil {
    62  		return nil, status.Error(codes.InvalidArgument, "empty request")
    63  	}
    64  
    65  	if err := token.ValidateContractID(req.ContractId); err != nil {
    66  		return nil, status.Error(codes.InvalidArgument, err.Error())
    67  	}
    68  
    69  	ctx := sdk.UnwrapSDKContext(c)
    70  	supply := s.keeper.GetSupply(ctx, req.ContractId)
    71  
    72  	return &token.QuerySupplyResponse{Amount: supply}, nil
    73  }
    74  
    75  // Minted queries the number of tokens from the given contract id.
    76  func (s queryServer) Minted(c context.Context, req *token.QueryMintedRequest) (*token.QueryMintedResponse, error) {
    77  	if req == nil {
    78  		return nil, status.Error(codes.InvalidArgument, "empty request")
    79  	}
    80  
    81  	if err := token.ValidateContractID(req.ContractId); err != nil {
    82  		return nil, status.Error(codes.InvalidArgument, err.Error())
    83  	}
    84  
    85  	ctx := sdk.UnwrapSDKContext(c)
    86  	minted := s.keeper.GetMinted(ctx, req.ContractId)
    87  
    88  	return &token.QueryMintedResponse{Amount: minted}, nil
    89  }
    90  
    91  // Burnt queries the number of tokens from the given contract id.
    92  func (s queryServer) Burnt(c context.Context, req *token.QueryBurntRequest) (*token.QueryBurntResponse, error) {
    93  	if req == nil {
    94  		return nil, status.Error(codes.InvalidArgument, "empty request")
    95  	}
    96  
    97  	if err := token.ValidateContractID(req.ContractId); err != nil {
    98  		return nil, status.Error(codes.InvalidArgument, err.Error())
    99  	}
   100  
   101  	ctx := sdk.UnwrapSDKContext(c)
   102  	burnt := s.keeper.GetBurnt(ctx, req.ContractId)
   103  
   104  	return &token.QueryBurntResponse{Amount: burnt}, nil
   105  }
   106  
   107  // Contract queries an token metadata based on its contract id.
   108  func (s queryServer) Contract(c context.Context, req *token.QueryContractRequest) (*token.QueryContractResponse, error) {
   109  	if req == nil {
   110  		return nil, status.Error(codes.InvalidArgument, "empty request")
   111  	}
   112  
   113  	if err := token.ValidateContractID(req.ContractId); err != nil {
   114  		return nil, status.Error(codes.InvalidArgument, err.Error())
   115  	}
   116  
   117  	ctx := sdk.UnwrapSDKContext(c)
   118  	class, err := s.keeper.GetClass(ctx, req.ContractId)
   119  	if err != nil {
   120  		return nil, status.Error(codes.NotFound, err.Error())
   121  	}
   122  
   123  	return &token.QueryContractResponse{Contract: *class}, nil
   124  }
   125  
   126  func (s queryServer) GranteeGrants(c context.Context, req *token.QueryGranteeGrantsRequest) (*token.QueryGranteeGrantsResponse, error) {
   127  	if req == nil {
   128  		return nil, status.Error(codes.InvalidArgument, "empty request")
   129  	}
   130  
   131  	if err := token.ValidateContractID(req.ContractId); err != nil {
   132  		return nil, status.Error(codes.InvalidArgument, err.Error())
   133  	}
   134  	grantee, err := s.addressFromBech32GRPC(req.Grantee, "grantee")
   135  	if err != nil {
   136  		return nil, err
   137  	}
   138  
   139  	ctx := sdk.UnwrapSDKContext(c)
   140  	store := ctx.KVStore(s.keeper.storeKey)
   141  	grantStore := prefix.NewStore(store, grantKeyPrefixByGrantee(req.ContractId, grantee))
   142  	var grants []token.Grant
   143  	pageRes, err := query.Paginate(grantStore, req.Pagination, func(key []byte, _ []byte) error {
   144  		permission := token.Permission(key[0])
   145  		grants = append(grants, token.Grant{
   146  			Grantee:    req.Grantee,
   147  			Permission: permission,
   148  		})
   149  		return nil
   150  	})
   151  	if err != nil {
   152  		return nil, err
   153  	}
   154  
   155  	return &token.QueryGranteeGrantsResponse{Grants: grants, Pagination: pageRes}, nil
   156  }
   157  
   158  func (s queryServer) IsOperatorFor(c context.Context, req *token.QueryIsOperatorForRequest) (*token.QueryIsOperatorForResponse, error) {
   159  	if req == nil {
   160  		return nil, status.Error(codes.InvalidArgument, "empty request")
   161  	}
   162  
   163  	if err := token.ValidateContractID(req.ContractId); err != nil {
   164  		return nil, status.Error(codes.InvalidArgument, err.Error())
   165  	}
   166  	operator, err := s.addressFromBech32GRPC(req.Operator, "operator")
   167  	if err != nil {
   168  		return nil, err
   169  	}
   170  	holder, err := s.addressFromBech32GRPC(req.Holder, "holder")
   171  	if err != nil {
   172  		return nil, err
   173  	}
   174  
   175  	ctx := sdk.UnwrapSDKContext(c)
   176  	_, err = s.keeper.GetAuthorization(ctx, req.ContractId, holder, operator)
   177  	authorized := err == nil
   178  
   179  	return &token.QueryIsOperatorForResponse{Authorized: authorized}, nil
   180  }
   181  
   182  func (s queryServer) HoldersByOperator(c context.Context, req *token.QueryHoldersByOperatorRequest) (*token.QueryHoldersByOperatorResponse, error) {
   183  	if req == nil {
   184  		return nil, status.Error(codes.InvalidArgument, "empty request")
   185  	}
   186  
   187  	if err := token.ValidateContractID(req.ContractId); err != nil {
   188  		return nil, status.Error(codes.InvalidArgument, err.Error())
   189  	}
   190  	operator, err := s.addressFromBech32GRPC(req.Operator, "operator")
   191  	if err != nil {
   192  		return nil, err
   193  	}
   194  
   195  	ctx := sdk.UnwrapSDKContext(c)
   196  	store := ctx.KVStore(s.keeper.storeKey)
   197  	authorizationStore := prefix.NewStore(store, authorizationKeyPrefixByOperator(req.ContractId, operator))
   198  	var holders []string
   199  	pageRes, err := query.Paginate(authorizationStore, req.Pagination, func(key []byte, value []byte) error {
   200  		holder := sdk.AccAddress(key)
   201  		holders = append(holders, holder.String())
   202  		return nil
   203  	})
   204  	if err != nil {
   205  		return nil, err
   206  	}
   207  
   208  	return &token.QueryHoldersByOperatorResponse{Holders: holders, Pagination: pageRes}, nil
   209  }