cosmos-sdk/x/distribution/client/cli/query.go
Marko e2a8f7ca32
refactor(distribution): remove global bech32 (#15684)
## Description

ref #13140 

remove global bech32 in distribution

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

* [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
* [ ] added `!` to the type prefix if API or client breaking change
* [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
* [ ] provided a link to the relevant issue or specification
* [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/docs/building-modules)
* [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
* [ ] added a changelog entry to `CHANGELOG.md`
* [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
* [ ] updated the relevant documentation or specification
* [ ] reviewed "Files changed" and left comments if necessary
* [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

* [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
* [ ] confirmed `!` in the type prefix if API or client breaking change
* [ ] confirmed all author checklist items have been addressed 
* [ ] reviewed state machine logic
* [ ] reviewed API design and naming
* [ ] reviewed documentation is accurate
* [ ] reviewed tests and test coverage
* [ ] manually tested (if applicable)
2023-04-05 14:14:51 +00:00

368 lines
9.9 KiB
Go

package cli
import (
"fmt"
"strconv"
"strings"
"cosmossdk.io/core/address"
"github.com/spf13/cobra"
"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/distribution/types"
)
// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd(ac address.Codec) *cobra.Command {
distQueryCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Querying commands for the distribution module",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
distQueryCmd.AddCommand(
GetCmdQueryParams(),
GetCmdQueryValidatorDistributionInfo(),
GetCmdQueryValidatorOutstandingRewards(),
GetCmdQueryValidatorCommission(),
GetCmdQueryValidatorSlashes(),
GetCmdQueryDelegatorRewards(ac),
GetCmdQueryCommunityPool(),
)
return distQueryCmd
}
// GetCmdQueryParams implements the query params command.
func GetCmdQueryParams() *cobra.Command {
cmd := &cobra.Command{
Use: "params",
Args: cobra.NoArgs,
Short: "Query distribution params",
RunE: func(cmd *cobra.Command, _ []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
}
// GetCmdQueryValidatorDistributionInfo implements the query validator distribution info command.
func GetCmdQueryValidatorDistributionInfo() *cobra.Command {
bech32PrefixValAddr := sdk.GetConfig().GetBech32ValidatorAddrPrefix()
cmd := &cobra.Command{
Use: "validator-distribution-info [validator]",
Args: cobra.ExactArgs(1),
Short: "Query validator distribution info",
Long: strings.TrimSpace(
fmt.Sprintf(`Query validator distribution info.
Example:
$ %s query distribution validator-distribution-info %s1lwjmdnks33xwnmfayc64ycprww49n33mtm92ne
`,
version.AppName, bech32PrefixValAddr,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
validatorAddr, err := sdk.ValAddressFromBech32(args[0])
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.ValidatorDistributionInfo(cmd.Context(), &types.QueryValidatorDistributionInfoRequest{
ValidatorAddress: validatorAddr.String(),
})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdQueryValidatorOutstandingRewards implements the query validator
// outstanding rewards command.
func GetCmdQueryValidatorOutstandingRewards() *cobra.Command {
bech32PrefixValAddr := sdk.GetConfig().GetBech32ValidatorAddrPrefix()
cmd := &cobra.Command{
Use: "validator-outstanding-rewards [validator]",
Args: cobra.ExactArgs(1),
Short: "Query distribution outstanding (un-withdrawn) rewards for a validator and all their delegations",
Long: strings.TrimSpace(
fmt.Sprintf(`Query distribution outstanding (un-withdrawn) rewards for a validator and all their delegations.
Example:
$ %s query distribution validator-outstanding-rewards %s1lwjmdnks33xwnmfayc64ycprww49n33mtm92ne
`,
version.AppName, bech32PrefixValAddr,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
validatorAddr, err := sdk.ValAddressFromBech32(args[0])
if err != nil {
return err
}
res, err := queryClient.ValidatorOutstandingRewards(
cmd.Context(),
&types.QueryValidatorOutstandingRewardsRequest{ValidatorAddress: validatorAddr.String()},
)
if err != nil {
return err
}
return clientCtx.PrintProto(&res.Rewards)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdQueryValidatorCommission implements the query validator commission command.
func GetCmdQueryValidatorCommission() *cobra.Command {
bech32PrefixValAddr := sdk.GetConfig().GetBech32ValidatorAddrPrefix()
cmd := &cobra.Command{
Use: "commission [validator]",
Args: cobra.ExactArgs(1),
Short: "Query distribution validator commission",
Long: strings.TrimSpace(
fmt.Sprintf(`Query validator commission rewards from delegators to that validator.
Example:
$ %s query distribution commission %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj
`,
version.AppName, bech32PrefixValAddr,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
validatorAddr, err := sdk.ValAddressFromBech32(args[0])
if err != nil {
return err
}
res, err := queryClient.ValidatorCommission(
cmd.Context(),
&types.QueryValidatorCommissionRequest{ValidatorAddress: validatorAddr.String()},
)
if err != nil {
return err
}
return clientCtx.PrintProto(&res.Commission)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdQueryValidatorSlashes implements the query validator slashes command.
func GetCmdQueryValidatorSlashes() *cobra.Command {
bech32PrefixValAddr := sdk.GetConfig().GetBech32ValidatorAddrPrefix()
cmd := &cobra.Command{
Use: "slashes [validator] [start-height] [end-height]",
Args: cobra.ExactArgs(3),
Short: "Query distribution validator slashes",
Long: strings.TrimSpace(
fmt.Sprintf(`Query all slashes of a validator for a given block range.
Example:
$ %s query distribution slashes %svaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj 0 100
`,
version.AppName, bech32PrefixValAddr,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
validatorAddr, err := sdk.ValAddressFromBech32(args[0])
if err != nil {
return err
}
startHeight, err := strconv.ParseUint(args[1], 10, 64)
if err != nil {
return fmt.Errorf("start-height %s not a valid uint, please input a valid start-height", args[1])
}
endHeight, err := strconv.ParseUint(args[2], 10, 64)
if err != nil {
return fmt.Errorf("end-height %s not a valid uint, please input a valid end-height", args[2])
}
pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}
res, err := queryClient.ValidatorSlashes(
cmd.Context(),
&types.QueryValidatorSlashesRequest{
ValidatorAddress: validatorAddr.String(),
StartingHeight: startHeight,
EndingHeight: endHeight,
Pagination: pageReq,
},
)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "validator slashes")
return cmd
}
// GetCmdQueryDelegatorRewards implements the query delegator rewards command.
func GetCmdQueryDelegatorRewards(ac address.Codec) *cobra.Command {
bech32PrefixAccAddr := sdk.GetConfig().GetBech32AccountAddrPrefix()
bech32PrefixValAddr := sdk.GetConfig().GetBech32ValidatorAddrPrefix()
cmd := &cobra.Command{
Use: "rewards [delegator-addr] [validator-addr]",
Args: cobra.RangeArgs(1, 2),
Short: "Query all distribution delegator rewards or rewards from a particular validator",
Long: strings.TrimSpace(
fmt.Sprintf(`Query all rewards earned by a delegator, optionally restrict to rewards from a single validator.
Example:
$ %s query distribution rewards %s1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p
$ %s query distribution rewards %s1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj
`,
version.AppName, bech32PrefixAccAddr, version.AppName, bech32PrefixAccAddr, bech32PrefixValAddr,
),
),
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
}
// query for rewards from a particular delegation
ctx := cmd.Context()
if len(args) == 2 {
validatorAddr, err := sdk.ValAddressFromBech32(args[1])
if err != nil {
return err
}
res, err := queryClient.DelegationRewards(
ctx,
&types.QueryDelegationRewardsRequest{DelegatorAddress: args[0], ValidatorAddress: validatorAddr.String()},
)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
}
res, err := queryClient.DelegationTotalRewards(
ctx,
&types.QueryDelegationTotalRewardsRequest{DelegatorAddress: args[0]},
)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdQueryCommunityPool returns the command for fetching community pool info.
func GetCmdQueryCommunityPool() *cobra.Command {
cmd := &cobra.Command{
Use: "community-pool",
Args: cobra.NoArgs,
Short: "Query the amount of coins in the community pool",
Long: strings.TrimSpace(
fmt.Sprintf(`Query all coins in the community pool which is under Governance control.
Example:
$ %s query distribution community-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.CommunityPool(cmd.Context(), &types.QueryCommunityPoolRequest{})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}