ccxt 4.5.10 → 4.5.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/README.md +32 -33
  2. package/dist/ccxt.browser.min.js +3 -3
  3. package/dist/cjs/ccxt.js +1 -6
  4. package/dist/cjs/src/apex.js +9 -0
  5. package/dist/cjs/src/base/Exchange.js +15 -3
  6. package/dist/cjs/src/base/functions/throttle.js +1 -1
  7. package/dist/cjs/src/binance.js +1 -1
  8. package/dist/cjs/src/bingx.js +1 -1
  9. package/dist/cjs/src/bitfinex.js +4 -2
  10. package/dist/cjs/src/bitget.js +20 -2
  11. package/dist/cjs/src/bitmart.js +2 -2
  12. package/dist/cjs/src/btcmarkets.js +4 -2
  13. package/dist/cjs/src/bybit.js +28 -7
  14. package/dist/cjs/src/coinbase.js +1 -0
  15. package/dist/cjs/src/coincatch.js +2 -1
  16. package/dist/cjs/src/deribit.js +21 -19
  17. package/dist/cjs/src/kraken.js +4 -0
  18. package/dist/cjs/src/krakenfutures.js +26 -5
  19. package/dist/cjs/src/kucoin.js +1 -1
  20. package/dist/cjs/src/latoken.js +1 -0
  21. package/dist/cjs/src/mexc.js +1 -0
  22. package/dist/cjs/src/okx.js +18 -9
  23. package/dist/cjs/src/onetrading.js +2 -1
  24. package/dist/cjs/src/phemex.js +1 -0
  25. package/dist/cjs/src/pro/alpaca.js +17 -1
  26. package/dist/cjs/src/pro/apex.js +17 -0
  27. package/dist/cjs/src/pro/binance.js +5 -5
  28. package/dist/cjs/src/pro/bitget.js +10 -3
  29. package/dist/cjs/src/pro/coinbase.js +8 -7
  30. package/dist/cjs/src/pro/htx.js +22 -3
  31. package/dist/cjs/src/pro/hyperliquid.js +4 -0
  32. package/dist/cjs/src/pro/kraken.js +282 -458
  33. package/dist/cjs/src/pro/mexc.js +16 -11
  34. package/dist/cjs/src/pro/woo.js +48 -4
  35. package/dist/cjs/src/probit.js +1 -1
  36. package/dist/cjs/src/woo.js +20 -2
  37. package/dist/cjs/src/xt.js +1 -1
  38. package/js/ccxt.d.ts +2 -8
  39. package/js/ccxt.js +2 -6
  40. package/js/src/abstract/coinbase.d.ts +1 -0
  41. package/js/src/abstract/coinbaseadvanced.d.ts +1 -0
  42. package/js/src/abstract/phemex.d.ts +1 -0
  43. package/js/src/abstract/xt.d.ts +1 -1
  44. package/js/src/apex.js +9 -0
  45. package/js/src/base/Exchange.d.ts +5 -2
  46. package/js/src/base/Exchange.js +15 -3
  47. package/js/src/base/functions/throttle.js +1 -1
  48. package/js/src/binance.js +1 -1
  49. package/js/src/bingx.js +1 -1
  50. package/js/src/bitfinex.d.ts +1 -1
  51. package/js/src/bitfinex.js +4 -2
  52. package/js/src/bitget.d.ts +15 -2
  53. package/js/src/bitget.js +20 -2
  54. package/js/src/bithumb.d.ts +1 -1
  55. package/js/src/bitmart.js +2 -2
  56. package/js/src/bitmex.d.ts +1 -1
  57. package/js/src/bitopro.d.ts +1 -1
  58. package/js/src/bitso.d.ts +1 -1
  59. package/js/src/bittrade.d.ts +1 -1
  60. package/js/src/blofin.d.ts +1 -1
  61. package/js/src/btcmarkets.d.ts +1 -1
  62. package/js/src/btcmarkets.js +4 -2
  63. package/js/src/bybit.d.ts +1 -1
  64. package/js/src/bybit.js +28 -7
  65. package/js/src/coinbase.d.ts +1 -1
  66. package/js/src/coinbase.js +1 -0
  67. package/js/src/coincatch.js +2 -1
  68. package/js/src/coinex.d.ts +1 -1
  69. package/js/src/cryptocom.d.ts +1 -1
  70. package/js/src/deribit.js +21 -19
  71. package/js/src/digifinex.d.ts +1 -1
  72. package/js/src/hibachi.d.ts +1 -1
  73. package/js/src/htx.d.ts +1 -1
  74. package/js/src/hyperliquid.d.ts +1 -1
  75. package/js/src/kraken.d.ts +1 -1
  76. package/js/src/kraken.js +4 -0
  77. package/js/src/krakenfutures.js +26 -5
  78. package/js/src/kucoin.js +1 -1
  79. package/js/src/kucoinfutures.d.ts +1 -1
  80. package/js/src/latoken.js +1 -0
  81. package/js/src/mexc.d.ts +1 -1
  82. package/js/src/mexc.js +1 -0
  83. package/js/src/oceanex.d.ts +1 -1
  84. package/js/src/okx.d.ts +1 -1
  85. package/js/src/okx.js +18 -9
  86. package/js/src/onetrading.d.ts +1 -1
  87. package/js/src/onetrading.js +2 -1
  88. package/js/src/phemex.js +1 -0
  89. package/js/src/pro/alpaca.js +17 -1
  90. package/js/src/pro/apex.d.ts +2 -0
  91. package/js/src/pro/apex.js +18 -1
  92. package/js/src/pro/binance.js +5 -5
  93. package/js/src/pro/bitget.d.ts +1 -0
  94. package/js/src/pro/bitget.js +10 -3
  95. package/js/src/pro/bitvavo.d.ts +2 -2
  96. package/js/src/pro/coinbase.js +8 -7
  97. package/js/src/pro/htx.js +22 -3
  98. package/js/src/pro/hyperliquid.js +4 -0
  99. package/js/src/pro/kraken.d.ts +7 -8
  100. package/js/src/pro/kraken.js +282 -458
  101. package/js/src/pro/mexc.js +16 -11
  102. package/js/src/pro/woo.js +48 -4
  103. package/js/src/probit.js +1 -1
  104. package/js/src/timex.d.ts +1 -1
  105. package/js/src/woo.js +20 -2
  106. package/js/src/xt.js +1 -1
  107. package/package.json +1 -1
  108. package/js/src/abstract/okcoin.d.ts +0 -77
  109. package/js/src/abstract/okcoin.js +0 -11
  110. package/js/src/okcoin.d.ts +0 -346
  111. package/js/src/okcoin.js +0 -3214
  112. package/js/src/pro/okcoin.d.ts +0 -91
  113. package/js/src/pro/okcoin.js +0 -763
