-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
315 lines (301 loc) · 6.36 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/adshao/go-binance/v2"
"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 {
var origFindAccounts = findAccounts
defer func() {
findAccounts = origFindAccounts
}()
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,
},
cli.StringFlag{
Name: "configfile, f",
Usage: "config file",
},
}
app.Commands = []cli.Command{
{
Name: "list-balance",
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", "USDT"},
},
cli.BoolTFlag{
Name: "total",
Usage: "show total balance",
},
},
Action: func(c *cli.Context) error {
return listBalances(c)
},
},
{
Name: "list-price",
Usage: "list latest price for a symbol or symbols",
Flags: []cli.Flag{
cli.StringFlag{
Name: "symbol, s",
Usage: "filter with symbol",
},
},
Action: func(c *cli.Context) error {
return listPrices(c)
},
},
{
Name: "list-order",
Usage: "list open orders",
Flags: []cli.Flag{
cli.StringFlag{
Name: "symbol, s",
Usage: "list orders with symbol",
},
cli.BoolFlag{
Name: "all",
Usage: "List all account orders",
},
cli.IntFlag{
Name: "limit, l",
Usage: "limit num of trades",
},
},
Action: func(c *cli.Context) error {
return listOrders(c)
},
},
{
Name: "create-order",
Usage: "create order",
Flags: []cli.Flag{
cli.StringFlag{
Name: "symbol, s",
Usage: "symbol name: BNBBTC",
},
cli.StringFlag{
Name: "side",
Usage: "side type: SELL or BUY",
},
cli.StringFlag{
Name: "quantity",
Usage: "quantity of symbol: 20.120 or 50%",
},
cli.StringFlag{
Name: "price",
Usage: "price of symbol",
},
cli.BoolFlag{
Name: "test",
Usage: "for test only, will not actually create order",
},
},
Action: func(c *cli.Context) error {
return createOrder(c)
},
},
{
Name: "cancel-order",
Usage: "cancel open orders",
Flags: []cli.Flag{
cli.StringFlag{
Name: "symbol, s",
Usage: "cancel open orders with symbol",
},
cli.Int64Flag{
Name: "order-id, id",
Usage: "cancel open order with order id",
},
},
Action: func(c *cli.Context) error {
return cancelOrders(c)
},
},
{
Name: "list-symbol",
Usage: "list symbols info",
Flags: []cli.Flag{
cli.StringFlag{
Name: "symbol, s",
Usage: "symbol name",
},
},
Action: func(c *cli.Context) error {
return listSymbols(c)
},
},
{
Name: "list-trade",
Usage: "list trades",
Flags: []cli.Flag{
cli.StringFlag{
Name: "symbol, s",
Usage: "symbol name",
},
cli.IntFlag{
Name: "limit, l",
Usage: "limit num of trades",
},
},
Action: func(c *cli.Context) error {
return listTrades(c)
},
},
{
Name: "list-margin-balance",
Usage: "list margin account balances",
Flags: []cli.Flag{
cli.StringSliceFlag{
Name: "assets",
EnvVar: "BINANCE_ASSETS",
Usage: "list balances with asset BTC, BNB ...",
},
cli.BoolTFlag{
Name: "total",
Usage: "show total balance",
},
cli.BoolFlag{
Name: "borrowed",
Usage: "only show borrowed asset",
},
},
Action: func(c *cli.Context) error {
return listMarginBalances(c)
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(errors.ErrorStack(err))
}
}
func init() {
findAccounts = findAccountsImpl
}