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

     1  package keeper
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/gogo/protobuf/proto"
     7  	"google.golang.org/grpc/codes"
     8  	"google.golang.org/grpc/status"
     9  
    10  	codectypes "github.com/Finschia/finschia-sdk/codec/types"
    11  	"github.com/Finschia/finschia-sdk/store/prefix"
    12  	sdk "github.com/Finschia/finschia-sdk/types"
    13  	"github.com/Finschia/finschia-sdk/types/query"
    14  	"github.com/Finschia/finschia-sdk/x/feegrant"
    15  )
    16  
    17  var _ feegrant.QueryServer = Keeper{}
    18  
    19  // Allowance returns fee granted to the grantee by the granter.
    20  func (q Keeper) Allowance(c context.Context, req *feegrant.QueryAllowanceRequest) (*feegrant.QueryAllowanceResponse, error) {
    21  	if req == nil {
    22  		return nil, status.Error(codes.InvalidArgument, "invalid request")
    23  	}
    24  
    25  	granterAddr, err := sdk.AccAddressFromBech32(req.Granter)
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  
    30  	granteeAddr, err := sdk.AccAddressFromBech32(req.Grantee)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  
    35  	ctx := sdk.UnwrapSDKContext(c)
    36  
    37  	feeAllowance, err := q.GetAllowance(ctx, granterAddr, granteeAddr)
    38  	if err != nil {
    39  		return nil, status.Errorf(codes.Internal, err.Error())
    40  	}
    41  
    42  	msg, ok := feeAllowance.(proto.Message)
    43  	if !ok {
    44  		return nil, status.Errorf(codes.Internal, "can't proto marshal %T", msg)
    45  	}
    46  
    47  	feeAllowanceAny, err := codectypes.NewAnyWithValue(msg)
    48  	if err != nil {
    49  		return nil, status.Errorf(codes.Internal, err.Error())
    50  	}
    51  
    52  	return &feegrant.QueryAllowanceResponse{
    53  		Allowance: &feegrant.Grant{
    54  			Granter:   granterAddr.String(),
    55  			Grantee:   granteeAddr.String(),
    56  			Allowance: feeAllowanceAny,
    57  		},
    58  	}, nil
    59  }
    60  
    61  // Allowances queries all the allowances granted to the given grantee.
    62  func (q Keeper) Allowances(c context.Context, req *feegrant.QueryAllowancesRequest) (*feegrant.QueryAllowancesResponse, error) {
    63  	if req == nil {
    64  		return nil, status.Error(codes.InvalidArgument, "invalid request")
    65  	}
    66  
    67  	granteeAddr, err := sdk.AccAddressFromBech32(req.Grantee)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  
    72  	ctx := sdk.UnwrapSDKContext(c)
    73  
    74  	var grants []*feegrant.Grant
    75  
    76  	store := ctx.KVStore(q.storeKey)
    77  	grantsStore := prefix.NewStore(store, feegrant.FeeAllowancePrefixByGrantee(granteeAddr))
    78  
    79  	pageRes, err := query.Paginate(grantsStore, req.Pagination, func(key, value []byte) error {
    80  		var grant feegrant.Grant
    81  
    82  		if err := q.cdc.Unmarshal(value, &grant); err != nil {
    83  			return err
    84  		}
    85  
    86  		grants = append(grants, &grant)
    87  		return nil
    88  	})
    89  	if err != nil {
    90  		return nil, status.Error(codes.Internal, err.Error())
    91  	}
    92  
    93  	return &feegrant.QueryAllowancesResponse{Allowances: grants, Pagination: pageRes}, nil
    94  }
    95  
    96  // AllowancesByGranter queries all the allowances granted by the given granter
    97  func (q Keeper) AllowancesByGranter(c context.Context, req *feegrant.QueryAllowancesByGranterRequest) (*feegrant.QueryAllowancesByGranterResponse, error) {
    98  	if req == nil {
    99  		return nil, status.Error(codes.InvalidArgument, "invalid request")
   100  	}
   101  
   102  	granterAddr, err := sdk.AccAddressFromBech32(req.Granter)
   103  	if err != nil {
   104  		return nil, err
   105  	}
   106  
   107  	ctx := sdk.UnwrapSDKContext(c)
   108  
   109  	var grants []*feegrant.Grant
   110  
   111  	store := ctx.KVStore(q.storeKey)
   112  	prefixStore := prefix.NewStore(store, feegrant.FeeAllowanceKeyPrefix)
   113  	pageRes, err := query.FilteredPaginate(prefixStore, req.Pagination, func(key, value []byte, accumulate bool) (bool, error) {
   114  		// ParseAddressesFromFeeAllowanceKey expects the full key including the prefix.
   115  		granter, _ := feegrant.ParseAddressesFromFeeAllowanceKey(append(feegrant.FeeAllowanceKeyPrefix, key...))
   116  		if !granter.Equals(granterAddr) {
   117  			return false, nil
   118  		}
   119  
   120  		if accumulate {
   121  			var grant feegrant.Grant
   122  			if err := q.cdc.Unmarshal(value, &grant); err != nil {
   123  				return false, err
   124  			}
   125  			grants = append(grants, &grant)
   126  		}
   127  
   128  		return true, nil
   129  	})
   130  	if err != nil {
   131  		return nil, status.Error(codes.Internal, err.Error())
   132  	}
   133  
   134  	return &feegrant.QueryAllowancesByGranterResponse{Allowances: grants, Pagination: pageRes}, nil
   135  }