-
Notifications
You must be signed in to change notification settings - Fork 1
/
private.go
582 lines (448 loc) · 12.7 KB
/
private.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
package gemini
import (
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/claudiocandio/gemini-api/logger"
)
// Past Trades
// Args{"limit_trades": 50, "timestamp": "2021-12-01T15:04:01"}
// limit_trades": 0 -> retrieves all trades
func (api *Api) PastTrades(symbol string, args Args) ([]PastTrade, error) {
const max_limit_tradesAPI int = 500
var maxTrades, limit_trades int = 0, max_limit_tradesAPI
if arg, ok := args["limit_trades"]; ok {
limit_trades = int(arg.(int))
maxTrades = limit_trades
if limit_trades > max_limit_tradesAPI {
limit_trades = max_limit_tradesAPI
}
}
var timestamp int64 = 0 // default
if arg, ok := args["timestamp"]; ok {
// timestamp ms
timestamp = arg.(time.Time).UnixNano() / 1e6
}
logger.Debug("func PastTrades",
fmt.Sprintf("args:%v", args),
fmt.Sprintf("maxTrades:%d", maxTrades),
fmt.Sprintf("limit_trades:%d", limit_trades),
fmt.Sprintf("timestamp:%v", timestamp),
)
url := api.url + past_trades_URI
var ptrade []PastTrade
var pastTrade []PastTrade
readTrades := true
for readTrades {
params := map[string]interface{}{
"request": past_trades_URI,
"nonce": nonce(),
"symbol": symbol,
"limit_trades": limit_trades,
"timestamp": timestamp,
}
logger.Debug("func PastTrades",
fmt.Sprintf("url:%v", url),
fmt.Sprintf("params:%v", params),
)
body, err := api.request("POST", url, params)
if err != nil {
return nil, err
}
if err := json.Unmarshal(body, &ptrade); err != nil {
return nil, err
}
pastTrade = append(pastTrade, ptrade...)
if len(ptrade) > 0 &&
(len(pastTrade) < maxTrades || maxTrades == 0) &&
len(ptrade) == limit_trades {
timestamp = ptrade[0].Timestamp + 1
logger.Debug("func PastTrades: next limit_trades page")
} else {
readTrades = false
logger.Debug("func PastTrades: end limit_trades page")
}
}
// adding TimestampmsT
for i, t := range pastTrade {
pastTrade[i].TimestampmsT = msToTime(t.Timestampms)
}
logger.Debug("func PastTrades: unmarshal",
fmt.Sprintf("pastTrade:%v", pastTrade),
)
return pastTrade, nil
}
// Trade Volume
func (api *Api) TradeVolume() ([][]TradeVolume, error) {
url := api.url + trade_volume_URI
params := map[string]interface{}{
"request": trade_volume_URI,
"nonce": nonce(),
}
logger.Debug("func TradeVolume",
fmt.Sprintf("url:%v", url),
fmt.Sprintf("params:%v", params),
)
var tradeVolume [][]TradeVolume
body, err := api.request("POST", url, params)
if err != nil {
return nil, err
}
if err := json.Unmarshal(body, &tradeVolume); err != nil {
return nil, err
}
logger.Debug("func TradeVolume: unmarshal",
fmt.Sprintf("tradeVolume:%v", tradeVolume),
)
return tradeVolume, nil
}
// Active Orders
func (api *Api) ActiveOrders() ([]Order, error) {
url := api.url + active_orders_URI
params := map[string]interface{}{
"request": active_orders_URI,
"nonce": nonce(),
}
logger.Debug("func ActiveOrders",
fmt.Sprintf("url:%v", url),
fmt.Sprintf("params:%v", params),
)
var order []Order
body, err := api.request("POST", url, params)
if err != nil {
return nil, err
}
if err := json.Unmarshal(body, &order); err != nil {
return nil, err
}
logger.Debug("func ActiveOrders: unmarshal",
fmt.Sprintf("orders:%v", order),
)
return order, nil
}
// Order Status
func (api *Api) OrderStatus(orderId string) (Order, error) {
url := api.url + order_status_URI
params := map[string]interface{}{
"request": order_status_URI,
"nonce": nonce(),
"order_id": orderId,
}
logger.Debug("func OrderStatus",
fmt.Sprintf("url:%v", url),
fmt.Sprintf("params:%v", params),
)
var order Order
body, err := api.request("POST", url, params)
if err != nil {
return order, err
}
if err := json.Unmarshal(body, &order); err != nil {
return order, err
}
logger.Debug("func OrderStatus: unmarshal",
fmt.Sprintf("order:%v", order),
)
return order, nil
}
// New Order
func (api *Api) NewOrder(symbol, clientOrderId string, amount, price float64, side string, options []string) (Order, error) {
url := api.url + new_order_URI
params := map[string]interface{}{
"request": new_order_URI,
"nonce": nonce(),
"client_order_id": clientOrderId,
"symbol": symbol,
"amount": strconv.FormatFloat(amount, 'f', -1, 64),
"price": strconv.FormatFloat(price, 'f', -1, 64),
"side": side,
"type": "exchange limit",
}
if options != nil {
params["options"] = options
}
logger.Debug("func NewOrder",
fmt.Sprintf("url:%v", url),
fmt.Sprintf("params:%v", params),
)
var order Order
body, err := api.request("POST", url, params)
if err != nil {
return order, err
}
if err := json.Unmarshal(body, &order); err != nil {
return order, err
}
order.TimestampmsT = msToTime(order.Timestampms)
logger.Debug("func NewOrder: unmarshal",
fmt.Sprintf("order:%v", order),
)
return order, nil
}
// Cancel Order
func (api *Api) CancelOrder(orderId string) (Order, error) {
url := api.url + cancel_order_URI
params := map[string]interface{}{
"request": cancel_order_URI,
"nonce": nonce(),
"order_id": orderId,
}
logger.Debug("func CancelOrder",
fmt.Sprintf("url:%v", url),
fmt.Sprintf("params:%v", params),
)
var order Order
body, err := api.request("POST", url, params)
if err != nil {
return order, err
}
if err := json.Unmarshal(body, &order); err != nil {
return order, err
}
logger.Debug("func CancelOrder: unmarshal",
fmt.Sprintf("order:%v", order),
)
return order, nil
}
// Cancel All
// This will cancel all outstanding orders created by all sessions owned
// by this account, including interactive orders placed through the UI.
// Note that this cancels orders that were not placed using this API key.
func (api *Api) CancelAll() (CancelResult, error) {
url := api.url + cancel_all_URI
params := map[string]interface{}{
"request": cancel_all_URI,
"nonce": nonce(),
}
logger.Debug("func CancelAll",
fmt.Sprintf("url:%v", url),
fmt.Sprintf("params:%v", params),
)
var cancelResult CancelResult
body, err := api.request("POST", url, params)
if err != nil {
return cancelResult, err
}
if err := json.Unmarshal(body, &cancelResult); err != nil {
return cancelResult, err
}
logger.Debug("func CancelAll: unmarshal",
fmt.Sprintf("cancelResult:%v", cancelResult),
)
return cancelResult, nil
}
// This will cancel all orders opened by this session.
// This will have the same effect as heartbeat expiration if "Require Heartbeat" is selected for the session.
func (api *Api) CancelSession() (CancelResult, error) {
url := api.url + cancel_session_URI
params := map[string]interface{}{
"request": cancel_session_URI,
"nonce": nonce(),
}
logger.Debug("func CancelSession",
fmt.Sprintf("url:%v", url),
fmt.Sprintf("params:%v", params),
)
var cancelResult CancelResult
body, err := api.request("POST", url, params)
if err != nil {
return cancelResult, err
}
if err := json.Unmarshal(body, &cancelResult); err != nil {
return cancelResult, err
}
logger.Debug("func CancelSession: unmarshal",
fmt.Sprintf("cancelResult:%v", cancelResult),
)
return cancelResult, nil
}
// Heartbeat
// This will prevent a session from timing out and canceling orders if the
// require heartbeat flag has been set. Note that this is only required if
// no other private API requests have been made. The arrival of any message
// resets the heartbeat timer.
func (api *Api) Heartbeat() (GenericResponse, error) {
url := api.url + heartbeat_URI
params := map[string]interface{}{
"request": heartbeat_URI,
"nonce": nonce(),
}
logger.Debug("func Heartbeat",
fmt.Sprintf("url:%v", url),
fmt.Sprintf("params:%v", params),
)
var genericResponse GenericResponse
body, err := api.request("POST", url, params)
if err != nil {
return genericResponse, err
}
if err := json.Unmarshal(body, &genericResponse); err != nil {
return genericResponse, err
}
logger.Debug("func Heartbeat: unmarshal",
fmt.Sprintf("genericResponse:%v", genericResponse),
)
return genericResponse, nil
}
// Balances
func (api *Api) Balances() ([]FundBalance, error) {
url := api.url + balances_URI
params := map[string]interface{}{
"request": balances_URI,
"nonce": nonce(),
}
logger.Debug("func Balances",
fmt.Sprintf("url:%v", url),
fmt.Sprintf("params:%v", params),
)
var fundBalance []FundBalance
body, err := api.request("POST", url, params)
if err != nil {
return nil, err
}
if err := json.Unmarshal(body, &fundBalance); err != nil {
return nil, err
}
logger.Debug("func Balances: unmarshal",
fmt.Sprintf("fundBalance:%v", fundBalance),
)
return fundBalance, nil
}
// Account
func (api *Api) AccountDetail() (AccountDetail, error) {
url := api.url + account_URI
params := map[string]interface{}{
"request": account_URI,
"nonce": nonce(),
}
logger.Debug("func AccountDetail",
fmt.Sprintf("url:%v", url),
fmt.Sprintf("params:%v", params),
)
var accountDetail AccountDetail
body, err := api.request("POST", url, params)
if err != nil {
return accountDetail, err
}
if err := json.Unmarshal(body, &accountDetail); err != nil {
return accountDetail, err
}
accountDetail.Account.CreatedT = msToTime(accountDetail.Account.Created)
logger.Debug("func AccountDetail: unmarshal",
fmt.Sprintf("accountDetail:%v", accountDetail),
)
return accountDetail, nil
}
// New Deposit Address
// currency can be bitcoin, ethereum, bitcoincash, litecoin, zcash, or filecoin
func (api *Api) NewDepositAddress(currency, label string) (NewDepositAddress, error) {
path := new_deposit_address_URI + currency + "/newAddress"
url := api.url + path
params := map[string]interface{}{
"request": path,
"nonce": nonce(),
"label": label,
}
logger.Debug("func NewDepositAddress",
fmt.Sprintf("url:%v", url),
fmt.Sprintf("params:%v", params),
)
var newDepositAddress NewDepositAddress
body, err := api.request("POST", url, params)
if err != nil {
return newDepositAddress, err
}
if err := json.Unmarshal(body, &newDepositAddress); err != nil {
return newDepositAddress, err
}
logger.Debug("func NewDepositAddress: unmarshal",
fmt.Sprintf("newDepositAddress:%v", newDepositAddress),
)
return newDepositAddress, nil
}
// Get Deposit Addresseses
// currency can be bitcoin, ethereum, bitcoincash, litecoin, zcash, filecoin
func (api *Api) DepositAddresses(currency string) ([]DepositAddresses, error) {
path := deposit_addresses_URI + currency
url := api.url + path
params := map[string]interface{}{
"request": path,
"nonce": nonce(),
}
logger.Debug("func DepositAddresses",
fmt.Sprintf("url:%v", url),
fmt.Sprintf("params:%v", params),
)
var depositAddresses []DepositAddresses
body, err := api.request("POST", url, params)
if err != nil {
return depositAddresses, err
}
if err := json.Unmarshal(body, &depositAddresses); err != nil {
return depositAddresses, err
}
// adding TimestampmsT
for i, address := range depositAddresses {
depositAddresses[i].TimestampmsT = msToTime(address.Timestamp)
}
logger.Debug("func DepositAddresses: unmarshal",
fmt.Sprintf("depositAddresses:%v", depositAddresses),
)
return depositAddresses, nil
}
// Withdraw Crypto Funds
// currency can be btc or eth
func (api *Api) WithdrawFunds(currency, address string, amount float64) (WithdrawFundsResult, error) {
path := withdraw_funds_URI + currency
url := api.url + path
amountstr := fmt.Sprintf("%f", amount)
params := map[string]interface{}{
"request": path,
"nonce": nonce(),
"address": address,
"amount": amountstr,
}
logger.Debug("func WithdrawFunds",
fmt.Sprintf("url:%v", url),
fmt.Sprintf("params:%v", params),
)
var withdrawFundsResult WithdrawFundsResult
body, err := api.request("POST", url, params)
if err != nil {
return withdrawFundsResult, err
}
if err := json.Unmarshal(body, &withdrawFundsResult); err != nil {
return withdrawFundsResult, err
}
logger.Debug("func WithdrawFunds: unmarshal",
fmt.Sprintf("withdrawFundsResult:%v", withdrawFundsResult),
)
return withdrawFundsResult, nil
}
// Args{"timestamp": "2021-12-01T15:04:01", "limit_transfers": 20,"show_completed_deposit_advances": false}
func (api *Api) Transfers(args Args) ([]Transfer, error) {
url := api.url + transfers_URI
args["request"] = transfers_URI
args["nonce"] = nonce()
logger.Debug("func Transfers",
fmt.Sprintf("url:%v", url),
fmt.Sprintf("args:%v", args),
)
var transfer []Transfer
body, err := api.request("POST", url, args)
if err != nil {
return nil, err
}
if err := json.Unmarshal(body, &transfer); err != nil {
return nil, err
}
// adding TimestampmsT
for i, tr := range transfer {
transfer[i].TimestampmsT = msToTime(tr.Timestampms)
}
logger.Debug("func Transfers: unmarshal",
fmt.Sprintf("transfer:%v", transfer),
)
return transfer, nil
}