forked from eoscanada/eosc
/
getBalance.go
40 lines (32 loc) · 980 Bytes
/
getBalance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package cmd
import (
"context"
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var getBalanceCmd = &cobra.Command{
Use: "balance [account]",
Short: "Retrieve currency balance for an account",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
api := getAPI()
account := toAccount(args[0], "account")
symbol := viper.GetString("get-balance-cmd-symbol")
tokenAccount := toAccount(viper.GetString("get-balance-cmd-contract"), "--contract")
balances, err := api.GetCurrencyBalance(context.Background(), account, symbol, tokenAccount)
if err != nil {
fmt.Printf("Error: get balance: %s\n", err)
os.Exit(1)
}
for _, asset := range balances {
fmt.Printf("%s\n", asset)
}
},
}
func init() {
getCmd.AddCommand(getBalanceCmd)
getBalanceCmd.Flags().StringP("contract", "", "eosio.token", "Account managing the token")
getBalanceCmd.Flags().StringP("symbol", "", "", "Only query this symbol. Try EOS")
}