/
kucoin.go
687 lines (643 loc) · 18.8 KB
/
kucoin.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
// Package kucoin is an implementation of the Kucoin API in Golang.
package kucoin
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"time"
)
const (
kucoinUrl = "https://api.kucoin.com/v1/"
)
// New returns an instantiated Kucoin struct.
func New(apiKey, apiSecret string) *Kucoin {
client := newClient(apiKey, apiSecret)
return &Kucoin{client}
}
// NewCustomClient returns an instantiated Kucoin struct with custom http client.
func NewCustomClient(apiKey, apiSecret string, httpClient http.Client) *Kucoin {
client := newClient(apiKey, apiSecret)
client.httpClient = httpClient
return &Kucoin{client}
}
// NewCustomTimeout returns an instantiated Kucoin struct with custom timeout.
func NewCustomTimeout(apiKey, apiSecret string, timeout time.Duration) *Kucoin {
client := newClient(apiKey, apiSecret)
client.httpClient.Timeout = timeout
return &Kucoin{client}
}
func doArgs(args ...string) map[string]string {
m := make(map[string]string)
var lastK = ""
for i, v := range args {
if i&1 == 0 {
lastK = v
} else {
m[lastK] = v
}
}
return m
}
// handleErr gets JSON response from livecoin API en deal with error.
func handleErr(r interface{}) error {
switch v := r.(type) {
case map[string]interface{}:
err := r.(map[string]interface{})["error"]
if err != nil {
switch v := err.(type) {
case map[string]interface{}:
errorMessage := err.(map[string]interface{})["message"]
return errors.New(errorMessage.(string))
default:
return fmt.Errorf("don't recognized type %T", v)
}
}
case []interface{}:
return nil
default:
return fmt.Errorf("don't recognized type %T", v)
}
return nil
}
// Kucoin represent a Kucoin client.
type Kucoin struct {
client *client
}
// SetDebug enables/disables http request/response dump.
func (b *Kucoin) SetDebug(enable bool) {
b.client.debug = enable
}
// GetUserInfo is used to get the user information at Kucoin along with other meta data.
func (b *Kucoin) GetUserInfo() (userInfo UserInfo, err error) {
r, err := b.client.do("GET", "user/info", nil, true)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
var rawRes rawUserInfo
err = json.Unmarshal(r, &rawRes)
userInfo = rawRes.Data
return
}
// GetSymbols is used to get the all open and available trading markets at Kucoin along with other meta data.
func (b *Kucoin) GetSymbols() (symbols []Symbol, err error) {
r, err := b.client.do("GET", "market/open/symbols", nil, false)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
var rawRes rawSymbols
err = json.Unmarshal(r, &rawRes)
symbols = rawRes.Data
return
}
// GetUserSymbols is used to get the all open and available trading markets at Kucoin along with other meta data.
// The user should be logged to call this method.
// Filter parameter can be whether 'FAVOURITE' or 'STICK',
// market and symbol parameters can be any as presented at exchange.
func (b *Kucoin) GetUserSymbols(market, symbol, filter string) (symbols []Symbol, err error) {
payload := map[string]string{}
if len(market) > 1 {
payload["market"] = market
}
if len(symbol) > 1 {
payload["symbol"] = symbol
}
if len(filter) > 1 {
payload["filter"] = filter
}
r, err := b.client.do("GET", "market/symbols", payload, true)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
var rawRes rawSymbols
err = json.Unmarshal(r, &rawRes)
symbols = rawRes.Data
return
}
// GetSymbol is used to get the open and available trading market at Kucoin along with other meta data.
// Trading symbol e.g. KCS-BTC. If not specified then you will get data of all symbols.
func (b *Kucoin) GetSymbol(market string) (symbol Symbol, err error) {
r, err := b.client.do("GET",
"open/tick", doArgs("symbol", strings.ToUpper(market)), false,
)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
var rawRes rawSymbol
err = json.Unmarshal(r, &rawRes)
symbol = rawRes.Data
return
}
// GetCoins is used to get the all open and available trading coins at Kucoin along with other meta data.
func (b *Kucoin) GetCoins() (coins []Coin, err error) {
r, err := b.client.do("GET", "market/open/coins", nil, false)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
var rawRes rawCoins
err = json.Unmarshal(r, &rawRes)
coins = rawRes.Data
return
}
// GetCoin is used to get the open and available trading coin at Kucoin along with other meta data.
func (b *Kucoin) GetCoin(c string) (coin Coin, err error) {
r, err := b.client.do(
"GET", "market/open/coin-info", doArgs("coin", strings.ToUpper(c)), false,
)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
var rawRes rawCoin
err = json.Unmarshal(r, &rawRes)
coin = rawRes.Data
return
}
// GetCoinBalance is used to get the balance at chosen coin at Kucoin along with other meta data.
func (b *Kucoin) GetCoinBalance(c string) (coinBalance CoinBalance, err error) {
r, err := b.client.do("GET", fmt.Sprintf("account/%s/balance", strings.ToUpper(c)), nil, true)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
var rawRes rawCoinBalance
err = json.Unmarshal(r, &rawRes)
coinBalance = rawRes.Data
return
}
// GetCoinDepositAddress is used to get the address at chosen coin at Kucoin along with other meta data.
func (b *Kucoin) GetCoinDepositAddress(c string) (coinDepositAddress CoinDepositAddress, err error) {
r, err := b.client.do("GET", fmt.Sprintf("account/%s/wallet/address", strings.ToUpper(c)), nil, true)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
var rawRes rawCoinDepositAddress
err = json.Unmarshal(r, &rawRes)
coinDepositAddress = rawRes.Data
return
}
// ListActiveMapOrders is used to get the information about active orders in user-friendly view
// at Kucoin along with other meta data.
// Symbol is required parameter, and side (or type of order in kucoin docs) may be empty.
func (b *Kucoin) ListActiveMapOrders(symbol string, side string) (activeMapOrders ActiveMapOrder, err error) {
if len(symbol) < 1 {
return activeMapOrders, fmt.Errorf("Symbol is required")
}
payload := make(map[string]string)
payload["symbol"] = strings.ToUpper(symbol)
if len(side) > 1 {
payload["side"] = strings.ToUpper(side)
}
r, err := b.client.do("GET", "order/active-map", payload, true)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
var rawRes rawActiveMapOrder
err = json.Unmarshal(r, &rawRes)
activeMapOrders = rawRes.Data
return
}
// ListActiveOrders is used to get the information about active orders in array mode
// at Kucoin along with other meta data.
// Symbol is required parameter, and side (or type of order in kucoin docs) may be empty.
func (b *Kucoin) ListActiveOrders(symbol string, side string) (activeOrders ActiveOrder, err error) {
if len(symbol) < 1 {
return activeOrders, fmt.Errorf("The symbol is required")
}
payload := make(map[string]string)
payload["symbol"] = strings.ToUpper(symbol)
if len(side) > 1 {
payload["side"] = strings.ToUpper(side)
}
r, err := b.client.do("GET", "order/active", payload, true)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
var rawRes rawActiveOrder
err = json.Unmarshal(r, &rawRes)
activeOrders = rawRes.Data
return
}
// OrdersBook is used to get the information about active orders at Kucoin along with other meta data.
// Symbol is required parameter, geoup and limit may be empty.
func (b *Kucoin) OrdersBook(symbol string, group, limit int) (ordersBook OrdersBook, err error) {
if len(symbol) < 1 {
return ordersBook, fmt.Errorf("The symbol is required")
}
payload := map[string]string{}
payload["symbol"] = strings.ToUpper(symbol)
if group > 0 {
payload["group"] = fmt.Sprintf("%v", group)
}
if limit == 0 {
payload["limit"] = fmt.Sprintf("%v", 1000)
} else {
payload["limit"] = fmt.Sprintf("%v", limit)
}
r, err := b.client.do("GET", "open/orders", payload, true)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
var rawRes rawOrdersBook
err = json.Unmarshal(r, &rawRes)
ordersBook = rawRes.Data
return
}
// CreateOrder is used to create order at Kucoin along with other meta data.
func (b *Kucoin) CreateOrder(symbol, side string, price, amount float64) (orderOid string, err error) {
payload := make(map[string]string)
payload["amount"] = strconv.FormatFloat(amount, 'f', 8, 64)
payload["price"] = strconv.FormatFloat(price, 'f', 8, 64)
payload["type"] = strings.ToUpper(side)
r, err := b.client.do("POST", fmt.Sprintf("%s/order", strings.ToUpper(symbol)), payload, true)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
var rawRes rawOrder
err = json.Unmarshal(r, &rawRes)
if err != nil {
return
}
if !rawRes.Success {
err = errors.New(string(r))
return
}
orderOid = rawRes.Data.OrderOid
return
}
// CreateOrderByString is used to create order at Kucoin along with other meta data.
// This ByString version is fix precise problem.
func (b *Kucoin) CreateOrderByString(symbol, side, price, amount string) (orderOid string, err error) {
payload := make(map[string]string)
payload["amount"] = amount
payload["price"] = price
payload["type"] = strings.ToUpper(side)
r, err := b.client.do("POST", fmt.Sprintf("%s/order", strings.ToUpper(symbol)), payload, true)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
var rawRes rawOrder
err = json.Unmarshal(r, &rawRes)
if err != nil {
return
}
if !rawRes.Success {
err = errors.New(string(r))
return
}
orderOid = rawRes.Data.OrderOid
return
}
// AccountHistory is used to get the information about list deposit & withdrawal
// at Kucoin along with other meta data. Coin, Side (type in Kucoin docs.)
// and Status are required parameters. Limit and page may be zeros.
// Example:
// - Coin = KCS
// - Side = DEPOSIT | WITHDRAW
// - Status = FINISHED | CANCEL | PENDING
func (b *Kucoin) AccountHistory(coin, side, status string, limit, page int) (accountHistory AccountHistory, err error) {
if len(coin) < 1 || len(side) < 1 || len(status) < 1 {
return accountHistory, fmt.Errorf("The not all required parameters are presented")
}
payload := map[string]string{}
payload["type"] = side
payload["status"] = status
if limit == 0 {
payload["limit"] = fmt.Sprintf("%v", 1000)
} else {
payload["limit"] = fmt.Sprintf("%v", limit)
}
if page != 0 {
payload["page"] = fmt.Sprintf("%v", page)
}
r, err := b.client.do("GET", fmt.Sprintf(
"account/%s/wallet/records", strings.ToUpper(coin)), payload, true)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
var rawRes rawAccountHistory
err = json.Unmarshal(r, &rawRes)
accountHistory = rawRes.Data
return
}
// ListSpecificDealtOrders is used to get the information about dealt orders for specific symbol at Kucoin along with other meta data.
// Symbol, Side (type in Kucoin docs.) are required parameters. Limit and page may be zeros.
// Example:
// - Symbol = KCS-BTC
// - Side = BUY | SELL
func (b *Kucoin) ListSpecificDealtOrders(symbol, side string, limit, page int) (specificDealtOrders SpecificDealtOrder, err error) {
if len(symbol) < 1 || len(side) < 1 {
return specificDealtOrders, fmt.Errorf("The not all required parameters are presented")
}
payload := map[string]string{}
payload["symbol"] = symbol
payload["type"] = side
if limit == 0 {
payload["limit"] = fmt.Sprintf("%v", 1000)
} else {
payload["limit"] = fmt.Sprintf("%v", limit)
}
if page != 0 {
payload["page"] = fmt.Sprintf("%v", page)
}
r, err := b.client.do("GET", "deal-orders", payload, true)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
var rawRes rawSpecificDealtOrder
err = json.Unmarshal(r, &rawRes)
specificDealtOrders = rawRes.Data
return
}
// ListMergedDealtOrders is used to get the information about dealt orders for
// all symbols at Kucoin along with other meta data.
// All parameters are optional. Timestamp must be in milliseconds from Unix epoch.
func (b *Kucoin) ListMergedDealtOrders(symbol, side string, limit, page int, since, before int64) (mergedDealtOrders MergedDealtOrder, err error) {
payload := map[string]string{}
if len(symbol) > 1 {
payload["symbol"] = symbol
}
if len(side) > 1 {
payload["type"] = side
}
if (limit == 0 || limit > 100) && len(symbol) > 1 {
payload["limit"] = fmt.Sprintf("%v", 100)
} else if (limit == 0 || limit > 20) && len(symbol) < 1 {
payload["limit"] = fmt.Sprintf("%v", 20)
} else {
payload["limit"] = fmt.Sprintf("%v", limit)
}
if page != 0 {
payload["page"] = fmt.Sprintf("%v", page)
}
if since != 0 {
payload["since"] = fmt.Sprintf("%v", since)
}
if before != 0 {
payload["before"] = fmt.Sprintf("%v", before)
}
r, err := b.client.do("GET", "order/dealt", payload, true)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
var rawRes rawMergedDealtOrder
err = json.Unmarshal(r, &rawRes)
mergedDealtOrders = rawRes.Data
return
}
// OrderDetails is used to get the information about orders for specific symbol at Kucoin along with other meta data.
// Symbol, Side (type in Kucoin docs.) are required parameters.
// Limit may be zero, and not greater than 20. Page may be zero and by default is equal to 1.
// Example:
// - Symbol = KCS-BTC
// - Side = BUY | SELL
func (b *Kucoin) OrderDetails(symbol, side, orderOid string, limit, page int) (orderDetails OrderDetails, err error) {
if len(symbol) < 1 || len(side) < 1 || len(orderOid) < 1 {
return orderDetails, fmt.Errorf("The not all required parameters are presented")
}
payload := map[string]string{}
payload["orderOid"] = orderOid
payload["symbol"] = symbol
payload["type"] = side
if limit == 0 {
payload["limit"] = fmt.Sprintf("%v", 20)
} else {
payload["limit"] = fmt.Sprintf("%v", limit)
}
if page == 0 {
payload["page"] = fmt.Sprintf("%v", 1)
} else {
payload["page"] = fmt.Sprintf("%v", page)
}
r, err := b.client.do("GET", "order/detail", payload, true)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
var rawRes rawOrderDetails
err = json.Unmarshal(r, &rawRes)
orderDetails = rawRes.Data
return
}
// CreateWithdrawalApply is used to create withdrawal for specific coin
// at Kucoin along with other meta data.
// coin, address and amount are required parameters.
// Example:
// - coin = KCS
// - address = example_address
// - amount 0.68
// Result:
// - Nothing.
func (b *Kucoin) CreateWithdrawalApply(coin, address string, amount float64) (withdrawalApply Withdrawal, err error) {
if len(coin) < 1 || len(address) < 1 || amount == 0 {
return withdrawalApply, fmt.Errorf("The not all required parameters are presented")
}
payload := map[string]string{}
payload["coin"] = coin
payload["address"] = address
payload["amount"] = fmt.Sprintf("%v", amount)
r, err := b.client.do("POST", fmt.Sprintf(
"account/%s/withdraw/apply", strings.ToUpper(coin)), payload, true)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
var rawRes rawWithdrawal
err = json.Unmarshal(r, &rawRes)
withdrawalApply = rawRes.Data
return
}
// CancelWithdrawal used to cancel withdrawal for specific coin
// at Kucoin along with other meta data.
// coin, txOid are required parameters.
// Example:
// - coin = KCS
// - txOid = example_tx
// Result:
// - Nothing.
func (b *Kucoin) CancelWithdrawal(coin, txOid string) (withdrawal Withdrawal, err error) {
if len(coin) < 1 || len(txOid) < 1 {
return withdrawal, fmt.Errorf("The not all required parameters are presented")
}
payload := map[string]string{}
payload["txOid"] = txOid
r, err := b.client.do("POST", fmt.Sprintf(
"account/%s/withdraw/cancel", strings.ToUpper(coin)), payload, true)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
var rawRes rawWithdrawal
err = json.Unmarshal(r, &rawRes)
withdrawal = rawRes.Data
return
}
// CancelOrder is used to cancel execution of current order at Kucoin along with other meta data.
// Side (type in Kucoin docs.) and order ID are required parameters. Symbol is optional.
func (b *Kucoin) CancelOrder(orderOid, side, symbol string) error {
if len(symbol) < 1 || len(side) < 1 || len(orderOid) < 1 {
return fmt.Errorf("The not all required parameters are presented")
}
payload := map[string]string{}
payload["orderOid"] = orderOid
payload["type"] = side
r, err := b.client.do("POST", fmt.Sprintf("%s/cancel-order", strings.ToUpper(symbol)), payload, true)
if err != nil {
return err
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return err
}
return handleErr(response)
}
// CancelAllOrders is used to cancel execution of all orders at Kucoin along with other meta data.
// Symbol, Side (type in Kucoin docs.) are optional parameters.
func (b *Kucoin) CancelAllOrders(symbol, side string) error {
payload := map[string]string{}
if len(symbol) > 1 {
payload["symbol"] = strings.ToUpper(symbol)
}
if len(side) > 1 {
payload["type"] = side
}
r, err := b.client.do("POST", "order/cancel-all", payload, true)
if err != nil {
return err
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return err
}
return handleErr(response)
}