diff --git a/x/staking/autocli.go b/x/staking/autocli.go new file mode 100644 index 000000000000..fc7ba4070397 --- /dev/null +++ b/x/staking/autocli.go @@ -0,0 +1,147 @@ +package staking + +import ( + "fmt" + + autocliv1 "cosmossdk.io/api/cosmos/autocli/v1" + _ "cosmossdk.io/api/cosmos/crypto/ed25519" // register to that it shows up in protoregistry.GlobalTypes + stakingv1beta "cosmossdk.io/api/cosmos/staking/v1beta1" + + "github.com/cosmos/cosmos-sdk/version" +) + +func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions { + return &autocliv1.ModuleOptions{ + Query: &autocliv1.ServiceCommandDescriptor{ + Service: stakingv1beta.Query_ServiceDesc.ServiceName, + RpcCommandOptions: []*autocliv1.RpcCommandOptions{ + { + RpcMethod: "Validators", + Short: "Query for all validators", + Long: "Query details about all validators on a network.", + }, + { + RpcMethod: "Validator", + Use: "validator [validator-addr]", + Short: "Query a validator", + Long: "Query details about an individual validator.", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{ + {ProtoField: "validator_addr"}, + }, + }, + { + RpcMethod: "ValidatorDelegations", + Use: "delegations-to [validator-addr]", + Short: "Query all delegations made to one validator", + Long: "Query delegations on an individual validator.", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{ + { + ProtoField: "validator_addr", + }, + }, + }, + { + RpcMethod: "ValidatorUnbondingDelegations", + Use: "unbonding-delegations-from [validator-addr]", + Short: "Query all unbonding delegatations from a validator", + Long: "Query delegations that are unbonding _from_ a validator.", + Example: fmt.Sprintf("$ %s query staking unbonding-delegations-from [val-addr]", version.AppName), + PositionalArgs: []*autocliv1.PositionalArgDescriptor{ + {ProtoField: "validator_addr"}, + }, + }, + { + RpcMethod: "Delegation", + Use: "delegation [delegator-addr] [validator-addr]", + Short: "Query a delegation based on address and validator address", + Long: "Query delegations for an individual delegator on an individual validator", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{ + {ProtoField: "delegator_addr"}, + {ProtoField: "validator_addr"}, + }, + }, + { + RpcMethod: "UnbondingDelegation", + Use: "unbonding-delegation [delegator-addr] [validator-addr]", + Short: "Query an unbonding-delegation record based on delegator and validator address", + Long: "Query unbonding delegations for an individual delegator on an individual validator.", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{ + {ProtoField: "delegator_addr"}, + {ProtoField: "validator_addr"}, + }, + }, + { + RpcMethod: "DelegatorDelegations", + Use: "delegations [delegator-addr]", + Short: "Query all delegations made by one delegator", + Long: "Query delegations for an individual delegator on all validators.", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{ + {ProtoField: "delegator_addr"}, + }, + }, + { + RpcMethod: "DelegatorValidators", + Use: "delegator-validators [delegator-addr]", + Short: "Query all validators info for given delegator address", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{ + {ProtoField: "delegator_addr"}, + }, + }, + { + RpcMethod: "DelegatorValidator", + Use: "delegator-validator [delegator-addr] [validator-addr]", + Short: "Query validator info for given delegator validator pair", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{ + {ProtoField: "delegator_addr"}, + {ProtoField: "validator_addr"}, + }, + }, + { + RpcMethod: "DelegatorUnbondingDelegations", + Use: "unbonding-delegations [delegator-addr]", + Short: "Query all unbonding-delegations records for one delegator", + Long: "Query unbonding delegations for an individual delegator.", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{ + {ProtoField: "delegator_addr"}, + }, + }, + { + RpcMethod: "Redelegations", + Use: "redelegation [delegator-addr] [src-validator-addr] [dst-validator-addr]", + Short: "Query a redelegation record based on delegator and a source and destination validator address", + Long: "Query a redelegation record for an individual delegator between a source and destination validator.", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{ + {ProtoField: "delegator_addr"}, + {ProtoField: "src_validator_addr"}, + {ProtoField: "dst_validator_addr"}, + }, + }, + { + RpcMethod: "HistoricalInfo", + Use: "historical-info [height]", + Short: "Query historical info at given height", + Long: "Query historical info at given height.", + Example: fmt.Sprintf("$ %s query staking historical-info 5", version.AppName), + PositionalArgs: []*autocliv1.PositionalArgDescriptor{ + {ProtoField: "height"}, + }, + }, + { + RpcMethod: "Pool", + Use: "pool", + Short: "Query the current staking pool values", + Long: "Query values for amounts stored in the staking pool.", + }, + { + RpcMethod: "Params", + Use: "params", + Short: "Query the current staking parameters information", + Long: "Query values set as staking parameters.", + }, + }, + }, + Tx: &autocliv1.ServiceCommandDescriptor{ + Service: stakingv1beta.Msg_ServiceDesc.ServiceName, + }, + } +} diff --git a/x/staking/client/cli/query.go b/x/staking/client/cli/query.go deleted file mode 100644 index 4007c5b5ae41..000000000000 --- a/x/staking/client/cli/query.go +++ /dev/null @@ -1,739 +0,0 @@ -package cli - -import ( - "fmt" - "strconv" - "strings" - - "github.com/spf13/cobra" - - "cosmossdk.io/core/address" - - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/client/flags" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/version" - "github.com/cosmos/cosmos-sdk/x/staking/types" -) - -// GetQueryCmd returns the cli query commands for this module -func GetQueryCmd(ac address.Codec) *cobra.Command { - stakingQueryCmd := &cobra.Command{ - Use: types.ModuleName, - Short: "Querying commands for the staking module", - DisableFlagParsing: true, - SuggestionsMinimumDistance: 2, - RunE: client.ValidateCmd, - } - - stakingQueryCmd.AddCommand( - GetCmdQueryDelegation(ac), - GetCmdQueryDelegations(ac), - GetCmdQueryUnbondingDelegation(ac), - GetCmdQueryUnbondingDelegations(ac), - GetCmdQueryRedelegation(ac), - GetCmdQueryRedelegations(ac), - GetCmdQueryValidator(), - GetCmdQueryValidators(), - GetCmdQueryValidatorDelegations(), - GetCmdQueryValidatorUnbondingDelegations(), - GetCmdQueryValidatorRedelegations(), - GetCmdQueryHistoricalInfo(), - GetCmdQueryParams(), - GetCmdQueryPool(), - ) - - return stakingQueryCmd -} - -// GetCmdQueryValidator implements the validator query command. -func GetCmdQueryValidator() *cobra.Command { - bech32PrefixValAddr := sdk.GetConfig().GetBech32ValidatorAddrPrefix() - - cmd := &cobra.Command{ - Use: "validator [validator-addr]", - Short: "Query a validator", - Long: strings.TrimSpace( - fmt.Sprintf(`Query details about an individual validator. - -Example: -$ %s query staking validator %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj -`, - version.AppName, bech32PrefixValAddr, - ), - ), - Args: cobra.ExactArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { - clientCtx, err := client.GetClientQueryContext(cmd) - if err != nil { - return err - } - queryClient := types.NewQueryClient(clientCtx) - - addr, err := sdk.ValAddressFromBech32(args[0]) - if err != nil { - return err - } - - params := &types.QueryValidatorRequest{ValidatorAddr: addr.String()} - res, err := queryClient.Validator(cmd.Context(), params) - if err != nil { - return err - } - - return clientCtx.PrintProto(&res.Validator) - }, - } - - flags.AddQueryFlagsToCmd(cmd) - - return cmd -} - -// GetCmdQueryValidators implements the query all validators command. -func GetCmdQueryValidators() *cobra.Command { - cmd := &cobra.Command{ - Use: "validators", - Short: "Query for all validators", - Args: cobra.NoArgs, - Long: strings.TrimSpace( - fmt.Sprintf(`Query details about all validators on a network. - -Example: -$ %s query staking validators -`, - version.AppName, - ), - ), - RunE: func(cmd *cobra.Command, args []string) error { - clientCtx, err := client.GetClientQueryContext(cmd) - if err != nil { - return err - } - queryClient := types.NewQueryClient(clientCtx) - pageReq, err := client.ReadPageRequest(cmd.Flags()) - if err != nil { - return err - } - - result, err := queryClient.Validators(cmd.Context(), &types.QueryValidatorsRequest{ - // Leaving status empty on purpose to query all validators. - Pagination: pageReq, - }) - if err != nil { - return err - } - - return clientCtx.PrintProto(result) - }, - } - - flags.AddQueryFlagsToCmd(cmd) - flags.AddPaginationFlagsToCmd(cmd, "validators") - - return cmd -} - -// GetCmdQueryValidatorUnbondingDelegations implements the query all unbonding delegations from a validator command. -func GetCmdQueryValidatorUnbondingDelegations() *cobra.Command { - bech32PrefixValAddr := sdk.GetConfig().GetBech32ValidatorAddrPrefix() - - cmd := &cobra.Command{ - Use: "unbonding-delegations-from [validator-addr]", - Short: "Query all unbonding delegations from a validator", - Long: strings.TrimSpace( - fmt.Sprintf(`Query delegations that are unbonding _from_ a validator. - -Example: -$ %s query staking unbonding-delegations-from %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj -`, - version.AppName, bech32PrefixValAddr, - ), - ), - Args: cobra.ExactArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { - clientCtx, err := client.GetClientQueryContext(cmd) - if err != nil { - return err - } - queryClient := types.NewQueryClient(clientCtx) - - valAddr, err := sdk.ValAddressFromBech32(args[0]) - if err != nil { - return err - } - - pageReq, err := client.ReadPageRequest(cmd.Flags()) - if err != nil { - return err - } - - params := &types.QueryValidatorUnbondingDelegationsRequest{ - ValidatorAddr: valAddr.String(), - Pagination: pageReq, - } - - res, err := queryClient.ValidatorUnbondingDelegations(cmd.Context(), params) - if err != nil { - return err - } - - return clientCtx.PrintProto(res) - }, - } - - flags.AddQueryFlagsToCmd(cmd) - flags.AddPaginationFlagsToCmd(cmd, "unbonding delegations") - - return cmd -} - -// GetCmdQueryValidatorRedelegations implements the query all redelegations -// from a validator command. -func GetCmdQueryValidatorRedelegations() *cobra.Command { - bech32PrefixValAddr := sdk.GetConfig().GetBech32ValidatorAddrPrefix() - - cmd := &cobra.Command{ - Use: "redelegations-from [validator-addr]", - Short: "Query all outgoing redelegations from a validator", - Long: strings.TrimSpace( - fmt.Sprintf(`Query delegations that are redelegating _from_ a validator. - -Example: -$ %s query staking redelegations-from %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj -`, - version.AppName, bech32PrefixValAddr, - ), - ), - Args: cobra.ExactArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { - clientCtx, err := client.GetClientQueryContext(cmd) - if err != nil { - return err - } - queryClient := types.NewQueryClient(clientCtx) - - valSrcAddr, err := sdk.ValAddressFromBech32(args[0]) - if err != nil { - return err - } - - pageReq, err := client.ReadPageRequest(cmd.Flags()) - if err != nil { - return err - } - - params := &types.QueryRedelegationsRequest{ - SrcValidatorAddr: valSrcAddr.String(), - Pagination: pageReq, - } - - res, err := queryClient.Redelegations(cmd.Context(), params) - if err != nil { - return err - } - - return clientCtx.PrintProto(res) - }, - } - - flags.AddQueryFlagsToCmd(cmd) - flags.AddPaginationFlagsToCmd(cmd, "validator redelegations") - - return cmd -} - -// GetCmdQueryDelegation the query delegation command. -func GetCmdQueryDelegation(ac address.Codec) *cobra.Command { - cmd := &cobra.Command{ - Use: "delegation [delegator-addr] [validator-addr]", - Short: "Query a delegation based on address and validator address", - Example: fmt.Sprintf(`%s query staking delegation [delegator-address] [validator-address]`, - version.AppName), - Long: "Query delegations for an individual delegator on an individual validator", - Args: cobra.ExactArgs(2), - RunE: func(cmd *cobra.Command, args []string) error { - clientCtx, err := client.GetClientQueryContext(cmd) - if err != nil { - return err - } - queryClient := types.NewQueryClient(clientCtx) - - _, err = ac.StringToBytes(args[0]) - if err != nil { - return err - } - - valAddr, err := sdk.ValAddressFromBech32(args[1]) - if err != nil { - return err - } - - params := &types.QueryDelegationRequest{ - DelegatorAddr: args[0], - ValidatorAddr: valAddr.String(), - } - - res, err := queryClient.Delegation(cmd.Context(), params) - if err != nil { - return err - } - - return clientCtx.PrintProto(res.DelegationResponse) - }, - } - - flags.AddQueryFlagsToCmd(cmd) - - return cmd -} - -// GetCmdQueryDelegations implements the command to query all the delegations -// made from one delegator. -func GetCmdQueryDelegations(ac address.Codec) *cobra.Command { - bech32PrefixAccAddr := sdk.GetConfig().GetBech32AccountAddrPrefix() - - cmd := &cobra.Command{ - Use: "delegations [delegator-addr]", - Short: "Query all delegations made by one delegator", - Long: strings.TrimSpace( - fmt.Sprintf(`Query delegations for an individual delegator on all validators. - -Example: -$ %s query staking delegations %s1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p -`, - version.AppName, bech32PrefixAccAddr, - ), - ), - Args: cobra.ExactArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { - clientCtx, err := client.GetClientQueryContext(cmd) - if err != nil { - return err - } - queryClient := types.NewQueryClient(clientCtx) - - _, err = ac.StringToBytes(args[0]) - if err != nil { - return err - } - - pageReq, err := client.ReadPageRequest(cmd.Flags()) - if err != nil { - return err - } - - params := &types.QueryDelegatorDelegationsRequest{ - DelegatorAddr: args[0], - Pagination: pageReq, - } - - res, err := queryClient.DelegatorDelegations(cmd.Context(), params) - if err != nil { - return err - } - - return clientCtx.PrintProto(res) - }, - } - - flags.AddQueryFlagsToCmd(cmd) - flags.AddPaginationFlagsToCmd(cmd, "delegations") - - return cmd -} - -// GetCmdQueryValidatorDelegations implements the command to query all the -// delegations to a specific validator. -func GetCmdQueryValidatorDelegations() *cobra.Command { - bech32PrefixValAddr := sdk.GetConfig().GetBech32ValidatorAddrPrefix() - - cmd := &cobra.Command{ - Use: "delegations-to [validator-addr]", - Short: "Query all delegations made to one validator", - Long: strings.TrimSpace( - fmt.Sprintf(`Query delegations on an individual validator. - -Example: -$ %s query staking delegations-to %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj -`, - version.AppName, bech32PrefixValAddr, - ), - ), - Args: cobra.ExactArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { - clientCtx, err := client.GetClientQueryContext(cmd) - if err != nil { - return err - } - queryClient := types.NewQueryClient(clientCtx) - - valAddr, err := sdk.ValAddressFromBech32(args[0]) - if err != nil { - return err - } - - pageReq, err := client.ReadPageRequest(cmd.Flags()) - if err != nil { - return err - } - - params := &types.QueryValidatorDelegationsRequest{ - ValidatorAddr: valAddr.String(), - Pagination: pageReq, - } - - res, err := queryClient.ValidatorDelegations(cmd.Context(), params) - if err != nil { - return err - } - - return clientCtx.PrintProto(res) - }, - } - - flags.AddQueryFlagsToCmd(cmd) - flags.AddPaginationFlagsToCmd(cmd, "validator delegations") - - return cmd -} - -// GetCmdQueryUnbondingDelegation implements the command to query a single -// unbonding-delegation record. -func GetCmdQueryUnbondingDelegation(ac address.Codec) *cobra.Command { - bech32PrefixAccAddr := sdk.GetConfig().GetBech32AccountAddrPrefix() - bech32PrefixValAddr := sdk.GetConfig().GetBech32ValidatorAddrPrefix() - - cmd := &cobra.Command{ - Use: "unbonding-delegation [delegator-addr] [validator-addr]", - Short: "Query an unbonding-delegation record based on delegator and validator address", - Long: strings.TrimSpace( - fmt.Sprintf(`Query unbonding delegations for an individual delegator on an individual validator. - -Example: -$ %s query staking unbonding-delegation %s1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj -`, - version.AppName, bech32PrefixAccAddr, bech32PrefixValAddr, - ), - ), - Args: cobra.ExactArgs(2), - RunE: func(cmd *cobra.Command, args []string) error { - clientCtx, err := client.GetClientQueryContext(cmd) - if err != nil { - return err - } - queryClient := types.NewQueryClient(clientCtx) - - valAddr, err := sdk.ValAddressFromBech32(args[1]) - if err != nil { - return err - } - - _, err = ac.StringToBytes(args[0]) - if err != nil { - return err - } - - params := &types.QueryUnbondingDelegationRequest{ - DelegatorAddr: args[0], - ValidatorAddr: valAddr.String(), - } - - res, err := queryClient.UnbondingDelegation(cmd.Context(), params) - if err != nil { - return err - } - - return clientCtx.PrintProto(&res.Unbond) - }, - } - - flags.AddQueryFlagsToCmd(cmd) - - return cmd -} - -// GetCmdQueryUnbondingDelegations implements the command to query all the -// unbonding-delegation records for a delegator. -func GetCmdQueryUnbondingDelegations(ac address.Codec) *cobra.Command { - bech32PrefixAccAddr := sdk.GetConfig().GetBech32AccountAddrPrefix() - - cmd := &cobra.Command{ - Use: "unbonding-delegations [delegator-addr]", - Short: "Query all unbonding-delegations records for one delegator", - Long: strings.TrimSpace( - fmt.Sprintf(`Query unbonding delegations for an individual delegator. - -Example: -$ %s query staking unbonding-delegations %s1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p -`, - version.AppName, bech32PrefixAccAddr, - ), - ), - Args: cobra.ExactArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { - clientCtx, err := client.GetClientQueryContext(cmd) - if err != nil { - return err - } - queryClient := types.NewQueryClient(clientCtx) - - _, err = ac.StringToBytes(args[0]) - if err != nil { - return err - } - - pageReq, err := client.ReadPageRequest(cmd.Flags()) - if err != nil { - return err - } - - params := &types.QueryDelegatorUnbondingDelegationsRequest{ - DelegatorAddr: args[0], - Pagination: pageReq, - } - - res, err := queryClient.DelegatorUnbondingDelegations(cmd.Context(), params) - if err != nil { - return err - } - - return clientCtx.PrintProto(res) - }, - } - - flags.AddQueryFlagsToCmd(cmd) - flags.AddPaginationFlagsToCmd(cmd, "unbonding delegations") - - return cmd -} - -// GetCmdQueryRedelegation implements the command to query a single -// redelegation record. -func GetCmdQueryRedelegation(ac address.Codec) *cobra.Command { - bech32PrefixAccAddr := sdk.GetConfig().GetBech32AccountAddrPrefix() - bech32PrefixValAddr := sdk.GetConfig().GetBech32ValidatorAddrPrefix() - - cmd := &cobra.Command{ - Use: "redelegation [delegator-addr] [src-validator-addr] [dst-validator-addr]", - Short: "Query a redelegation record based on delegator and a source and destination validator address", - Long: strings.TrimSpace( - fmt.Sprintf(`Query a redelegation record for an individual delegator between a source and destination validator. - -Example: -$ %s query staking redelegation %s1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p %s1l2rsakp388kuv9k8qzq6lrm9taddae7fpx59wm %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj -`, - version.AppName, bech32PrefixAccAddr, bech32PrefixValAddr, bech32PrefixValAddr, - ), - ), - Args: cobra.ExactArgs(3), - RunE: func(cmd *cobra.Command, args []string) error { - clientCtx, err := client.GetClientQueryContext(cmd) - if err != nil { - return err - } - queryClient := types.NewQueryClient(clientCtx) - - _, err = ac.StringToBytes(args[0]) - if err != nil { - return err - } - - valSrcAddr, err := sdk.ValAddressFromBech32(args[1]) - if err != nil { - return err - } - - valDstAddr, err := sdk.ValAddressFromBech32(args[2]) - if err != nil { - return err - } - - params := &types.QueryRedelegationsRequest{ - DelegatorAddr: args[0], - DstValidatorAddr: valDstAddr.String(), - SrcValidatorAddr: valSrcAddr.String(), - } - - res, err := queryClient.Redelegations(cmd.Context(), params) - if err != nil { - return err - } - - return clientCtx.PrintProto(res) - }, - } - - flags.AddQueryFlagsToCmd(cmd) - - return cmd -} - -// GetCmdQueryRedelegations implements the command to query all the -// redelegation records for a delegator. -func GetCmdQueryRedelegations(ac address.Codec) *cobra.Command { - bech32PrefixAccAddr := sdk.GetConfig().GetBech32AccountAddrPrefix() - - cmd := &cobra.Command{ - Use: "redelegations [delegator-addr]", - Args: cobra.ExactArgs(1), - Short: "Query all redelegations records for one delegator", - Long: strings.TrimSpace( - fmt.Sprintf(`Query all redelegation records for an individual delegator. - -Example: -$ %s query staking redelegation %s1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p -`, - version.AppName, bech32PrefixAccAddr, - ), - ), - RunE: func(cmd *cobra.Command, args []string) error { - clientCtx, err := client.GetClientQueryContext(cmd) - if err != nil { - return err - } - queryClient := types.NewQueryClient(clientCtx) - - _, err = ac.StringToBytes(args[0]) - if err != nil { - return err - } - - pageReq, err := client.ReadPageRequest(cmd.Flags()) - if err != nil { - return err - } - - params := &types.QueryRedelegationsRequest{ - DelegatorAddr: args[0], - Pagination: pageReq, - } - - res, err := queryClient.Redelegations(cmd.Context(), params) - if err != nil { - return err - } - - return clientCtx.PrintProto(res) - }, - } - - flags.AddQueryFlagsToCmd(cmd) - flags.AddPaginationFlagsToCmd(cmd, "delegator redelegations") - - return cmd -} - -// GetCmdQueryHistoricalInfo implements the historical info query command -func GetCmdQueryHistoricalInfo() *cobra.Command { - cmd := &cobra.Command{ - Use: "historical-info [height]", - Args: cobra.ExactArgs(1), - Short: "Query historical info at given height", - Long: strings.TrimSpace( - fmt.Sprintf(`Query historical info at given height. - -Example: -$ %s query staking historical-info 5 -`, - version.AppName, - ), - ), - RunE: func(cmd *cobra.Command, args []string) error { - clientCtx, err := client.GetClientQueryContext(cmd) - if err != nil { - return err - } - queryClient := types.NewQueryClient(clientCtx) - - height, err := strconv.ParseInt(args[0], 10, 64) - if err != nil || height < 0 { - return fmt.Errorf("height argument provided must be a non-negative-integer: %w", err) - } - - params := &types.QueryHistoricalInfoRequest{Height: height} - res, err := queryClient.HistoricalInfo(cmd.Context(), params) - if err != nil { - return err - } - - return clientCtx.PrintProto(res.Hist) - }, - } - - flags.AddQueryFlagsToCmd(cmd) - - return cmd -} - -// GetCmdQueryPool implements the pool query command. -func GetCmdQueryPool() *cobra.Command { - cmd := &cobra.Command{ - Use: "pool", - Args: cobra.NoArgs, - Short: "Query the current staking pool values", - Long: strings.TrimSpace( - fmt.Sprintf(`Query values for amounts stored in the staking pool. - -Example: -$ %s query staking pool -`, - version.AppName, - ), - ), - RunE: func(cmd *cobra.Command, args []string) error { - clientCtx, err := client.GetClientQueryContext(cmd) - if err != nil { - return err - } - queryClient := types.NewQueryClient(clientCtx) - - res, err := queryClient.Pool(cmd.Context(), &types.QueryPoolRequest{}) - if err != nil { - return err - } - - return clientCtx.PrintProto(&res.Pool) - }, - } - - flags.AddQueryFlagsToCmd(cmd) - - return cmd -} - -// GetCmdQueryParams implements the params query command. -func GetCmdQueryParams() *cobra.Command { - cmd := &cobra.Command{ - Use: "params", - Args: cobra.NoArgs, - Short: "Query the current staking parameters information", - Long: strings.TrimSpace( - fmt.Sprintf(`Query values set as staking parameters. - -Example: -$ %s query staking params -`, - version.AppName, - ), - ), - RunE: func(cmd *cobra.Command, args []string) error { - clientCtx, err := client.GetClientQueryContext(cmd) - if err != nil { - return err - } - queryClient := types.NewQueryClient(clientCtx) - - res, err := queryClient.Params(cmd.Context(), &types.QueryParamsRequest{}) - if err != nil { - return err - } - - return clientCtx.PrintProto(&res.Params) - }, - } - - flags.AddQueryFlagsToCmd(cmd) - - return cmd -} diff --git a/x/staking/client/cli/query_test.go b/x/staking/client/cli/query_test.go deleted file mode 100644 index febffe77da3a..000000000000 --- a/x/staking/client/cli/query_test.go +++ /dev/null @@ -1,563 +0,0 @@ -package cli_test - -import ( - "fmt" - "strings" - - "github.com/cosmos/gogoproto/proto" - - "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/codec/address" - clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/staking/client/cli" - "github.com/cosmos/cosmos-sdk/x/staking/types" -) - -func (s *CLITestSuite) TestGetCmdQueryValidator() { - testCases := []struct { - name string - args []string - expectErr bool - }{ - { - "with invalid address ", - []string{"somethinginvalidaddress", fmt.Sprintf("--%s=json", flags.FlagOutput)}, - true, - }, - { - "happy case", - []string{sdk.ValAddress(s.addrs[0]).String(), fmt.Sprintf("--%s=json", flags.FlagOutput)}, - false, - }, - } - for _, tc := range testCases { - tc := tc - s.Run(tc.name, func() { - cmd := cli.GetCmdQueryValidator() - clientCtx := s.clientCtx - out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args) - if tc.expectErr { - s.Require().Error(err) - s.Require().NotEqual("internal", err.Error()) - } else { - var result types.Validator - s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), &result)) - } - }) - } -} - -func (s *CLITestSuite) TestGetCmdQueryValidators() { - testCases := []struct { - name string - args []string - minValidatorCount int - }{ - { - "one validator case", - []string{ - fmt.Sprintf("--%s=json", flags.FlagOutput), - fmt.Sprintf("--%s=1", flags.FlagLimit), - }, - 1, - }, - { - "multi validator case", - []string{fmt.Sprintf("--%s=json", flags.FlagOutput)}, - 0, - }, - } - - for _, tc := range testCases { - tc := tc - - s.Run(tc.name, func() { - cmd := cli.GetCmdQueryValidators() - clientCtx := s.clientCtx - - out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args) - s.Require().NoError(err) - - var result types.QueryValidatorsResponse - s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), &result)) - }) - } -} - -func (s *CLITestSuite) TestGetCmdQueryDelegation() { - testCases := []struct { - name string - args []string - expErr bool - respType proto.Message - }{ - { - "with wrong delegator address", - []string{ - "wrongDelAddr", - s.addrs[1].String(), - fmt.Sprintf("--%s=json", flags.FlagOutput), - }, - true, nil, - }, - { - "with wrong validator address", - []string{ - s.addrs[0].String(), - "wrongValAddr", - fmt.Sprintf("--%s=json", flags.FlagOutput), - }, - true, nil, - }, - { - "with json output", - []string{ - s.addrs[0].String(), - sdk.ValAddress(s.addrs[1]).String(), - fmt.Sprintf("--%s=json", flags.FlagOutput), - }, - false, - &types.DelegationResponse{}, - }, - } - - for _, tc := range testCases { - tc := tc - s.Run(tc.name, func() { - cmd := cli.GetCmdQueryDelegation(address.NewBech32Codec("cosmos")) - clientCtx := s.clientCtx - - _, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args) - if tc.expErr { - s.Require().Error(err) - } else { - s.Require().Contains(err.Error(), "Marshal called with nil") - } - }) - } -} - -func (s *CLITestSuite) TestGetCmdQueryDelegations() { - testCases := []struct { - name string - args []string - expErr bool - respType proto.Message - }{ - { - "with no delegator address", - []string{}, - true, nil, - }, - { - "with wrong delegator address", - []string{"wrongDelAddr"}, - true, nil, - }, - { - "valid request (height specific)", - []string{ - s.addrs[0].String(), - fmt.Sprintf("--%s=json", flags.FlagOutput), - fmt.Sprintf("--%s=1", flags.FlagHeight), - }, - false, - &types.QueryDelegatorDelegationsResponse{}, - }, - } - - for _, tc := range testCases { - tc := tc - s.Run(tc.name, func() { - cmd := cli.GetCmdQueryDelegations(address.NewBech32Codec("cosmos")) - clientCtx := s.clientCtx - - out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args) - if tc.expErr { - s.Require().Error(err) - } else { - s.Require().NoError(err) - s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), tc.respType), out.String()) - } - }) - } -} - -func (s *CLITestSuite) TestGetCmdQueryValidatorDelegations() { - testCases := []struct { - name string - args []string - expErr bool - respType proto.Message - }{ - { - "with no validator address", - []string{}, - true, nil, - }, - { - "wrong validator address", - []string{"wrongValAddr"}, - true, nil, - }, - { - "valid request(height specific)", - []string{ - s.addrs[0].String(), - fmt.Sprintf("--%s=json", flags.FlagOutput), - fmt.Sprintf("--%s=1", flags.FlagHeight), - }, - false, - &types.QueryValidatorDelegationsResponse{}, - }, - } - - for _, tc := range testCases { - tc := tc - s.Run(tc.name, func() { - cmd := cli.GetCmdQueryDelegations(address.NewBech32Codec("cosmos")) - clientCtx := s.clientCtx - - out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args) - if tc.expErr { - s.Require().Error(err) - } else { - s.Require().NoError(err) - s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), tc.respType), out.String()) - } - }) - } -} - -func (s *CLITestSuite) TestGetCmdQueryUnbondingDelegations() { - testCases := []struct { - name string - args []string - expErr bool - }{ - { - "wrong delegator address", - []string{ - "wrongDelAddr", - fmt.Sprintf("--%s=json", flags.FlagOutput), - }, - true, - }, - { - "valid request", - []string{ - s.addrs[0].String(), - fmt.Sprintf("--%s=json", flags.FlagOutput), - }, - false, - }, - } - - for _, tc := range testCases { - tc := tc - s.Run(tc.name, func() { - cmd := cli.GetCmdQueryUnbondingDelegations(address.NewBech32Codec("cosmos")) - clientCtx := s.clientCtx - - out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args) - - if tc.expErr { - s.Require().Error(err) - } else { - var ubds types.QueryDelegatorUnbondingDelegationsResponse - err = s.clientCtx.Codec.UnmarshalJSON(out.Bytes(), &ubds) - - s.Require().NoError(err) - } - }) - } -} - -func (s *CLITestSuite) TestGetCmdQueryUnbondingDelegation() { - testCases := []struct { - name string - args []string - expErr bool - }{ - { - "wrong delegator address", - []string{ - "wrongDelAddr", - s.addrs[0].String(), - fmt.Sprintf("--%s=json", flags.FlagOutput), - }, - true, - }, - { - "wrong validator address", - []string{ - s.addrs[0].String(), - "wrongValAddr", - fmt.Sprintf("--%s=json", flags.FlagOutput), - }, - true, - }, - { - "valid request", - []string{ - s.addrs[0].String(), - sdk.ValAddress(s.addrs[1]).String(), - fmt.Sprintf("--%s=json", flags.FlagOutput), - }, - false, - }, - } - - for _, tc := range testCases { - tc := tc - s.Run(tc.name, func() { - cmd := cli.GetCmdQueryUnbondingDelegation(address.NewBech32Codec("cosmos")) - clientCtx := s.clientCtx - - out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args) - - if tc.expErr { - s.Require().Error(err) - } else { - var ubd types.UnbondingDelegation - - err = s.clientCtx.Codec.UnmarshalJSON(out.Bytes(), &ubd) - s.Require().NoError(err) - } - }) - } -} - -func (s *CLITestSuite) TestGetCmdQueryValidatorUnbondingDelegations() { - testCases := []struct { - name string - args []string - expErr bool - }{ - { - "wrong validator address", - []string{ - "wrongValAddr", - fmt.Sprintf("--%s=json", flags.FlagOutput), - }, - true, - }, - { - "valid request", - []string{ - sdk.ValAddress(s.addrs[0]).String(), - fmt.Sprintf("--%s=json", flags.FlagOutput), - }, - false, - }, - } - - for _, tc := range testCases { - tc := tc - s.Run(tc.name, func() { - cmd := cli.GetCmdQueryValidatorUnbondingDelegations() - clientCtx := s.clientCtx - - out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args) - - if tc.expErr { - s.Require().Error(err) - } else { - var ubds types.QueryValidatorUnbondingDelegationsResponse - err = s.clientCtx.Codec.UnmarshalJSON(out.Bytes(), &ubds) - s.Require().NoError(err) - } - }) - } -} - -func (s *CLITestSuite) TestGetCmdQueryRedelegations() { - testCases := []struct { - name string - args []string - expErr bool - }{ - { - "wrong delegator address", - []string{ - "wrongdeladdr", - fmt.Sprintf("--%s=json", flags.FlagOutput), - }, - true, - }, - { - "valid request", - []string{ - s.addrs[0].String(), - fmt.Sprintf("--%s=json", flags.FlagOutput), - }, - false, - }, - } - - for _, tc := range testCases { - tc := tc - s.Run(tc.name, func() { - cmd := cli.GetCmdQueryRedelegations(address.NewBech32Codec("cosmos")) - clientCtx := s.clientCtx - - out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args) - - if tc.expErr { - s.Require().Error(err) - } else { - var redelegations types.QueryRedelegationsResponse - err = s.clientCtx.Codec.UnmarshalJSON(out.Bytes(), &redelegations) - s.Require().NoError(err) - } - }) - } -} - -func (s *CLITestSuite) TestGetCmdQueryRedelegation() { - testCases := []struct { - name string - args []string - expErr bool - }{ - { - "wrong delegator address", - []string{ - "wrongdeladdr", - sdk.ValAddress(s.addrs[0]).String(), - sdk.ValAddress(s.addrs[1]).String(), - fmt.Sprintf("--%s=json", flags.FlagOutput), - }, - true, - }, - { - "wrong source validator address address", - []string{ - s.addrs[0].String(), - "wrongSrcValAddress", - sdk.ValAddress(s.addrs[1]).String(), - fmt.Sprintf("--%s=json", flags.FlagOutput), - }, - true, - }, - { - "wrong destination validator address address", - []string{ - s.addrs[0].String(), - sdk.ValAddress(s.addrs[0]).String(), - "wrongDestValAddress", - fmt.Sprintf("--%s=json", flags.FlagOutput), - }, - true, - }, - { - "valid request", - []string{ - s.addrs[0].String(), - sdk.ValAddress(s.addrs[0]).String(), - sdk.ValAddress(s.addrs[1]).String(), - fmt.Sprintf("--%s=json", flags.FlagOutput), - }, - false, - }, - } - - for _, tc := range testCases { - tc := tc - s.Run(tc.name, func() { - cmd := cli.GetCmdQueryRedelegation(address.NewBech32Codec("cosmos")) - clientCtx := s.clientCtx - - out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args) - - if tc.expErr { - s.Require().Error(err) - } else { - var redelegations types.QueryRedelegationsResponse - - err = s.clientCtx.Codec.UnmarshalJSON(out.Bytes(), &redelegations) - s.Require().NoError(err) - } - }) - } -} - -func (s *CLITestSuite) TestGetCmdQueryValidatorRedelegations() { - testCases := []struct { - name string - args []string - expErr bool - }{ - { - "wrong validator address", - []string{ - "wrongValAddr", - fmt.Sprintf("--%s=json", flags.FlagOutput), - }, - true, - }, - { - "valid request", - []string{ - sdk.ValAddress(s.addrs[0]).String(), - fmt.Sprintf("--%s=json", flags.FlagOutput), - }, - false, - }, - } - - for _, tc := range testCases { - tc := tc - s.Run(tc.name, func() { - cmd := cli.GetCmdQueryValidatorRedelegations() - clientCtx := s.clientCtx - - out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args) - - if tc.expErr { - s.Require().Error(err) - } else { - var redelegations types.QueryRedelegationsResponse - err = s.clientCtx.Codec.UnmarshalJSON(out.Bytes(), &redelegations) - s.Require().NoError(err) - } - }) - } -} - -func (s *CLITestSuite) TestGetCmdQueryPool() { - testCases := []struct { - name string - args []string - expectedOutput string - }{ - { - "with text", - []string{ - fmt.Sprintf("--%s=text", flags.FlagOutput), - fmt.Sprintf("--%s=1", flags.FlagHeight), - }, - `bonded_tokens: "0" -not_bonded_tokens: "0"`, - }, - { - "with json", - []string{ - fmt.Sprintf("--%s=json", flags.FlagOutput), - fmt.Sprintf("--%s=1", flags.FlagHeight), - }, - `{"not_bonded_tokens":"0","bonded_tokens":"0"}`, - }, - } - for _, tc := range testCases { - tc := tc - s.Run(tc.name, func() { - cmd := cli.GetCmdQueryPool() - clientCtx := s.clientCtx - out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args) - s.Require().NoError(err) - s.Require().Equal(tc.expectedOutput, strings.TrimSpace(out.String())) - }) - } -} diff --git a/x/staking/module.go b/x/staking/module.go index da51959896f5..affb0e957774 100644 --- a/x/staking/module.go +++ b/x/staking/module.go @@ -95,7 +95,7 @@ func (AppModuleBasic) GetTxCmd() *cobra.Command { // GetQueryCmd returns no root query command for the staking module. func (ab AppModuleBasic) GetQueryCmd() *cobra.Command { - return cli.GetQueryCmd(ab.ak.AddressCodec()) + return nil } // AppModule implements an application module for the staking module.