forked from ccxt/ccxt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coinbase.js
2826 lines (2768 loc) · 130 KB
/
coinbase.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, ArgumentsRequired, AuthenticationError, BadRequest, InvalidOrder, NotSupported, OrderNotFound, RateLimitExceeded, InvalidNonce } = require ('./base/errors');
const { TICK_SIZE, TRUNCATE, DECIMAL_PLACES } = require ('./base/functions/number');
const Precise = require ('./base/Precise');
// ----------------------------------------------------------------------------
module.exports = class coinbase extends Exchange {
describe () {
return this.deepExtend (super.describe (), {
'id': 'coinbase',
'name': 'Coinbase',
'countries': [ 'US' ],
'rateLimit': 400, // 10k calls per hour
'version': 'v2',
'userAgent': this.userAgents['chrome'],
'headers': {
'CB-VERSION': '2018-05-30',
},
'has': {
'CORS': true,
'spot': true,
'margin': false,
'swap': false,
'future': false,
'option': false,
'addMargin': false,
'cancelOrder': true,
'cancelOrders': true,
'createDepositAddress': true,
'createLimitBuyOrder': true,
'createLimitSellOrder': true,
'createMarketBuyOrder': true,
'createMarketSellOrder': true,
'createOrder': true,
'createPostOnlyOrder': true,
'createReduceOnlyOrder': false,
'createStopLimitOrder': true,
'createStopMarketOrder': false,
'createStopOrder': true,
'fetchAccounts': true,
'fetchBalance': true,
'fetchBorrowRate': false,
'fetchBorrowRateHistories': false,
'fetchBorrowRateHistory': false,
'fetchBorrowRates': false,
'fetchBorrowRatesPerSymbol': false,
'fetchCanceledOrders': true,
'fetchClosedOrders': true,
'fetchCurrencies': true,
'fetchDeposits': true,
'fetchFundingHistory': false,
'fetchFundingRate': false,
'fetchFundingRateHistory': false,
'fetchFundingRates': false,
'fetchIndexOHLCV': false,
'fetchL2OrderBook': false,
'fetchLedger': true,
'fetchLeverage': false,
'fetchLeverageTiers': false,
'fetchMarginMode': false,
'fetchMarkets': true,
'fetchMarkOHLCV': false,
'fetchMyBuys': true,
'fetchMySells': true,
'fetchMyTrades': true,
'fetchOHLCV': true,
'fetchOpenInterestHistory': false,
'fetchOpenOrders': true,
'fetchOrder': true,
'fetchOrderBook': false,
'fetchOrders': true,
'fetchPosition': false,
'fetchPositionMode': false,
'fetchPositions': false,
'fetchPositionsRisk': false,
'fetchPremiumIndexOHLCV': false,
'fetchTicker': true,
'fetchTickers': true,
'fetchTime': true,
'fetchTrades': true,
'fetchTradingFee': false,
'fetchTradingFees': false,
'fetchWithdrawals': true,
'reduceMargin': false,
'setLeverage': false,
'setMarginMode': false,
'setPositionMode': false,
'withdraw': undefined,
},
'urls': {
'logo': 'https://user-images.githubusercontent.com/1294454/40811661-b6eceae2-653a-11e8-829e-10bfadb078cf.jpg',
'api': {
'rest': 'https://api.coinbase.com',
},
'www': 'https://www.coinbase.com',
'doc': [
'https://developers.coinbase.com/api/v2',
'https://docs.cloud.coinbase.com/advanced-trade-api/docs/welcome',
],
'fees': [
'https://support.coinbase.com/customer/portal/articles/2109597-buy-sell-bank-transfer-fees',
'https://www.coinbase.com/advanced-fees',
],
'referral': 'https://www.coinbase.com/join/58cbe25a355148797479dbd2',
},
'requiredCredentials': {
'apiKey': true,
'secret': true,
},
'api': {
'v2': {
'public': {
'get': [
'currencies',
'time',
'exchange-rates',
'users/{user_id}',
'prices/{symbol}/buy',
'prices/{symbol}/sell',
'prices/{symbol}/spot',
],
},
'private': {
'get': [
'accounts',
'accounts/{account_id}',
'accounts/{account_id}/addresses',
'accounts/{account_id}/addresses/{address_id}',
'accounts/{account_id}/addresses/{address_id}/transactions',
'accounts/{account_id}/transactions',
'accounts/{account_id}/transactions/{transaction_id}',
'accounts/{account_id}/buys',
'accounts/{account_id}/buys/{buy_id}',
'accounts/{account_id}/sells',
'accounts/{account_id}/sells/{sell_id}',
'accounts/{account_id}/deposits',
'accounts/{account_id}/deposits/{deposit_id}',
'accounts/{account_id}/withdrawals',
'accounts/{account_id}/withdrawals/{withdrawal_id}',
'payment-methods',
'payment-methods/{payment_method_id}',
'user',
'user/auth',
],
'post': [
'accounts',
'accounts/{account_id}/primary',
'accounts/{account_id}/addresses',
'accounts/{account_id}/transactions',
'accounts/{account_id}/transactions/{transaction_id}/complete',
'accounts/{account_id}/transactions/{transaction_id}/resend',
'accounts/{account_id}/buys',
'accounts/{account_id}/buys/{buy_id}/commit',
'accounts/{account_id}/sells',
'accounts/{account_id}/sells/{sell_id}/commit',
'accounts/{account_id}/deposits',
'accounts/{account_id}/deposits/{deposit_id}/commit',
'accounts/{account_id}/withdrawals',
'accounts/{account_id}/withdrawals/{withdrawal_id}/commit',
],
'put': [
'accounts/{account_id}',
'user',
],
'delete': [
'accounts/{id}',
'accounts/{account_id}/transactions/{transaction_id}',
],
},
},
'v3': {
'private': {
'get': [
'brokerage/accounts',
'brokerage/accounts/{account_uuid}',
'brokerage/orders/historical/batch',
'brokerage/orders/historical/fills',
'brokerage/orders/historical/{order_id}',
'brokerage/products',
'brokerage/products/{product_id}',
'brokerage/products/{product_id}/candles',
'brokerage/products/{product_id}/ticker',
'brokerage/transaction_summary',
],
'post': [
'brokerage/orders',
'brokerage/orders/batch_cancel',
],
},
},
},
'fees': {
'trading': {
'taker': this.parseNumber ('0.006'),
'maker': this.parseNumber ('0.004'),
'tierBased': true,
'percentage': true,
'tiers': {
'taker': [
[ this.parseNumber ('0'), this.parseNumber ('0.006') ],
[ this.parseNumber ('10000'), this.parseNumber ('0.004') ],
[ this.parseNumber ('50000'), this.parseNumber ('0.0025') ],
[ this.parseNumber ('100000'), this.parseNumber ('0.002') ],
[ this.parseNumber ('1000000'), this.parseNumber ('0.0018') ],
[ this.parseNumber ('15000000'), this.parseNumber ('0.0016') ],
[ this.parseNumber ('75000000'), this.parseNumber ('0.0012') ],
[ this.parseNumber ('250000000'), this.parseNumber ('0.0008') ],
[ this.parseNumber ('400000000'), this.parseNumber ('0.0005') ],
],
'maker': [
[ this.parseNumber ('0'), this.parseNumber ('0.004') ],
[ this.parseNumber ('10000'), this.parseNumber ('0.0025') ],
[ this.parseNumber ('50000'), this.parseNumber ('0.0015') ],
[ this.parseNumber ('100000'), this.parseNumber ('0.001') ],
[ this.parseNumber ('1000000'), this.parseNumber ('0.0008') ],
[ this.parseNumber ('15000000'), this.parseNumber ('0.0006') ],
[ this.parseNumber ('75000000'), this.parseNumber ('0.0003') ],
[ this.parseNumber ('250000000'), this.parseNumber ('0.0') ],
[ this.parseNumber ('400000000'), this.parseNumber ('0.0') ],
],
},
},
},
'precisionMode': TICK_SIZE,
'exceptions': {
'exact': {
'two_factor_required': AuthenticationError, // 402 When sending money over 2fa limit
'param_required': ExchangeError, // 400 Missing parameter
'validation_error': ExchangeError, // 400 Unable to validate POST/PUT
'invalid_request': ExchangeError, // 400 Invalid request
'personal_details_required': AuthenticationError, // 400 User’s personal detail required to complete this request
'identity_verification_required': AuthenticationError, // 400 Identity verification is required to complete this request
'jumio_verification_required': AuthenticationError, // 400 Document verification is required to complete this request
'jumio_face_match_verification_required': AuthenticationError, // 400 Document verification including face match is required to complete this request
'unverified_email': AuthenticationError, // 400 User has not verified their email
'authentication_error': AuthenticationError, // 401 Invalid auth (generic)
'invalid_authentication_method': AuthenticationError, // 401 API access is blocked for deleted users.
'invalid_token': AuthenticationError, // 401 Invalid Oauth token
'revoked_token': AuthenticationError, // 401 Revoked Oauth token
'expired_token': AuthenticationError, // 401 Expired Oauth token
'invalid_scope': AuthenticationError, // 403 User hasn’t authenticated necessary scope
'not_found': ExchangeError, // 404 Resource not found
'rate_limit_exceeded': RateLimitExceeded, // 429 Rate limit exceeded
'internal_server_error': ExchangeError, // 500 Internal server error
},
'broad': {
'request timestamp expired': InvalidNonce, // {"errors":[{"id":"authentication_error","message":"request timestamp expired"}]}
'order with this orderID was not found': OrderNotFound, // {"error":"unknown","error_details":"order with this orderID was not found","message":"order with this orderID was not found"}
},
},
'timeframes': {
'1m': 'ONE_MINUTE',
'5m': 'FIVE_MINUTE',
'15m': 'FIFTEEN_MINUTE',
'30m': 'THIRTY_MINUTE',
'1h': 'ONE_HOUR',
'2h': 'TWO_HOUR',
'6h': 'SIX_HOUR',
'1d': 'ONE_DAY',
},
'commonCurrencies': {
'CGLD': 'CELO',
},
'options': {
'stablePairs': [ 'BUSD-USD', 'CBETH-ETH', 'DAI-USD', 'GUSD-USD', 'GYEN-USD', 'PAX-USD', 'PAX-USDT', 'USDC-EUR', 'USDC-GBP', 'USDT-EUR', 'USDT-GBP', 'USDT-USD', 'USDT-USDC', 'WBTC-BTC' ],
'fetchCurrencies': {
'expires': 5000,
},
'accounts': [
'wallet',
'fiat',
// 'vault',
],
'createMarketBuyOrderRequiresPrice': true,
'advanced': true, // set to true if using any v3 endpoints from the advanced trade API
'fetchMarkets': 'fetchMarketsV3', // 'fetchMarketsV3' or 'fetchMarketsV2'
'fetchTicker': 'fetchTickerV3', // 'fetchTickerV3' or 'fetchTickerV2'
'fetchTickers': 'fetchTickersV3', // 'fetchTickersV3' or 'fetchTickersV2'
'fetchAccounts': 'fetchAccountsV3', // 'fetchAccountsV3' or 'fetchAccountsV2'
},
});
}
async fetchTime (params = {}) {
/**
* @method
* @name coinbase#fetchTime
* @description fetches the current integer timestamp in milliseconds from the exchange server
* @param {object} params extra parameters specific to the coinbase api endpoint
* @returns {int} the current integer timestamp in milliseconds from the exchange server
*/
const response = await this.v2PublicGetTime (params);
//
// {
// "data": {
// "epoch": 1589295679,
// "iso": "2020-05-12T15:01:19Z"
// }
// }
//
const data = this.safeValue (response, 'data', {});
return this.safeTimestamp (data, 'epoch');
}
async fetchAccounts (params = {}) {
/**
* @method
* @name coinbase#fetchAccounts
* @description fetch all the accounts associated with a profile
* @param {object} params extra parameters specific to the coinbase api endpoint
* @returns {object} a dictionary of [account structures]{@link https://docs.ccxt.com/en/latest/manual.html#account-structure} indexed by the account type
*/
const method = this.safeString (this.options, 'fetchAccounts', 'fetchAccountsV3');
if (method === 'fetchAccountsV3') {
return await this.fetchAccountsV3 (params);
}
return await this.fetchAccountsV2 (params);
}
async fetchAccountsV2 (params = {}) {
await this.loadMarkets ();
const request = {
'limit': 100,
};
const response = await this.v2PrivateGetAccounts (this.extend (request, params));
//
// {
// "pagination": {
// "ending_before": null,
// "starting_after": null,
// "previous_ending_before": null,
// "next_starting_after": null,
// "limit": 244,
// "order": "desc",
// "previous_uri": null,
// "next_uri": null
// },
// "data": [
// {
// "id": "XLM",
// "name": "XLM Wallet",
// "primary": false,
// "type": "wallet",
// "currency": {
// "code": "XLM",
// "name": "Stellar Lumens",
// "color": "#000000",
// "sort_index": 127,
// "exponent": 7,
// "type": "crypto",
// "address_regex": "^G[A-Z2-7]{55}$",
// "asset_id": "13b83335-5ede-595b-821e-5bcdfa80560f",
// "destination_tag_name": "XLM Memo ID",
// "destination_tag_regex": "^[ -~]{1,28}$"
// },
// "balance": {
// "amount": "0.0000000",
// "currency": "XLM"
// },
// "created_at": null,
// "updated_at": null,
// "resource": "account",
// "resource_path": "/v2/accounts/XLM",
// "allow_deposits": true,
// "allow_withdrawals": true
// },
// ]
// }
//
const data = this.safeValue (response, 'data', []);
return this.parseAccounts (data, params);
}
async fetchAccountsV3 (params = {}) {
await this.loadMarkets ();
const request = {
'limit': 100,
};
const response = await this.v3PrivateGetBrokerageAccounts (this.extend (request, params));
//
// {
// "accounts": [
// {
// "uuid": "11111111-1111-1111-1111-111111111111",
// "name": "USDC Wallet",
// "currency": "USDC",
// "available_balance": {
// "value": "0.0000000000000000",
// "currency": "USDC"
// },
// "default": true,
// "active": true,
// "created_at": "2023-01-04T06:20:06.456Z",
// "updated_at": "2023-01-04T06:20:07.181Z",
// "deleted_at": null,
// "type": "ACCOUNT_TYPE_CRYPTO",
// "ready": false,
// "hold": {
// "value": "0.0000000000000000",
// "currency": "USDC"
// }
// },
// ...
// ],
// "has_next": false,
// "cursor": "",
// "size": 9
// }
//
const data = this.safeValue (response, 'accounts', []);
return this.parseAccounts (data, params);
}
parseAccount (account) {
//
// fetchAccountsV2
//
// {
// "id": "XLM",
// "name": "XLM Wallet",
// "primary": false,
// "type": "wallet",
// "currency": {
// "code": "XLM",
// "name": "Stellar Lumens",
// "color": "#000000",
// "sort_index": 127,
// "exponent": 7,
// "type": "crypto",
// "address_regex": "^G[A-Z2-7]{55}$",
// "asset_id": "13b83335-5ede-595b-821e-5bcdfa80560f",
// "destination_tag_name": "XLM Memo ID",
// "destination_tag_regex": "^[ -~]{1,28}$"
// },
// "balance": {
// "amount": "0.0000000",
// "currency": "XLM"
// },
// "created_at": null,
// "updated_at": null,
// "resource": "account",
// "resource_path": "/v2/accounts/XLM",
// "allow_deposits": true,
// "allow_withdrawals": true
// }
//
// fetchAccountsV3
//
// {
// "uuid": "11111111-1111-1111-1111-111111111111",
// "name": "USDC Wallet",
// "currency": "USDC",
// "available_balance": {
// "value": "0.0000000000000000",
// "currency": "USDC"
// },
// "default": true,
// "active": true,
// "created_at": "2023-01-04T06:20:06.456Z",
// "updated_at": "2023-01-04T06:20:07.181Z",
// "deleted_at": null,
// "type": "ACCOUNT_TYPE_CRYPTO",
// "ready": false,
// "hold": {
// "value": "0.0000000000000000",
// "currency": "USDC"
// }
// }
//
const active = this.safeValue (account, 'active');
const currencyIdV3 = this.safeString (account, 'currency');
const currency = this.safeValue (account, 'currency', {});
const currencyId = this.safeString (currency, 'code', currencyIdV3);
const typeV3 = this.safeString (account, 'name');
const typeV2 = this.safeString (account, 'type');
const parts = typeV3.split (' ');
return {
'id': this.safeString2 (account, 'id', 'uuid'),
'type': (active !== undefined) ? this.safeStringLower (parts, 1) : typeV2,
'code': this.safeCurrencyCode (currencyId),
'info': account,
};
}
async createDepositAddress (code, params = {}) {
/**
* @method
* @name coinbase#createDepositAddress
* @description create a currency deposit address
* @param {string} code unified currency code of the currency for the deposit address
* @param {object} params extra parameters specific to the coinbase api endpoint
* @returns {object} an [address structure]{@link https://docs.ccxt.com/en/latest/manual.html#address-structure}
*/
let accountId = this.safeString (params, 'account_id');
params = this.omit (params, 'account_id');
if (accountId === undefined) {
await this.loadAccounts ();
for (let i = 0; i < this.accounts.length; i++) {
const account = this.accounts[i];
if (account['code'] === code && account['type'] === 'wallet') {
accountId = account['id'];
break;
}
}
}
if (accountId === undefined) {
throw new ExchangeError (this.id + ' createDepositAddress() could not find the account with matching currency code, specify an `account_id` extra param');
}
const request = {
'account_id': accountId,
};
const response = await this.v2PrivatePostAccountsAccountIdAddresses (this.extend (request, params));
//
// {
// "data": {
// "id": "05b1ebbf-9438-5dd4-b297-2ddedc98d0e4",
// "address": "coinbasebase",
// "address_info": {
// "address": "coinbasebase",
// "destination_tag": "287594668"
// },
// "name": null,
// "created_at": "2019-07-01T14:39:29Z",
// "updated_at": "2019-07-01T14:39:29Z",
// "network": "eosio",
// "uri_scheme": "eosio",
// "resource": "address",
// "resource_path": "/v2/accounts/14cfc769-e852-52f3-b831-711c104d194c/addresses/05b1ebbf-9438-5dd4-b297-2ddedc98d0e4",
// "warnings": [
// {
// "title": "Only send EOS (EOS) to this address",
// "details": "Sending any other cryptocurrency will result in permanent loss.",
// "image_url": "https://dynamic-assets.coinbase.com/deaca3d47b10ed4a91a872e9618706eec34081127762d88f2476ac8e99ada4b48525a9565cf2206d18c04053f278f693434af4d4629ca084a9d01b7a286a7e26/asset_icons/1f8489bb280fb0a0fd643c1161312ba49655040e9aaaced5f9ad3eeaf868eadc.png"
// },
// {
// "title": "Both an address and EOS memo are required to receive EOS",
// "details": "If you send funds without an EOS memo or with an incorrect EOS memo, your funds cannot be credited to your account.",
// "image_url": "https://www.coinbase.com/assets/receive-warning-2f3269d83547a7748fb39d6e0c1c393aee26669bfea6b9f12718094a1abff155.png"
// }
// ],
// "warning_title": "Only send EOS (EOS) to this address",
// "warning_details": "Sending any other cryptocurrency will result in permanent loss.",
// "destination_tag": "287594668",
// "deposit_uri": "eosio:coinbasebase?dt=287594668",
// "callback_url": null
// }
// }
//
const data = this.safeValue (response, 'data', {});
const tag = this.safeString (data, 'destination_tag');
const address = this.safeString (data, 'address');
return {
'currency': code,
'tag': tag,
'address': address,
'info': response,
};
}
async fetchMySells (symbol = undefined, since = undefined, limit = undefined, params = {}) {
/**
* @method
* @name coinbase#fetchMySells
* @description fetch sells
* @param {string|undefined} symbol not used by coinbase fetchMySells ()
* @param {int|undefined} since timestamp in ms of the earliest sell, default is undefined
* @param {int|undefined} limit max number of sells to return, default is undefined
* @param {object} params extra parameters specific to the coinbase api endpoint
* @returns {object} a [list of order structures]{@link https://docs.ccxt.com/en/latest/manual.html#order-structure}
*/
// v2 did't have an endpoint for all historical trades
const request = this.prepareAccountRequest (limit, params);
await this.loadMarkets ();
const query = this.omit (params, [ 'account_id', 'accountId' ]);
const sells = await this.v2PrivateGetAccountsAccountIdSells (this.extend (request, query));
return this.parseTrades (sells['data'], undefined, since, limit);
}
async fetchMyBuys (symbol = undefined, since = undefined, limit = undefined, params = {}) {
/**
* @method
* @name coinbase#fetchMyBuys
* @description fetch buys
* @param {string|undefined} symbol not used by coinbase fetchMyBuys ()
* @param {int|undefined} since timestamp in ms of the earliest buy, default is undefined
* @param {int|undefined} limit max number of buys to return, default is undefined
* @param {object} params extra parameters specific to the coinbase api endpoint
* @returns {object} a list of [order structures]{@link https://docs.ccxt.com/en/latest/manual.html#order-structure}
*/
// v2 did't have an endpoint for all historical trades
const request = this.prepareAccountRequest (limit, params);
await this.loadMarkets ();
const query = this.omit (params, [ 'account_id', 'accountId' ]);
const buys = await this.v2PrivateGetAccountsAccountIdBuys (this.extend (request, query));
return this.parseTrades (buys['data'], undefined, since, limit);
}
async fetchTransactionsWithMethod (method, code = undefined, since = undefined, limit = undefined, params = {}) {
const request = await this.prepareAccountRequestWithCurrencyCode (code, limit, params);
await this.loadMarkets ();
const query = this.omit (params, [ 'account_id', 'accountId' ]);
const response = await this[method] (this.extend (request, query));
return this.parseTransactions (response['data'], undefined, since, limit);
}
async fetchWithdrawals (code = undefined, since = undefined, limit = undefined, params = {}) {
/**
* @method
* @name coinbase#fetchWithdrawals
* @description fetch all withdrawals made from an account
* @param {string|undefined} code unified currency code
* @param {int|undefined} since the earliest time in ms to fetch withdrawals for
* @param {int|undefined} limit the maximum number of withdrawals structures to retrieve
* @param {object} params extra parameters specific to the coinbase api endpoint
* @returns {[object]} a list of [transaction structures]{@link https://docs.ccxt.com/en/latest/manual.html#transaction-structure}
*/
// fiat only, for crypto transactions use fetchLedger
return await this.fetchTransactionsWithMethod ('v2PrivateGetAccountsAccountIdWithdrawals', code, since, limit, params);
}
async fetchDeposits (code = undefined, since = undefined, limit = undefined, params = {}) {
/**
* @method
* @name coinbase#fetchDeposits
* @description fetch all deposits made to an account
* @param {string|undefined} code unified currency code
* @param {int|undefined} since the earliest time in ms to fetch deposits for
* @param {int|undefined} limit the maximum number of deposits structures to retrieve
* @param {object} params extra parameters specific to the coinbase api endpoint
* @returns {[object]} a list of [transaction structures]{@link https://docs.ccxt.com/en/latest/manual.html#transaction-structure}
*/
// fiat only, for crypto transactions use fetchLedger
return await this.fetchTransactionsWithMethod ('v2PrivateGetAccountsAccountIdDeposits', code, since, limit, params);
}
parseTransactionStatus (status) {
const statuses = {
'created': 'pending',
'completed': 'ok',
'canceled': 'canceled',
};
return this.safeString (statuses, status, status);
}
parseTransaction (transaction, market = undefined) {
//
// fiat deposit
//
// {
// "id": "f34c19f3-b730-5e3d-9f72",
// "status": "completed",
// "payment_method": {
// "id": "a022b31d-f9c7-5043-98f2",
// "resource": "payment_method",
// "resource_path": "/v2/payment-methods/a022b31d-f9c7-5043-98f2"
// },
// "transaction": {
// "id": "04ed4113-3732-5b0c-af86-b1d2146977d0",
// "resource": "transaction",
// "resource_path": "/v2/accounts/91cd2d36-3a91-55b6-a5d4-0124cf105483/transactions/04ed4113-3732-5b0c-af86"
// },
// "user_reference": "2VTYTH",
// "created_at": "2017-02-09T07:01:18Z",
// "updated_at": "2017-02-09T07:01:26Z",
// "resource": "deposit",
// "resource_path": "/v2/accounts/91cd2d36-3a91-55b6-a5d4-0124cf105483/deposits/f34c19f3-b730-5e3d-9f72",
// "committed": true,
// "payout_at": "2017-02-12T07:01:17Z",
// "instant": false,
// "fee": { "amount": "0.00", "currency": "EUR" },
// "amount": { "amount": "114.02", "currency": "EUR" },
// "subtotal": { "amount": "114.02", "currency": "EUR" },
// "hold_until": null,
// "hold_days": 0,
// "hold_business_days": 0,
// "next_step": null
// }
//
// fiat_withdrawal
//
// {
// "id": "cfcc3b4a-eeb6-5e8c-8058",
// "status": "completed",
// "payment_method": {
// "id": "8b94cfa4-f7fd-5a12-a76a",
// "resource": "payment_method",
// "resource_path": "/v2/payment-methods/8b94cfa4-f7fd-5a12-a76a"
// },
// "transaction": {
// "id": "fcc2550b-5104-5f83-a444",
// "resource": "transaction",
// "resource_path": "/v2/accounts/91cd2d36-3a91-55b6-a5d4-0124cf105483/transactions/fcc2550b-5104-5f83-a444"
// },
// "user_reference": "MEUGK",
// "created_at": "2018-07-26T08:55:12Z",
// "updated_at": "2018-07-26T08:58:18Z",
// "resource": "withdrawal",
// "resource_path": "/v2/accounts/91cd2d36-3a91-55b6-a5d4-0124cf105483/withdrawals/cfcc3b4a-eeb6-5e8c-8058",
// "committed": true,
// "payout_at": "2018-07-31T08:55:12Z",
// "instant": false,
// "fee": { "amount": "0.15", "currency": "EUR" },
// "amount": { "amount": "13130.69", "currency": "EUR" },
// "subtotal": { "amount": "13130.84", "currency": "EUR" },
// "idem": "e549dee5-63ed-4e79-8a96",
// "next_step": null
// }
//
const subtotalObject = this.safeValue (transaction, 'subtotal', {});
const feeObject = this.safeValue (transaction, 'fee', {});
const id = this.safeString (transaction, 'id');
const timestamp = this.parse8601 (this.safeValue (transaction, 'created_at'));
const updated = this.parse8601 (this.safeValue (transaction, 'updated_at'));
const type = this.safeString (transaction, 'resource');
const amount = this.safeNumber (subtotalObject, 'amount');
const currencyId = this.safeString (subtotalObject, 'currency');
const currency = this.safeCurrencyCode (currencyId);
const feeCost = this.safeNumber (feeObject, 'amount');
const feeCurrencyId = this.safeString (feeObject, 'currency');
const feeCurrency = this.safeCurrencyCode (feeCurrencyId);
const fee = {
'cost': feeCost,
'currency': feeCurrency,
};
let status = this.parseTransactionStatus (this.safeString (transaction, 'status'));
if (status === undefined) {
const committed = this.safeValue (transaction, 'committed');
status = committed ? 'ok' : 'pending';
}
return {
'info': transaction,
'id': id,
'txid': id,
'timestamp': timestamp,
'datetime': this.iso8601 (timestamp),
'network': undefined,
'address': undefined,
'addressTo': undefined,
'addressFrom': undefined,
'tag': undefined,
'tagTo': undefined,
'tagFrom': undefined,
'type': type,
'amount': amount,
'currency': currency,
'status': status,
'updated': updated,
'fee': fee,
};
}
parseTrade (trade, market = undefined) {
//
// fetchMyBuys, fetchMySells
//
// {
// "id": "67e0eaec-07d7-54c4-a72c-2e92826897df",
// "status": "completed",
// "payment_method": {
// "id": "83562370-3e5c-51db-87da-752af5ab9559",
// "resource": "payment_method",
// "resource_path": "/v2/payment-methods/83562370-3e5c-51db-87da-752af5ab9559"
// },
// "transaction": {
// "id": "441b9494-b3f0-5b98-b9b0-4d82c21c252a",
// "resource": "transaction",
// "resource_path": "/v2/accounts/2bbf394c-193b-5b2a-9155-3b4732659ede/transactions/441b9494-b3f0-5b98-b9b0-4d82c21c252a"
// },
// "amount": { "amount": "1.00000000", "currency": "BTC" },
// "total": { "amount": "10.25", "currency": "USD" },
// "subtotal": { "amount": "10.10", "currency": "USD" },
// "created_at": "2015-01-31T20:49:02Z",
// "updated_at": "2015-02-11T16:54:02-08:00",
// "resource": "buy",
// "resource_path": "/v2/accounts/2bbf394c-193b-5b2a-9155-3b4732659ede/buys/67e0eaec-07d7-54c4-a72c-2e92826897df",
// "committed": true,
// "instant": false,
// "fee": { "amount": "0.15", "currency": "USD" },
// "payout_at": "2015-02-18T16:54:00-08:00"
// }
//
// fetchTrades
//
// {
// "trade_id": "10092327",
// "product_id": "BTC-USDT",
// "price": "17488.12",
// "size": "0.0000623",
// "time": "2023-01-11T00:52:37.557001Z",
// "side": "BUY",
// "bid": "",
// "ask": ""
// }
//
// fetchMyTrades
//
// {
// "entry_id": "b88b82cc89e326a2778874795102cbafd08dd979a2a7a3c69603fc4c23c2e010",
// "trade_id": "cdc39e45-bbd3-44ec-bf02-61742dfb16a1",
// "order_id": "813a53c5-3e39-47bb-863d-2faf685d22d8",
// "trade_time": "2023-01-18T01:37:38.091377090Z",
// "trade_type": "FILL",
// "price": "21220.64",
// "size": "0.0046830664333996",
// "commission": "0.0000280983986004",
// "product_id": "BTC-USDT",
// "sequence_timestamp": "2023-01-18T01:37:38.092520Z",
// "liquidity_indicator": "UNKNOWN_LIQUIDITY_INDICATOR",
// "size_in_quote": true,
// "user_id": "1111111-1111-1111-1111-111111111111",
// "side": "BUY"
// }
//
let symbol = undefined;
const totalObject = this.safeValue (trade, 'total', {});
const amountObject = this.safeValue (trade, 'amount', {});
const subtotalObject = this.safeValue (trade, 'subtotal', {});
const feeObject = this.safeValue (trade, 'fee', {});
const marketId = this.safeString (trade, 'product_id');
market = this.safeMarket (marketId, market, '-');
if (market !== undefined) {
symbol = market['symbol'];
} else {
const baseId = this.safeString (amountObject, 'currency');
const quoteId = this.safeString (totalObject, 'currency');
if ((baseId !== undefined) && (quoteId !== undefined)) {
const base = this.safeCurrencyCode (baseId);
const quote = this.safeCurrencyCode (quoteId);
symbol = base + '/' + quote;
}
}
const sizeInQuote = this.safeValue (trade, 'size_in_quote');
const v3Price = this.safeString (trade, 'price');
const v3Amount = (sizeInQuote) ? undefined : this.safeString (trade, 'size');
const v3Cost = (sizeInQuote) ? this.safeString (trade, 'size') : undefined;
const v3FeeCost = this.safeString (trade, 'commission');
const amountString = this.safeString (amountObject, 'amount', v3Amount);
const costString = this.safeString (subtotalObject, 'amount', v3Cost);
let priceString = undefined;
let cost = undefined;
if ((costString !== undefined) && (amountString !== undefined)) {
priceString = Precise.stringDiv (costString, amountString);
} else {
priceString = v3Price;
}
if ((priceString !== undefined) && (amountString !== undefined)) {
cost = Precise.stringMul (priceString, amountString);
} else {
cost = costString;
}
const feeCurrencyId = this.safeString (feeObject, 'currency');
const datetime = this.safeStringN (trade, [ 'created_at', 'trade_time', 'time' ]);
const side = this.safeStringLower2 (trade, 'resource', 'side');
const takerOrMaker = this.safeStringLower (trade, 'liquidity_indicator');
return this.safeTrade ({
'info': trade,
'id': this.safeString2 (trade, 'id', 'trade_id'),
'order': this.safeString (trade, 'order_id'),
'timestamp': this.parse8601 (datetime),
'datetime': datetime,
'symbol': symbol,
'type': undefined,
'side': (side === 'unknown_order_side') ? undefined : side,
'takerOrMaker': (takerOrMaker === 'unknown_liquidity_indicator') ? undefined : takerOrMaker,
'price': priceString,
'amount': amountString,
'cost': cost,
'fee': {
'cost': this.safeNumber (feeObject, 'amount', v3FeeCost),
'currency': this.safeCurrencyCode (feeCurrencyId),
},
});
}
async fetchMarkets (params = {}) {
/**
* @method
* @name coinbase#fetchMarkets
* @description retrieves data on all markets for coinbase
* @param {object} params extra parameters specific to the exchange api endpoint
* @returns {[object]} an array of objects representing market data
*/
const method = this.safeString (this.options, 'fetchMarkets', 'fetchMarketsV3');
return await this[method] (params);
}
async fetchMarketsV2 (params = {}) {
const response = await this.fetchCurrenciesFromCache (params);
const currencies = this.safeValue (response, 'currencies', {});
const exchangeRates = this.safeValue (response, 'exchangeRates', {});
const data = this.safeValue (currencies, 'data', []);
const dataById = this.indexBy (data, 'id');
const rates = this.safeValue (this.safeValue (exchangeRates, 'data', {}), 'rates', {});
const baseIds = Object.keys (rates);
const result = [];
for (let i = 0; i < baseIds.length; i++) {
const baseId = baseIds[i];
const base = this.safeCurrencyCode (baseId);
const type = (baseId in dataById) ? 'fiat' : 'crypto';
// https://github.com/ccxt/ccxt/issues/6066
if (type === 'crypto') {
for (let j = 0; j < data.length; j++) {
const quoteCurrency = data[j];
const quoteId = this.safeString (quoteCurrency, 'id');
const quote = this.safeCurrencyCode (quoteId);
result.push ({
'id': baseId + '-' + quoteId,
'symbol': base + '/' + quote,
'base': base,
'quote': quote,
'settle': undefined,
'baseId': baseId,
'quoteId': quoteId,
'settleId': undefined,
'type': 'spot',
'spot': true,
'margin': false,
'swap': false,
'future': false,
'option': false,
'active': undefined,
'contract': false,
'linear': undefined,
'inverse': undefined,
'contractSize': undefined,
'expiry': undefined,
'expiryDatetime': undefined,
'strike': undefined,
'optionType': undefined,
'precision': {
'amount': undefined,
'price': undefined,
},
'limits': {
'leverage': {
'min': undefined,
'max': undefined,
},
'amount': {
'min': undefined,
'max': undefined,
},
'price': {
'min': undefined,
'max': undefined,
},
'cost': {
'min': this.safeNumber (quoteCurrency, 'min_size'),
'max': undefined,
},
},
'info': quoteCurrency,
});
}
}
}
return result;
}
async fetchMarketsV3 (params = {}) {
const response = await this.v3PrivateGetBrokerageProducts (params);
//
// [
// {
// "product_id": "TONE-USD",
// "price": "0.01523",
// "price_percentage_change_24h": "1.94109772423025",
// "volume_24h": "19773129",
// "volume_percentage_change_24h": "437.0170530929949",
// "base_increment": "1",
// "quote_increment": "0.00001",
// "quote_min_size": "1",
// "quote_max_size": "10000000",
// "base_min_size": "26.7187147229469674",
// "base_max_size": "267187147.2294696735908216",
// "base_name": "TE-FOOD",
// "quote_name": "US Dollar",
// "watched": false,
// "is_disabled": false,
// "new": false,
// "status": "online",
// "cancel_only": false,
// "limit_only": false,
// "post_only": false,
// "trading_disabled": false,
// "auction_mode": false,
// "product_type": "SPOT",
// "quote_currency_id": "USD",
// "base_currency_id": "TONE",
// "fcm_trading_session_details": null,
// "mid_market_price": ""
// },
// ...
// ]
//