-
Notifications
You must be signed in to change notification settings - Fork 320
/
accountbalance.go
85 lines (76 loc) · 2.45 KB
/
accountbalance.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (c) 2019 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package account
import (
"fmt"
"math/big"
"github.com/spf13/cobra"
ioAddress "github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/ioctl/output"
"github.com/iotexproject/iotex-core/ioctl/util"
)
// Multi-language support
var (
balanceCmdUses = map[config.Language]string{
config.English: "balance [ALIAS|ADDRESS]",
config.Chinese: "balance [别名|地址]",
}
balanceCmdShorts = map[config.Language]string{
config.English: "Get balance of an account",
config.Chinese: "查询账号余额",
}
)
// accountBalanceCmd represents the account balance command
var accountBalanceCmd = &cobra.Command{
Use: config.TranslateInLang(balanceCmdUses, config.UILanguage),
Short: config.TranslateInLang(balanceCmdShorts, config.UILanguage),
Args: cobra.RangeArgs(0, 1),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
arg := ""
if len(args) == 1 {
arg = args[0]
}
err := balance(arg)
return output.PrintError(err)
},
}
type balanceMessage struct {
Address string `json:"address"`
Balance string `json:"balance"`
}
// balance gets balance of an IoTeX blockchain address
func balance(arg string) error {
addr := arg
if arg != ioAddress.StakingBucketPoolAddr && arg != ioAddress.RewardingPoolAddr {
var err error
addr, err = util.GetAddress(arg)
if err != nil {
return output.NewError(output.AddressError, "", err)
}
}
accountMeta, err := GetAccountMeta(addr)
if err != nil {
return output.NewError(0, "", err) // TODO: undefined error
}
balance, ok := big.NewInt(0).SetString(accountMeta.Balance, 10)
if !ok {
return output.NewError(output.ConvertError, "", err)
}
message := balanceMessage{
Address: addr,
Balance: util.RauToString(balance, util.IotxDecimalNum),
}
fmt.Println((message.String()))
return nil
}
func (m *balanceMessage) String() string {
if output.Format == "" {
return fmt.Sprintf("%s: %s IOTX", m.Address, m.Balance)
}
return output.FormatString(output.Result, m)
}