forked from toorop/go-bittrex
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bittrex.go
550 lines (505 loc) · 15.4 KB
/
bittrex.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
// Package Bittrex is an implementation of the Biitrex API in Golang.
package bittrex
import (
"encoding/json"
"errors"
"fmt"
"math/rand"
"net/http"
"strconv"
"strings"
"time"
)
const (
API_BASE = "https://bittrex.com/api/" // Bittrex API endpoint
API_VERSION = "v1.1"
WS_BASE = "socket.bittrex.com" // Bittrex WS API endpoint
WS_HUB = "CoreHub" // SignalR main hub
)
// New returns an instantiated bittrex struct
func New(apiKey, apiSecret string) *Bittrex {
client := NewClient(apiKey, apiSecret)
return &Bittrex{client}
}
// NewWithCustomHttpClient returns an instantiated bittrex struct with custom http client
func NewWithCustomHttpClient(apiKey, apiSecret string, httpClient *http.Client) *Bittrex {
client := NewClientWithCustomHttpConfig(apiKey, apiSecret, httpClient)
return &Bittrex{client}
}
// NewWithCustomTimeout returns an instantiated bittrex struct with custom timeout
func NewWithCustomTimeout(apiKey, apiSecret string, timeout time.Duration) *Bittrex {
client := NewClientWithCustomTimeout(apiKey, apiSecret, timeout)
return &Bittrex{client}
}
// handleErr gets JSON response from Bittrex API en deal with error
func handleErr(r jsonResponse) error {
if !r.Success {
return errors.New(r.Message)
}
return nil
}
// bittrex represent a bittrex client
type Bittrex struct {
client *client
}
// GetDistribution is used to get the distribution.
func (b *Bittrex) GetDistribution(market string) (distribution Distribution, err error) {
r, err := b.client.do("GET", "https://bittrex.com/Api/v2.0/pub/currency/GetBalanceDistribution?currencyName="+strings.ToUpper(market), "", false)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(response.Result, &distribution)
return
}
// GetCurrencies is used to get all supported currencies at Bittrex along with other meta data.
func (b *Bittrex) GetCurrencies() (currencies []Currency, err error) {
r, err := b.client.do("GET", "public/getcurrencies", "", false)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(response.Result, ¤cies)
return
}
// GetMarkets is used to get the open and available trading markets at Bittrex along with other meta data.
func (b *Bittrex) GetMarkets() (markets []Market, err error) {
r, err := b.client.do("GET", "public/getmarkets", "", false)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(response.Result, &markets)
return
}
// GetTicker is used to get the current ticker values for a market.
func (b *Bittrex) GetTicker(market string) (ticker Ticker, err error) {
r, err := b.client.do("GET", "public/getticker?market="+strings.ToUpper(market), "", false)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(response.Result, &ticker)
return
}
// GetMarketSummaries is used to get the last 24 hour summary of all active exchanges
func (b *Bittrex) GetMarketSummaries() (marketSummaries []MarketSummary, err error) {
r, err := b.client.do("GET", "public/getmarketsummaries", "", false)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(response.Result, &marketSummaries)
return
}
// GetMarketSummary is used to get the last 24 hour summary for a given market
func (b *Bittrex) GetMarketSummary(market string) (marketSummary []MarketSummary, err error) {
r, err := b.client.do("GET", fmt.Sprintf("public/getmarketsummary?market=%s", strings.ToUpper(market)), "", false)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(response.Result, &marketSummary)
return
}
// GetOrderBook is used to get retrieve the orderbook for a given market
// market: a string literal for the market (ex: BTC-LTC)
// cat: buy, sell or both to identify the type of orderbook to return.
// depth: how deep of an order book to retrieve. Max is 100
func (b *Bittrex) GetOrderBook(market, cat string, depth int) (orderBook OrderBook, err error) {
if cat != "buy" && cat != "sell" && cat != "both" {
cat = "both"
}
if depth > 100 {
depth = 100
}
if depth < 1 {
depth = 1
}
r, err := b.client.do("GET", fmt.Sprintf("public/getorderbook?market=%s&type=%s&depth=%d", strings.ToUpper(market), cat, depth), "", false)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
if cat == "buy" {
err = json.Unmarshal(response.Result, &orderBook.Buy)
} else if cat == "sell" {
err = json.Unmarshal(response.Result, &orderBook.Sell)
} else {
err = json.Unmarshal(response.Result, &orderBook)
}
return
}
// GetOrderBookBuySell is used to get retrieve the buy or sell side of an orderbook for a given market
// market: a string literal for the market (ex: BTC-LTC)
// cat: buy or sell to identify the type of orderbook to return.
// depth: how deep of an order book to retrieve. Max is 100
func (b *Bittrex) GetOrderBookBuySell(market, cat string, depth int) (orderb []Orderb, err error) {
if cat != "buy" && cat != "sell" {
cat = "buy"
}
if depth > 100 {
depth = 100
}
if depth < 1 {
depth = 1
}
r, err := b.client.do("GET", fmt.Sprintf("public/getorderbook?market=%s&type=%s&depth=%d", strings.ToUpper(market), cat, depth), "", false)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(response.Result, &orderb)
return
}
// GetMarketHistory is used to retrieve the latest trades that have occured for a specific market.
// market a string literal for the market (ex: BTC-LTC)
func (b *Bittrex) GetMarketHistory(market string) (trades []Trade, err error) {
r, err := b.client.do("GET", fmt.Sprintf("public/getmarkethistory?market=%s", strings.ToUpper(market)), "", false)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(response.Result, &trades)
return
}
// Market
// BuyLimit is used to place a limited buy order in a specific market.
func (b *Bittrex) BuyLimit(market string, quantity, rate float64) (uuid string, err error) {
r, err := b.client.do("GET", "market/buylimit?market="+market+"&quantity="+strconv.FormatFloat(quantity, 'f', 8, 64)+"&rate="+strconv.FormatFloat(rate, 'f', 8, 64), "", true)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
var u Uuid
err = json.Unmarshal(response.Result, &u)
uuid = u.Id
return
}
// BuyMarket is used to place a market buy order in a spacific market.
func (b *Bittrex) BuyMarket(market string, quantity float64) (uuid string, err error) {
r, err := b.client.do("GET", "market/buymarket?market="+market+"&quantity="+strconv.FormatFloat(quantity, 'f', 8, 64), "", true)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
var u Uuid
err = json.Unmarshal(response.Result, &u)
uuid = u.Id
return
}
// SellLimit is used to place a limited sell order in a specific market.
func (b *Bittrex) SellLimit(market string, quantity, rate float64) (uuid string, err error) {
r, err := b.client.do("GET", "market/selllimit?market="+market+"&quantity="+strconv.FormatFloat(quantity, 'f', 8, 64)+"&rate="+strconv.FormatFloat(rate, 'f', 8, 64), "", true)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
var u Uuid
err = json.Unmarshal(response.Result, &u)
uuid = u.Id
return
}
// SellMarket is used to place a market sell order in a specific market.
func (b *Bittrex) SellMarket(market string, quantity float64) (uuid string, err error) {
r, err := b.client.do("GET", "market/sellmarket?market="+market+"&quantity="+strconv.FormatFloat(quantity, 'f', 8, 64), "", true)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
var u Uuid
err = json.Unmarshal(response.Result, &u)
uuid = u.Id
return
}
// CancelOrder is used to cancel a buy or sell order.
func (b *Bittrex) CancelOrder(orderID string) (err error) {
r, err := b.client.do("GET", "market/cancel?uuid="+orderID, "", true)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
err = handleErr(response)
return
}
// GetOpenOrders returns orders that you currently have opened.
// If market is set to "all", GetOpenOrders return all orders
// If market is set to a specific order, GetOpenOrders return orders for this market
func (b *Bittrex) GetOpenOrders(market string) (openOrders []Order, err error) {
ressource := "market/getopenorders"
if market != "all" {
ressource += "?market=" + strings.ToUpper(market)
}
r, err := b.client.do("GET", ressource, "", true)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = json.Unmarshal(r, &response); err != nil {
return
}
err = json.Unmarshal(response.Result, &openOrders)
return
}
// Account
// GetBalances is used to retrieve all balances from your account
func (b *Bittrex) GetBalances() (balances []Balance, err error) {
r, err := b.client.do("GET", "account/getbalances", "", true)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(response.Result, &balances)
return
}
// Getbalance is used to retrieve the balance from your account for a specific currency.
// currency: a string literal for the currency (ex: LTC)
func (b *Bittrex) GetBalance(currency string) (balance Balance, err error) {
r, err := b.client.do("GET", "account/getbalance?currency="+strings.ToUpper(currency), "", true)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(response.Result, &balance)
return
}
// GetDepositAddress is sed to generate or retrieve an address for a specific currency.
// currency a string literal for the currency (ie. BTC)
func (b *Bittrex) GetDepositAddress(currency string) (address Address, err error) {
r, err := b.client.do("GET", "account/getdepositaddress?currency="+strings.ToUpper(currency), "", true)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(response.Result, &address)
return
}
// Withdraw is used to withdraw funds from your account.
// address string the address where to send the funds.
// currency string literal for the currency (ie. BTC)
// quantity float the quantity of coins to withdraw
func (b *Bittrex) Withdraw(address, currency string, quantity float64) (withdrawUuid string, err error) {
r, err := b.client.do("GET", "account/withdraw?currency="+strings.ToUpper(currency)+"&quantity="+strconv.FormatFloat(quantity, 'f', 8, 64)+"&address="+address, "", true)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
var u Uuid
err = json.Unmarshal(response.Result, &u)
withdrawUuid = u.Id
return
}
// GetOrderHistory used to retrieve your order history.
// market string literal for the market (ie. BTC-LTC). If set to "all", will return for all market
func (b *Bittrex) GetOrderHistory(market string) (orders []Order, err error) {
ressource := "account/getorderhistory"
if market != "all" {
ressource += "?market=" + market
}
r, err := b.client.do("GET", ressource, "", true)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(response.Result, &orders)
return
}
// GetWithdrawalHistory is used to retrieve your withdrawal history
// currency string a string literal for the currency (ie. BTC). If set to "all", will return for all currencies
func (b *Bittrex) GetWithdrawalHistory(currency string) (withdrawals []Withdrawal, err error) {
ressource := "account/getwithdrawalhistory"
if currency != "all" {
ressource += "currency=" + currency
}
r, err := b.client.do("GET", ressource, "", true)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(response.Result, &withdrawals)
return
}
// GetDepositHistory is used to retrieve your deposit history
// currency string a string literal for the currency (ie. BTC). If set to "all", will return for all currencies
func (b *Bittrex) GetDepositHistory(currency string) (deposits []Deposit, err error) {
ressource := "account/getdeposithistory"
if currency != "all" {
ressource += "currency=" + currency
}
r, err := b.client.do("GET", ressource, "", true)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(response.Result, &deposits)
return
}
func (b *Bittrex) GetOrder(order_uuid string) (order Order2, err error) {
ressource := "account/getorder?uuid=" + order_uuid
r, err := b.client.do("GET", ressource, "", true)
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = json.Unmarshal(r, &response); err != nil {
return
}
err = json.Unmarshal(response.Result, &order)
return
}
// GetTicks is used to get ticks history values for a market.
func (b *Bittrex) GetTicks(market string, interval string) ([]Candle, error) {
_, ok := CANDLE_INTERVALS[interval]
if !ok {
return nil, errors.New("wrong interval")
}
endpoint := fmt.Sprintf(
"https://bittrex.com/Api/v2.0/pub/market/GetTicks?tickInterval=%s&marketName=%s&_=%d",
interval, strings.ToUpper(market), rand.Int(),
)
r, err := b.client.do("GET", endpoint, "", false)
if err != nil {
return nil, fmt.Errorf("could not get market ticks: %v", err)
}
var response jsonResponse
if err := json.Unmarshal(r, &response); err != nil {
return nil, err
}
if err := handleErr(response); err != nil {
return nil, err
}
var candles []Candle
if err := json.Unmarshal(response.Result, &candles); err != nil {
return nil, fmt.Errorf("could not unmarshal candles: %v", err)
}
return candles, nil
}