319 lines
8.6 KiB
Go
319 lines
8.6 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"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() *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(),
|
|
GetCmdQueryValidatorOutstandingRewards(),
|
|
GetCmdQueryValidatorCommission(),
|
|
GetCmdQueryValidatorSlashes(),
|
|
GetCmdQueryDelegatorRewards(),
|
|
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 := client.GetClientContextFromCmd(cmd)
|
|
clientCtx, err := client.ReadQueryCommandFlags(clientCtx, cmd.Flags())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
queryClient := types.NewQueryClient(clientCtx)
|
|
|
|
res, err := queryClient.Params(context.Background(), &types.QueryParamsRequest{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return clientCtx.PrintOutput(&res.Params)
|
|
},
|
|
}
|
|
|
|
flags.AddQueryFlagsToCmd(cmd)
|
|
return cmd
|
|
}
|
|
|
|
// GetCmdQueryValidatorOutstandingRewards implements the query validator
|
|
// outstanding rewards command.
|
|
func GetCmdQueryValidatorOutstandingRewards() *cobra.Command {
|
|
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 cosmosvaloper1lwjmdnks33xwnmfayc64ycprww49n33mtm92ne
|
|
`,
|
|
version.AppName,
|
|
),
|
|
),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
clientCtx := client.GetClientContextFromCmd(cmd)
|
|
clientCtx, err := client.ReadQueryCommandFlags(clientCtx, cmd.Flags())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
queryClient := types.NewQueryClient(clientCtx)
|
|
|
|
validatorAddr, err := sdk.ValAddressFromBech32(args[0])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
res, err := queryClient.ValidatorOutstandingRewards(
|
|
context.Background(),
|
|
&types.QueryValidatorOutstandingRewardsRequest{ValidatorAddress: validatorAddr},
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return clientCtx.PrintOutput(&res.Rewards)
|
|
},
|
|
}
|
|
|
|
flags.AddQueryFlagsToCmd(cmd)
|
|
return cmd
|
|
}
|
|
|
|
// GetCmdQueryValidatorCommission implements the query validator commission command.
|
|
func GetCmdQueryValidatorCommission() *cobra.Command {
|
|
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 cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj
|
|
`,
|
|
version.AppName,
|
|
),
|
|
),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
clientCtx := client.GetClientContextFromCmd(cmd)
|
|
clientCtx, err := client.ReadQueryCommandFlags(clientCtx, cmd.Flags())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
queryClient := types.NewQueryClient(clientCtx)
|
|
|
|
validatorAddr, err := sdk.ValAddressFromBech32(args[0])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
res, err := queryClient.ValidatorCommission(
|
|
context.Background(),
|
|
&types.QueryValidatorCommissionRequest{ValidatorAddress: validatorAddr},
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return clientCtx.PrintOutput(&res.Commission)
|
|
},
|
|
}
|
|
|
|
flags.AddQueryFlagsToCmd(cmd)
|
|
return cmd
|
|
}
|
|
|
|
// GetCmdQueryValidatorSlashes implements the query validator slashes command.
|
|
func GetCmdQueryValidatorSlashes() *cobra.Command {
|
|
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 cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj 0 100
|
|
`,
|
|
version.AppName,
|
|
),
|
|
),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
clientCtx := client.GetClientContextFromCmd(cmd)
|
|
clientCtx, err := client.ReadQueryCommandFlags(clientCtx, cmd.Flags())
|
|
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(
|
|
context.Background(),
|
|
&types.QueryValidatorSlashesRequest{
|
|
ValidatorAddress: validatorAddr,
|
|
StartingHeight: startHeight,
|
|
EndingHeight: endHeight,
|
|
Pagination: pageReq,
|
|
},
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return clientCtx.PrintOutput(res)
|
|
},
|
|
}
|
|
|
|
flags.AddQueryFlagsToCmd(cmd)
|
|
flags.AddPaginationFlagsToCmd(cmd, "validator slashes")
|
|
return cmd
|
|
}
|
|
|
|
// GetCmdQueryDelegatorRewards implements the query delegator rewards command.
|
|
func GetCmdQueryDelegatorRewards() *cobra.Command {
|
|
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 cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p
|
|
$ %s query distribution rewards cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj
|
|
`,
|
|
version.AppName, version.AppName,
|
|
),
|
|
),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
clientCtx := client.GetClientContextFromCmd(cmd)
|
|
clientCtx, err := client.ReadQueryCommandFlags(clientCtx, cmd.Flags())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
queryClient := types.NewQueryClient(clientCtx)
|
|
|
|
delegatorAddr, err := sdk.AccAddressFromBech32(args[0])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// query for rewards from a particular delegation
|
|
if len(args) == 2 {
|
|
validatorAddr, err := sdk.ValAddressFromBech32(args[1])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
res, err := queryClient.DelegationRewards(
|
|
context.Background(),
|
|
&types.QueryDelegationRewardsRequest{DelegatorAddress: delegatorAddr, ValidatorAddress: validatorAddr},
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return clientCtx.PrintOutput(res)
|
|
}
|
|
|
|
res, err := queryClient.DelegationTotalRewards(
|
|
context.Background(),
|
|
&types.QueryDelegationTotalRewardsRequest{DelegatorAddress: delegatorAddr},
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return clientCtx.PrintOutput(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 := client.GetClientContextFromCmd(cmd)
|
|
clientCtx, err := client.ReadQueryCommandFlags(clientCtx, cmd.Flags())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
queryClient := types.NewQueryClient(clientCtx)
|
|
|
|
res, err := queryClient.CommunityPool(context.Background(), &types.QueryCommunityPoolRequest{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return clientCtx.PrintOutput(res)
|
|
},
|
|
}
|
|
|
|
flags.AddQueryFlagsToCmd(cmd)
|
|
return cmd
|
|
}
|