Update x/bank CLI query commands
This commit is contained in:
parent
d962526b70
commit
d21020e0f8
|
@ -18,7 +18,95 @@ const (
|
|||
flagDenom = "denom"
|
||||
)
|
||||
|
||||
// NewQueryCmd returns a root CLI command handler for all x/bank query commands.
|
||||
func NewQueryCmd(m codec.Marshaler) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: types.ModuleName,
|
||||
Short: "Querying commands for the bank module",
|
||||
DisableFlagParsing: true,
|
||||
SuggestionsMinimumDistance: 2,
|
||||
RunE: client.ValidateCmd,
|
||||
}
|
||||
|
||||
cmd.AddCommand(NewBalancesCmd(m))
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
// NewBalancesCmd returns a CLI command handler for querying account balance(s).
|
||||
func NewBalancesCmd(m codec.Marshaler) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "balances [address]",
|
||||
Short: "Query for account balance(s) by address",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
cliCtx := context.NewCLIContext().WithMarshaler(m)
|
||||
|
||||
addr, err := sdk.AccAddressFromBech32(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var (
|
||||
params interface{}
|
||||
result interface{}
|
||||
route string
|
||||
)
|
||||
|
||||
denom := viper.GetString(flagDenom)
|
||||
if denom == "" {
|
||||
params = types.NewQueryAllBalancesParams(addr)
|
||||
route = fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryAllBalances)
|
||||
} else {
|
||||
params = types.NewQueryBalanceParams(addr, denom)
|
||||
route = fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryBalance)
|
||||
}
|
||||
|
||||
bz, err := m.MarshalJSON(params)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal params: %w", err)
|
||||
}
|
||||
|
||||
res, _, err := cliCtx.QueryWithData(route, bz)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if denom == "" {
|
||||
var balances sdk.Coins
|
||||
if err := m.UnmarshalJSON(res, &balances); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
result = balances
|
||||
} else {
|
||||
var balance sdk.Coin
|
||||
if err := m.UnmarshalJSON(res, &balance); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
result = balance
|
||||
}
|
||||
|
||||
return cliCtx.Print(result)
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Flags().String(flagDenom, "", "The specific balance denomination to query for")
|
||||
|
||||
return flags.GetCommands(cmd)[0]
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Deprecated
|
||||
//
|
||||
// TODO: Remove once client-side Protobuf migration has been completed.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// GetQueryCmd returns the parent querying command for the bank module.
|
||||
//
|
||||
// TODO: Remove once client-side Protobuf migration has been completed.
|
||||
// ref: https://github.com/cosmos/cosmos-sdk/issues/5864
|
||||
func GetQueryCmd(cdc *codec.Codec) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: types.ModuleName,
|
||||
|
@ -35,6 +123,9 @@ func GetQueryCmd(cdc *codec.Codec) *cobra.Command {
|
|||
|
||||
// GetAccountCmd returns a CLI command handler that facilitates querying for a
|
||||
// single or all account balances by address.
|
||||
//
|
||||
// TODO: Remove once client-side Protobuf migration has been completed.
|
||||
// ref: https://github.com/cosmos/cosmos-sdk/issues/5864
|
||||
func GetBalancesCmd(cdc *codec.Codec) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "balances [address]",
|
||||
|
|
|
@ -27,9 +27,7 @@ func NewTxCmd(m codec.Marshaler, txg tx.Generator, ar tx.AccountRetriever) *cobr
|
|||
RunE: client.ValidateCmd,
|
||||
}
|
||||
|
||||
txCmd.AddCommand(
|
||||
NewSendTxCmd(m, txg, ar),
|
||||
)
|
||||
txCmd.AddCommand( NewSendTxCmd(m, txg, ar), )
|
||||
|
||||
return txCmd
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue