feat(x/feegrant): feegrant autcli module query (#16213)

This commit is contained in:
Jeancarlo Barrios 2023-06-07 16:15:33 -05:00 committed by GitHub
parent d0e1a9be4e
commit c80d64e350
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 332 deletions

View File

@ -1,175 +0,0 @@
package cli
import (
"fmt"
"strings"
"cosmossdk.io/core/address"
"github.com/spf13/cobra"
"cosmossdk.io/x/feegrant"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/version"
)
// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd(ac address.Codec) *cobra.Command {
feegrantQueryCmd := &cobra.Command{
Use: feegrant.ModuleName,
Short: "Querying commands for the feegrant module",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
feegrantQueryCmd.AddCommand(
GetCmdQueryFeeGrant(ac),
GetCmdQueryFeeGrantsByGrantee(ac),
GetCmdQueryFeeGrantsByGranter(ac),
)
return feegrantQueryCmd
}
// GetCmdQueryFeeGrant returns cmd to query for a grant between granter and grantee.
func GetCmdQueryFeeGrant(ac address.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "grant [granter] [grantee]",
Args: cobra.ExactArgs(2),
Short: "Query details of a single grant",
Long: strings.TrimSpace(
fmt.Sprintf(`Query details for a grant.
You can find the fee-grant of a granter and grantee.
Example:
$ %s query feegrant grant [granter] [grantee]
`, version.AppName),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
queryClient := feegrant.NewQueryClient(clientCtx)
if _, err := ac.StringToBytes(args[0]); err != nil {
return err
}
if _, err := ac.StringToBytes(args[1]); err != nil {
return err
}
res, err := queryClient.Allowance(
cmd.Context(),
&feegrant.QueryAllowanceRequest{
Granter: args[0],
Grantee: args[1],
},
)
if err != nil {
return err
}
return clientCtx.PrintProto(res.Allowance)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdQueryFeeGrantsByGrantee returns cmd to query for all grants for a grantee.
func GetCmdQueryFeeGrantsByGrantee(ac address.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "grants-by-grantee [grantee]",
Args: cobra.ExactArgs(1),
Short: "Query all grants of a grantee",
Long: strings.TrimSpace(
fmt.Sprintf(`Queries all the grants for a grantee address.
Example:
$ %s query feegrant grants-by-grantee [grantee]
`, version.AppName),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
queryClient := feegrant.NewQueryClient(clientCtx)
_, err := ac.StringToBytes(args[0])
if err != nil {
return err
}
pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}
res, err := queryClient.Allowances(
cmd.Context(),
&feegrant.QueryAllowancesRequest{
Grantee: args[0],
Pagination: pageReq,
},
)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "grants")
return cmd
}
// GetCmdQueryFeeGrantsByGranter returns cmd to query for all grants by a granter.
func GetCmdQueryFeeGrantsByGranter(ac address.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "grants-by-granter [granter]",
Args: cobra.ExactArgs(1),
Short: "Query all grants by a granter",
Long: strings.TrimSpace(
fmt.Sprintf(`Queries all the grants issued for a granter address.
Example:
$ %s query feegrant grants-by-granter [granter]
`, version.AppName),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
queryClient := feegrant.NewQueryClient(clientCtx)
_, err := ac.StringToBytes(args[0])
if err != nil {
return err
}
pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}
res, err := queryClient.AllowancesByGranter(
cmd.Context(),
&feegrant.QueryAllowancesByGranterRequest{
Granter: args[0],
Pagination: pageReq,
},
)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "grants")
return cmd
}

View File

@ -1,156 +0,0 @@
package cli_test
import (
"fmt"
"cosmossdk.io/x/feegrant"
"cosmossdk.io/x/feegrant/client/cli"
"github.com/cosmos/cosmos-sdk/client/flags"
codecaddress "github.com/cosmos/cosmos-sdk/codec/address"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
)
func (s *CLITestSuite) TestCmdGetFeeGrant() {
granter := s.addedGranter
grantee := s.addedGrantee
testCases := []struct {
name string
args []string
expectErrMsg string
expectErr bool
respType *feegrant.QueryAllowanceResponse
resp *feegrant.Grant
}{
{
"wrong granter",
[]string{
"wrong_granter",
grantee.String(),
fmt.Sprintf("--%s=json", flags.FlagOutput),
},
"decoding bech32 failed",
true, nil, nil,
},
{
"wrong grantee",
[]string{
granter.String(),
"wrong_grantee",
fmt.Sprintf("--%s=json", flags.FlagOutput),
},
"decoding bech32 failed",
true, nil, nil,
},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
cmd := cli.GetCmdQueryFeeGrant(codecaddress.NewBech32Codec("cosmos"))
out, err := clitestutil.ExecTestCLICmd(s.clientCtx, cmd, tc.args)
if tc.expectErr {
s.Require().Error(err)
s.Require().Contains(err.Error(), tc.expectErrMsg)
} else {
s.Require().NoError(err)
s.Require().NoError(s.clientCtx.Codec.UnmarshalJSON(out.Bytes(), tc.respType), out.String())
}
})
}
}
func (s *CLITestSuite) TestCmdGetFeeGrantsByGrantee() {
grantee := s.addedGrantee
clientCtx := s.clientCtx
testCases := []struct {
name string
args []string
expectErr bool
resp *feegrant.QueryAllowancesResponse
expectLength int
}{
{
"wrong grantee",
[]string{
"wrong_grantee",
fmt.Sprintf("--%s=json", flags.FlagOutput),
},
true, nil, 0,
},
{
"valid req",
[]string{
grantee.String(),
fmt.Sprintf("--%s=json", flags.FlagOutput),
},
false, &feegrant.QueryAllowancesResponse{}, 1,
},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
cmd := cli.GetCmdQueryFeeGrantsByGrantee(codecaddress.NewBech32Codec("cosmos"))
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
if tc.expectErr {
s.Require().Error(err)
} else {
s.Require().NoError(err)
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), tc.resp), out.String())
}
})
}
}
func (s *CLITestSuite) TestCmdGetFeeGrantsByGranter() {
granter := s.addedGranter
clientCtx := s.clientCtx
testCases := []struct {
name string
args []string
expectErr bool
resp *feegrant.QueryAllowancesByGranterResponse
expectLength int
}{
{
"wrong grantee",
[]string{
"wrong_grantee",
fmt.Sprintf("--%s=json", flags.FlagOutput),
},
true, nil, 0,
},
{
"valid req",
[]string{
granter.String(),
fmt.Sprintf("--%s=json", flags.FlagOutput),
},
false, &feegrant.QueryAllowancesByGranterResponse{}, 1,
},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
cmd := cli.GetCmdQueryFeeGrantsByGranter(codecaddress.NewBech32Codec("cosmos"))
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
if tc.expectErr {
s.Require().Error(err)
} else {
s.Require().NoError(err)
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), tc.resp), out.String())
}
})
}
}

