Skip to content

Commit

Permalink
Add balances cmd to query multi chain balances
Browse files Browse the repository at this point in the history
  • Loading branch information
neitdung committed May 7, 2024
1 parent bf4e3e8 commit 03896e8
Show file tree
Hide file tree
Showing 2 changed files with 82 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(flagMemo, 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
73 changes: 73 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,78 @@ $ %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()
}
// Convert the map to a JSON 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 03896e8

Please sign in to comment.