/
cmd_wallet_balances.go
159 lines (141 loc) · 4.19 KB
/
cmd_wallet_balances.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package client
import (
"errors"
"fmt"
"github.com/keybase/cli"
"github.com/adamwalz/keybase-client/go/libcmdline"
"github.com/adamwalz/keybase-client/go/libkb"
"github.com/adamwalz/keybase-client/go/protocol/stellar1"
"github.com/adamwalz/keybase-client/go/stellar"
"github.com/keybase/stellarnet"
"golang.org/x/net/context"
)
type cmdWalletBalances struct {
libkb.Contextified
accountID string
}
func newCmdWalletBalances(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
cmd := &cmdWalletBalances{
Contextified: libkb.NewContextified(g),
}
return cli.Command{
Name: "balances",
Aliases: []string{"list", "accounts"},
Usage: "Show account balances",
Description: "Show account balances",
Action: func(c *cli.Context) {
cl.ChooseCommand(cmd, "balances", c)
},
}
}
func (c *cmdWalletBalances) ParseArgv(ctx *cli.Context) error {
if len(ctx.Args()) > 1 {
return errors.New("one Stellar address required, multiple found")
} else if len(ctx.Args()) == 1 {
c.accountID = ctx.Args()[0]
}
return nil
}
func (c *cmdWalletBalances) printBalance(dui libkb.DumbOutputUI, balance stellar1.Balance, xchgRate *stellar1.OutsideExchangeRate) {
localAmountStr := ""
if balance.Asset.IsNativeXLM() {
if xchgRate != nil {
localAmount, err := stellarnet.ConvertXLMToOutside(balance.Amount, xchgRate.Rate)
if err == nil {
localAmountStr = fmt.Sprintf(" (%s %s)", string(xchgRate.Currency), localAmount)
} else {
c.G().Log.Warning("Unable to convert to local currency: %s", err)
}
}
_, _ = dui.PrintfUnescaped("XLM\t%s%s\n", balance.Amount, ColorString(c.G(), "green", localAmountStr))
} else {
_, _ = dui.Printf("%s\t%s\t(issued by: %s, %s)\n", balance.Asset.Code, balance.Amount, stellar.FormatAssetIssuerString(balance.Asset), balance.Asset.Issuer)
}
}
func (c *cmdWalletBalances) runForAccountID(cli stellar1.LocalClient) error {
accountID, err := libkb.ParseStellarAccountID(c.accountID)
if err != nil {
return err
}
dui := c.G().UI.GetDumbOutputUI()
balances, err := cli.BalancesLocal(context.Background(), accountID)
if err != nil {
return err
}
var xchgRate *stellar1.OutsideExchangeRate
accountCurrency, err := cli.GetDisplayCurrencyLocal(context.Background(), stellar1.GetDisplayCurrencyLocalArg{
AccountID: &accountID,
})
if err == nil {
// Ignore error - GetDisplayCurrencyLocal will error out if we don't
// own the account. In that case we just skip currency conversion but
// still display balances.
rate, err := cli.ExchangeRateLocal(context.Background(), accountCurrency.Code)
if err != nil {
c.G().Log.Warning("Unable to get exchange rate for %q: %s", accountCurrency.Code, err)
} else {
xchgRate = &rate
}
}
for _, balance := range balances {
c.printBalance(dui, balance, xchgRate)
}
return nil
}
func (c *cmdWalletBalances) runForUser(cli stellar1.LocalClient) error {
accounts, err := cli.WalletGetAccountsCLILocal(context.Background())
if err != nil {
return err
}
dui := c.G().UI.GetDumbOutputUI()
for i, acc := range accounts {
var accountName string
if acc.Name != "" {
var isPrimary string
if acc.IsPrimary {
isPrimary = " (Primary)"
}
accountName = fmt.Sprintf("'%s' (%s)%s", acc.Name, acc.AccountID, isPrimary)
} else {
accountName = acc.AccountID.String()
if acc.IsPrimary {
accountName += " (Primary)"
}
}
dui.Printf("Balances for account %s:\n", accountName)
if len(acc.Balance) == 0 {
// If there are no balance entries the account is not on the network.
// Make a fake entry of 0 XLM to display.
acc.Balance = []stellar1.Balance{{
Asset: stellar1.AssetNative(),
Amount: "0",
Limit: "",
}}
}
for _, balance := range acc.Balance {
c.printBalance(dui, balance, acc.ExchangeRate)
}
if i != len(accounts)-1 {
dui.Printf("\n")
}
}
return nil
}
func (c *cmdWalletBalances) Run() (err error) {
defer transformStellarCLIError(&err)
cli, err := GetWalletClient(c.G())
if err != nil {
return err
}
if c.accountID == "" {
return c.runForUser(cli)
}
return c.runForAccountID(cli)
}
func (c *cmdWalletBalances) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
API: true,
KbKeyring: true,
}
}