View File

@ -0,0 +1,56 @@
package module
import (
"fmt"
"strings"
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
feegrantv1beta1 "cosmossdk.io/api/cosmos/feegrant/v1beta1"
"github.com/cosmos/cosmos-sdk/version"
)
func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
return &autocliv1.ModuleOptions{
Query: &autocliv1.ServiceCommandDescriptor{
Service: feegrantv1beta1.Query_ServiceDesc.ServiceName,
RpcCommandOptions: []*autocliv1.RpcCommandOptions{
{
RpcMethod: "Allowance",
Use: "grant [granter] [grantee]",
Short: "Query details of a single grant",
Long: strings.TrimSpace(
`Query details for a grant.
You can find the fee-grant of a granter and grantee.`),
Example: fmt.Sprintf(`$ %s query feegrant grant [granter] [grantee]`, version.AppName),
PositionalArgs: []*autocliv1.PositionalArgDescriptor{
{ProtoField: "granter"},
{ProtoField: "grantee"},
},
},
{
RpcMethod: "Allowances",
Use: "grants-by-grantee [grantee]",
Short: "Query all grants of a grantee",
Long: "Queries all the grants for a grantee address.",
Example: fmt.Sprintf(`$ %s query feegrant grants-by-grantee [grantee]`, version.AppName),
PositionalArgs: []*autocliv1.PositionalArgDescriptor{
{ProtoField: "grantee"},
},
},
{
RpcMethod: "AllowancesByGranter",
Use: "grants-by-granter [granter]",
Short: "Query all grants by a granter",
Example: fmt.Sprintf(`$ %s query feegrant grants-by-granter [granter]`, version.AppName),
PositionalArgs: []*autocliv1.PositionalArgDescriptor{
{ProtoField: "granter"},
},
},
},
},
Tx: &autocliv1.ServiceCommandDescriptor{
Service: feegrantv1beta1.Msg_ServiceDesc.ServiceName,
},
}
}

View File

@ -103,7 +103,7 @@ func (ab AppModuleBasic) GetTxCmd() *cobra.Command {
// GetQueryCmd returns no root query command for the feegrant module.
func (ab AppModuleBasic) GetQueryCmd() *cobra.Command {
return cli.GetQueryCmd(ab.ac)
return nil
}
// ----------------------------------------------------------------------------