@@ -1,763 +0,0 @@
1
- // ----------------------------------------------------------------------------
2
-
3
- // PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN:
4
- // https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code
5
- // EDIT THE CORRESPONDENT .ts FILE INSTEAD
6
-
7
- // ---------------------------------------------------------------------------
8
- import okcoinRest from '../okcoin.js';
9
- import { ArgumentsRequired, AuthenticationError } from '../base/errors.js';
10
- import { ArrayCache, ArrayCacheByTimestamp, ArrayCacheBySymbolById } from '../base/ws/Cache.js';
11
- import { sha256 } from '../static_dependencies/noble-hashes/sha256.js';
12
- // ---------------------------------------------------------------------------
13
- export default class okcoin extends okcoinRest {
14
- describe() {
15
- return this.deepExtend(super.describe(), {
16
- 'has': {
17
- 'ws': true,
18
- 'watchTicker': true,
19
- 'watchTickers': false,
20
- 'watchOrderBook': true,
21
- 'watchOrders': true,
22
- 'watchTrades': true,
23
- 'watchTradesForSymbols': false,
24
- 'watchBalance': true,
25
- 'watchOHLCV': true,
26
- },
27
- 'urls': {
28
- 'api': {
29
- 'ws': 'wss://real.okcoin.com:8443/ws/v3',
30
- },
31
- 'logo': 'https://user-images.githubusercontent.com/1294454/27766791-89ffb502-5ee5-11e7-8a5b-c5950b68ac65.jpg',
32
- 'www': 'https://www.okcoin.com',
33
- 'doc': 'https://www.okcoin.com/docs/en/',
34
- 'fees': 'https://www.okcoin.com/coin-fees',
35
- 'referral': 'https://www.okcoin.com/account/register?flag=activity&channelId=600001513',
36
- },
37
- 'options': {
38
- 'fetchMarkets': ['spot'],
39
- 'watchOrders': 'order',
40
- 'watchOrderBook': {
41
- 'limit': 400,
42
- 'type': 'spot',
43
- 'depth': 'depth_l2_tbt', // depth5, depth
44
- },
45
- 'watchBalance': 'spot',
46
- 'ws': {
47
- 'inflate': true,
48
- },
49
- },
50
- 'streaming': {
51
- // okex does not support built-in ws protocol-level ping-pong
52
- // instead it requires a custom text-based ping-pong
53
- 'ping': this.ping,
54
- 'keepAlive': 20000,
55
- },
56
- });
57
- }
58
- async subscribe(channel, symbol, params = {}) {
59
- await this.loadMarkets();
60
- const market = this.market(symbol);
61
- const url = this.urls['api']['ws'];
62
- const messageHash = market['type'] + '/' + channel + ':' + market['id'];
63
- const request = {
64
- 'op': 'subscribe',
65
- 'args': [messageHash],
66
- };
67
- return await this.watch(url, messageHash, this.deepExtend(request, params), messageHash);
68
- }
69
- /**
70
- * @method
71
- * @name okcoin#watchTrades
72
- * @description get the list of most recent trades for a particular symbol
73
- * @see https://www.okcoin.com/docs-v5/en/#websocket-api-public-channel-trades-channel
74
- * @param {string} symbol unified symbol of the market to fetch trades for
75
- * @param {int} [since] timestamp in ms of the earliest trade to fetch
76
- * @param {int} [limit] the maximum amount of trades to fetch
77
- * @param {object} [params] extra parameters specific to the exchange API endpoint
78
- * @returns {object[]} a list of [trade structures]{@link https://docs.ccxt.com/#/?id=public-trades}
79
- */
80
- async watchTrades(symbol, since = undefined, limit = undefined, params = {}) {
81
- await this.loadMarkets();
82
- symbol = this.symbol(symbol);
83
- const trades = await this.subscribe('trade', symbol, params);
84
- if (this.newUpdates) {
85
- limit = trades.getLimit(symbol, limit);
86
- }
87
- return this.filterBySinceLimit(trades, since, limit, 'timestamp', true);
88
- }
89
- /**
90
- * @method
91
- * @name okcoin#watchOrders
92
- * @description watches information on multiple orders made by the user
93
- * @see https://www.okcoin.com/docs-v5/en/#websocket-api-private-channel-order-channel
94
- * @param {string} symbol unified market symbol of the market orders were made in
95
- * @param {int} [since] the earliest time in ms to fetch orders for
96
- * @param {int} [limit] the maximum number of order structures to retrieve
97
- * @param {object} [params] extra parameters specific to the exchange API endpoint
98
- * @returns {object[]} a list of [order structures]{@link https://docs.ccxt.com/#/?id=order-structure}
99
- */
100
- async watchOrders(symbol = undefined, since = undefined, limit = undefined, params = {}) {
101
- await this.loadMarkets();
102
- await this.authenticate();
103
- if (symbol !== undefined) {
104
- symbol = this.symbol(symbol);
105
- }
106
- const orderType = this.safeString(this.options, 'watchOrders', 'order');
107
- const trades = await this.subscribe(orderType, symbol, params);
108
- if (this.newUpdates) {
109
- limit = trades.getLimit(symbol, limit);
110
- }
111
- return this.filterBySinceLimit(trades, since, limit, 'timestamp', true);
112
- }
113
- handleOrders(client, message, subscription = undefined) {
114
- //
115
- // {
116
- // "table": "spot/order",
117
- // "data": [
118
- // {
119
- // "client_oid": '',
120
- // "created_at": "2022-03-04T16:44:58.530Z",
121
- // "event_code": "0",
122
- // "event_message": '',
123
- // "fee": '',
124
- // "fee_currency": '',
125
- // "filled_notional": "0",
126
- // "filled_size": "0",
127
- // "instrument_id": "LTC-USD",
128
- // "last_amend_result": '',
129
- // "last_fill_id": "0",
130
- // "last_fill_px": "0",
131
- // "last_fill_qty": "0",
132
- // "last_fill_time": "1970-01-01T00:00:00.000Z",
133
- // "last_request_id": '',
134
- // "margin_trading": "1",
135
- // "notional": '',
136
- // "order_id": "8629537900471296",
137
- // "order_type": "0",
138
- // "price": "1500",
139
- // "rebate": '',
140
- // "rebate_currency": '',
141
- // "side": "sell",
142
- // "size": "0.0133",
143
- // "state": "0",
144
- // "status": "open",
145
- // "timestamp": "2022-03-04T16:44:58.530Z",
146
- // "type": "limit"
147
- // }
148
- // ]
149
- // }
150
- //
151
- const table = this.safeString(message, 'table');
152
- const orders = this.safeValue(message, 'data', []);
153
- const ordersLength = orders.length;
154
- if (ordersLength > 0) {
155
- const limit = this.safeInteger(this.options, 'ordersLimit', 1000);
156
- if (this.orders === undefined) {
157
- this.orders = new ArrayCacheBySymbolById(limit);
158
- }
159
- const stored = this.orders;
160
- const marketIds = {};
161
- const parsed = this.parseOrders(orders);
162
- for (let i = 0; i < parsed.length; i++) {
163
- const order = parsed[i];
164
- stored.append(order);
165
- const symbol = order['symbol'];
166
- const market = this.market(symbol);
167
- marketIds[market['id']] = true;
168
- }
169
- const keys = Object.keys(marketIds);
170
- for (let i = 0; i < keys.length; i++) {
171
- const messageHash = table + ':' + keys[i];
172
- client.resolve(this.orders, messageHash);
173
- }
174
- }
175
- }
176
- /**
177
- * @method
178
- * @name okcoin#watchTicker
179
- * @description watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market
180
- * @see https://www.okcoin.com/docs-v5/en/#websocket-api-public-channel-tickers-channel
181
- * @param {string} symbol unified symbol of the market to fetch the ticker for
182
- * @param {object} [params] extra parameters specific to the exchange API endpoint
183
- * @returns {object} a [ticker structure]{@link https://docs.ccxt.com/#/?id=ticker-structure}
184
- */
185
- async watchTicker(symbol, params = {}) {
186
- return await this.subscribe('ticker', symbol, params);
187
- }
188
- handleTrade(client, message) {
189
- //
190
- // {
191
- // "table": "spot/trade",
192
- // "data": [
193
- // {
194
- // "side": "buy",
195
- // "trade_id": "30770973",
196
- // "price": "4665.4",
197
- // "size": "0.019",
198
- // "instrument_id": "BTC-USDT",
199
- // "timestamp": "2020-03-16T13:41:46.526Z"
200
- // }
201
- // ]
202
- // }
203
- //
204
- const table = this.safeString(message, 'table');
205
- const data = this.safeValue(message, 'data', []);
206
- const tradesLimit = this.safeInteger(this.options, 'tradesLimit', 1000);
207
- for (let i = 0; i < data.length; i++) {
208
- const trade = this.parseTrade(data[i]);
209
- const symbol = trade['symbol'];
210
- const marketId = this.safeString(trade['info'], 'instrument_id');
211
- const messageHash = table + ':' + marketId;
212
- let stored = this.safeValue(this.trades, symbol);
213
- if (stored === undefined) {
214
- stored = new ArrayCache(tradesLimit);
215
- this.trades[symbol] = stored;
216
- }
217
- stored.append(trade);
218
- client.resolve(stored, messageHash);
219
- }
220
- return message;
221
- }
222
- handleTicker(client, message) {
223
- //
224
- // {
225
- // "table": "spot/ticker",
226
- // "data": [
227
- // {
228
- // "last": "4634.1",
229
- // "open_24h": "5305.6",
230
- // "best_bid": "4631.6",
231
- // "high_24h": "5950",
232
- // "low_24h": "4448.8",
233
- // "base_volume_24h": "147913.11435388",
234
- // "quote_volume_24h": "756850119.99108082",
235
- // "best_ask": "4631.7",
236
- // "instrument_id": "BTC-USDT",
237
- // "timestamp": "2020-03-16T13:16:25.677Z",
238
- // "best_bid_size": "0.12348942",
239
- // "best_ask_size": "0.00100014",
240
- // "last_qty": "0.00331822"
241
- // }
242
- // ]
243
- // }
244
- //
245
- const table = this.safeString(message, 'table');
246
- const data = this.safeValue(message, 'data', []);
247
- for (let i = 0; i < data.length; i++) {
248
- const ticker = this.parseTicker(data[i]);
249
- const symbol = ticker['symbol'];
250
- const marketId = this.safeString(ticker['info'], 'instrument_id');
251
- const messageHash = table + ':' + marketId;
252
- this.tickers[symbol] = ticker;
253
- client.resolve(ticker, messageHash);
254
- }
255
- return message;
256
- }
257
- /**
258
- * @method
259
- * @name okcoin#watchOHLCV
260
- * @description watches historical candlestick data containing the open, high, low, and close price, and the volume of a market
261
- * @see https://www.okcoin.com/docs-v5/en/#websocket-api-public-channel-candlesticks-channel
262
- * @param {string} symbol unified symbol of the market to fetch OHLCV data for
263
- * @param {string} timeframe the length of time each candle represents
264
- * @param {int} [since] timestamp in ms of the earliest candle to fetch
265
- * @param {int} [limit] the maximum amount of candles to fetch
266
- * @param {object} [params] extra parameters specific to the exchange API endpoint
267
- * @returns {int[][]} A list of candles ordered as timestamp, open, high, low, close, volume
268
- */
269
- async watchOHLCV(symbol, timeframe = '1m', since = undefined, limit = undefined, params = {}) {
270
- await this.loadMarkets();
271
- symbol = this.symbol(symbol);
272
- const interval = this.safeString(this.timeframes, timeframe, timeframe);
273
- const name = 'candle' + interval + 's';
274
- const ohlcv = await this.subscribe(name, symbol, params);
275
- if (this.newUpdates) {
276
- limit = ohlcv.getLimit(symbol, limit);
277
- }
278
- return this.filterBySinceLimit(ohlcv, since, limit, 0, true);
279
- }
280
- handleOHLCV(client, message) {
281
- //
282
- // {
283
- // "table": "spot/candle60s",
284
- // "data": [
285
- // {
286
- // "candle": [
287
- // "2020-03-16T14:29:00.000Z",
288
- // "4948.3",
289
- // "4966.7",
290
- // "4939.1",
291
- // "4945.3",
292
- // "238.36021657"
293
- // ],
294
- // "instrument_id": "BTC-USDT"
295
- // }
296
- // ]
297
- // }
298
- //
299
- const table = this.safeString(message, 'table');
300
- const data = this.safeValue(message, 'data', []);
301
- const parts = table.split('/');
302
- const part1 = this.safeString(parts, 1);
303
- let interval = part1.replace('candle', '');
304
- interval = interval.replace('s', '');
305
- // use a reverse lookup in a static map instead
306
- const timeframe = this.findTimeframe(interval);
307
- for (let i = 0; i < data.length; i++) {
308
- const marketId = this.safeString(data[i], 'instrument_id');
309
- const candle = this.safeValue(data[i], 'candle');
310
- const market = this.safeMarket(marketId);
311
- const symbol = market['symbol'];
312
- const parsed = this.parseOHLCV(candle, market);
313
- this.ohlcvs[symbol] = this.safeValue(this.ohlcvs, symbol, {});
314
- let stored = this.safeValue(this.ohlcvs[symbol], timeframe);
315
- if (stored === undefined) {
316
- const limit = this.safeInteger(this.options, 'OHLCVLimit', 1000);
317
- stored = new ArrayCacheByTimestamp(limit);
318
- this.ohlcvs[symbol][timeframe] = stored;
319
- }
320
- stored.append(parsed);
321
- const messageHash = table + ':' + marketId;
322
- client.resolve(stored, messageHash);
323
- }
324
- }
325
- /**
326
- * @method
327
- * @name okcoin#watchOrderBook
328
- * @description watches information on open orders with bid (buy) and ask (sell) prices, volumes and other data
329
- * @see https://www.okcoin.com/docs-v5/en/#websocket-api-public-channel-order-book-channel
330
- * @param {string} symbol unified symbol of the market to fetch the order book for
331
- * @param {int} [limit] the maximum amount of order book entries to return
332
- * @param {object} [params] extra parameters specific to the exchange API endpoint
333
- * @returns {object} A dictionary of [order book structures]{@link https://docs.ccxt.com/#/?id=order-book-structure} indexed by market symbols
334
- */
335
- async watchOrderBook(symbol, limit = undefined, params = {}) {
336
- const options = this.safeValue(this.options, 'watchOrderBook', {});
337
- const depth = this.safeString(options, 'depth', 'depth_l2_tbt');
338
- const orderbook = await this.subscribe(depth, symbol, params);
339
- return orderbook.limit();
340
- }
341
- handleDelta(bookside, delta) {
342
- const price = this.safeFloat(delta, 0);
343
- const amount = this.safeFloat(delta, 1);
344
- bookside.store(price, amount);
345
- }
346
- handleDeltas(bookside, deltas) {
347
- for (let i = 0; i < deltas.length; i++) {
348
- this.handleDelta(bookside, deltas[i]);
349
- }
350
- }
351
- handleOrderBookMessage(client, message, orderbook) {
352
- //
353
- // {
354
- // "instrument_id": "BTC-USDT",
355
- // "asks": [
356
- // ["4568.5", "0.49723138", "2"],
357
- // ["4568.7", "0.5013", "1"],
358
- // ["4569.1", "0.4398", "1"],
359
- // ],
360
- // "bids": [
361
- // ["4568.4", "0.84187666", "5"],
362
- // ["4568.3", "0.75661506", "6"],
363
- // ["4567.8", "2.01", "2"],
364
- // ],
365
- // "timestamp": "2020-03-16T11:11:43.388Z",
366
- // "checksum": 473370408
367
- // }
368
- //
369
- const asks = this.safeValue(message, 'asks', []);
370
- const bids = this.safeValue(message, 'bids', []);
371
- this.handleDeltas(orderbook['asks'], asks);
372
- this.handleDeltas(orderbook['bids'], bids);
373
- const timestamp = this.parse8601(this.safeString(message, 'timestamp'));
374
- orderbook['timestamp'] = timestamp;
375
- orderbook['datetime'] = this.iso8601(timestamp);
376
- return orderbook;
377
- }
378
- handleOrderBook(client, message) {
379
- //
380
- // first message (snapshot)
381
- //
382
- // {
383
- // "table": "spot/depth",
384
- // "action": "partial",
385
- // "data": [
386
- // {
387
- // "instrument_id": "BTC-USDT",
388
- // "asks": [
389
- // ["4568.5", "0.49723138", "2"],
390
- // ["4568.7", "0.5013", "1"],
391
- // ["4569.1", "0.4398", "1"],
392
- // ],
393
- // "bids": [
394
- // ["4568.4", "0.84187666", "5"],
395
- // ["4568.3", "0.75661506", "6"],
396
- // ["4567.8", "2.01", "2"],
397
- // ],
398
- // "timestamp": "2020-03-16T11:11:43.388Z",
399
- // "checksum": 473370408
400
- // }
401
- // ]
402
- // }
403
- //
404
- // subsequent updates
405
- //
406
- // {
407
- // "table": "spot/depth",
408
- // "action": "update",
409
- // "data": [
410
- // {
411
- // "instrument_id": "BTC-USDT",
412
- // "asks": [
413
- // ["4598.8", "0", "0"],
414
- // ["4599.1", "0", "0"],
415
- // ["4600.3", "0", "0"],
416
- // ],
417
- // "bids": [
418
- // ["4598.5", "0.08", "1"],
419
- // ["4598.2", "0.0337323", "1"],
420
- // ["4598.1", "0.12681801", "3"],
421
- // ],
422
- // "timestamp": "2020-03-16T11:20:35.139Z",
423
- // "checksum": 740786981
424
- // }
425
- // ]
426
- // }
427
- //
428
- const action = this.safeString(message, 'action');
429
- const data = this.safeValue(message, 'data', []);
430
- const table = this.safeString(message, 'table');
431
- if (action === 'partial') {
432
- for (let i = 0; i < data.length; i++) {
433
- const update = data[i];
434
- const marketId = this.safeString(update, 'instrument_id');
435
- const market = this.safeMarket(marketId);
436
- const symbol = market['symbol'];
437
- const options = this.safeValue(this.options, 'watchOrderBook', {});
438
- // default limit is 400 bidasks
439
- const limit = this.safeInteger(options, 'limit', 400);
440
- const orderbook = this.orderBook({}, limit);
441
- this.orderbooks[symbol] = orderbook;
442
- this.handleOrderBookMessage(client, update, orderbook);
443
- const messageHash = table + ':' + marketId;
444
- client.resolve(orderbook, messageHash);
445
- }
446
- }
447
- else {
448
- for (let i = 0; i < data.length; i++) {
449
- const update = data[i];
450
- const marketId = this.safeString(update, 'instrument_id');
451
- const market = this.safeMarket(marketId);
452
- const symbol = market['symbol'];
453
- if (symbol in this.orderbooks) {
454
- const orderbook = this.orderbooks[symbol];
455
- this.handleOrderBookMessage(client, update, orderbook);
456
- const messageHash = table + ':' + marketId;
457
- client.resolve(orderbook, messageHash);
458
- }
459
- }
460
- }
461
- return message;
462
- }
463
- async authenticate(params = {}) {
464
- this.checkRequiredCredentials();
465
- const url = this.urls['api']['ws'];
466
- const messageHash = 'login';
467
- const client = this.client(url);
468
- let future = this.safeValue(client.subscriptions, messageHash);
469
- if (future === undefined) {
470
- future = client.future('authenticated');
471
- const timestamp = this.seconds().toString();
472
- const method = 'GET';
473
- const path = '/users/self/verify';
474
- const auth = timestamp + method + path;
475
- const signature = this.hmac(this.encode(auth), this.encode(this.secret), sha256, 'base64');
476
- const request = {
477
- 'op': messageHash,
478
- 'args': [
479
- this.apiKey,
480
- this.password,
481
- timestamp,
482
- signature,
483
- ],
484
- };
485
- this.spawn(this.watch, url, messageHash, request, messageHash, future);
486
- }
487
- return await future;
488
- }
489
- /**
490
- * @method
491
- * @name okcoin#watchBalance
492
- * @description watch balance and get the amount of funds available for trading or funds locked in orders
493
- * @see https://www.okcoin.com/docs-v5/en/#websocket-api-private-channel-account-channel
494
- * @param {object} [params] extra parameters specific to the exchange API endpoint
495
- * @returns {object} a [balance structure]{@link https://docs.ccxt.com/#/?id=balance-structure}
496
- */
497
- async watchBalance(params = {}) {
498
- const defaultType = this.safeString2(this.options, 'watchBalance', 'defaultType');
499
- const type = this.safeString(params, 'type', defaultType);
500
- if (type === undefined) {
501
- throw new ArgumentsRequired(this.id + " watchBalance requires a type parameter (one of 'spot', 'margin', 'futures', 'swap')");
502
- }
503
- // const query = this.omit (params, 'type');
504
- const negotiation = await this.authenticate();
505
- return await this.subscribeToUserAccount(negotiation, params);
506
- }
507
- async subscribeToUserAccount(negotiation, params = {}) {
508
- const defaultType = this.safeString2(this.options, 'watchBalance', 'defaultType');
509
- const type = this.safeString(params, 'type', defaultType);
510
- if (type === undefined) {
511
- throw new ArgumentsRequired(this.id + " watchBalance requires a type parameter (one of 'spot', 'margin', 'futures', 'swap')");
512
- }
513
- await this.loadMarkets();
514
- const currencyId = this.safeString(params, 'currency');
515
- const code = this.safeString(params, 'code', this.safeCurrencyCode(currencyId));
516
- let currency = undefined;
517
- if (code !== undefined) {
518
- currency = this.currency(code);
519
- }
520
- const symbol = this.safeString(params, 'symbol');
521
- const market = this.market(symbol);
522
- const marketUndefined = (market === undefined);
523
- const currencyUndefined = (currency === undefined);
524
- if (type === 'spot') {
525
- if (currencyUndefined) {
526
- throw new ArgumentsRequired(this.id + " watchBalance requires a 'currency' (id) or a unified 'code' parameter for " + type + ' accounts');
527
- }
528
- }
529
- else if ((type === 'margin') || (type === 'swap') || (type === 'option')) {
530
- if (marketUndefined) {
531
- throw new ArgumentsRequired(this.id + " watchBalance requires a 'instrument_id' (id) or a unified 'symbol' parameter for " + type + ' accounts');
532
- }
533
- }
534
- else if (type === 'futures') {
535
- if (currencyUndefined && marketUndefined) {
536
- throw new ArgumentsRequired(this.id + " watchBalance requires a 'currency' (id), or unified 'code', or 'instrument_id' (id), or unified 'symbol' parameter for " + type + ' accounts');
537
- }
538
- }
539
- let suffix = undefined;
540
- if (!currencyUndefined) {
541
- suffix = currency['id'];
542
- }
543
- else if (!marketUndefined) {
544
- suffix = market['id'];
545
- }
546
- const accountType = (type === 'margin') ? 'spot' : type;
547
- const account = (type === 'margin') ? 'margin_account' : 'account';
548
- const messageHash = accountType + '/' + account;
549
- const subscriptionHash = messageHash + ':' + suffix;
550
- const url = this.urls['api']['ws'];
551
- const request = {
552
- 'op': 'subscribe',
553
- 'args': [subscriptionHash],
554
- };
555
- const query = this.omit(params, ['currency', 'code', 'instrument_id', 'symbol', 'type']);
556
- return await this.watch(url, messageHash, this.deepExtend(request, query), subscriptionHash);
557
- }
558
- handleBalance(client, message) {
559
- //
560
- // spot
561
- //
562
- // {
563
- // "table": "spot/account",
564
- // "data": [
565
- // {
566
- // "available": "11.044827320825",
567
- // "currency": "USDT",
568
- // "id": '',
569
- // "balance": "11.044827320825",
570
- // "hold": "0"
571
- // }
572
- // ]
573
- // }
574
- //
575
- // margin
576
- //
577
- // {
578
- // "table": "spot/margin_account",
579
- // "data": [
580
- // {
581
- // "maint_margin_ratio": "0.08",
582
- // "liquidation_price": "0",
583
- // "currency:USDT": { available: "0", balance: "0", borrowed: "0", hold: "0", lending_fee: "0" },
584
- // "tiers": "1",
585
- // "instrument_id": "ETH-USDT",
586
- // "currency:ETH": { available: "0", balance: "0", borrowed: "0", hold: "0", lending_fee: "0" }
587
- // }
588
- // ]
589
- // }
590
- //
591
- const table = this.safeString(message, 'table');
592
- const parts = table.split('/');
593
- const data = this.safeValue(message, 'data', []);
594
- this.balance['info'] = data;
595
- let type = this.safeString(parts, 0);
596
- if (type === 'spot') {
597
- const part1 = this.safeString(parts, 1);
598
- if (part1 === 'margin_account') {
599
- type = 'margin';
600
- }
601
- }
602
- for (let i = 0; i < data.length; i++) {
603
- const balance = this.parseBalanceByType(type, data);
604
- const oldBalance = this.safeValue(this.balance, type, {});
605
- const newBalance = this.deepExtend(oldBalance, balance);
606
- this.balance[type] = this.safeBalance(newBalance);
607
- client.resolve(this.balance[type], table);
608
- }
609
- }
610
- handleSubscriptionStatus(client, message) {
611
- //
612
- // {"event":"subscribe","channel":"spot/depth:BTC-USDT"}
613
- //
614
- // const channel = this.safeString (message, 'channel');
615
- // client.subscriptions[channel] = message;
616
- return message;
617
- }
618
- handleAuthenticate(client, message) {
619
- //
620
- // { event: "login", success: true }
621
- //
622
- client.resolve(message, 'authenticated');
623
- return message;
624
- }
625
- ping(client) {
626
- // okex does not support built-in ws protocol-level ping-pong
627
- // instead it requires custom text-based ping-pong
628
- return 'ping';
629
- }
630
- handlePong(client, message) {
631
- client.lastPong = this.milliseconds();
632
- return message;
633
- }
634
- handleErrorMessage(client, message) {
635
- //
636
- // { event: "error", message: "Invalid sign", errorCode: 30013 }
637
- // {"event":"error","message":"Unrecognized request: {\"event\":\"subscribe\",\"channel\":\"spot/depth:BTC-USDT\"}","errorCode":30039}
638
- // { event: "error", message: "Channel spot/order doesn't exist", errorCode: 30040 }
639
- //
640
- const errorCode = this.safeString(message, 'errorCode');
641
- try {
642
- if (errorCode !== undefined) {
643
- const feedback = this.id + ' ' + this.json(message);
644
- this.throwExactlyMatchedException(this.exceptions['exact'], errorCode, feedback);
645
- const messageString = this.safeValue(message, 'message');
646
- if (messageString !== undefined) {
647
- this.throwBroadlyMatchedException(this.exceptions['broad'], messageString, feedback);
648
- }
649
- }
650
- }
651
- catch (e) {
652
- if (e instanceof AuthenticationError) {
653
- client.reject(e, 'authenticated');
654
- const method = 'login';
655
- if (method in client.subscriptions) {
656
- delete client.subscriptions[method];
657
- }
658
- return false;
659
- }
660
- }
661
- return message;
662
- }
663
- handleMessage(client, message) {
664
- if (!this.handleErrorMessage(client, message)) {
665
- return;
666
- }
667
- //
668
- // {"event":"error","message":"Unrecognized request: {\"event\":\"subscribe\",\"channel\":\"spot/depth:BTC-USDT\"}","errorCode":30039}
669
- // {"event":"subscribe","channel":"spot/depth:BTC-USDT"}
670
- // {
671
- // "table": "spot/depth",
672
- // "action": "partial",
673
- // "data": [
674
- // {
675
- // "instrument_id": "BTC-USDT",
676
- // "asks": [
677
- // ["5301.8", "0.03763319", "1"],
678
- // ["5302.4", "0.00305", "2"],
679
- // ],
680
- // "bids": [
681
- // ["5301.7", "0.58911427", "6"],
682
- // ["5301.6", "0.01222922", "4"],
683
- // ],
684
- // "timestamp": "2020-03-16T03:25:00.440Z",
685
- // "checksum": -2088736623
686
- // }
687
- // ]
688
- // }
689
- // {
690
- // "table":"spot/order",
691
- // "data":[
692
- // {
693
- // "client_oid":"",
694
- // "filled_notional":"0",
695
- // "filled_size":"0",
696
- // "instrument_id":"ETC-USDT",
697
- // "last_fill_px":"0",
698
- // "last_fill_qty":"0",
699
- // "last_fill_time":"1970-01-01T00:00:00.000Z",
700
- // "margin_trading":"1",
701
- // "notional":"",
702
- // "order_id":"3576398568830976",
703
- // "order_type":"0",
704
- // "price":"5.826",
705
- // "side":"buy",
706
- // "size":"0.1",
707
- // "state":"0",
708
- // "status":"open",
709
- // "fee_currency":"ETC",
710
- // "fee":"-0.01",
711
- // "rebate_currency":"open",
712
- // "rebate":"0.05",
713
- // "timestamp":"2019-09-24T06:45:11.394Z",
714
- // "type":"limit",
715
- // "created_at":"2019-09-24T06:45:11.394Z"
716
- // }
717
- // ]
718
- // }
719
- //
720
- if (message === 'pong') {
721
- this.handlePong(client, message);
722
- return;
723
- }
724
- const table = this.safeString(message, 'table');
725
- if (table === undefined) {
726
- const event = this.safeString(message, 'event');
727
- if (event !== undefined) {
728
- const methods = {
729
- // 'info': this.handleSystemStatus,
730
- // 'book': 'handleOrderBook',
731
- 'login': this.handleAuthenticate,
732
- 'subscribe': this.handleSubscriptionStatus,
733
- };
734
- const method = this.safeValue(methods, event);
735
- if (method !== undefined) {
736
- method.call(this, client, message);
737
- }
738
- }
739
- }
740
- else {
741
- const parts = table.split('/');
742
- const name = this.safeString(parts, 1);
743
- const methods = {
744
- 'depth': this.handleOrderBook,
745
- 'depth5': this.handleOrderBook,
746
- 'depth_l2_tbt': this.handleOrderBook,
747
- 'ticker': this.handleTicker,
748
- 'trade': this.handleTrade,
749
- 'account': this.handleBalance,
750
- 'margin_account': this.handleBalance,
751
- 'order': this.handleOrders,
752
- // ...
753
- };
754
- let method = this.safeValue(methods, name);
755
- if (name.indexOf('candle') >= 0) {
756
- method = this.handleOHLCV;
757
- }
758
- if (method !== undefined) {
759
- method.call(this, client, message);
760
- }
761
- }
762
- }
763
- }