cosmos-sdk/x/bank/client/cli/query.go

216 lines
5.3 KiB
Go
Raw Normal View History

2020-01-30 13:31:16 -08:00
package cli
import (
"fmt"
"strings"
2020-01-30 13:31:16 -08:00
"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/bank/types"
2020-01-30 13:31:16 -08:00
)
const (
FlagDenom = "denom"
2020-01-30 13:31:16 -08:00
)
// GetQueryCmd returns the parent command for all x/bank CLi query commands. The
// provided clientCtx should have, at a minimum, a verifier, Tendermint RPC client,
// and marshaler set.
2020-07-08 07:25:02 -07:00
func GetQueryCmd() *cobra.Command {
2020-01-30 13:31:16 -08:00
cmd := &cobra.Command{
Use: types.ModuleName,
Short: "Querying commands for the bank module",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
cmd.AddCommand(
2020-07-08 07:25:02 -07:00
GetBalancesCmd(),
GetCmdQueryTotalSupply(),
GetCmdDenomsMetadata(),
)
2020-01-30 13:31:16 -08:00
return cmd
}
2020-07-08 07:25:02 -07:00
func GetBalancesCmd() *cobra.Command {
2020-01-30 13:31:16 -08:00
cmd := &cobra.Command{
Use: "balances [address]",
Short: "Query for account balances by address",
Long: strings.TrimSpace(
fmt.Sprintf(`Query the total balance of an account or of a specific denomination.
Example:
$ %s query %s balances [address]
$ %s query %s balances [address] --denom=[denom]
`,
version.AppName, types.ModuleName, version.AppName, types.ModuleName,
),
),
Args: cobra.ExactArgs(1),
2020-01-30 13:31:16 -08:00
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
denom, err := cmd.Flags().GetString(FlagDenom)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
2020-01-30 13:31:16 -08:00
addr, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}
pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}
ctx := cmd.Context()
2020-01-30 13:31:16 -08:00
if denom == "" {
params := types.NewQueryAllBalancesRequest(addr, pageReq)
res, err := queryClient.AllBalances(ctx, params)
if err != nil {
2020-01-30 13:31:16 -08:00
return err
}
return clientCtx.PrintProto(res)
2020-01-30 13:31:16 -08:00
}
params := types.NewQueryBalanceRequest(addr, denom)
res, err := queryClient.Balance(ctx, params)
if err != nil {
return err
}
return clientCtx.PrintProto(res.Balance)
2020-01-30 13:31:16 -08:00
},
}
cmd.Flags().String(FlagDenom, "", "The specific balance denomination to query for")
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "all balances")
return cmd
2020-01-30 13:31:16 -08:00
}
// GetCmdDenomsMetadata defines the cobra command to query client denomination metadata.
func GetCmdDenomsMetadata() *cobra.Command {
cmd := &cobra.Command{
Use: "denom-metadata",
Short: "Query the client metadata for coin denominations",
Long: strings.TrimSpace(
fmt.Sprintf(`Query the client metadata for all the registered coin denominations
Example:
To query for the client metadata of all coin denominations use:
$ %s query %s denom-metadata
To query for the client metadata of a specific coin denomination use:
$ %s query %s denom-metadata --denom=[denom]
`,
version.AppName, types.ModuleName, version.AppName, types.ModuleName,
),
),
RunE: func(cmd *cobra.Command, _ []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
denom, err := cmd.Flags().GetString(FlagDenom)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
if denom == "" {
res, err := queryClient.DenomsMetadata(cmd.Context(), &types.QueryDenomsMetadataRequest{})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
}
res, err := queryClient.DenomMetadata(cmd.Context(), &types.QueryDenomMetadataRequest{Denom: denom})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
cmd.Flags().String(FlagDenom, "", "The specific denomination to query client metadata for")
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
2020-07-08 07:25:02 -07:00
func GetCmdQueryTotalSupply() *cobra.Command {
cmd := &cobra.Command{
Use: "total",
Short: "Query the total supply of coins of the chain",
fix: fix the invalid argument for bank total supply cli query (#9980) <!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description Closes: #9977 <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> --- ### 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/master/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/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/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)
2021-08-23 07:16:56 -07:00
Args: cobra.NoArgs,
Long: strings.TrimSpace(
fmt.Sprintf(`Query total supply of coins that are held by accounts in the chain.
Example:
$ %s query %s total
To query for the total supply of a specific coin denomination use:
$ %s query %s total --denom=[denom]
`,
version.AppName, types.ModuleName, version.AppName, types.ModuleName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
denom, err := cmd.Flags().GetString(FlagDenom)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
ctx := cmd.Context()
pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}
if denom == "" {
res, err := queryClient.TotalSupply(ctx, &types.QueryTotalSupplyRequest{Pagination: pageReq})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
}
res, err := queryClient.SupplyOf(ctx, &types.QuerySupplyOfRequest{Denom: denom})
if err != nil {
return err
}
return clientCtx.PrintProto(&res.Amount)
},
}
cmd.Flags().String(FlagDenom, "", "The specific balance denomination to query for")
flags.AddQueryFlagsToCmd(cmd)
fix: Update query.go to include pagination for bank q totals (#11355) ## Description Closes: #11354 --- ### 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... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [x] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [x] 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)
2022-03-15 07:43:21 -07:00
flags.AddPaginationFlagsToCmd(cmd, "all supply totals")
return cmd
}