Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: multiple balance query #1461

Merged
merged 5 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading