From d21020e0f8a85002bcd827efbfbc01497a69975f Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Wed, 25 Mar 2020 12:27:03 -0400 Subject: [PATCH] Update x/bank CLI query commands --- x/bank/client/cli/query.go | 91 ++++++++++++++++++++++++++++++++++++++ x/bank/client/cli/tx.go | 4 +- 2 files changed, 92 insertions(+), 3 deletions(-) diff --git a/x/bank/client/cli/query.go b/x/bank/client/cli/query.go index e7327a091e..b63b9eabae 100644 --- a/x/bank/client/cli/query.go +++ b/x/bank/client/cli/query.go @@ -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]", diff --git a/x/bank/client/cli/tx.go b/x/bank/client/cli/tx.go index 906db932b3..6f59ec426d 100644 --- a/x/bank/client/cli/tx.go +++ b/x/bank/client/cli/tx.go @@ -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 }