Skip to content

Commit

Permalink
add queryserver
Browse files Browse the repository at this point in the history
  • Loading branch information
facundomedica committed Oct 12, 2023
1 parent 2a52753 commit ac0f712
Show file tree
Hide file tree
Showing 8 changed files with 1,488 additions and 117 deletions.
1,042 changes: 971 additions & 71 deletions api/cosmos/feegrant/v1beta1/query.pulsar.go

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions api/cosmos/feegrant/v1beta1/query_grpc.pb.go

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

12 changes: 12 additions & 0 deletions proto/cosmos/feegrant/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ service Query {
rpc AllowancesByGranter(QueryAllowancesByGranterRequest) returns (QueryAllowancesByGranterResponse) {
option (google.api.http).get = "/cosmos/feegrant/v1beta1/issued/{granter}";
}

rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/cosmos/feegrant/v1beta1/params";
}
}

// QueryAllowanceRequest is the request type for the Query/Allowance RPC method.
Expand Down Expand Up @@ -82,3 +86,11 @@ message QueryAllowancesByGranterResponse {
// pagination defines an pagination for the response.
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

// QueryParamsRequest is the request type for the Query/Params RPC method.
message QueryParamsRequest {}

// QueryParamsResponse is the response type for the Query/Params RPC method.
message QueryParamsResponse {
Params params = 1;
}
36 changes: 25 additions & 11 deletions x/feegrant/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,31 @@ import (
"github.com/cosmos/cosmos-sdk/types/query"
)

var _ feegrant.QueryServer = Keeper{}
var _ feegrant.QueryServer = queryServer{}

type queryServer struct{ k Keeper }

func NewQueryServer(k Keeper) queryServer {
return queryServer{k: k}
}

// Allowance returns granted allowance to the grantee by the granter.
func (q Keeper) Allowance(ctx context.Context, req *feegrant.QueryAllowanceRequest) (*feegrant.QueryAllowanceResponse, error) {
func (q queryServer) Allowance(ctx context.Context, req *feegrant.QueryAllowanceRequest) (*feegrant.QueryAllowanceResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

granterAddr, err := q.authKeeper.AddressCodec().StringToBytes(req.Granter)
granterAddr, err := q.k.authKeeper.AddressCodec().StringToBytes(req.Granter)
if err != nil {
return nil, err
}

granteeAddr, err := q.authKeeper.AddressCodec().StringToBytes(req.Grantee)
granteeAddr, err := q.k.authKeeper.AddressCodec().StringToBytes(req.Grantee)
if err != nil {
return nil, err
}

feeAllowance, err := q.GetAllowance(ctx, granterAddr, granteeAddr)
feeAllowance, err := q.k.GetAllowance(ctx, granterAddr, granteeAddr)
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
Expand All @@ -58,19 +64,19 @@ func (q Keeper) Allowance(ctx context.Context, req *feegrant.QueryAllowanceReque
}

// Allowances queries all the allowances granted to the given grantee.
func (q Keeper) Allowances(c context.Context, req *feegrant.QueryAllowancesRequest) (*feegrant.QueryAllowancesResponse, error) {
func (q queryServer) Allowances(c context.Context, req *feegrant.QueryAllowancesRequest) (*feegrant.QueryAllowancesResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

granteeAddr, err := q.authKeeper.AddressCodec().StringToBytes(req.Grantee)
granteeAddr, err := q.k.authKeeper.AddressCodec().StringToBytes(req.Grantee)
if err != nil {
return nil, err
}

var grants []*feegrant.Grant

_, pageRes, err := query.CollectionFilteredPaginate(c, q.FeeAllowance, req.Pagination,
_, pageRes, err := query.CollectionFilteredPaginate(c, q.k.FeeAllowance, req.Pagination,
func(key collections.Pair[sdk.AccAddress, sdk.AccAddress], grant feegrant.Grant) (include bool, err error) {
grants = append(grants, &grant)
return true, nil
Expand All @@ -86,18 +92,18 @@ func (q Keeper) Allowances(c context.Context, req *feegrant.QueryAllowancesReque
}

// AllowancesByGranter queries all the allowances granted by the given granter
func (q Keeper) AllowancesByGranter(c context.Context, req *feegrant.QueryAllowancesByGranterRequest) (*feegrant.QueryAllowancesByGranterResponse, error) {
func (q queryServer) AllowancesByGranter(c context.Context, req *feegrant.QueryAllowancesByGranterRequest) (*feegrant.QueryAllowancesByGranterResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

granterAddr, err := q.authKeeper.AddressCodec().StringToBytes(req.Granter)
granterAddr, err := q.k.authKeeper.AddressCodec().StringToBytes(req.Granter)
if err != nil {
return nil, err
}

var grants []*feegrant.Grant
_, pageRes, err := query.CollectionFilteredPaginate(c, q.FeeAllowance, req.Pagination,
_, pageRes, err := query.CollectionFilteredPaginate(c, q.k.FeeAllowance, req.Pagination,
func(key collections.Pair[sdk.AccAddress, sdk.AccAddress], grant feegrant.Grant) (include bool, err error) {
if !sdk.AccAddress(granterAddr).Equals(key.K2()) {
return false, nil
Expand All @@ -116,3 +122,11 @@ func (q Keeper) AllowancesByGranter(c context.Context, req *feegrant.QueryAllowa

return &feegrant.QueryAllowancesByGranterResponse{Allowances: grants, Pagination: pageRes}, nil
}

func (q queryServer) Params(c context.Context, req *feegrant.QueryParamsRequest) (*feegrant.QueryParamsResponse, error) {
p, err := q.k.Params.Get(c)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &feegrant.QueryParamsResponse{Params: &p}, nil
}
4 changes: 4 additions & 0 deletions x/feegrant/module/autocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ You can find the fee-grant of a granter and grantee.`),
Long: "Prune a limited amount of expired allowances in order to reduce the size of the store when the number of expired allowances is large.",
Example: fmt.Sprintf(`$ %s tx feegrant prune --from [mykey]`, version.AppName),
},
{
RpcMethod: "UpdateParams",
Skip: true, // skipped because authority gated
},
},
EnhanceCustomCommand: true,
},
Expand Down
2 changes: 1 addition & 1 deletion x/feegrant/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (ab AppModuleBasic) Name() string {
// module-specific gRPC queries.
func (am AppModule) RegisterServices(cfg module.Configurator) {
feegrant.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
feegrant.RegisterQueryServer(cfg.QueryServer(), am.keeper)
feegrant.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServer(am.keeper))
m := keeper.NewMigrator(am.keeper)
err := cfg.RegisterMigration(feegrant.ModuleName, 1, m.Migrate1to2)
if err != nil {
Expand Down
Loading

0 comments on commit ac0f712

Please sign in to comment.