Skip to content

Commit

Permalink
feat: multiple balance query (#1461)
Browse files Browse the repository at this point in the history
* Add balances cmd to query multi chain balances

* fix mistake flag name
  • Loading branch information
neitdung authored Jun 2, 2024
1 parent 98da147 commit df42391
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const (
flagInitialBlockHistory = "block-history"
flagFlushInterval = "flush-interval"
flagMemo = "memo"
flagKeyName = "key-name"
flagFilterRule = "filter-rule"
flagFilterChannels = "filter-channels"
flagSrcChainID = "src-chain-id"
Expand Down Expand Up @@ -477,6 +478,14 @@ func memoFlag(v *viper.Viper, cmd *cobra.Command) *cobra.Command {
return cmd
}

func keyNameFlag(v *viper.Viper, cmd *cobra.Command) *cobra.Command {
cmd.Flags().String(flagKeyName, "", "a key from the keychain associated with a particular chain")
if err := v.BindPFlag(flagKeyName, cmd.Flags().Lookup(flagKeyName)); err != nil {
panic(err)
}
return cmd
}

func OverwriteConfigFlag(v *viper.Viper, cmd *cobra.Command) *cobra.Command {
cmd.Flags().BoolP(flagOverwriteConfig, "o", false,
"overwrite already configured paths - will clear channel filter(s)")
Expand Down
72 changes: 72 additions & 0 deletions cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func queryCmd(a *appState) *cobra.Command {
queryUnrelayedAcknowledgements(a),
lineBreakCommand(),
queryBalanceCmd(a),
queryBalancesCmd(a),
queryHeaderCmd(a),
queryNodeStateCmd(a),
queryTxs(a),
Expand Down Expand Up @@ -325,6 +326,77 @@ $ %s query balance ibc-0 testkey`,
return cmd
}

func queryBalancesCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "balances [chain-name...]",
Short: "query the relayer's account balances on given networks by chain-ID",
Args: withUsage(cobra.MinimumNArgs(1)),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s query balances ibc-0 ibc-1
$ %s query balances ibc-0 ibc-1 --key-name=test`,
appName, appName,
)),
RunE: func(cmd *cobra.Command, args []string) error {
keyName, _ := cmd.Flags().GetString(flagKeyName)

data := map[string]string{}
for _, arg := range args {
chain, ok := a.config.Chains[arg]
if !ok {
return errChainNotFound(args[0])
}

chainKey := keyName
if chainKey == "" {
chainKey = chain.ChainProvider.Key()
}

showDenoms, err := cmd.Flags().GetBool(flagIBCDenoms)
if err != nil {
return err
}

if !chain.ChainProvider.KeyExists(chainKey) {
return errKeyDoesntExist(chainKey)
}
addr, err := chain.ChainProvider.ShowAddress(chainKey)
if err != nil {
return err
}

coins, err := relayer.QueryBalance(cmd.Context(), chain, addr, showDenoms)
if err != nil {
return err
}

data[addr] = coins.String()
}
jsonOutput, err := json.Marshal(data)
if err != nil {
return err
}

output, _ := cmd.Flags().GetString(flagOutput)
switch output {
case formatJson:
fmt.Fprint(cmd.OutOrStdout(), string(jsonOutput))
case formatLegacy:
fallthrough
default:
for addr, balance := range data {
fmt.Fprintf(cmd.OutOrStdout(), "address {%s} balance {%s} \n", addr, balance)
}
}
return nil
},
}

cmd = addOutputFlag(a.viper, cmd)
cmd = ibcDenomFlags(a.viper, cmd)
cmd = keyNameFlag(a.viper, cmd)
return cmd
}

func queryHeaderCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "header chain_name [height]",
Expand Down

0 comments on commit df42391

Please sign in to comment.