From c80d64e3505e2b3ef097689d32f24c65b7dc9c6a Mon Sep 17 00:00:00 2001 From: Jeancarlo Barrios Date: Wed, 7 Jun 2023 16:15:33 -0500 Subject: [PATCH] feat(x/feegrant): feegrant autcli module query (#16213) --- x/feegrant/client/cli/query.go | 175 ---------------------------- x/feegrant/client/cli/query_test.go | 156 ------------------------- x/feegrant/module/autocli.go | 56 +++++++++ x/feegrant/module/module.go | 2 +- 4 files changed, 57 insertions(+), 332 deletions(-) delete mode 100644 x/feegrant/client/cli/query.go delete mode 100644 x/feegrant/client/cli/query_test.go create mode 100644 x/feegrant/module/autocli.go diff --git a/x/feegrant/client/cli/query.go b/x/feegrant/client/cli/query.go deleted file mode 100644 index d2808f5f5dff..000000000000 --- a/x/feegrant/client/cli/query.go +++ /dev/null @@ -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 -} diff --git a/x/feegrant/client/cli/query_test.go b/x/feegrant/client/cli/query_test.go deleted file mode 100644 index b066af7309e5..000000000000 --- a/x/feegrant/client/cli/query_test.go +++ /dev/null @@ -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()) - } - }) - } -} diff --git a/x/feegrant/module/autocli.go b/x/feegrant/module/autocli.go new file mode 100644 index 000000000000..454ac09b1e69 --- /dev/null +++ b/x/feegrant/module/autocli.go @@ -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, + }, + } +} diff --git a/x/feegrant/module/module.go b/x/feegrant/module/module.go index 50a14d2f7692..6363912c747a 100644 --- a/x/feegrant/module/module.go +++ b/x/feegrant/module/module.go @@ -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 } // ----------------------------------------------------------------------------