forked from ccxt/ccxt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitfinex2.js
2626 lines (2572 loc) · 117 KB
/
bitfinex2.js
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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
// ---------------------------------------------------------------------------
const Exchange = require ('./base/Exchange');
const { ExchangeError, InvalidAddress, ArgumentsRequired, InsufficientFunds, AuthenticationError, OrderNotFound, InvalidOrder, BadRequest, InvalidNonce, BadSymbol, OnMaintenance, NotSupported, PermissionDenied, ExchangeNotAvailable } = require ('./base/errors');
const Precise = require ('./base/Precise');
const { SIGNIFICANT_DIGITS, DECIMAL_PLACES, TRUNCATE, ROUND } = require ('./base/functions/number');
// ---------------------------------------------------------------------------
module.exports = class bitfinex2 extends Exchange {
describe () {
return this.deepExtend (super.describe (), {
'id': 'bitfinex2',
'name': 'Bitfinex',
'countries': [ 'VG' ],
'version': 'v2',
'certified': false,
'pro': false,
// new metainfo interface
'has': {
'CORS': undefined,
'spot': true,
'margin': undefined, // has but unimplemented
'swap': undefined, // has but unimplemented
'future': undefined,
'option': undefined,
'cancelAllOrders': true,
'cancelOrder': true,
'createDepositAddress': true,
'createLimitOrder': true,
'createMarketOrder': true,
'createOrder': true,
'createStopLimitOrder': true,
'createStopMarketOrder': true,
'createStopOrder': true,
'editOrder': false,
'fetchBalance': true,
'fetchClosedOrder': true,
'fetchClosedOrders': true,
'fetchCurrencies': true,
'fetchDepositAddress': true,
'fetchIndexOHLCV': false,
'fetchLedger': true,
'fetchMarginMode': false,
'fetchMarkOHLCV': false,
'fetchMyTrades': true,
'fetchOHLCV': true,
'fetchOpenOrder': true,
'fetchOpenOrders': true,
'fetchOrder': true,
'fetchOrderTrades': true,
'fetchPositionMode': false,
'fetchStatus': true,
'fetchTickers': true,
'fetchTime': false,
'fetchTradingFee': false,
'fetchTradingFees': true,
'fetchTransactionFees': undefined,
'fetchTransactions': true,
'withdraw': true,
},
'timeframes': {
'1m': '1m',
'5m': '5m',
'15m': '15m',
'30m': '30m',
'1h': '1h',
'3h': '3h',
'4h': '4h',
'6h': '6h',
'12h': '12h',
'1d': '1D',
'1w': '7D',
'2w': '14D',
'1M': '1M',
},
// cheapest endpoint is 240 requests per minute => ~ 4 requests per second => ( 1000ms / 4 ) = 250ms between requests on average
'rateLimit': 250,
'urls': {
'logo': 'https://user-images.githubusercontent.com/1294454/27766244-e328a50c-5ed2-11e7-947b-041416579bb3.jpg',
'api': {
'v1': 'https://api.bitfinex.com',
'public': 'https://api-pub.bitfinex.com',
'private': 'https://api.bitfinex.com',
},
'www': 'https://www.bitfinex.com',
'doc': [
'https://docs.bitfinex.com/v2/docs/',
'https://github.com/bitfinexcom/bitfinex-api-node',
],
'fees': 'https://www.bitfinex.com/fees',
},
'api': {
'public': {
'get': {
'conf/{config}': 2.66, // 90 requests a minute
'conf/pub:{action}:{object}': 2.66,
'conf/pub:{action}:{object}:{detail}': 2.66,
'conf/pub:map:{object}': 2.66,
'conf/pub:map:{object}:{detail}': 2.66,
'conf/pub:map:currency:{detail}': 2.66,
'conf/pub:map:currency:sym': 2.66, // maps symbols to their API symbols, BAB > BCH
'conf/pub:map:currency:label': 2.66, // verbose friendly names, BNT > Bancor
'conf/pub:map:currency:unit': 2.66, // maps symbols to unit of measure where applicable
'conf/pub:map:currency:undl': 2.66, // maps derivatives symbols to their underlying currency
'conf/pub:map:currency:pool': 2.66, // maps symbols to underlying network/protocol they operate on
'conf/pub:map:currency:explorer': 2.66, // maps symbols to their recognised block explorer URLs
'conf/pub:map:currency:tx:fee': 2.66, // maps currencies to their withdrawal fees https://github.com/ccxt/ccxt/issues/7745
'conf/pub:map:tx:method': 2.66,
'conf/pub:list:{object}': 2.66,
'conf/pub:list:{object}:{detail}': 2.66,
'conf/pub:list:currency': 2.66,
'conf/pub:list:pair:exchange': 2.66,
'conf/pub:list:pair:margin': 2.66,
'conf/pub:list:pair:futures': 2.66,
'conf/pub:list:competitions': 2.66,
'conf/pub:info:{object}': 2.66,
'conf/pub:info:{object}:{detail}': 2.66,
'conf/pub:info:pair': 2.66,
'conf/pub:info:pair:futures': 2.66,
'conf/pub:info:tx:status': 2.66, // [ deposit, withdrawal ] statuses 1 = active, 0 = maintenance
'conf/pub:fees': 2.66,
'platform/status': 8, // 30 requests per minute = 0.5 requests per second => ( 1000ms / rateLimit ) / 0.5 = 8
'tickers': 2.66, // 90 requests a minute = 1.5 requests per second => ( 1000 / rateLimit ) / 1.5 = 2.666666666
'ticker/{symbol}': 2.66,
'tickers/hist': 2.66,
'trades/{symbol}/hist': 2.66,
'book/{symbol}/{precision}': 1, // 240 requests a minute
'book/{symbol}/P0': 1,
'book/{symbol}/P1': 1,
'book/{symbol}/P2': 1,
'book/{symbol}/P3': 1,
'book/{symbol}/R0': 1,
'stats1/{key}:{size}:{symbol}:{side}/{section}': 2.66,
'stats1/{key}:{size}:{symbol}:{side}/last': 2.66,
'stats1/{key}:{size}:{symbol}:{side}/hist': 2.66,
'stats1/{key}:{size}:{symbol}/{section}': 2.66,
'stats1/{key}:{size}:{symbol}/last': 2.66,
'stats1/{key}:{size}:{symbol}/hist': 2.66,
'stats1/{key}:{size}:{symbol}:long/last': 2.66,
'stats1/{key}:{size}:{symbol}:long/hist': 2.66,
'stats1/{key}:{size}:{symbol}:short/last': 2.66,
'stats1/{key}:{size}:{symbol}:short/hist': 2.66,
'candles/trade:{timeframe}:{symbol}:{period}/{section}': 2.66,
'candles/trade:{timeframe}:{symbol}/{section}': 2.66,
'candles/trade:{timeframe}:{symbol}/last': 2.66,
'candles/trade:{timeframe}:{symbol}/hist': 2.66,
'status/{type}': 2.66,
'status/deriv': 2.66,
'liquidations/hist': 80, // 3 requests a minute = 0.05 requests a second => ( 1000ms / rateLimit ) / 0.05 = 80
'rankings/{key}:{timeframe}:{symbol}/{section}': 2.66,
'rankings/{key}:{timeframe}:{symbol}/hist': 2.66,
'pulse/hist': 2.66,
'pulse/profile/{nickname}': 2.66,
'funding/stats/{symbol}/hist': 10, // ratelimit not in docs
},
'post': {
'calc/trade/avg': 2.66,
'calc/fx': 2.66,
},
},
'private': {
'post': {
// 'auth/r/orders/{symbol}/new', // outdated
// 'auth/r/stats/perf:{timeframe}/hist', // outdated
'auth/r/wallets': 2.66,
'auth/r/wallets/hist': 2.66,
'auth/r/orders': 2.66,
'auth/r/orders/{symbol}': 2.66,
'auth/w/order/submit': 2.66,
'auth/w/order/update': 2.66,
'auth/w/order/cancel': 2.66,
'auth/w/order/multi': 2.66,
'auth/w/order/cancel/multi': 2.66,
'auth/r/orders/{symbol}/hist': 2.66,
'auth/r/orders/hist': 2.66,
'auth/r/order/{symbol}:{id}/trades': 2.66,
'auth/r/trades/{symbol}/hist': 2.66,
'auth/r/trades/hist': 2.66,
'auth/r/ledgers/{currency}/hist': 2.66,
'auth/r/ledgers/hist': 2.66,
'auth/r/info/margin/{key}': 2.66,
'auth/r/info/margin/base': 2.66,
'auth/r/info/margin/sym_all': 2.66,
'auth/r/positions': 2.66,
'auth/w/position/claim': 2.66,
'auth/w/position/increase:': 2.66,
'auth/r/position/increase/info': 2.66,
'auth/r/positions/hist': 2.66,
'auth/r/positions/audit': 2.66,
'auth/r/positions/snap': 2.66,
'auth/w/deriv/collateral/set': 2.66,
'auth/w/deriv/collateral/limits': 2.66,
'auth/r/funding/offers': 2.66,
'auth/r/funding/offers/{symbol}': 2.66,
'auth/w/funding/offer/submit': 2.66,
'auth/w/funding/offer/cancel': 2.66,
'auth/w/funding/offer/cancel/all': 2.66,
'auth/w/funding/close': 2.66,
'auth/w/funding/auto': 2.66,
'auth/w/funding/keep': 2.66,
'auth/r/funding/offers/{symbol}/hist': 2.66,
'auth/r/funding/offers/hist': 2.66,
'auth/r/funding/loans': 2.66,
'auth/r/funding/loans/hist': 2.66,
'auth/r/funding/loans/{symbol}': 2.66,
'auth/r/funding/loans/{symbol}/hist': 2.66,
'auth/r/funding/credits': 2.66,
'auth/r/funding/credits/hist': 2.66,
'auth/r/funding/credits/{symbol}': 2.66,
'auth/r/funding/credits/{symbol}/hist': 2.66,
'auth/r/funding/trades/{symbol}/hist': 2.66,
'auth/r/funding/trades/hist': 2.66,
'auth/r/info/funding/{key}': 2.66,
'auth/r/info/user': 2.66,
'auth/r/summary': 2.66,
'auth/r/logins/hist': 2.66,
'auth/r/permissions': 2.66,
'auth/w/token': 2.66,
'auth/r/audit/hist': 2.66,
'auth/w/transfer': 2.66, // ratelimit not in docs...
'auth/w/deposit/address': 24, // 10 requests a minute = 0.166 requests per second => ( 1000ms / rateLimit ) / 0.166 = 24
'auth/w/deposit/invoice': 24, // ratelimit not in docs
'auth/w/withdraw': 24, // ratelimit not in docs
'auth/r/movements/{currency}/hist': 2.66,
'auth/r/movements/hist': 2.66,
'auth/r/alerts': 5.33, // 45 requests a minute = 0.75 requests per second => ( 1000ms / rateLimit ) / 0.75 => 5.33
'auth/w/alert/set': 2.66,
'auth/w/alert/price:{symbol}:{price}/del': 2.66,
'auth/w/alert/{type}:{symbol}:{price}/del': 2.66,
'auth/calc/order/avail': 2.66,
'auth/w/settings/set': 2.66,
'auth/r/settings': 2.66,
'auth/w/settings/del': 2.66,
'auth/r/pulse/hist': 2.66,
'auth/w/pulse/add': 16, // 15 requests a minute = 0.25 requests per second => ( 1000ms / rateLimit ) / 0.25 => 16
'auth/w/pulse/del': 2.66,
},
},
},
'fees': {
'trading': {
'feeSide': 'get',
'percentage': true,
'tierBased': true,
'maker': this.parseNumber ('0.001'),
'taker': this.parseNumber ('0.002'),
'tiers': {
'taker': [
[ this.parseNumber ('0'), this.parseNumber ('0.002') ],
[ this.parseNumber ('500000'), this.parseNumber ('0.002') ],
[ this.parseNumber ('1000000'), this.parseNumber ('0.002') ],
[ this.parseNumber ('2500000'), this.parseNumber ('0.002') ],
[ this.parseNumber ('5000000'), this.parseNumber ('0.002') ],
[ this.parseNumber ('7500000'), this.parseNumber ('0.002') ],
[ this.parseNumber ('10000000'), this.parseNumber ('0.0018') ],
[ this.parseNumber ('15000000'), this.parseNumber ('0.0016') ],
[ this.parseNumber ('20000000'), this.parseNumber ('0.0014') ],
[ this.parseNumber ('25000000'), this.parseNumber ('0.0012') ],
[ this.parseNumber ('30000000'), this.parseNumber ('0.001') ],
],
'maker': [
[ this.parseNumber ('0'), this.parseNumber ('0.001') ],
[ this.parseNumber ('500000'), this.parseNumber ('0.0008') ],
[ this.parseNumber ('1000000'), this.parseNumber ('0.0006') ],
[ this.parseNumber ('2500000'), this.parseNumber ('0.0004') ],
[ this.parseNumber ('5000000'), this.parseNumber ('0.0002') ],
[ this.parseNumber ('7500000'), this.parseNumber ('0') ],
[ this.parseNumber ('10000000'), this.parseNumber ('0') ],
[ this.parseNumber ('15000000'), this.parseNumber ('0') ],
[ this.parseNumber ('20000000'), this.parseNumber ('0') ],
[ this.parseNumber ('25000000'), this.parseNumber ('0') ],
[ this.parseNumber ('30000000'), this.parseNumber ('0') ],
],
},
},
'funding': {
'withdraw': {},
},
},
'precisionMode': SIGNIFICANT_DIGITS,
'options': {
'precision': 'R0', // P0, P1, P2, P3, P4, R0
// convert 'EXCHANGE MARKET' to lowercase 'market'
// convert 'EXCHANGE LIMIT' to lowercase 'limit'
// everything else remains uppercase
'exchangeTypes': {
// 'MARKET': undefined,
'EXCHANGE MARKET': 'market',
// 'LIMIT': undefined,
'EXCHANGE LIMIT': 'limit',
// 'STOP': undefined,
'EXCHANGE STOP': 'market',
// 'TRAILING STOP': undefined,
// 'EXCHANGE TRAILING STOP': undefined,
// 'FOK': undefined,
'EXCHANGE FOK': 'limit',
// 'STOP LIMIT': undefined,
'EXCHANGE STOP LIMIT': 'limit',
// 'IOC': undefined,
'EXCHANGE IOC': 'limit',
},
// convert 'market' to 'EXCHANGE MARKET'
// convert 'limit' 'EXCHANGE LIMIT'
// everything else remains as is
'orderTypes': {
'market': 'EXCHANGE MARKET',
'limit': 'EXCHANGE LIMIT',
},
'fiat': {
'USD': 'USD',
'EUR': 'EUR',
'JPY': 'JPY',
'GBP': 'GBP',
'CHN': 'CHN',
},
// actually the correct names unlike the v1
// we don't want to extend this with accountsByType in v1
'v2AccountsByType': {
'spot': 'exchange',
'exchange': 'exchange',
'funding': 'funding',
'margin': 'margin',
'derivatives': 'margin',
'future': 'margin',
},
},
'exceptions': {
'exact': {
'10001': PermissionDenied, // api_key: permission invalid (#10001)
'10020': BadRequest,
'10100': AuthenticationError,
'10114': InvalidNonce,
'20060': OnMaintenance,
// {"code":503,"error":"temporarily_unavailable","error_description":"Sorry, the service is temporarily unavailable. See https://www.bitfinex.com/ for more info."}
'temporarily_unavailable': ExchangeNotAvailable,
},
'broad': {
'address': InvalidAddress,
'available balance is only': InsufficientFunds,
'not enough exchange balance': InsufficientFunds,
'Order not found': OrderNotFound,
'symbol: invalid': BadSymbol,
'Invalid order': InvalidOrder,
},
},
'commonCurrencies': {
'UST': 'USDT',
'EUTF0': 'EURT',
'USTF0': 'USDT',
'ALG': 'ALGO', // https://github.com/ccxt/ccxt/issues/6034
'AMP': 'AMPL',
'ATO': 'ATOM', // https://github.com/ccxt/ccxt/issues/5118
'BCHABC': 'XEC',
'BCHN': 'BCH',
'DAT': 'DATA',
'DOG': 'MDOGE',
'DSH': 'DASH',
'EDO': 'PNT',
'EUS': 'EURS',
'EUT': 'EURT',
'IDX': 'ID',
'IOT': 'IOTA',
'IQX': 'IQ',
'LUNA': 'LUNC',
'LUNA2': 'LUNA',
'MNA': 'MANA',
'ORS': 'ORS Group', // conflict with Origin Sport #3230
'PAS': 'PASS',
'QSH': 'QASH',
'QTM': 'QTUM',
'RBT': 'RBTC',
'SNG': 'SNGLS',
'STJ': 'STORJ',
'TERRAUST': 'USTC',
'TSD': 'TUSD',
'YGG': 'YEED', // conflict with Yield Guild Games
'YYW': 'YOYOW',
'UDC': 'USDC',
'VSY': 'VSYS',
'WAX': 'WAXP',
'XCH': 'XCHF',
'ZBT': 'ZB',
},
});
}
isFiat (code) {
return (code in this.options['fiat']);
}
getCurrencyId (code) {
return 'f' + code;
}
getCurrencyName (code) {
// temporary fix for transpiler recognition, even though this is in parent class
if (code in this.options['currencyNames']) {
return this.options['currencyNames'][code];
}
throw new NotSupported (this.id + ' ' + code + ' not supported for withdrawal');
}
amountToPrecision (symbol, amount) {
// https://docs.bitfinex.com/docs/introduction#amount-precision
// The amount field allows up to 8 decimals.
// Anything exceeding this will be rounded to the 8th decimal.
symbol = this.safeSymbol (symbol);
return this.decimalToPrecision (amount, TRUNCATE, this.markets[symbol]['precision']['amount'], DECIMAL_PLACES);
}
priceToPrecision (symbol, price) {
symbol = this.safeSymbol (symbol);
price = this.decimalToPrecision (price, ROUND, this.markets[symbol]['precision']['price'], this.precisionMode);
// https://docs.bitfinex.com/docs/introduction#price-precision
// The precision level of all trading prices is based on significant figures.
// All pairs on Bitfinex use up to 5 significant digits and up to 8 decimals (e.g. 1.2345, 123.45, 1234.5, 0.00012345).
// Prices submit with a precision larger than 5 will be cut by the API.
return this.decimalToPrecision (price, TRUNCATE, 8, DECIMAL_PLACES);
}
async fetchStatus (params = {}) {
/**
* @method
* @name bitfinex2#fetchStatus
* @description the latest known information on the availability of the exchange API
* @param {object} params extra parameters specific to the bitfinex2 api endpoint
* @returns {object} a [status structure]{@link https://docs.ccxt.com/en/latest/manual.html#exchange-status-structure}
*/
//
// [1] // operative
// [0] // maintenance
//
const response = await this.publicGetPlatformStatus (params);
const statusRaw = this.safeString (response, 0);
return {
'status': this.safeString ({ '0': 'maintenance', '1': 'ok' }, statusRaw, statusRaw),
'updated': undefined,
'eta': undefined,
'url': undefined,
'info': response,
};
}
async fetchMarkets (params = {}) {
/**
* @method
* @name bitfinex2#fetchMarkets
* @description retrieves data on all markets for bitfinex2
* @param {object} params extra parameters specific to the exchange api endpoint
* @returns {[object]} an array of objects representing market data
*/
let spotMarketsInfo = await this.publicGetConfPubInfoPair (params);
let futuresMarketsInfo = await this.publicGetConfPubInfoPairFutures (params);
spotMarketsInfo = this.safeValue (spotMarketsInfo, 0, []);
futuresMarketsInfo = this.safeValue (futuresMarketsInfo, 0, []);
const markets = this.arrayConcat (spotMarketsInfo, futuresMarketsInfo);
let marginIds = await this.publicGetConfPubListPairMargin (params);
marginIds = this.safeValue (marginIds, 0, []);
//
// [
// "1INCH:USD",
// [
// null,
// null,
// null,
// "2.0",
// "100000.0",
// null,
// null,
// null,
// null,
// null,
// null,
// null
// ]
// ]
//
const result = [];
for (let i = 0; i < markets.length; i++) {
const pair = markets[i];
const id = this.safeStringUpper (pair, 0);
const market = this.safeValue (pair, 1, {});
let spot = true;
if (id.indexOf ('F0') >= 0) {
spot = false;
}
const swap = !spot;
let baseId = undefined;
let quoteId = undefined;
if (id.indexOf (':') >= 0) {
const parts = id.split (':');
baseId = parts[0];
quoteId = parts[1];
} else {
baseId = id.slice (0, 3);
quoteId = id.slice (3, 6);
}
let base = this.safeCurrencyCode (baseId);
let quote = this.safeCurrencyCode (quoteId);
const splitBase = base.split ('F0');
const splitQuote = quote.split ('F0');
base = this.safeString (splitBase, 0);
quote = this.safeString (splitQuote, 0);
let symbol = base + '/' + quote;
baseId = this.getCurrencyId (baseId);
quoteId = this.getCurrencyId (quoteId);
let settle = undefined;
if (swap) {
settle = quote;
symbol = symbol + ':' + settle;
}
const minOrderSizeString = this.safeString (market, 3);
const maxOrderSizeString = this.safeString (market, 4);
let margin = false;
if (this.inArray (id, marginIds)) {
margin = true;
}
result.push ({
'id': 't' + id,
'symbol': symbol,
'base': base,
'quote': quote,
'settle': settle,
'baseId': baseId,
'quoteId': quoteId,
'settleId': quoteId,
'type': spot ? 'spot' : 'swap',
'spot': spot,
'margin': margin,
'swap': swap,
'future': false,
'option': false,
'active': true,
'contract': swap,
'linear': swap ? true : undefined,
'inverse': swap ? false : undefined,
'contractSize': swap ? this.parseNumber ('1') : undefined,
'expiry': undefined,
'expiryDatetime': undefined,
'strike': undefined,
'optionType': undefined,
'precision': {
'amount': parseInt ('8'), // https://github.com/ccxt/ccxt/issues/7310
'price': parseInt ('5'),
},
'limits': {
'leverage': {
'min': undefined,
'max': undefined,
},
'amount': {
'min': this.parseNumber (minOrderSizeString),
'max': this.parseNumber (maxOrderSizeString),
},
'price': {
'min': this.parseNumber ('1e-8'),
'max': undefined,
},
'cost': {
'min': undefined,
'max': undefined,
},
},
'info': market,
});
}
return result;
}
async fetchCurrencies (params = {}) {
/**
* @method
* @name bitfinex2#fetchCurrencies
* @description fetches all available currencies on an exchange
* @param {object} params extra parameters specific to the bitfinex2 api endpoint
* @returns {object} an associative dictionary of currencies
*/
const labels = [
'pub:list:currency',
'pub:map:currency:sym', // maps symbols to their API symbols, BAB > BCH
'pub:map:currency:label', // verbose friendly names, BNT > Bancor
'pub:map:currency:unit', // maps symbols to unit of measure where applicable
'pub:map:currency:undl', // maps derivatives symbols to their underlying currency
'pub:map:currency:pool', // maps symbols to underlying network/protocol they operate on
'pub:map:currency:explorer', // maps symbols to their recognised block explorer URLs
'pub:map:currency:tx:fee', // maps currencies to their withdrawal fees https://github.com/ccxt/ccxt/issues/7745,
'pub:map:tx:method', // maps withdrawal/deposit methods to their API symbols
];
const config = labels.join (',');
const request = {
'config': config,
};
const response = await this.publicGetConfConfig (this.extend (request, params));
//
// [
//
// a list of symbols
// ["AAA","ABS","ADA"],
//
// // sym
// // maps symbols to their API symbols, BAB > BCH
// [
// [ 'BAB', 'BCH' ],
// [ 'CNHT', 'CNHt' ],
// [ 'DSH', 'DASH' ],
// [ 'IOT', 'IOTA' ],
// [ 'LES', 'LEO-EOS' ],
// [ 'LET', 'LEO-ERC20' ],
// [ 'STJ', 'STORJ' ],
// [ 'TSD', 'TUSD' ],
// [ 'UDC', 'USDC' ],
// [ 'USK', 'USDK' ],
// [ 'UST', 'USDt' ],
// [ 'USTF0', 'USDt0' ],
// [ 'XCH', 'XCHF' ],
// [ 'YYW', 'YOYOW' ],
// // ...
// ],
// // label
// // verbose friendly names, BNT > Bancor
// [
// [ 'BAB', 'Bitcoin Cash' ],
// [ 'BCH', 'Bitcoin Cash' ],
// [ 'LEO', 'Unus Sed LEO' ],
// [ 'LES', 'Unus Sed LEO (EOS)' ],
// [ 'LET', 'Unus Sed LEO (ERC20)' ],
// // ...
// ],
// // unit
// // maps symbols to unit of measure where applicable
// [
// [ 'IOT', 'Mi|MegaIOTA' ],
// ],
// // undl
// // maps derivatives symbols to their underlying currency
// [
// [ 'USTF0', 'UST' ],
// [ 'BTCF0', 'BTC' ],
// [ 'ETHF0', 'ETH' ],
// ],
// // pool
// // maps symbols to underlying network/protocol they operate on
// [
// [ 'SAN', 'ETH' ], [ 'OMG', 'ETH' ], [ 'AVT', 'ETH' ], [ 'EDO', 'ETH' ],
// [ 'ESS', 'ETH' ], [ 'ATD', 'EOS' ], [ 'ADD', 'EOS' ], [ 'MTO', 'EOS' ],
// [ 'PNK', 'ETH' ], [ 'BAB', 'BCH' ], [ 'WLO', 'XLM' ], [ 'VLD', 'ETH' ],
// [ 'BTT', 'TRX' ], [ 'IMP', 'ETH' ], [ 'SCR', 'ETH' ], [ 'GNO', 'ETH' ],
// // ...
// ],
// // explorer
// // maps symbols to their recognised block explorer URLs
// [
// [
// 'AIO',
// [
// "https://mainnet.aion.network",
// "https://mainnet.aion.network/#/account/VAL",
// "https://mainnet.aion.network/#/transaction/VAL"
// ]
// ],
// // ...
// ],
// // fee
// // maps currencies to their withdrawal fees
// [
// ["AAA",[0,0]],
// ["ABS",[0,131.3]],
// ["ADA",[0,0.3]],
// ],
// ]
//
const indexed = {
'sym': this.indexBy (this.safeValue (response, 1, []), 0),
'label': this.indexBy (this.safeValue (response, 2, []), 0),
'unit': this.indexBy (this.safeValue (response, 3, []), 0),
'undl': this.indexBy (this.safeValue (response, 4, []), 0),
'pool': this.indexBy (this.safeValue (response, 5, []), 0),
'explorer': this.indexBy (this.safeValue (response, 6, []), 0),
'fees': this.indexBy (this.safeValue (response, 7, []), 0),
};
const ids = this.safeValue (response, 0, []);
const result = {};
for (let i = 0; i < ids.length; i++) {
const id = ids[i];
if (id.indexOf ('F0') >= 0) {
// we get a lot of F0 currencies, skip those
continue;
}
const code = this.safeCurrencyCode (id);
const label = this.safeValue (indexed['label'], id, []);
const name = this.safeString (label, 1);
const pool = this.safeValue (indexed['pool'], id, []);
const type = this.safeString (pool, 1);
const feeValues = this.safeValue (indexed['fees'], id, []);
const fees = this.safeValue (feeValues, 1, []);
const fee = this.safeNumber (fees, 1);
const undl = this.safeValue (indexed['undl'], id, []);
const precision = '8'; // default precision, todo: fix "magic constants"
const fid = 'f' + id;
result[code] = {
'id': fid,
'uppercaseId': id,
'code': code,
'info': [ id, label, pool, feeValues, undl ],
'type': type,
'name': name,
'active': true,
'deposit': undefined,
'withdraw': undefined,
'fee': fee,
'precision': parseInt (precision),
'limits': {
'amount': {
'min': this.parseNumber (this.parsePrecision (precision)),
'max': undefined,
},
'withdraw': {
'min': fee,
'max': undefined,
},
},
};
const networks = {};
const currencyNetworks = this.safeValue (response, 8, []);
const cleanId = id.replace ('F0', '');
for (let j = 0; j < currencyNetworks.length; j++) {
const pair = currencyNetworks[j];
const networkId = this.safeString (pair, 0);
const currencyId = this.safeString (this.safeValue (pair, 1, []), 0);
if (currencyId === cleanId) {
const network = this.safeNetwork (networkId);
networks[network] = {
'info': networkId,
'id': networkId.toLowerCase (),
'network': networkId,
'active': undefined,
'deposit': undefined,
'withdraw': undefined,
'fee': undefined,
'precision': undefined,
'limits': {
'withdraw': {
'min': undefined,
'max': undefined,
},
},
};
}
}
const keysNetworks = Object.keys (networks);
const networksLength = keysNetworks.length;
if (networksLength > 0) {
result[code]['networks'] = networks;
}
}
return result;
}
safeNetwork (networkId) {
const networksById = {
'BITCOIN': 'BTC',
'LITECOIN': 'LTC',
'ETHEREUM': 'ERC20',
'TETHERUSE': 'ERC20',
'TETHERUSO': 'OMNI',
'TETHERUSL': 'LIQUID',
'TETHERUSX': 'TRC20',
'TETHERUSS': 'EOS',
'TETHERUSDTAVAX': 'AVAX',
'TETHERUSDTSOL': 'SOL',
'TETHERUSDTALG': 'ALGO',
'TETHERUSDTBCH': 'BCH',
'TETHERUSDTKSM': 'KSM',
'TETHERUSDTDVF': 'DVF',
'TETHERUSDTOMG': 'OMG',
};
return this.safeString (networksById, networkId, networkId);
}
async fetchBalance (params = {}) {
/**
* @method
* @name bitfinex2#fetchBalance
* @description query for balance and get the amount of funds available for trading or funds locked in orders
* @param {object} params extra parameters specific to the bitfinex2 api endpoint
* @returns {object} a [balance structure]{@link https://docs.ccxt.com/en/latest/manual.html?#balance-structure}
*/
// this api call does not return the 'used' amount - use the v1 version instead (which also returns zero balances)
// there is a difference between this and the v1 api, namely trading wallet is called margin in v2
await this.loadMarkets ();
const accountsByType = this.safeValue (this.options, 'v2AccountsByType', {});
const requestedType = this.safeString (params, 'type', 'exchange');
const accountType = this.safeString (accountsByType, requestedType, requestedType);
if (accountType === undefined) {
const keys = Object.keys (accountsByType);
throw new ExchangeError (this.id + ' fetchBalance() type parameter must be one of ' + keys.join (', '));
}
const isDerivative = requestedType === 'derivatives';
const query = this.omit (params, 'type');
const response = await this.privatePostAuthRWallets (query);
const result = { 'info': response };
for (let i = 0; i < response.length; i++) {
const balance = response[i];
const type = this.safeString (balance, 0);
const currencyId = this.safeStringLower (balance, 1, '');
const start = currencyId.length - 2;
const isDerivativeCode = currencyId.slice (start) === 'f0';
// this will only filter the derivative codes if the requestedType is 'derivatives'
const derivativeCondition = (!isDerivative || isDerivativeCode);
if ((accountType === type) && derivativeCondition) {
const code = this.safeCurrencyCode (currencyId);
const account = this.account ();
account['total'] = this.safeString (balance, 2);
account['free'] = this.safeString (balance, 4);
result[code] = account;
}
}
return this.safeBalance (result);
}
async transfer (code, amount, fromAccount, toAccount, params = {}) {
/**
* @method
* @name bitfinex2#transfer
* @description transfer currency internally between wallets on the same account
* @param {string} code unified currency code
* @param {float} amount amount to transfer
* @param {string} fromAccount account to transfer from
* @param {string} toAccount account to transfer to
* @param {object} params extra parameters specific to the bitfinex2 api endpoint
* @returns {object} a [transfer structure]{@link https://docs.ccxt.com/en/latest/manual.html#transfer-structure}
*/
// transferring between derivatives wallet and regular wallet is not documented in their API
// however we support it in CCXT (from just looking at web inspector)
await this.loadMarkets ();
const accountsByType = this.safeValue (this.options, 'v2AccountsByType', {});
const fromId = this.safeString (accountsByType, fromAccount);
if (fromId === undefined) {
const keys = Object.keys (accountsByType);
throw new ArgumentsRequired (this.id + ' transfer() fromAccount must be one of ' + keys.join (', '));
}
const toId = this.safeString (accountsByType, toAccount);
if (toId === undefined) {
const keys = Object.keys (accountsByType);
throw new ArgumentsRequired (this.id + ' transfer() toAccount must be one of ' + keys.join (', '));
}
const currency = this.currency (code);
const fromCurrencyId = this.convertDerivativesId (currency, fromAccount);
const toCurrencyId = this.convertDerivativesId (currency, toAccount);
const requestedAmount = this.currencyToPrecision (code, amount);
// this request is slightly different from v1 fromAccount -> from
const request = {
'amount': requestedAmount,
'currency': fromCurrencyId,
'currency_to': toCurrencyId,
'from': fromId,
'to': toId,
};
const response = await this.privatePostAuthWTransfer (this.extend (request, params));
//
// [
// 1616451183763,
// "acc_tf",
// null,
// null,
// [
// 1616451183763,
// "exchange",
// "margin",
// null,
// "UST",
// "UST",
// null,
// 1
// ],
// null,
// "SUCCESS",
// "1.0 Tether USDt transfered from Exchange to Margin"
// ]
//
const error = this.safeString (response, 0);
if (error === 'error') {
const message = this.safeString (response, 2, '');
// same message as in v1
this.throwExactlyMatchedException (this.exceptions['exact'], message, this.id + ' ' + message);
throw new ExchangeError (this.id + ' ' + message);
}
return this.parseTransfer (response, currency);
}
parseTransfer (transfer, currency = undefined) {
//
// transfer
//
// [
// 1616451183763,
// "acc_tf",
// null,
// null,
// [
// 1616451183763,
// "exchange",
// "margin",
// null,
// "UST",
// "UST",
// null,
// 1
// ],
// null,
// "SUCCESS",
// "1.0 Tether USDt transfered from Exchange to Margin"
// ]
//
const timestamp = this.safeInteger (transfer, 0);
const info = this.safeValue (transfer, 4);
const fromAccount = this.safeString (info, 1);
const toAccount = this.safeString (info, 2);
const currencyId = this.safeString (info, 5);
const status = this.safeString (transfer, 6);
return {
'id': undefined,
'timestamp': timestamp,
'datetime': this.iso8601 (timestamp),
'status': this.parseTransferStatus (status),
'amount': this.safeNumber (info, 7),
'currency': this.safeCurrencyCode (currencyId, currency),
'fromAccount': fromAccount,
'toAccount': toAccount,
'info': transfer,
};
}
parseTransferStatus (status) {
const statuses = {
'SUCCESS': 'ok',
'ERROR': 'failed',
'FAILURE': 'failed',
};
return this.safeString (statuses, status, status);
}
convertDerivativesId (currency, type) {
// there is a difference between this and the v1 api, namely trading wallet is called margin in v2
// {
// id: 'fUSTF0',
// code: 'USTF0',
// info: [ 'USTF0', [], [], [], [ 'USTF0', 'UST' ] ],
const info = this.safeValue (currency, 'info');
const transferId = this.safeString (info, 0);
const underlying = this.safeValue (info, 4, []);
let currencyId = undefined;
if (type === 'derivatives') {
currencyId = this.safeString (underlying, 0, transferId);
const start = currencyId.length - 2;
const isDerivativeCode = currencyId.slice (start) === 'F0';
if (!isDerivativeCode) {
currencyId = currencyId + 'F0';
}
} else if (type !== 'margin') {
currencyId = this.safeString (underlying, 1, transferId);
} else {
currencyId = transferId;
}
return currencyId;
}
async fetchOrder (id, symbol = undefined, params = {}) {
/**
* @method
* @name bitfinex2#fetchOrder
* @description fetches information on an order made by the user
* @param {string|undefined} symbol unified symbol of the market the order was made in
* @param {object} params extra parameters specific to the bitfinex2 api endpoint
* @returns {object} An [order structure]{@link https://docs.ccxt.com/en/latest/manual.html#order-structure}
*/
throw new NotSupported (this.id + ' fetchOrder() is not supported yet. Consider using fetchOpenOrder() or fetchClosedOrder() instead.');
}
async fetchOrderBook (symbol, limit = undefined, params = {}) {
/**
* @method
* @name bitfinex2#fetchOrderBook
* @description fetches information on open orders with bid (buy) and ask (sell) prices, volumes and other data
* @param {string} symbol unified symbol of the market to fetch the order book for
* @param {int|undefined} limit the maximum amount of order book entries to return
* @param {object} params extra parameters specific to the bitfinex2 api endpoint
* @returns {object} A dictionary of [order book structures]{@link https://docs.ccxt.com/en/latest/manual.html#order-book-structure} indexed by market symbols
*/
await this.loadMarkets ();
const precision = this.safeValue (this.options, 'precision', 'R0');
const market = this.market (symbol);
const request = {
'symbol': market['id'],
'precision': precision,
};
if (limit !== undefined) {