forked from adshao/binance-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
240 lines (227 loc) · 4.95 KB
/
main.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/adshao/go-binance"
"github.com/juju/errors"
"gopkg.in/urfave/cli.v1"
)
var (
name string
keyfile string
debug bool
accounts map[string]*Account
assets []string
)
// AccountKey define key info for account
type AccountKey struct {
Name string `json:"name"`
APIKey string `json:"api_key"`
SecretKey string `json:"secret_key"`
}
func loadKeys(filePath string) ([]AccountKey, error) {
keyBytes, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, errors.Trace(err)
}
var keys []AccountKey
err = json.Unmarshal(keyBytes, &keys)
if err != nil {
return nil, errors.Trace(err)
}
return keys, nil
}
func initAccounts() {
if keyfile == "" {
keyfile = "keys.json"
}
keys, err := loadKeys(keyfile)
if err != nil {
log.Fatal("failed to load keys: ", err)
}
accounts = make(map[string]*Account)
for _, key := range keys {
client := binance.NewClient(
key.APIKey,
key.SecretKey,
)
if debug {
client.Debug = true
}
account := new(Account)
account.Client = client
account.Name = key.Name
accounts[account.Name] = account
}
}
var findAccounts func(name string) map[string]*Account
func findAccountsImpl(name string) map[string]*Account {
initAccounts()
if name == "" {
return accounts
}
return map[string]*Account{name: accounts[name]}
}
func runOnce(action func(*Account) (interface{}, error),
postAction ...func(map[string]interface{}) (interface{}, error)) error {
findAccounts = func(name string) map[string]*Account {
initAccounts()
for k, v := range accounts {
return map[string]*Account{k: v}
}
return nil
}
return accountsDo(action, postAction...)
}
func accountsDo(action func(*Account) (interface{}, error),
postAction ...func(map[string]interface{}) (interface{}, error)) error {
accounts := findAccounts(name)
var ret interface{}
var err error
results := make(map[string]interface{})
for _, account := range accounts {
res, err := action(account)
if err != nil {
// return errors.Trace(err)
results[account.Name] = fmt.Sprintf("error: %s", err)
} else {
results[account.Name] = res
}
}
if len(postAction) > 0 {
ret, err = postAction[0](results)
if err != nil {
return errors.Trace(err)
}
} else {
ret = results
}
return print(ret)
}
func print(ret interface{}) error {
out, err := json.MarshalIndent(ret, "", " ")
if err != nil {
return errors.Trace(err)
}
fmt.Println(string(out))
return nil
}
func main() {
app := cli.NewApp()
app.Name = "binance-cli"
app.Usage = "Binance CLI"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "name",
Usage: "account name",
Destination: &name,
},
cli.StringFlag{
Name: "keyfile",
Usage: "file path of api keys",
Destination: &keyfile,
},
cli.BoolFlag{
Name: "debug, d",
Usage: "show debug info",
Destination: &debug,
},
}
app.Commands = []cli.Command{
{
Name: "list-balances",
Usage: "list account balances",
Flags: []cli.Flag{
cli.StringSliceFlag{
Name: "assets",
EnvVar: "BINANCE_ASSETS",
Usage: "list balances with asset BTC, BNB ...",
Value: &cli.StringSlice{"BTC", "BNB", "WINK", "USDT"},
},
cli.BoolTFlag{
Name: "total",
Usage: "show total balance",
},
},
Action: func(c *cli.Context) error {
return listBalances(c.StringSlice("assets"), c.Bool("total"))
},
},
{
Name: "list-prices",
Usage: "list latest price for a symbol or symbols",
Flags: []cli.Flag{
cli.StringFlag{
Name: "symbol",
Usage: "filter with symbol",
},
},
Action: func(c *cli.Context) error {
return listPrices(c.String("symbol"))
},
},
{
Name: "list-orders",
Usage: "list open orders",
Flags: []cli.Flag{
cli.StringFlag{
Name: "symbol",
Usage: "list orders with symbol",
},
},
Action: func(c *cli.Context) error {
return listOpenOrders(c.String("symbol"))
},
},
{
Name: "create-order",
Usage: "create order",
Flags: []cli.Flag{
cli.StringFlag{
Name: "symbol",
Usage: "symbol name: BNBBTC",
},
cli.StringFlag{
Name: "side",
Usage: "side type: SELL or BUY",
},
cli.StringFlag{
Name: "quantity",
Usage: "quantity of symbol",
},
cli.StringFlag{
Name: "price",
Usage: "price of symbol",
},
},
Action: func(c *cli.Context) error {
return createOrder(
c.String("symbol"), c.String("side"),
c.String("quantity"), c.String("price"))
},
},
{
Name: "cancel-orders",
Usage: "cancel open orders",
Flags: []cli.Flag{
cli.StringFlag{
Name: "symbol",
Usage: "cancel open orders with symbol",
},
},
Action: func(c *cli.Context) error {
return cancelOrders(c.String("symbol"))
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(errors.ErrorStack(err))
}
}
func init() {
findAccounts = findAccountsImpl
}