ccxt 4.4.85 → 4.4.87

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 (115) hide show
  1. package/README.md +18 -7
  2. package/dist/ccxt.browser.min.js +3 -3
  3. package/dist/cjs/ccxt.js +16 -9
  4. package/dist/cjs/src/abstract/bittrade.js +9 -0
  5. package/dist/cjs/src/abstract/modetrade.js +9 -0
  6. package/dist/cjs/src/ascendex.js +189 -155
  7. package/dist/cjs/src/base/Exchange.js +10 -8
  8. package/dist/cjs/src/bequant.js +1 -1
  9. package/dist/cjs/src/binance.js +1 -1
  10. package/dist/cjs/src/bitget.js +5 -4
  11. package/dist/cjs/src/bitmart.js +1 -1
  12. package/dist/cjs/src/bitteam.js +31 -0
  13. package/dist/cjs/src/bittrade.js +2049 -0
  14. package/dist/cjs/src/coinbase.js +2 -6
  15. package/dist/cjs/src/coinmetro.js +5 -1
  16. package/dist/cjs/src/deribit.js +4 -5
  17. package/dist/cjs/src/derive.js +4 -5
  18. package/dist/cjs/src/ellipx.js +2 -3
  19. package/dist/cjs/src/gate.js +92 -76
  20. package/dist/cjs/src/hollaex.js +107 -49
  21. package/dist/cjs/src/htx.js +30 -52
  22. package/dist/cjs/src/hyperliquid.js +36 -20
  23. package/dist/cjs/src/kraken.js +5 -8
  24. package/dist/cjs/src/mexc.js +2 -2
  25. package/dist/cjs/src/modetrade.js +2839 -0
  26. package/dist/cjs/src/ndax.js +25 -24
  27. package/dist/cjs/src/okcoin.js +12 -31
  28. package/dist/cjs/src/okx.js +104 -2
  29. package/dist/cjs/src/okxus.js +53 -0
  30. package/dist/cjs/src/onetrading.js +9 -6
  31. package/dist/cjs/src/oxfun.js +42 -114
  32. package/dist/cjs/src/paradex.js +10 -1
  33. package/dist/cjs/src/phemex.js +4 -6
  34. package/dist/cjs/src/poloniex.js +181 -170
  35. package/dist/cjs/src/pro/binance.js +1 -0
  36. package/dist/cjs/src/pro/bittrade.js +605 -0
  37. package/dist/cjs/src/pro/luno.js +6 -5
  38. package/dist/cjs/src/pro/mexc.js +3 -0
  39. package/dist/cjs/src/pro/modetrade.js +1334 -0
  40. package/dist/cjs/src/pro/okxus.js +38 -0
  41. package/dist/cjs/src/probit.js +18 -51
  42. package/dist/cjs/src/timex.js +5 -10
  43. package/dist/cjs/src/vertex.js +3 -4
  44. package/dist/cjs/src/whitebit.js +41 -11
  45. package/dist/cjs/src/woo.js +101 -77
  46. package/dist/cjs/src/woofipro.js +24 -21
  47. package/dist/cjs/src/xt.js +36 -44
  48. package/js/ccxt.d.ts +20 -11
  49. package/js/ccxt.js +14 -8
  50. package/js/src/abstract/modetrade.d.ts +122 -0
  51. package/js/src/abstract/myokx.d.ts +2 -0
  52. package/js/src/abstract/okx.d.ts +2 -0
  53. package/js/src/abstract/okxus.d.ts +352 -0
  54. package/js/src/abstract/okxus.js +11 -0
  55. package/js/src/ascendex.d.ts +2 -0
  56. package/js/src/ascendex.js +189 -155
  57. package/js/src/base/Exchange.js +10 -8
  58. package/js/src/bequant.js +1 -1
  59. package/js/src/binance.js +1 -1
  60. package/js/src/bitget.js +5 -4
  61. package/js/src/bitmart.js +1 -1
  62. package/js/src/bitteam.js +31 -0
  63. package/js/src/{huobijp.d.ts → bittrade.d.ts} +29 -29
  64. package/js/src/{huobijp.js → bittrade.js} +35 -35
  65. package/js/src/coinbase.js +2 -6
  66. package/js/src/coinmetro.js +5 -1
  67. package/js/src/deribit.js +4 -5
  68. package/js/src/derive.js +4 -3
  69. package/js/src/ellipx.d.ts +1 -1
  70. package/js/src/ellipx.js +3 -5
  71. package/js/src/gate.js +92 -76
  72. package/js/src/hollaex.js +107 -49
  73. package/js/src/htx.js +30 -52
  74. package/js/src/hyperliquid.js +36 -20
  75. package/js/src/kraken.js +5 -8
  76. package/js/src/mexc.js +2 -2
  77. package/js/src/modetrade.d.ts +475 -0
  78. package/js/src/modetrade.js +2840 -0
  79. package/js/src/ndax.js +25 -24
  80. package/js/src/okcoin.js +12 -31
  81. package/js/src/okx.d.ts +24 -1
  82. package/js/src/okx.js +104 -2
  83. package/js/src/okxus.d.ts +4 -0
  84. package/js/src/okxus.js +54 -0
  85. package/js/src/onetrading.js +9 -6
  86. package/js/src/oxfun.js +42 -114
  87. package/js/src/paradex.js +10 -1
  88. package/js/src/phemex.js +4 -6
  89. package/js/src/poloniex.d.ts +2 -0
  90. package/js/src/poloniex.js +181 -170
  91. package/js/src/pro/binance.js +1 -0
  92. package/js/src/pro/{huobijp.d.ts → bittrade.d.ts} +6 -6
  93. package/js/src/pro/{huobijp.js → bittrade.js} +7 -7
  94. package/js/src/pro/luno.js +6 -5
  95. package/js/src/pro/mexc.js +3 -0
  96. package/js/src/pro/modetrade.d.ts +155 -0
  97. package/js/src/pro/modetrade.js +1335 -0
  98. package/js/src/pro/okxus.d.ts +4 -0
  99. package/js/src/pro/okxus.js +39 -0
  100. package/js/src/probit.js +18 -51
  101. package/js/src/timex.js +5 -10
  102. package/js/src/vertex.js +3 -4
  103. package/js/src/whitebit.js +42 -11
  104. package/js/src/woo.d.ts +2 -0
  105. package/js/src/woo.js +101 -77
  106. package/js/src/woofipro.d.ts +2 -1
  107. package/js/src/woofipro.js +24 -21
  108. package/js/src/xt.js +36 -44
  109. package/package.json +1 -1
  110. package/js/src/abstract/kuna.d.ts +0 -185
  111. package/js/src/kuna.d.ts +0 -335
  112. package/js/src/kuna.js +0 -2006
  113. /package/js/src/abstract/{huobijp.d.ts → bittrade.d.ts} +0 -0
  114. /package/js/src/abstract/{huobijp.js → bittrade.js} +0 -0
  115. /package/js/src/abstract/{kuna.js → modetrade.js} +0 -0
@@ -0,0 +1,2840 @@
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 Exchange from './abstract/modetrade.js';
9
+ import { AuthenticationError, RateLimitExceeded, BadRequest, ExchangeError, InvalidOrder, InsufficientFunds, ArgumentsRequired, NetworkError, NotSupported } from './base/errors.js';
10
+ import { TICK_SIZE } from './base/functions/number.js';
11
+ import { Precise } from './base/Precise.js';
12
+ import { ecdsa, eddsa } from './base/functions/crypto.js';
13
+ import { ed25519 } from './static_dependencies/noble-curves/ed25519.js';
14
+ import { keccak_256 as keccak } from './static_dependencies/noble-hashes/sha3.js';
15
+ import { secp256k1 } from './static_dependencies/noble-curves/secp256k1.js';
16
+ // ---------------------------------------------------------------------------
17
+ /**
18
+ * @class modetrade
19
+ * @augments Exchange
20
+ */
21
+ export default class modetrade extends Exchange {
22
+ describe() {
23
+ return this.deepExtend(super.describe(), {
24
+ 'id': 'modetrade',
25
+ 'name': 'Mode Trade',
26
+ 'countries': ['KY'],
27
+ 'rateLimit': 100,
28
+ 'version': 'v1',
29
+ 'certified': true,
30
+ 'pro': true,
31
+ 'dex': true,
32
+ 'hostname': 'trade.mode.network',
33
+ 'has': {
34
+ 'CORS': undefined,
35
+ 'spot': false,
36
+ 'margin': false,
37
+ 'swap': true,
38
+ 'future': false,
39
+ 'option': false,
40
+ 'addMargin': false,
41
+ 'cancelAllOrders': true,
42
+ 'cancelOrder': true,
43
+ 'cancelOrders': true,
44
+ 'cancelWithdraw': false,
45
+ 'closeAllPositions': false,
46
+ 'closePosition': false,
47
+ 'createConvertTrade': false,
48
+ 'createDepositAddress': false,
49
+ 'createMarketBuyOrderWithCost': false,
50
+ 'createMarketOrder': false,
51
+ 'createMarketOrderWithCost': false,
52
+ 'createMarketSellOrderWithCost': false,
53
+ 'createOrder': true,
54
+ 'createOrderWithTakeProfitAndStopLoss': true,
55
+ 'createReduceOnlyOrder': true,
56
+ 'createStopLimitOrder': false,
57
+ 'createStopLossOrder': true,
58
+ 'createStopMarketOrder': false,
59
+ 'createStopOrder': false,
60
+ 'createTakeProfitOrder': true,
61
+ 'createTrailingAmountOrder': false,
62
+ 'createTrailingPercentOrder': false,
63
+ 'createTriggerOrder': true,
64
+ 'fetchAccounts': false,
65
+ 'fetchBalance': true,
66
+ 'fetchCanceledOrders': false,
67
+ 'fetchClosedOrder': false,
68
+ 'fetchClosedOrders': true,
69
+ 'fetchConvertCurrencies': false,
70
+ 'fetchConvertQuote': false,
71
+ 'fetchCurrencies': true,
72
+ 'fetchDepositAddress': false,
73
+ 'fetchDeposits': true,
74
+ 'fetchDepositsWithdrawals': true,
75
+ 'fetchFundingHistory': true,
76
+ 'fetchFundingInterval': true,
77
+ 'fetchFundingIntervals': false,
78
+ 'fetchFundingRate': true,
79
+ 'fetchFundingRateHistory': true,
80
+ 'fetchFundingRates': true,
81
+ 'fetchIndexOHLCV': false,
82
+ 'fetchLedger': true,
83
+ 'fetchLeverage': true,
84
+ 'fetchMarginAdjustmentHistory': false,
85
+ 'fetchMarginMode': false,
86
+ 'fetchMarkets': true,
87
+ 'fetchMarkOHLCV': false,
88
+ 'fetchMyTrades': true,
89
+ 'fetchOHLCV': true,
90
+ 'fetchOpenInterestHistory': false,
91
+ 'fetchOpenOrder': false,
92
+ 'fetchOpenOrders': true,
93
+ 'fetchOrder': true,
94
+ 'fetchOrderBook': true,
95
+ 'fetchOrders': true,
96
+ 'fetchOrderTrades': true,
97
+ 'fetchPosition': true,
98
+ 'fetchPositionMode': false,
99
+ 'fetchPositions': true,
100
+ 'fetchPremiumIndexOHLCV': false,
101
+ 'fetchStatus': true,
102
+ 'fetchTicker': false,
103
+ 'fetchTickers': false,
104
+ 'fetchTime': true,
105
+ 'fetchTrades': true,
106
+ 'fetchTradingFee': false,
107
+ 'fetchTradingFees': true,
108
+ 'fetchTransactions': 'emulated',
109
+ 'fetchTransfers': false,
110
+ 'fetchWithdrawals': true,
111
+ 'reduceMargin': false,
112
+ 'setLeverage': true,
113
+ 'setMargin': false,
114
+ 'setPositionMode': false,
115
+ 'transfer': false,
116
+ 'withdraw': true, // exchange have that endpoint disabled atm, but was once implemented in ccxt per old docs: https://kronosresearch.github.io/wootrade-documents/#token-withdraw
117
+ },
118
+ 'timeframes': {
119
+ '1m': '1m',
120
+ '5m': '5m',
121
+ '15m': '15m',
122
+ '30m': '30m',
123
+ '1h': '1h',
124
+ '4h': '4h',
125
+ '12h': '12h',
126
+ '1d': '1d',
127
+ '1w': '1w',
128
+ '1M': '1mon',
129
+ '1y': '1y',
130
+ },
131
+ 'urls': {
132
+ 'logo': 'https://github.com/user-attachments/assets/cec2b7f1-3b2b-4502-971b-447ee1937d6b',
133
+ 'api': {
134
+ 'public': 'https://api-evm.orderly.org',
135
+ 'private': 'https://api-evm.orderly.org',
136
+ },
137
+ 'test': {
138
+ 'public': 'https://testnet-api-evm.orderly.org',
139
+ 'private': 'https://testnet-api-evm.orderly.org',
140
+ },
141
+ 'www': 'https://trade.mode.network',
142
+ 'referral': {
143
+ 'url': 'https://trade.mode.network?ref=MODETRADE',
144
+ 'discount': 0.2,
145
+ },
146
+ },
147
+ 'api': {
148
+ 'v1': {
149
+ 'public': {
150
+ 'get': {
151
+ 'public/volume/stats': 1,
152
+ 'public/broker/name': 1,
153
+ 'public/chain_info/{broker_id}': 1,
154
+ 'public/system_info': 1,
155
+ 'public/vault_balance': 1,
156
+ 'public/insurancefund': 1,
157
+ 'public/chain_info': 1,
158
+ 'faucet/usdc': 1,
159
+ 'public/account': 1,
160
+ 'get_account': 1,
161
+ 'registration_nonce': 1,
162
+ 'get_orderly_key': 1,
163
+ 'public/liquidation': 1,
164
+ 'public/liquidated_positions': 1,
165
+ 'public/config': 1,
166
+ 'public/campaign/ranking': 10,
167
+ 'public/campaign/stats': 10,
168
+ 'public/campaign/user': 10,
169
+ 'public/campaign/stats/details': 10,
170
+ 'public/campaigns': 10,
171
+ 'public/points/leaderboard': 1,
172
+ 'client/points': 1,
173
+ 'public/points/epoch': 1,
174
+ 'public/points/epoch_dates': 1,
175
+ 'public/referral/check_ref_code': 1,
176
+ 'public/referral/verify_ref_code': 1,
177
+ 'referral/admin_info': 1,
178
+ 'referral/info': 1,
179
+ 'referral/referee_info': 1,
180
+ 'referral/referee_rebate_summary': 1,
181
+ 'referral/referee_history': 1,
182
+ 'referral/referral_history': 1,
183
+ 'referral/rebate_summary': 1,
184
+ 'client/distribution_history': 1,
185
+ 'tv/config': 1,
186
+ 'tv/history': 1,
187
+ 'tv/symbol_info': 1,
188
+ 'public/funding_rate_history': 1,
189
+ 'public/funding_rate/{symbol}': 0.33,
190
+ 'public/funding_rates': 1,
191
+ 'public/info': 1,
192
+ 'public/info/{symbol}': 1,
193
+ 'public/market_trades': 1,
194
+ 'public/token': 1,
195
+ 'public/futures': 1,
196
+ 'public/futures/{symbol}': 1,
197
+ },
198
+ 'post': {
199
+ 'register_account': 1,
200
+ },
201
+ },
202
+ 'private': {
203
+ 'get': {
204
+ 'client/key_info': 6,
205
+ 'client/orderly_key_ip_restriction': 6,
206
+ 'order/{oid}': 1,
207
+ 'client/order/{client_order_id}': 1,
208
+ 'algo/order/{oid}': 1,
209
+ 'algo/client/order/{client_order_id}': 1,
210
+ 'orders': 1,
211
+ 'algo/orders': 1,
212
+ 'trade/{tid}': 1,
213
+ 'trades': 1,
214
+ 'order/{oid}/trades': 1,
215
+ 'client/liquidator_liquidations': 1,
216
+ 'liquidations': 1,
217
+ 'asset/history': 60,
218
+ 'client/holding': 1,
219
+ 'withdraw_nonce': 1,
220
+ 'settle_nonce': 1,
221
+ 'pnl_settlement/history': 1,
222
+ 'volume/user/daily': 60,
223
+ 'volume/user/stats': 60,
224
+ 'client/statistics': 60,
225
+ 'client/info': 60,
226
+ 'client/statistics/daily': 60,
227
+ 'positions': 3.33,
228
+ 'position/{symbol}': 3.33,
229
+ 'funding_fee/history': 30,
230
+ 'notification/inbox/notifications': 60,
231
+ 'notification/inbox/unread': 60,
232
+ 'volume/broker/daily': 60,
233
+ 'broker/fee_rate/default': 10,
234
+ 'broker/user_info': 10,
235
+ 'orderbook/{symbol}': 1,
236
+ 'kline': 1,
237
+ },
238
+ 'post': {
239
+ 'orderly_key': 1,
240
+ 'client/set_orderly_key_ip_restriction': 6,
241
+ 'client/reset_orderly_key_ip_restriction': 6,
242
+ 'order': 1,
243
+ 'batch-order': 10,
244
+ 'algo/order': 1,
245
+ 'liquidation': 1,
246
+ 'claim_insurance_fund': 1,
247
+ 'withdraw_request': 1,
248
+ 'settle_pnl': 1,
249
+ 'notification/inbox/mark_read': 60,
250
+ 'notification/inbox/mark_read_all': 60,
251
+ 'client/leverage': 120,
252
+ 'client/maintenance_config': 60,
253
+ 'delegate_signer': 10,
254
+ 'delegate_orderly_key': 10,
255
+ 'delegate_settle_pnl': 10,
256
+ 'delegate_withdraw_request': 10,
257
+ 'broker/fee_rate/set': 10,
258
+ 'broker/fee_rate/set_default': 10,
259
+ 'broker/fee_rate/default': 10,
260
+ 'referral/create': 10,
261
+ 'referral/update': 10,
262
+ 'referral/bind': 10,
263
+ 'referral/edit_split': 10,
264
+ },
265
+ 'put': {
266
+ 'order': 1,
267
+ 'algo/order': 1,
268
+ },
269
+ 'delete': {
270
+ 'order': 1,
271
+ 'algo/order': 1,
272
+ 'client/order': 1,
273
+ 'algo/client/order': 1,
274
+ 'algo/orders': 1,
275
+ 'orders': 1,
276
+ 'batch-order': 1,
277
+ 'client/batch-order': 1,
278
+ },
279
+ },
280
+ },
281
+ },
282
+ 'requiredCredentials': {
283
+ 'apiKey': true,
284
+ 'secret': true,
285
+ 'accountId': true,
286
+ 'privateKey': false,
287
+ },
288
+ 'fees': {
289
+ 'trading': {
290
+ 'tierBased': true,
291
+ 'percentage': true,
292
+ 'maker': this.parseNumber('0.0002'),
293
+ 'taker': this.parseNumber('0.0005'),
294
+ },
295
+ },
296
+ 'options': {
297
+ 'sandboxMode': false,
298
+ 'brokerId': 'CCXT',
299
+ 'verifyingContractAddress': '0x6F7a338F2aA472838dEFD3283eB360d4Dff5D203',
300
+ },
301
+ 'features': {
302
+ 'default': {
303
+ 'sandbox': true,
304
+ 'createOrder': {
305
+ 'marginMode': false,
306
+ 'triggerPrice': true,
307
+ 'triggerPriceType': undefined,
308
+ 'triggerDirection': false,
309
+ 'stopLossPrice': false,
310
+ 'takeProfitPrice': false,
311
+ 'attachedStopLossTakeProfit': undefined,
312
+ 'timeInForce': {
313
+ 'IOC': true,
314
+ 'FOK': true,
315
+ 'PO': true,
316
+ 'GTD': false,
317
+ },
318
+ 'hedged': false,
319
+ 'trailing': true,
320
+ 'leverage': true,
321
+ 'marketBuyByCost': false,
322
+ 'marketBuyRequiresPrice': false,
323
+ 'selfTradePrevention': false,
324
+ 'iceberg': true, // todo implement
325
+ },
326
+ 'createOrders': {
327
+ 'max': 10,
328
+ },
329
+ 'fetchMyTrades': {
330
+ 'marginMode': false,
331
+ 'limit': 500,
332
+ 'daysBack': undefined,
333
+ 'untilDays': 100000,
334
+ 'symbolRequired': false,
335
+ },
336
+ 'fetchOrder': {
337
+ 'marginMode': false,
338
+ 'trigger': true,
339
+ 'trailing': false,
340
+ 'symbolRequired': false,
341
+ },
342
+ 'fetchOpenOrders': {
343
+ 'marginMode': false,
344
+ 'limit': 500,
345
+ 'trigger': true,
346
+ 'trailing': false,
347
+ 'symbolRequired': false,
348
+ },
349
+ 'fetchOrders': undefined,
350
+ 'fetchClosedOrders': {
351
+ 'marginMode': false,
352
+ 'limit': 500,
353
+ 'daysBack': undefined,
354
+ 'daysBackCanceled': undefined,
355
+ 'untilDays': 100000,
356
+ 'trigger': true,
357
+ 'trailing': false,
358
+ 'symbolRequired': false,
359
+ },
360
+ 'fetchOHLCV': {
361
+ 'limit': 1000,
362
+ },
363
+ },
364
+ 'spot': {
365
+ 'extends': 'default',
366
+ },
367
+ 'forDerivatives': {
368
+ 'extends': 'default',
369
+ 'createOrder': {
370
+ // todo: implementation needs unification
371
+ 'triggerPriceType': undefined,
372
+ 'attachedStopLossTakeProfit': {
373
+ // todo: implementation needs unification
374
+ 'triggerPriceType': undefined,
375
+ 'price': false,
376
+ },
377
+ },
378
+ },
379
+ 'swap': {
380
+ 'linear': {
381
+ 'extends': 'forDerivatives',
382
+ },
383
+ 'inverse': undefined,
384
+ },
385
+ 'future': {
386
+ 'linear': undefined,
387
+ 'inverse': undefined,
388
+ },
389
+ },
390
+ 'commonCurrencies': {},
391
+ 'exceptions': {
392
+ 'exact': {
393
+ '-1000': ExchangeError,
394
+ '-1001': AuthenticationError,
395
+ '-1002': AuthenticationError,
396
+ '-1003': RateLimitExceeded,
397
+ '-1004': BadRequest,
398
+ '-1005': BadRequest,
399
+ '-1006': InvalidOrder,
400
+ '-1007': BadRequest,
401
+ '-1008': InvalidOrder,
402
+ '-1009': InsufficientFunds,
403
+ '-1011': NetworkError,
404
+ '-1012': BadRequest,
405
+ '-1101': InsufficientFunds,
406
+ '-1102': InvalidOrder,
407
+ '-1103': InvalidOrder,
408
+ '-1104': InvalidOrder,
409
+ '-1105': InvalidOrder,
410
+ '-1201': BadRequest,
411
+ '-1202': BadRequest,
412
+ '29': BadRequest,
413
+ '9': AuthenticationError,
414
+ '3': AuthenticationError,
415
+ '2': BadRequest,
416
+ '15': BadRequest, // {"success":false,"code":15,"message":"BrokerId is not exist"}
417
+ },
418
+ 'broad': {},
419
+ },
420
+ 'precisionMode': TICK_SIZE,
421
+ });
422
+ }
423
+ setSandboxMode(enable) {
424
+ super.setSandboxMode(enable);
425
+ this.options['sandboxMode'] = enable;
426
+ }
427
+ /**
428
+ * @method
429
+ * @name modetrade#fetchStatus
430
+ * @description the latest known information on the availability of the exchange API
431
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/public/get-system-maintenance-status
432
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
433
+ * @returns {object} a [status structure]{@link https://docs.ccxt.com/#/?id=exchange-status-structure}
434
+ */
435
+ async fetchStatus(params = {}) {
436
+ const response = await this.v1PublicGetPublicSystemInfo(params);
437
+ //
438
+ // {
439
+ // "success": true,
440
+ // "data": {
441
+ // "status": 0,
442
+ // "msg": "System is functioning properly."
443
+ // },
444
+ // "timestamp": "1709274106602"
445
+ // }
446
+ //
447
+ const data = this.safeDict(response, 'data', {});
448
+ let status = this.safeString(data, 'status');
449
+ if (status === undefined) {
450
+ status = 'error';
451
+ }
452
+ else if (status === '0') {
453
+ status = 'ok';
454
+ }
455
+ else {
456
+ status = 'maintenance';
457
+ }
458
+ return {
459
+ 'status': status,
460
+ 'updated': undefined,
461
+ 'eta': undefined,
462
+ 'url': undefined,
463
+ 'info': response,
464
+ };
465
+ }
466
+ /**
467
+ * @method
468
+ * @name modetrade#fetchTime
469
+ * @description fetches the current integer timestamp in milliseconds from the exchange server
470
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/public/get-system-maintenance-status
471
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
472
+ * @returns {int} the current integer timestamp in milliseconds from the exchange server
473
+ */
474
+ async fetchTime(params = {}) {
475
+ const response = await this.v1PublicGetPublicSystemInfo(params);
476
+ //
477
+ // {
478
+ // "success": true,
479
+ // "data": {
480
+ // "status": 0,
481
+ // "msg": "System is functioning properly."
482
+ // },
483
+ // "timestamp": "1709274106602"
484
+ // }
485
+ //
486
+ return this.safeInteger(response, 'timestamp');
487
+ }
488
+ parseMarket(market) {
489
+ //
490
+ // {
491
+ // "symbol": "PERP_BTC_USDC",
492
+ // "quote_min": 123,
493
+ // "quote_max": 100000,
494
+ // "quote_tick": 0.1,
495
+ // "base_min": 0.00001,
496
+ // "base_max": 20,
497
+ // "base_tick": 0.00001,
498
+ // "min_notional": 1,
499
+ // "price_range": 0.02,
500
+ // "price_scope": 0.4,
501
+ // "std_liquidation_fee": 0.03,
502
+ // "liquidator_fee": 0.015,
503
+ // "claim_insurance_fund_discount": 0.0075,
504
+ // "funding_period": 8,
505
+ // "cap_funding": 0.000375,
506
+ // "floor_funding": -0.000375,
507
+ // "interest_rate": 0.0001,
508
+ // "created_time": 1684140107326,
509
+ // "updated_time": 1685345968053,
510
+ // "base_mmr": 0.05,
511
+ // "base_imr": 0.1,
512
+ // "imr_factor": 0.0002512,
513
+ // "liquidation_tier": "1"
514
+ // }
515
+ //
516
+ const marketId = this.safeString(market, 'symbol');
517
+ const parts = marketId.split('_');
518
+ const marketType = 'swap';
519
+ const baseId = this.safeString(parts, 1);
520
+ const quoteId = this.safeString(parts, 2);
521
+ const base = this.safeCurrencyCode(baseId);
522
+ const quote = this.safeCurrencyCode(quoteId);
523
+ const settleId = this.safeString(parts, 2);
524
+ const settle = this.safeCurrencyCode(settleId);
525
+ const symbol = base + '/' + quote + ':' + settle;
526
+ return {
527
+ 'id': marketId,
528
+ 'symbol': symbol,
529
+ 'base': base,
530
+ 'quote': quote,
531
+ 'settle': settle,
532
+ 'baseId': baseId,
533
+ 'quoteId': quoteId,
534
+ 'settleId': settleId,
535
+ 'type': marketType,
536
+ 'spot': false,
537
+ 'margin': false,
538
+ 'swap': true,
539
+ 'future': false,
540
+ 'option': false,
541
+ 'active': undefined,
542
+ 'contract': true,
543
+ 'linear': true,
544
+ 'inverse': false,
545
+ 'contractSize': this.parseNumber('1'),
546
+ 'expiry': undefined,
547
+ 'expiryDatetime': undefined,
548
+ 'strike': undefined,
549
+ 'optionType': undefined,
550
+ 'precision': {
551
+ 'amount': this.safeNumber(market, 'base_tick'),
552
+ 'price': this.safeNumber(market, 'quote_tick'),
553
+ },
554
+ 'limits': {
555
+ 'leverage': {
556
+ 'min': undefined,
557
+ 'max': undefined,
558
+ },
559
+ 'amount': {
560
+ 'min': this.safeNumber(market, 'base_min'),
561
+ 'max': this.safeNumber(market, 'base_max'),
562
+ },
563
+ 'price': {
564
+ 'min': this.safeNumber(market, 'quote_min'),
565
+ 'max': this.safeNumber(market, 'quote_max'),
566
+ },
567
+ 'cost': {
568
+ 'min': this.safeNumber(market, 'min_notional'),
569
+ 'max': undefined,
570
+ },
571
+ },
572
+ 'created': this.safeInteger(market, 'created_time'),
573
+ 'info': market,
574
+ };
575
+ }
576
+ /**
577
+ * @method
578
+ * @name modetrade#fetchMarkets
579
+ * @description retrieves data on all markets for modetrade
580
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/public/get-available-symbols
581
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
582
+ * @returns {object[]} an array of objects representing market data
583
+ */
584
+ async fetchMarkets(params = {}) {
585
+ const response = await this.v1PublicGetPublicInfo(params);
586
+ //
587
+ // {
588
+ // "success": true,
589
+ // "timestamp": 1702989203989,
590
+ // "data": {
591
+ // "rows": [
592
+ // {
593
+ // "symbol": "PERP_BTC_USDC",
594
+ // "quote_min": 123,
595
+ // "quote_max": 100000,
596
+ // "quote_tick": 0.1,
597
+ // "base_min": 0.00001,
598
+ // "base_max": 20,
599
+ // "base_tick": 0.00001,
600
+ // "min_notional": 1,
601
+ // "price_range": 0.02,
602
+ // "price_scope": 0.4,
603
+ // "std_liquidation_fee": 0.03,
604
+ // "liquidator_fee": 0.015,
605
+ // "claim_insurance_fund_discount": 0.0075,
606
+ // "funding_period": 8,
607
+ // "cap_funding": 0.000375,
608
+ // "floor_funding": -0.000375,
609
+ // "interest_rate": 0.0001,
610
+ // "created_time": 1684140107326,
611
+ // "updated_time": 1685345968053,
612
+ // "base_mmr": 0.05,
613
+ // "base_imr": 0.1,
614
+ // "imr_factor": 0.0002512,
615
+ // "liquidation_tier": "1"
616
+ // }
617
+ // ]
618
+ // }
619
+ // }
620
+ //
621
+ const data = this.safeDict(response, 'data', {});
622
+ const rows = this.safeList(data, 'rows', []);
623
+ return this.parseMarkets(rows);
624
+ }
625
+ /**
626
+ * @method
627
+ * @name modetrade#fetchCurrencies
628
+ * @description fetches all available currencies on an exchange
629
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/public/get-token-info
630
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
631
+ * @returns {object} an associative dictionary of currencies
632
+ */
633
+ async fetchCurrencies(params = {}) {
634
+ const result = {};
635
+ const response = await this.v1PublicGetPublicToken(params);
636
+ //
637
+ // {
638
+ // "success": true,
639
+ // "timestamp": 1702989203989,
640
+ // "data": {
641
+ // "rows": [{
642
+ // "token": "USDC",
643
+ // "decimals": 6,
644
+ // "minimum_withdraw_amount": 0.000001,
645
+ // "token_hash": "0xd6aca1be9729c13d677335161321649cccae6a591554772516700f986f942eaa",
646
+ // "chain_details": [{
647
+ // "chain_id": 43113,
648
+ // "contract_address": "0x5d64c9cfb0197775b4b3ad9be4d3c7976e0d8dc3",
649
+ // "cross_chain_withdrawal_fee": 123,
650
+ // "decimals": 6,
651
+ // "withdraw_fee": 2
652
+ // }]
653
+ // }
654
+ // ]
655
+ // }
656
+ // }
657
+ //
658
+ const data = this.safeDict(response, 'data', {});
659
+ const tokenRows = this.safeList(data, 'rows', []);
660
+ for (let i = 0; i < tokenRows.length; i++) {
661
+ const token = tokenRows[i];
662
+ const currencyId = this.safeString(token, 'token');
663
+ const networks = this.safeList(token, 'chain_details');
664
+ const code = this.safeCurrencyCode(currencyId);
665
+ let minPrecision = undefined;
666
+ const resultingNetworks = {};
667
+ for (let j = 0; j < networks.length; j++) {
668
+ const network = networks[j];
669
+ // TODO: transform chain id to human readable name
670
+ const networkId = this.safeString(network, 'chain_id');
671
+ const precision = this.parsePrecision(this.safeString(network, 'decimals'));
672
+ if (precision !== undefined) {
673
+ minPrecision = (minPrecision === undefined) ? precision : Precise.stringMin(precision, minPrecision);
674
+ }
675
+ resultingNetworks[networkId] = {
676
+ 'id': networkId,
677
+ 'network': networkId,
678
+ 'limits': {
679
+ 'withdraw': {
680
+ 'min': undefined,
681
+ 'max': undefined,
682
+ },
683
+ 'deposit': {
684
+ 'min': undefined,
685
+ 'max': undefined,
686
+ },
687
+ },
688
+ 'active': undefined,
689
+ 'deposit': undefined,
690
+ 'withdraw': undefined,
691
+ 'fee': this.safeNumber(network, 'withdrawal_fee'),
692
+ 'precision': this.parseNumber(precision),
693
+ 'info': network,
694
+ };
695
+ }
696
+ result[code] = this.safeCurrencyStructure({
697
+ 'id': currencyId,
698
+ 'name': currencyId,
699
+ 'code': code,
700
+ 'precision': this.parseNumber(minPrecision),
701
+ 'active': undefined,
702
+ 'fee': undefined,
703
+ 'networks': resultingNetworks,
704
+ 'deposit': undefined,
705
+ 'withdraw': undefined,
706
+ 'limits': {
707
+ 'deposit': {
708
+ 'min': undefined,
709
+ 'max': undefined,
710
+ },
711
+ 'withdraw': {
712
+ 'min': this.safeNumber(token, 'minimum_withdraw_amount'),
713
+ 'max': undefined,
714
+ },
715
+ },
716
+ 'info': token,
717
+ });
718
+ }
719
+ return result;
720
+ }
721
+ parseTokenAndFeeTemp(item, feeTokenKey, feeAmountKey) {
722
+ const feeCost = this.safeString(item, feeAmountKey);
723
+ let fee = undefined;
724
+ if (feeCost !== undefined) {
725
+ const feeCurrencyId = this.safeString(item, feeTokenKey);
726
+ const feeCurrencyCode = this.safeCurrencyCode(feeCurrencyId);
727
+ fee = {
728
+ 'cost': feeCost,
729
+ 'currency': feeCurrencyCode,
730
+ };
731
+ }
732
+ return fee;
733
+ }
734
+ parseTrade(trade, market = undefined) {
735
+ //
736
+ // public/market_trades
737
+ //
738
+ // {
739
+ // "symbol": "PERP_ETH_USDC",
740
+ // "side": "SELL",
741
+ // "executed_price": 46222.35,
742
+ // "executed_quantity": 0.0012,
743
+ // "executed_timestamp": "1683878609166"
744
+ // }
745
+ //
746
+ // fetchOrderTrades, fetchOrder
747
+ //
748
+ // {
749
+ // "id": "99119876",
750
+ // "symbol": "PERP_BTC_USDC",
751
+ // "fee": "0.0024",
752
+ // "side": "BUY",
753
+ // "executed_timestamp": "1641481113084",
754
+ // "order_id": "87001234",
755
+ // "order_tag": "default", <-- this param only in "fetchOrderTrades"
756
+ // "executed_price": "1",
757
+ // "executed_quantity": "12",
758
+ // "fee_asset": "BTC",
759
+ // "is_maker": "1"
760
+ // }
761
+ //
762
+ const isFromFetchOrder = ('id' in trade);
763
+ const timestamp = this.safeInteger(trade, 'executed_timestamp');
764
+ const marketId = this.safeString(trade, 'symbol');
765
+ market = this.safeMarket(marketId, market);
766
+ const symbol = market['symbol'];
767
+ const price = this.safeString(trade, 'executed_price');
768
+ const amount = this.safeString(trade, 'executed_quantity');
769
+ const order_id = this.safeString(trade, 'order_id');
770
+ const fee = this.parseTokenAndFeeTemp(trade, 'fee_asset', 'fee');
771
+ const feeCost = this.safeString(fee, 'cost');
772
+ if (feeCost !== undefined) {
773
+ fee['cost'] = feeCost;
774
+ }
775
+ const cost = Precise.stringMul(price, amount);
776
+ const side = this.safeStringLower(trade, 'side');
777
+ const id = this.safeString(trade, 'id');
778
+ let takerOrMaker = undefined;
779
+ if (isFromFetchOrder) {
780
+ const isMaker = this.safeString(trade, 'is_maker') === '1';
781
+ takerOrMaker = isMaker ? 'maker' : 'taker';
782
+ }
783
+ return this.safeTrade({
784
+ 'id': id,
785
+ 'timestamp': timestamp,
786
+ 'datetime': this.iso8601(timestamp),
787
+ 'symbol': symbol,
788
+ 'side': side,
789
+ 'price': price,
790
+ 'amount': amount,
791
+ 'cost': cost,
792
+ 'order': order_id,
793
+ 'takerOrMaker': takerOrMaker,
794
+ 'type': undefined,
795
+ 'fee': fee,
796
+ 'info': trade,
797
+ }, market);
798
+ }
799
+ /**
800
+ * @method
801
+ * @name modetrade#fetchTrades
802
+ * @description get the list of most recent trades for a particular symbol
803
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/public/get-market-trades
804
+ * @param {string} symbol unified symbol of the market to fetch trades for
805
+ * @param {int} [since] timestamp in ms of the earliest trade to fetch
806
+ * @param {int} [limit] the maximum amount of trades to fetch
807
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
808
+ * @returns {Trade[]} a list of [trade structures]{@link https://docs.ccxt.com/#/?id=public-trades}
809
+ */
810
+ async fetchTrades(symbol, since = undefined, limit = undefined, params = {}) {
811
+ await this.loadMarkets();
812
+ const market = this.market(symbol);
813
+ const request = {
814
+ 'symbol': market['id'],
815
+ };
816
+ if (limit !== undefined) {
817
+ request['limit'] = limit;
818
+ }
819
+ const response = await this.v1PublicGetPublicMarketTrades(this.extend(request, params));
820
+ //
821
+ // {
822
+ // "success": true,
823
+ // "timestamp": 1702989203989,
824
+ // "data": {
825
+ // "rows": [{
826
+ // "symbol": "PERP_ETH_USDC",
827
+ // "side": "BUY",
828
+ // "executed_price": 2050,
829
+ // "executed_quantity": 1,
830
+ // "executed_timestamp": 1683878609166
831
+ // }]
832
+ // }
833
+ // }
834
+ //
835
+ const data = this.safeDict(response, 'data', {});
836
+ const rows = this.safeList(data, 'rows', []);
837
+ return this.parseTrades(rows, market, since, limit);
838
+ }
839
+ parseFundingRate(fundingRate, market = undefined) {
840
+ //
841
+ // {
842
+ // "symbol":"PERP_AAVE_USDT",
843
+ // "est_funding_rate":-0.00003447,
844
+ // "est_funding_rate_timestamp":1653633959001,
845
+ // "last_funding_rate":-0.00002094,
846
+ // "last_funding_rate_timestamp":1653631200000,
847
+ // "next_funding_time":1653634800000,
848
+ // "sum_unitary_funding": 521.367
849
+ // }
850
+ //
851
+ const symbol = this.safeString(fundingRate, 'symbol');
852
+ market = this.market(symbol);
853
+ const nextFundingTimestamp = this.safeInteger(fundingRate, 'next_funding_time');
854
+ const estFundingRateTimestamp = this.safeInteger(fundingRate, 'est_funding_rate_timestamp');
855
+ const lastFundingRateTimestamp = this.safeInteger(fundingRate, 'last_funding_rate_timestamp');
856
+ const fundingTimeString = this.safeString(fundingRate, 'last_funding_rate_timestamp');
857
+ const nextFundingTimeString = this.safeString(fundingRate, 'next_funding_time');
858
+ const millisecondsInterval = Precise.stringSub(nextFundingTimeString, fundingTimeString);
859
+ return {
860
+ 'info': fundingRate,
861
+ 'symbol': market['symbol'],
862
+ 'markPrice': undefined,
863
+ 'indexPrice': undefined,
864
+ 'interestRate': this.parseNumber('0'),
865
+ 'estimatedSettlePrice': undefined,
866
+ 'timestamp': estFundingRateTimestamp,
867
+ 'datetime': this.iso8601(estFundingRateTimestamp),
868
+ 'fundingRate': this.safeNumber(fundingRate, 'est_funding_rate'),
869
+ 'fundingTimestamp': nextFundingTimestamp,
870
+ 'fundingDatetime': this.iso8601(nextFundingTimestamp),
871
+ 'nextFundingRate': undefined,
872
+ 'nextFundingTimestamp': undefined,
873
+ 'nextFundingDatetime': undefined,
874
+ 'previousFundingRate': this.safeNumber(fundingRate, 'last_funding_rate'),
875
+ 'previousFundingTimestamp': lastFundingRateTimestamp,
876
+ 'previousFundingDatetime': this.iso8601(lastFundingRateTimestamp),
877
+ 'interval': this.parseFundingInterval(millisecondsInterval),
878
+ };
879
+ }
880
+ parseFundingInterval(interval) {
881
+ const intervals = {
882
+ '3600000': '1h',
883
+ '14400000': '4h',
884
+ '28800000': '8h',
885
+ '57600000': '16h',
886
+ '86400000': '24h',
887
+ };
888
+ return this.safeString(intervals, interval, interval);
889
+ }
890
+ /**
891
+ * @method
892
+ * @name modetrade#fetchFundingInterval
893
+ * @description fetch the current funding rate interval
894
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/public/get-predicted-funding-rate-for-one-market
895
+ * @param {string} symbol unified market symbol
896
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
897
+ * @returns {object} a [funding rate structure]{@link https://docs.ccxt.com/#/?id=funding-rate-structure}
898
+ */
899
+ async fetchFundingInterval(symbol, params = {}) {
900
+ return await this.fetchFundingRate(symbol, params);
901
+ }
902
+ /**
903
+ * @method
904
+ * @name modetrade#fetchFundingRate
905
+ * @description fetch the current funding rate
906
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/public/get-predicted-funding-rate-for-one-market
907
+ * @param {string} symbol unified market symbol
908
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
909
+ * @returns {object} a [funding rate structure]{@link https://docs.ccxt.com/#/?id=funding-rate-structure}
910
+ */
911
+ async fetchFundingRate(symbol, params = {}) {
912
+ await this.loadMarkets();
913
+ const market = this.market(symbol);
914
+ const request = {
915
+ 'symbol': market['id'],
916
+ };
917
+ const response = await this.v1PublicGetPublicFundingRateSymbol(this.extend(request, params));
918
+ //
919
+ // {
920
+ // "success": true,
921
+ // "timestamp": 1702989203989,
922
+ // "data": {
923
+ // "symbol": "PERP_ETH_USDC",
924
+ // "est_funding_rate": 123,
925
+ // "est_funding_rate_timestamp": 1683880020000,
926
+ // "last_funding_rate": 0.0001,
927
+ // "last_funding_rate_timestamp": 1683878400000,
928
+ // "next_funding_time": 1683907200000,
929
+ // "sum_unitary_funding": 521.367
930
+ // }
931
+ // }
932
+ //
933
+ const data = this.safeDict(response, 'data', {});
934
+ return this.parseFundingRate(data, market);
935
+ }
936
+ /**
937
+ * @method
938
+ * @name modetrade#fetchFundingRates
939
+ * @description fetch the current funding rate for multiple markets
940
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/public/get-predicted-funding-rates-for-all-markets
941
+ * @param {string[]} symbols unified market symbols
942
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
943
+ * @returns {object[]} an array of [funding rate structures]{@link https://docs.ccxt.com/#/?id=funding-rate-structure}
944
+ */
945
+ async fetchFundingRates(symbols = undefined, params = {}) {
946
+ await this.loadMarkets();
947
+ symbols = this.marketSymbols(symbols);
948
+ const response = await this.v1PublicGetPublicFundingRates(params);
949
+ //
950
+ // {
951
+ // "success": true,
952
+ // "timestamp": 1702989203989,
953
+ // "data": {
954
+ // "rows": [{
955
+ // "symbol": "PERP_ETH_USDC",
956
+ // "est_funding_rate": 123,
957
+ // "est_funding_rate_timestamp": 1683880020000,
958
+ // "last_funding_rate": 0.0001,
959
+ // "last_funding_rate_timestamp": 1683878400000,
960
+ // "next_funding_time": 1683907200000,
961
+ // "sum_unitary_funding": 521.367
962
+ // }]
963
+ // }
964
+ // }
965
+ //
966
+ const data = this.safeDict(response, 'data', {});
967
+ const rows = this.safeList(data, 'rows', []);
968
+ return this.parseFundingRates(rows, symbols);
969
+ }
970
+ /**
971
+ * @method
972
+ * @name modetrade#fetchFundingRateHistory
973
+ * @description fetches historical funding rate prices
974
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/public/get-funding-rate-history-for-one-market
975
+ * @param {string} symbol unified symbol of the market to fetch the funding rate history for
976
+ * @param {int} [since] timestamp in ms of the earliest funding rate to fetch
977
+ * @param {int} [limit] the maximum amount of [funding rate structures]{@link https://docs.ccxt.com/#/?id=funding-rate-history-structure} to fetch
978
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
979
+ * @param {int} [params.until] timestamp in ms of the latest funding rate
980
+ * @param {boolean} [params.paginate] default false, when true will automatically paginate by calling this endpoint multiple times. See in the docs all the [availble parameters](https://github.com/ccxt/ccxt/wiki/Manual#pagination-params)
981
+ * @returns {object[]} a list of [funding rate structures]{@link https://docs.ccxt.com/#/?id=funding-rate-history-structure}
982
+ */
983
+ async fetchFundingRateHistory(symbol = undefined, since = undefined, limit = undefined, params = {}) {
984
+ await this.loadMarkets();
985
+ let paginate = false;
986
+ [paginate, params] = this.handleOptionAndParams(params, 'fetchFundingRateHistory', 'paginate');
987
+ if (paginate) {
988
+ return await this.fetchPaginatedCallIncremental('fetchFundingRateHistory', symbol, since, limit, params, 'page', 25);
989
+ }
990
+ let request = {};
991
+ if (symbol !== undefined) {
992
+ const market = this.market(symbol);
993
+ symbol = market['symbol'];
994
+ request['symbol'] = market['id'];
995
+ }
996
+ if (since !== undefined) {
997
+ request['start_t'] = since;
998
+ }
999
+ [request, params] = this.handleUntilOption('end_t', request, params, 0.001);
1000
+ const response = await this.v1PublicGetPublicFundingRateHistory(this.extend(request, params));
1001
+ //
1002
+ // {
1003
+ // "success": true,
1004
+ // "timestamp": 1702989203989,
1005
+ // "data": {
1006
+ // "rows": [{
1007
+ // "symbol": "PERP_ETH_USDC",
1008
+ // "funding_rate": 0.0001,
1009
+ // "funding_rate_timestamp": 1684224000000,
1010
+ // "next_funding_time": 1684252800000
1011
+ // }],
1012
+ // "meta": {
1013
+ // "total": 9,
1014
+ // "records_per_page": 25,
1015
+ // "current_page": 1
1016
+ // }
1017
+ // }
1018
+ // }
1019
+ //
1020
+ const data = this.safeDict(response, 'data', {});
1021
+ const result = this.safeList(data, 'rows', []);
1022
+ const rates = [];
1023
+ for (let i = 0; i < result.length; i++) {
1024
+ const entry = result[i];
1025
+ const marketId = this.safeString(entry, 'symbol');
1026
+ const timestamp = this.safeInteger(entry, 'funding_rate_timestamp');
1027
+ rates.push({
1028
+ 'info': entry,
1029
+ 'symbol': this.safeSymbol(marketId),
1030
+ 'fundingRate': this.safeNumber(entry, 'funding_rate'),
1031
+ 'timestamp': timestamp,
1032
+ 'datetime': this.iso8601(timestamp),
1033
+ });
1034
+ }
1035
+ const sorted = this.sortBy(rates, 'timestamp');
1036
+ return this.filterBySymbolSinceLimit(sorted, symbol, since, limit);
1037
+ }
1038
+ /**
1039
+ * @method
1040
+ * @name modetrade#fetchTradingFees
1041
+ * @description fetch the trading fees for multiple markets
1042
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/get-account-information
1043
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
1044
+ * @returns {object} a dictionary of [fee structures]{@link https://docs.ccxt.com/#/?id=fee-structure} indexed by market symbols
1045
+ */
1046
+ async fetchTradingFees(params = {}) {
1047
+ await this.loadMarkets();
1048
+ const response = await this.v1PrivateGetClientInfo(params);
1049
+ //
1050
+ // {
1051
+ // "success": true,
1052
+ // "timestamp": 1702989203989,
1053
+ // "data": {
1054
+ // "account_id": "<string>",
1055
+ // "email": "test@test.com",
1056
+ // "account_mode": "FUTURES",
1057
+ // "max_leverage": 20,
1058
+ // "taker_fee_rate": 123,
1059
+ // "maker_fee_rate": 123,
1060
+ // "futures_taker_fee_rate": 123,
1061
+ // "futures_maker_fee_rate": 123,
1062
+ // "maintenance_cancel_orders": true,
1063
+ // "imr_factor": {
1064
+ // "PERP_BTC_USDC": 123,
1065
+ // "PERP_ETH_USDC": 123,
1066
+ // "PERP_NEAR_USDC": 123
1067
+ // },
1068
+ // "max_notional": {
1069
+ // "PERP_BTC_USDC": 123,
1070
+ // "PERP_ETH_USDC": 123,
1071
+ // "PERP_NEAR_USDC": 123
1072
+ // }
1073
+ // }
1074
+ // }
1075
+ //
1076
+ const data = this.safeDict(response, 'data', {});
1077
+ const maker = this.safeString(data, 'futures_maker_fee_rate');
1078
+ const taker = this.safeString(data, 'futures_taker_fee_rate');
1079
+ const result = {};
1080
+ for (let i = 0; i < this.symbols.length; i++) {
1081
+ const symbol = this.symbols[i];
1082
+ result[symbol] = {
1083
+ 'info': response,
1084
+ 'symbol': symbol,
1085
+ 'maker': this.parseNumber(Precise.stringDiv(maker, '10000')),
1086
+ 'taker': this.parseNumber(Precise.stringDiv(taker, '10000')),
1087
+ 'percentage': true,
1088
+ 'tierBased': true,
1089
+ };
1090
+ }
1091
+ return result;
1092
+ }
1093
+ /**
1094
+ * @method
1095
+ * @name modetrade#fetchOrderBook
1096
+ * @description fetches information on open orders with bid (buy) and ask (sell) prices, volumes and other data
1097
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/orderbook-snapshot
1098
+ * @param {string} symbol unified symbol of the market to fetch the order book for
1099
+ * @param {int} [limit] the maximum amount of order book entries to return
1100
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
1101
+ * @returns {object} A dictionary of [order book structures]{@link https://docs.ccxt.com/#/?id=order-book-structure} indexed by market symbols
1102
+ */
1103
+ async fetchOrderBook(symbol, limit = undefined, params = {}) {
1104
+ await this.loadMarkets();
1105
+ const market = this.market(symbol);
1106
+ const request = {
1107
+ 'symbol': market['id'],
1108
+ };
1109
+ if (limit !== undefined) {
1110
+ limit = Math.min(limit, 1000);
1111
+ request['max_level'] = limit;
1112
+ }
1113
+ const response = await this.v1PrivateGetOrderbookSymbol(this.extend(request, params));
1114
+ //
1115
+ // {
1116
+ // "success": true,
1117
+ // "timestamp": 1702989203989,
1118
+ // "data": {
1119
+ // "asks": [{
1120
+ // "price": 10669.4,
1121
+ // "quantity": 1.56263218
1122
+ // }],
1123
+ // "bids": [{
1124
+ // "price": 10669.4,
1125
+ // "quantity": 1.56263218
1126
+ // }],
1127
+ // "timestamp": 123
1128
+ // }
1129
+ // }
1130
+ //
1131
+ const data = this.safeDict(response, 'data', {});
1132
+ const timestamp = this.safeInteger(data, 'timestamp');
1133
+ return this.parseOrderBook(data, symbol, timestamp, 'bids', 'asks', 'price', 'quantity');
1134
+ }
1135
+ parseOHLCV(ohlcv, market = undefined) {
1136
+ return [
1137
+ this.safeInteger(ohlcv, 'start_timestamp'),
1138
+ this.safeNumber(ohlcv, 'open'),
1139
+ this.safeNumber(ohlcv, 'high'),
1140
+ this.safeNumber(ohlcv, 'low'),
1141
+ this.safeNumber(ohlcv, 'close'),
1142
+ this.safeNumber(ohlcv, 'volume'),
1143
+ ];
1144
+ }
1145
+ /**
1146
+ * @method
1147
+ * @name modetrade#fetchOHLCV
1148
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/get-kline
1149
+ * @description fetches historical candlestick data containing the open, high, low, and close price, and the volume of a market
1150
+ * @param {string} symbol unified symbol of the market to fetch OHLCV data for
1151
+ * @param {string} timeframe the length of time each candle represents
1152
+ * @param {int} [since] timestamp in ms of the earliest candle to fetch
1153
+ * @param {int} [limit] max=1000, max=100 when since is defined and is less than (now - (999 * (timeframe in ms)))
1154
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
1155
+ * @returns {int[][]} A list of candles ordered as timestamp, open, high, low, close, volume
1156
+ */
1157
+ async fetchOHLCV(symbol, timeframe = '1m', since = undefined, limit = undefined, params = {}) {
1158
+ await this.loadMarkets();
1159
+ const market = this.market(symbol);
1160
+ const request = {
1161
+ 'symbol': market['id'],
1162
+ 'type': this.safeString(this.timeframes, timeframe, timeframe),
1163
+ };
1164
+ if (limit !== undefined) {
1165
+ request['limit'] = Math.min(limit, 1000);
1166
+ }
1167
+ const response = await this.v1PrivateGetKline(this.extend(request, params));
1168
+ const data = this.safeDict(response, 'data', {});
1169
+ //
1170
+ // {
1171
+ // "success": true,
1172
+ // "timestamp": 1702989203989,
1173
+ // "data": {
1174
+ // "rows": [{
1175
+ // "open": 66166.23,
1176
+ // "close": 66124.56,
1177
+ // "low": 66038.06,
1178
+ // "high": 66176.97,
1179
+ // "volume": 23.45528526,
1180
+ // "amount": 1550436.21725288,
1181
+ // "symbol": "PERP_BTC_USDC",
1182
+ // "type": "1m",
1183
+ // "start_timestamp": 1636388220000,
1184
+ // "end_timestamp": 1636388280000
1185
+ // }]
1186
+ // }
1187
+ // }
1188
+ //
1189
+ const rows = this.safeList(data, 'rows', []);
1190
+ return this.parseOHLCVs(rows, market, timeframe, since, limit);
1191
+ }
1192
+ parseOrder(order, market = undefined) {
1193
+ //
1194
+ // Possible input functions:
1195
+ // * createOrder
1196
+ // * createOrders
1197
+ // * cancelOrder
1198
+ // * fetchOrder
1199
+ // * fetchOrders
1200
+ // const isFromFetchOrder = ('order_tag' in order); TO_DO
1201
+ //
1202
+ // stop order after creating it:
1203
+ // {
1204
+ // "orderId": "1578938",
1205
+ // "clientOrderId": "0",
1206
+ // "algoType": "STOP_LOSS",
1207
+ // "quantity": "0.1"
1208
+ // }
1209
+ // stop order after fetching it:
1210
+ // {
1211
+ // "algoOrderId": "1578958",
1212
+ // "clientOrderId": "0",
1213
+ // "rootAlgoOrderId": "1578958",
1214
+ // "parentAlgoOrderId": "0",
1215
+ // "symbol": "SPOT_LTC_USDT",
1216
+ // "orderTag": "default",
1217
+ // "algoType": "STOP_LOSS",
1218
+ // "side": "BUY",
1219
+ // "quantity": "0.1",
1220
+ // "isTriggered": false,
1221
+ // "triggerPrice": "100",
1222
+ // "triggerStatus": "USELESS",
1223
+ // "type": "LIMIT",
1224
+ // "rootAlgoStatus": "CANCELLED",
1225
+ // "algoStatus": "CANCELLED",
1226
+ // "triggerPriceType": "MARKET_PRICE",
1227
+ // "price": "75",
1228
+ // "triggerTime": "0",
1229
+ // "totalExecutedQuantity": "0",
1230
+ // "averageExecutedPrice": "0",
1231
+ // "totalFee": "0",
1232
+ // "feeAsset": '',
1233
+ // "reduceOnly": false,
1234
+ // "createdTime": "1686149609.744",
1235
+ // "updatedTime": "1686149903.362"
1236
+ // }
1237
+ //
1238
+ const timestamp = this.safeIntegerN(order, ['timestamp', 'created_time', 'createdTime']);
1239
+ const orderId = this.safeStringN(order, ['order_id', 'orderId', 'algoOrderId']);
1240
+ const clientOrderId = this.omitZero(this.safeString2(order, 'client_order_id', 'clientOrderId')); // Somehow, this always returns 0 for limit order
1241
+ const marketId = this.safeString(order, 'symbol');
1242
+ market = this.safeMarket(marketId, market);
1243
+ const symbol = market['symbol'];
1244
+ const price = this.safeString2(order, 'order_price', 'price');
1245
+ const amount = this.safeString2(order, 'order_quantity', 'quantity'); // This is base amount
1246
+ const cost = this.safeString2(order, 'order_amount', 'amount'); // This is quote amount
1247
+ const orderType = this.safeStringLower2(order, 'order_type', 'type');
1248
+ let status = this.safeValue2(order, 'status', 'algoStatus');
1249
+ const success = this.safeBool(order, 'success');
1250
+ if (success !== undefined) {
1251
+ status = (success) ? 'NEW' : 'REJECTED';
1252
+ }
1253
+ const side = this.safeStringLower(order, 'side');
1254
+ const filled = this.omitZero(this.safeValue2(order, 'executed', 'totalExecutedQuantity'));
1255
+ const average = this.omitZero(this.safeString2(order, 'average_executed_price', 'averageExecutedPrice'));
1256
+ const remaining = Precise.stringSub(cost, filled);
1257
+ const fee = this.safeValue2(order, 'total_fee', 'totalFee');
1258
+ const feeCurrency = this.safeString2(order, 'fee_asset', 'feeAsset');
1259
+ const transactions = this.safeValue(order, 'Transactions');
1260
+ const triggerPrice = this.safeNumber(order, 'triggerPrice');
1261
+ let takeProfitPrice = undefined;
1262
+ let stopLossPrice = undefined;
1263
+ const childOrders = this.safeValue(order, 'childOrders');
1264
+ if (childOrders !== undefined) {
1265
+ const first = this.safeValue(childOrders, 0);
1266
+ const innerChildOrders = this.safeValue(first, 'childOrders', []);
1267
+ const innerChildOrdersLength = innerChildOrders.length;
1268
+ if (innerChildOrdersLength > 0) {
1269
+ const takeProfitOrder = this.safeValue(innerChildOrders, 0);
1270
+ const stopLossOrder = this.safeValue(innerChildOrders, 1);
1271
+ takeProfitPrice = this.safeNumber(takeProfitOrder, 'triggerPrice');
1272
+ stopLossPrice = this.safeNumber(stopLossOrder, 'triggerPrice');
1273
+ }
1274
+ }
1275
+ const lastUpdateTimestamp = this.safeInteger2(order, 'updatedTime', 'updated_time');
1276
+ return this.safeOrder({
1277
+ 'id': orderId,
1278
+ 'clientOrderId': clientOrderId,
1279
+ 'timestamp': timestamp,
1280
+ 'datetime': this.iso8601(timestamp),
1281
+ 'lastTradeTimestamp': undefined,
1282
+ 'lastUpdateTimestamp': lastUpdateTimestamp,
1283
+ 'status': this.parseOrderStatus(status),
1284
+ 'symbol': symbol,
1285
+ 'type': this.parseOrderType(orderType),
1286
+ 'timeInForce': this.parseTimeInForce(orderType),
1287
+ 'postOnly': undefined,
1288
+ 'reduceOnly': this.safeBool(order, 'reduce_only'),
1289
+ 'side': side,
1290
+ 'price': price,
1291
+ 'triggerPrice': triggerPrice,
1292
+ 'takeProfitPrice': takeProfitPrice,
1293
+ 'stopLossPrice': stopLossPrice,
1294
+ 'average': average,
1295
+ 'amount': amount,
1296
+ 'filled': filled,
1297
+ 'remaining': remaining,
1298
+ 'cost': cost,
1299
+ 'trades': transactions,
1300
+ 'fee': {
1301
+ 'cost': fee,
1302
+ 'currency': feeCurrency,
1303
+ },
1304
+ 'info': order,
1305
+ }, market);
1306
+ }
1307
+ parseTimeInForce(timeInForce) {
1308
+ const timeInForces = {
1309
+ 'ioc': 'IOC',
1310
+ 'fok': 'FOK',
1311
+ 'post_only': 'PO',
1312
+ };
1313
+ return this.safeString(timeInForces, timeInForce, undefined);
1314
+ }
1315
+ parseOrderStatus(status) {
1316
+ if (status !== undefined) {
1317
+ const statuses = {
1318
+ 'NEW': 'open',
1319
+ 'FILLED': 'closed',
1320
+ 'CANCEL_SENT': 'canceled',
1321
+ 'CANCEL_ALL_SENT': 'canceled',
1322
+ 'CANCELLED': 'canceled',
1323
+ 'PARTIAL_FILLED': 'open',
1324
+ 'REJECTED': 'rejected',
1325
+ 'INCOMPLETE': 'open',
1326
+ 'COMPLETED': 'closed',
1327
+ };
1328
+ return this.safeString(statuses, status, status);
1329
+ }
1330
+ return status;
1331
+ }
1332
+ parseOrderType(type) {
1333
+ const types = {
1334
+ 'LIMIT': 'limit',
1335
+ 'MARKET': 'market',
1336
+ 'POST_ONLY': 'limit',
1337
+ };
1338
+ return this.safeStringLower(types, type, type);
1339
+ }
1340
+ createOrderRequest(symbol, type, side, amount, price = undefined, params = {}) {
1341
+ /**
1342
+ * @method
1343
+ * @ignore
1344
+ * @name modetrade#createOrderRequest
1345
+ * @description helper function to build the request
1346
+ * @param {string} symbol unified symbol of the market to create an order in
1347
+ * @param {string} type 'market' or 'limit'
1348
+ * @param {string} side 'buy' or 'sell'
1349
+ * @param {float} amount how much you want to trade in units of the base currency
1350
+ * @param {float} [price] the price that the order is to be fulfilled, in units of the quote currency, ignored in market orders
1351
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
1352
+ * @returns {object} request to be sent to the exchange
1353
+ */
1354
+ const reduceOnly = this.safeBool2(params, 'reduceOnly', 'reduce_only');
1355
+ const orderType = type.toUpperCase();
1356
+ const market = this.market(symbol);
1357
+ const orderSide = side.toUpperCase();
1358
+ const request = {
1359
+ 'symbol': market['id'],
1360
+ 'side': orderSide,
1361
+ };
1362
+ const triggerPrice = this.safeString2(params, 'triggerPrice', 'stopPrice');
1363
+ const stopLoss = this.safeValue(params, 'stopLoss');
1364
+ const takeProfit = this.safeValue(params, 'takeProfit');
1365
+ const algoType = this.safeString(params, 'algoType');
1366
+ const isConditional = triggerPrice !== undefined || stopLoss !== undefined || takeProfit !== undefined || (this.safeValue(params, 'childOrders') !== undefined);
1367
+ const isMarket = orderType === 'MARKET';
1368
+ const timeInForce = this.safeStringLower(params, 'timeInForce');
1369
+ const postOnly = this.isPostOnly(isMarket, undefined, params);
1370
+ const orderQtyKey = isConditional ? 'quantity' : 'order_quantity';
1371
+ const priceKey = isConditional ? 'price' : 'order_price';
1372
+ const typeKey = isConditional ? 'type' : 'order_type';
1373
+ request[typeKey] = orderType; // LIMIT/MARKET/IOC/FOK/POST_ONLY/ASK/BID
1374
+ if (!isConditional) {
1375
+ if (postOnly) {
1376
+ request['order_type'] = 'POST_ONLY';
1377
+ }
1378
+ else if (timeInForce === 'fok') {
1379
+ request['order_type'] = 'FOK';
1380
+ }
1381
+ else if (timeInForce === 'ioc') {
1382
+ request['order_type'] = 'IOC';
1383
+ }
1384
+ }
1385
+ if (reduceOnly) {
1386
+ request['reduce_only'] = reduceOnly;
1387
+ }
1388
+ if (price !== undefined) {
1389
+ request[priceKey] = this.priceToPrecision(symbol, price);
1390
+ }
1391
+ if (isMarket && !isConditional) {
1392
+ request[orderQtyKey] = this.amountToPrecision(symbol, amount);
1393
+ }
1394
+ else if (algoType !== 'POSITIONAL_TP_SL') {
1395
+ request[orderQtyKey] = this.amountToPrecision(symbol, amount);
1396
+ }
1397
+ const clientOrderId = this.safeStringN(params, ['clOrdID', 'clientOrderId', 'client_order_id']);
1398
+ if (clientOrderId !== undefined) {
1399
+ request['client_order_id'] = clientOrderId;
1400
+ }
1401
+ if (triggerPrice !== undefined) {
1402
+ request['trigger_price'] = this.priceToPrecision(symbol, triggerPrice);
1403
+ request['algo_type'] = 'STOP';
1404
+ }
1405
+ else if ((stopLoss !== undefined) || (takeProfit !== undefined)) {
1406
+ request['algo_type'] = 'TP_SL';
1407
+ const outterOrder = {
1408
+ 'symbol': market['id'],
1409
+ 'reduce_only': false,
1410
+ 'algo_type': 'POSITIONAL_TP_SL',
1411
+ 'child_orders': [],
1412
+ };
1413
+ const childOrders = outterOrder['child_orders'];
1414
+ const closeSide = (orderSide === 'BUY') ? 'SELL' : 'BUY';
1415
+ if (stopLoss !== undefined) {
1416
+ const stopLossPrice = this.safeNumber2(stopLoss, 'triggerPrice', 'price', stopLoss);
1417
+ const stopLossOrder = {
1418
+ 'side': closeSide,
1419
+ 'algo_type': 'TP_SL',
1420
+ 'trigger_price': this.priceToPrecision(symbol, stopLossPrice),
1421
+ 'type': 'LIMIT',
1422
+ 'reduce_only': true,
1423
+ };
1424
+ childOrders.push(stopLossOrder);
1425
+ }
1426
+ if (takeProfit !== undefined) {
1427
+ const takeProfitPrice = this.safeNumber2(takeProfit, 'triggerPrice', 'price', takeProfit);
1428
+ const takeProfitOrder = {
1429
+ 'side': closeSide,
1430
+ 'algo_type': 'TP_SL',
1431
+ 'trigger_price': this.priceToPrecision(symbol, takeProfitPrice),
1432
+ 'type': 'LIMIT',
1433
+ 'reduce_only': true,
1434
+ };
1435
+ outterOrder.push(takeProfitOrder);
1436
+ }
1437
+ request['child_orders'] = [outterOrder];
1438
+ }
1439
+ params = this.omit(params, ['reduceOnly', 'reduce_only', 'clOrdID', 'clientOrderId', 'client_order_id', 'postOnly', 'timeInForce', 'stopPrice', 'triggerPrice', 'stopLoss', 'takeProfit']);
1440
+ return this.extend(request, params);
1441
+ }
1442
+ /**
1443
+ * @method
1444
+ * @name modetrade#createOrder
1445
+ * @description create a trade order
1446
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/create-order
1447
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/create-algo-order
1448
+ * @param {string} symbol unified symbol of the market to create an order in
1449
+ * @param {string} type 'market' or 'limit'
1450
+ * @param {string} side 'buy' or 'sell'
1451
+ * @param {float} amount how much of currency you want to trade in units of base currency
1452
+ * @param {float} [price] the price at which the order is to be fulfilled, in units of the quote currency, ignored in market orders
1453
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
1454
+ * @param {float} [params.triggerPrice] The price a trigger order is triggered at
1455
+ * @param {object} [params.takeProfit] *takeProfit object in params* containing the triggerPrice at which the attached take profit order will be triggered (perpetual swap markets only)
1456
+ * @param {float} [params.takeProfit.triggerPrice] take profit trigger price
1457
+ * @param {object} [params.stopLoss] *stopLoss object in params* containing the triggerPrice at which the attached stop loss order will be triggered (perpetual swap markets only)
1458
+ * @param {float} [params.stopLoss.triggerPrice] stop loss trigger price
1459
+ * @param {float} [params.algoType] 'STOP'or 'TP_SL' or 'POSITIONAL_TP_SL'
1460
+ * @param {float} [params.cost] *spot market buy only* the quote quantity that can be used as an alternative for the amount
1461
+ * @param {string} [params.clientOrderId] a unique id for the order
1462
+ * @returns {object} an [order structure]{@link https://docs.ccxt.com/#/?id=order-structure}
1463
+ */
1464
+ async createOrder(symbol, type, side, amount, price = undefined, params = {}) {
1465
+ await this.loadMarkets();
1466
+ const market = this.market(symbol);
1467
+ const request = this.createOrderRequest(symbol, type, side, amount, price, params);
1468
+ const triggerPrice = this.safeString2(params, 'triggerPrice', 'stopPrice');
1469
+ const stopLoss = this.safeValue(params, 'stopLoss');
1470
+ const takeProfit = this.safeValue(params, 'takeProfit');
1471
+ const isConditional = triggerPrice !== undefined || stopLoss !== undefined || takeProfit !== undefined || (this.safeValue(params, 'childOrders') !== undefined);
1472
+ let response = undefined;
1473
+ if (isConditional) {
1474
+ response = await this.v1PrivatePostAlgoOrder(request);
1475
+ //
1476
+ // {
1477
+ // "success": true,
1478
+ // "timestamp": 1702989203989,
1479
+ // "data": {
1480
+ // "order_id": 13,
1481
+ // "client_order_id": "testclientid",
1482
+ // "algo_type": "STOP",
1483
+ // "quantity": 100.12
1484
+ // }
1485
+ // }
1486
+ //
1487
+ }
1488
+ else {
1489
+ response = await this.v1PrivatePostOrder(request);
1490
+ //
1491
+ // {
1492
+ // "success": true,
1493
+ // "timestamp": 1702989203989,
1494
+ // "data": {
1495
+ // "order_id": 13,
1496
+ // "client_order_id": "testclientid",
1497
+ // "order_type": "LIMIT",
1498
+ // "order_price": 100.12,
1499
+ // "order_quantity": 0.987654,
1500
+ // "order_amount": 0.8,
1501
+ // "error_message": "none"
1502
+ // }
1503
+ // }
1504
+ //
1505
+ }
1506
+ const data = this.safeDict(response, 'data');
1507
+ data['timestamp'] = this.safeInteger(response, 'timestamp');
1508
+ const order = this.parseOrder(data, market);
1509
+ order['type'] = type;
1510
+ return order;
1511
+ }
1512
+ /**
1513
+ * @method
1514
+ * @name modetrade#createOrders
1515
+ * @description *contract only* create a list of trade orders
1516
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/batch-create-order
1517
+ * @param {Array} orders list of orders to create, each object should contain the parameters required by createOrder, namely symbol, type, side, amount, price and params
1518
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
1519
+ * @returns {object} an [order structure]{@link https://docs.ccxt.com/#/?id=order-structure}
1520
+ */
1521
+ async createOrders(orders, params = {}) {
1522
+ await this.loadMarkets();
1523
+ const ordersRequests = [];
1524
+ for (let i = 0; i < orders.length; i++) {
1525
+ const rawOrder = orders[i];
1526
+ const marketId = this.safeString(rawOrder, 'symbol');
1527
+ const type = this.safeString(rawOrder, 'type');
1528
+ const side = this.safeString(rawOrder, 'side');
1529
+ const amount = this.safeValue(rawOrder, 'amount');
1530
+ const price = this.safeValue(rawOrder, 'price');
1531
+ const orderParams = this.safeDict(rawOrder, 'params', {});
1532
+ const triggerPrice = this.safeString2(orderParams, 'triggerPrice', 'stopPrice');
1533
+ const stopLoss = this.safeValue(orderParams, 'stopLoss');
1534
+ const takeProfit = this.safeValue(orderParams, 'takeProfit');
1535
+ const isConditional = triggerPrice !== undefined || stopLoss !== undefined || takeProfit !== undefined || (this.safeValue(orderParams, 'childOrders') !== undefined);
1536
+ if (isConditional) {
1537
+ throw new NotSupported(this.id + ' createOrders() only support non-stop order');
1538
+ }
1539
+ const orderRequest = this.createOrderRequest(marketId, type, side, amount, price, orderParams);
1540
+ ordersRequests.push(orderRequest);
1541
+ }
1542
+ const request = {
1543
+ 'orders': ordersRequests,
1544
+ };
1545
+ const response = await this.v1PrivatePostBatchOrder(this.extend(request, params));
1546
+ //
1547
+ // {
1548
+ // "success": true,
1549
+ // "timestamp": 1702989203989,
1550
+ // "data": {
1551
+ // "rows": [{
1552
+ // "order_id": 13,
1553
+ // "client_order_id": "testclientid",
1554
+ // "order_type": "LIMIT",
1555
+ // "order_price": 100.12,
1556
+ // "order_quantity": 0.987654,
1557
+ // "order_amount": 0.8,
1558
+ // "error_message": "none"
1559
+ // }]
1560
+ // }
1561
+ // }
1562
+ //
1563
+ const data = this.safeDict(response, 'data', {});
1564
+ const rows = this.safeList(data, 'rows', []);
1565
+ return this.parseOrders(rows);
1566
+ }
1567
+ /**
1568
+ * @method
1569
+ * @name modetrade#editOrder
1570
+ * @description edit a trade order
1571
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/edit-order
1572
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/edit-algo-order
1573
+ * @param {string} id order id
1574
+ * @param {string} symbol unified symbol of the market to create an order in
1575
+ * @param {string} type 'market' or 'limit'
1576
+ * @param {string} side 'buy' or 'sell'
1577
+ * @param {float} amount how much of currency you want to trade in units of base currency
1578
+ * @param {float} [price] the price at which the order is to be fulfilled, in units of the quote currency, ignored in market orders
1579
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
1580
+ * @param {float} [params.triggerPrice] The price a trigger order is triggered at
1581
+ * @param {float} [params.stopLossPrice] price to trigger stop-loss orders
1582
+ * @param {float} [params.takeProfitPrice] price to trigger take-profit orders
1583
+ * @returns {object} an [order structure]{@link https://docs.ccxt.com/#/?id=order-structure}
1584
+ */
1585
+ async editOrder(id, symbol, type, side, amount = undefined, price = undefined, params = {}) {
1586
+ await this.loadMarkets();
1587
+ const market = this.market(symbol);
1588
+ const request = {
1589
+ 'order_id': id,
1590
+ };
1591
+ const triggerPrice = this.safeStringN(params, ['triggerPrice', 'stopPrice', 'takeProfitPrice', 'stopLossPrice']);
1592
+ if (triggerPrice !== undefined) {
1593
+ request['triggerPrice'] = this.priceToPrecision(symbol, triggerPrice);
1594
+ }
1595
+ const isConditional = (triggerPrice !== undefined) || (this.safeValue(params, 'childOrders') !== undefined);
1596
+ const orderQtyKey = isConditional ? 'quantity' : 'order_quantity';
1597
+ const priceKey = isConditional ? 'price' : 'order_price';
1598
+ if (price !== undefined) {
1599
+ request[priceKey] = this.priceToPrecision(symbol, price);
1600
+ }
1601
+ if (amount !== undefined) {
1602
+ request[orderQtyKey] = this.amountToPrecision(symbol, amount);
1603
+ }
1604
+ params = this.omit(params, ['stopPrice', 'triggerPrice', 'takeProfitPrice', 'stopLossPrice', 'trailingTriggerPrice', 'trailingAmount', 'trailingPercent']);
1605
+ let response = undefined;
1606
+ if (isConditional) {
1607
+ response = await this.v1PrivatePutAlgoOrder(this.extend(request, params));
1608
+ }
1609
+ else {
1610
+ request['symbol'] = market['id'];
1611
+ request['side'] = side.toUpperCase();
1612
+ const orderType = type.toUpperCase();
1613
+ const timeInForce = this.safeStringLower(params, 'timeInForce');
1614
+ const isMarket = orderType === 'MARKET';
1615
+ const postOnly = this.isPostOnly(isMarket, undefined, params);
1616
+ if (postOnly) {
1617
+ request['order_type'] = 'POST_ONLY';
1618
+ }
1619
+ else if (timeInForce === 'fok') {
1620
+ request['order_type'] = 'FOK';
1621
+ }
1622
+ else if (timeInForce === 'ioc') {
1623
+ request['order_type'] = 'IOC';
1624
+ }
1625
+ else {
1626
+ request['order_type'] = orderType;
1627
+ }
1628
+ const clientOrderId = this.safeStringN(params, ['clOrdID', 'clientOrderId', 'client_order_id']);
1629
+ params = this.omit(params, ['clOrdID', 'clientOrderId', 'client_order_id', 'postOnly', 'timeInForce']);
1630
+ if (clientOrderId !== undefined) {
1631
+ request['client_order_id'] = clientOrderId;
1632
+ }
1633
+ // request['side'] = side.toUpperCase ();
1634
+ // request['symbol'] = market['id'];
1635
+ response = await this.v1PrivatePutOrder(this.extend(request, params));
1636
+ }
1637
+ //
1638
+ // {
1639
+ // "success": true,
1640
+ // "timestamp": 1702989203989,
1641
+ // "data": {
1642
+ // "status": "EDIT_SENT"
1643
+ // }
1644
+ // }
1645
+ //
1646
+ const data = this.safeDict(response, 'data', {});
1647
+ data['timestamp'] = this.safeInteger(response, 'timestamp');
1648
+ return this.parseOrder(data, market);
1649
+ }
1650
+ /**
1651
+ * @method
1652
+ * @name modetrade#cancelOrder
1653
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/cancel-order
1654
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/cancel-order-by-client_order_id
1655
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/cancel-algo-order
1656
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/cancel-algo-order-by-client_order_id
1657
+ * @description cancels an open order
1658
+ * @param {string} id order id
1659
+ * @param {string} symbol unified symbol of the market the order was made in
1660
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
1661
+ * @param {boolean} [params.trigger] whether the order is a stop/algo order
1662
+ * @param {string} [params.clientOrderId] a unique id for the order
1663
+ * @returns {object} An [order structure]{@link https://docs.ccxt.com/#/?id=order-structure}
1664
+ */
1665
+ async cancelOrder(id, symbol = undefined, params = {}) {
1666
+ const trigger = this.safeBool2(params, 'stop', 'trigger', false);
1667
+ params = this.omit(params, ['stop', 'trigger']);
1668
+ if (!trigger && (symbol === undefined)) {
1669
+ throw new ArgumentsRequired(this.id + ' cancelOrder() requires a symbol argument');
1670
+ }
1671
+ await this.loadMarkets();
1672
+ let market = undefined;
1673
+ if (symbol !== undefined) {
1674
+ market = this.market(symbol);
1675
+ }
1676
+ const request = {
1677
+ 'symbol': market['id'],
1678
+ };
1679
+ const clientOrderIdUnified = this.safeString2(params, 'clOrdID', 'clientOrderId');
1680
+ const clientOrderIdExchangeSpecific = this.safeString(params, 'client_order_id', clientOrderIdUnified);
1681
+ const isByClientOrder = clientOrderIdExchangeSpecific !== undefined;
1682
+ let response = undefined;
1683
+ if (trigger) {
1684
+ if (isByClientOrder) {
1685
+ request['client_order_id'] = clientOrderIdExchangeSpecific;
1686
+ params = this.omit(params, ['clOrdID', 'clientOrderId', 'client_order_id']);
1687
+ response = await this.v1PrivateDeleteAlgoClientOrder(this.extend(request, params));
1688
+ }
1689
+ else {
1690
+ request['order_id'] = id;
1691
+ response = await this.v1PrivateDeleteAlgoOrder(this.extend(request, params));
1692
+ }
1693
+ }
1694
+ else {
1695
+ if (isByClientOrder) {
1696
+ request['client_order_id'] = clientOrderIdExchangeSpecific;
1697
+ params = this.omit(params, ['clOrdID', 'clientOrderId', 'client_order_id']);
1698
+ response = await this.v1PrivateDeleteClientOrder(this.extend(request, params));
1699
+ }
1700
+ else {
1701
+ request['order_id'] = id;
1702
+ response = await this.v1PrivateDeleteOrder(this.extend(request, params));
1703
+ }
1704
+ }
1705
+ //
1706
+ // {
1707
+ // "success": true,
1708
+ // "timestamp": 1702989203989,
1709
+ // "data": {
1710
+ // "status": "CANCEL_SENT"
1711
+ // }
1712
+ // }
1713
+ //
1714
+ // {
1715
+ // "success": true,
1716
+ // "timestamp": 1702989203989,
1717
+ // "status": "CANCEL_SENT"
1718
+ // }
1719
+ //
1720
+ const extendParams = { 'symbol': symbol };
1721
+ if (isByClientOrder) {
1722
+ extendParams['client_order_id'] = clientOrderIdExchangeSpecific;
1723
+ }
1724
+ else {
1725
+ extendParams['id'] = id;
1726
+ }
1727
+ if (trigger) {
1728
+ return this.extend(this.parseOrder(response), extendParams);
1729
+ }
1730
+ const data = this.safeDict(response, 'data', {});
1731
+ return this.extend(this.parseOrder(data), extendParams);
1732
+ }
1733
+ /**
1734
+ * @method
1735
+ * @name modetrade#cancelOrders
1736
+ * @description cancel multiple orders
1737
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/batch-cancel-orders
1738
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/batch-cancel-orders-by-client_order_id
1739
+ * @param {string[]} ids order ids
1740
+ * @param {string} [symbol] unified market symbol
1741
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
1742
+ * @param {string[]} [params.client_order_ids] max length 10 e.g. ["my_id_1","my_id_2"], encode the double quotes. No space after comma
1743
+ * @returns {object} an list of [order structures]{@link https://docs.ccxt.com/#/?id=order-structure}
1744
+ */
1745
+ async cancelOrders(ids, symbol = undefined, params = {}) {
1746
+ await this.loadMarkets();
1747
+ const clientOrderIds = this.safeListN(params, ['clOrdIDs', 'clientOrderIds', 'client_order_ids']);
1748
+ params = this.omit(params, ['clOrdIDs', 'clientOrderIds', 'client_order_ids']);
1749
+ const request = {};
1750
+ let response = undefined;
1751
+ if (clientOrderIds) {
1752
+ request['client_order_ids'] = clientOrderIds.join(',');
1753
+ response = await this.v1PrivateDeleteClientBatchOrder(this.extend(request, params));
1754
+ }
1755
+ else {
1756
+ request['order_ids'] = ids.join(',');
1757
+ response = await this.v1PrivateDeleteBatchOrder(this.extend(request, params));
1758
+ }
1759
+ //
1760
+ // {
1761
+ // "success": true,
1762
+ // "timestamp": 1702989203989,
1763
+ // "data": {
1764
+ // "status": "CANCEL_ALL_SENT"
1765
+ // }
1766
+ // }
1767
+ //
1768
+ return [this.safeOrder({
1769
+ 'info': response,
1770
+ })];
1771
+ }
1772
+ /**
1773
+ * @method
1774
+ * @name modetrade#cancelAllOrders
1775
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/cancel-all-pending-algo-orders
1776
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/cancel-orders-in-bulk
1777
+ * @description cancel all open orders in a market
1778
+ * @param {string} symbol unified market symbol
1779
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
1780
+ * @param {boolean} [params.trigger] whether the order is a stop/algo order
1781
+ * @returns {object} an list of [order structures]{@link https://docs.ccxt.com/#/?id=order-structure}
1782
+ */
1783
+ async cancelAllOrders(symbol = undefined, params = {}) {
1784
+ await this.loadMarkets();
1785
+ const trigger = this.safeBool2(params, 'stop', 'trigger');
1786
+ params = this.omit(params, ['stop', 'trigger']);
1787
+ const request = {};
1788
+ if (symbol !== undefined) {
1789
+ const market = this.market(symbol);
1790
+ request['symbol'] = market['id'];
1791
+ }
1792
+ let response = undefined;
1793
+ if (trigger) {
1794
+ response = await this.v1PrivateDeleteAlgoOrders(this.extend(request, params));
1795
+ }
1796
+ else {
1797
+ response = await this.v1PrivateDeleteOrders(this.extend(request, params));
1798
+ }
1799
+ // trigger
1800
+ // {
1801
+ // "success": true,
1802
+ // "timestamp": 1702989203989,
1803
+ // "status": "CANCEL_ALL_SENT"
1804
+ // }
1805
+ //
1806
+ // {
1807
+ // "success": true,
1808
+ // "timestamp": 1702989203989,
1809
+ // "data": {
1810
+ // "status": "CANCEL_ALL_SENT"
1811
+ // }
1812
+ // }
1813
+ //
1814
+ return [
1815
+ {
1816
+ 'info': response,
1817
+ },
1818
+ ];
1819
+ }
1820
+ /**
1821
+ * @method
1822
+ * @name modetrade#fetchOrder
1823
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/get-order-by-order_id
1824
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/get-order-by-client_order_id
1825
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/get-algo-order-by-order_id
1826
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/get-algo-order-by-client_order_id
1827
+ * @description fetches information on an order made by the user
1828
+ * @param {string} id the order id
1829
+ * @param {string} symbol unified symbol of the market the order was made in
1830
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
1831
+ * @param {boolean} [params.trigger] whether the order is a stop/algo order
1832
+ * @param {string} [params.clientOrderId] a unique id for the order
1833
+ * @returns {object} An [order structure]{@link https://docs.ccxt.com/#/?id=order-structure}
1834
+ */
1835
+ async fetchOrder(id, symbol = undefined, params = {}) {
1836
+ await this.loadMarkets();
1837
+ let market = undefined;
1838
+ if (symbol !== undefined) {
1839
+ market = this.market(symbol);
1840
+ }
1841
+ const trigger = this.safeBool2(params, 'stop', 'trigger', false);
1842
+ const request = {};
1843
+ const clientOrderId = this.safeStringN(params, ['clOrdID', 'clientOrderId', 'client_order_id']);
1844
+ params = this.omit(params, ['stop', 'trigger', 'clOrdID', 'clientOrderId', 'client_order_id']);
1845
+ let response = undefined;
1846
+ if (trigger) {
1847
+ if (clientOrderId) {
1848
+ request['client_order_id'] = clientOrderId;
1849
+ response = await this.v1PrivateGetAlgoClientOrderClientOrderId(this.extend(request, params));
1850
+ }
1851
+ else {
1852
+ request['oid'] = id;
1853
+ response = await this.v1PrivateGetAlgoOrderOid(this.extend(request, params));
1854
+ }
1855
+ }
1856
+ else {
1857
+ if (clientOrderId) {
1858
+ request['client_order_id'] = clientOrderId;
1859
+ response = await this.v1PrivateGetClientOrderClientOrderId(this.extend(request, params));
1860
+ }
1861
+ else {
1862
+ request['oid'] = id;
1863
+ response = await this.v1PrivateGetOrderOid(this.extend(request, params));
1864
+ }
1865
+ }
1866
+ //
1867
+ // {
1868
+ // "success": true,
1869
+ // "timestamp": 1702989203989,
1870
+ // "data": {
1871
+ // "order_id": 78151,
1872
+ // "user_id": 12345,
1873
+ // "price": 0.67772,
1874
+ // "type": "LIMIT",
1875
+ // "quantity": 20,
1876
+ // "amount": 10,
1877
+ // "executed_quantity": 20,
1878
+ // "total_executed_quantity": 20,
1879
+ // "visible_quantity": 1,
1880
+ // "symbol": "PERP_BTC_USDC",
1881
+ // "side": "BUY",
1882
+ // "status": "FILLED",
1883
+ // "total_fee": 0.5,
1884
+ // "fee_asset": "BTC",
1885
+ // "client_order_id": 1,
1886
+ // "average_executed_price": 0.67772,
1887
+ // "created_time": 1653563963000,
1888
+ // "updated_time": 1653564213000,
1889
+ // "realized_pnl": 123
1890
+ // }
1891
+ // }
1892
+ //
1893
+ const orders = this.safeDict(response, 'data', response);
1894
+ return this.parseOrder(orders, market);
1895
+ }
1896
+ /**
1897
+ * @method
1898
+ * @name modetrade#fetchOrders
1899
+ * @description fetches information on multiple orders made by the user
1900
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/get-orders
1901
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/get-algo-orders
1902
+ * @param {string} symbol unified market symbol of the market orders were made in
1903
+ * @param {int} [since] the earliest time in ms to fetch orders for
1904
+ * @param {int} [limit] the maximum number of order structures to retrieve
1905
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
1906
+ * @param {boolean} [params.trigger] whether the order is a stop/algo order
1907
+ * @param {boolean} [params.is_triggered] whether the order has been triggered (false by default)
1908
+ * @param {string} [params.side] 'buy' or 'sell'
1909
+ * @param {boolean} [params.paginate] set to true if you want to fetch orders with pagination
1910
+ * @param {int} params.until timestamp in ms of the latest order to fetch
1911
+ * @returns {Order[]} a list of [order structures]{@link https://docs.ccxt.com/#/?id=order-structure}
1912
+ */
1913
+ async fetchOrders(symbol = undefined, since = undefined, limit = undefined, params = {}) {
1914
+ await this.loadMarkets();
1915
+ let paginate = false;
1916
+ const isTrigger = this.safeBool2(params, 'stop', 'trigger', false);
1917
+ const maxLimit = (isTrigger) ? 100 : 500;
1918
+ [paginate, params] = this.handleOptionAndParams(params, 'fetchOrders', 'paginate');
1919
+ if (paginate) {
1920
+ return await this.fetchPaginatedCallIncremental('fetchOrders', symbol, since, limit, params, 'page', maxLimit);
1921
+ }
1922
+ let request = {};
1923
+ let market = undefined;
1924
+ params = this.omit(params, ['stop', 'trigger']);
1925
+ if (symbol !== undefined) {
1926
+ market = this.market(symbol);
1927
+ request['symbol'] = market['id'];
1928
+ }
1929
+ if (since !== undefined) {
1930
+ request['start_t'] = since;
1931
+ }
1932
+ if (limit !== undefined) {
1933
+ request['size'] = limit;
1934
+ }
1935
+ else {
1936
+ request['size'] = maxLimit;
1937
+ }
1938
+ if (isTrigger) {
1939
+ request['algo_type'] = 'STOP';
1940
+ }
1941
+ [request, params] = this.handleUntilOption('end_t', request, params);
1942
+ let response = undefined;
1943
+ if (isTrigger) {
1944
+ response = await this.v1PrivateGetAlgoOrders(this.extend(request, params));
1945
+ }
1946
+ else {
1947
+ response = await this.v1PrivateGetOrders(this.extend(request, params));
1948
+ }
1949
+ //
1950
+ // {
1951
+ // "success": true,
1952
+ // "timestamp": 1702989203989,
1953
+ // "data": {
1954
+ // "meta": {
1955
+ // "total": 9,
1956
+ // "records_per_page": 25,
1957
+ // "current_page": 1
1958
+ // },
1959
+ // "rows": [{
1960
+ // "order_id": 78151,
1961
+ // "user_id": 12345,
1962
+ // "price": 0.67772,
1963
+ // "type": "LIMIT",
1964
+ // "quantity": 20,
1965
+ // "amount": 10,
1966
+ // "executed_quantity": 20,
1967
+ // "total_executed_quantity": 20,
1968
+ // "visible_quantity": 1,
1969
+ // "symbol": "PERP_BTC_USDC",
1970
+ // "side": "BUY",
1971
+ // "status": "FILLED",
1972
+ // "total_fee": 0.5,
1973
+ // "fee_asset": "BTC",
1974
+ // "client_order_id": 1,
1975
+ // "average_executed_price": 0.67772,
1976
+ // "created_time": 1653563963000,
1977
+ // "updated_time": 1653564213000,
1978
+ // "realized_pnl": 123
1979
+ // }]
1980
+ // }
1981
+ // }
1982
+ //
1983
+ const data = this.safeValue(response, 'data', response);
1984
+ const orders = this.safeList(data, 'rows');
1985
+ return this.parseOrders(orders, market, since, limit);
1986
+ }
1987
+ /**
1988
+ * @method
1989
+ * @name modetrade#fetchOpenOrders
1990
+ * @description fetches information on multiple orders made by the user
1991
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/get-orders
1992
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/get-algo-orders
1993
+ * @param {string} symbol unified market symbol of the market orders were made in
1994
+ * @param {int} [since] the earliest time in ms to fetch orders for
1995
+ * @param {int} [limit] the maximum number of order structures to retrieve
1996
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
1997
+ * @param {boolean} [params.trigger] whether the order is a stop/algo order
1998
+ * @param {boolean} [params.is_triggered] whether the order has been triggered (false by default)
1999
+ * @param {string} [params.side] 'buy' or 'sell'
2000
+ * @param {int} params.until timestamp in ms of the latest order to fetch
2001
+ * @param {boolean} [params.paginate] set to true if you want to fetch orders with pagination
2002
+ * @returns {Order[]} a list of [order structures]{@link https://docs.ccxt.com/#/?id=order-structure}
2003
+ */
2004
+ async fetchOpenOrders(symbol = undefined, since = undefined, limit = undefined, params = {}) {
2005
+ await this.loadMarkets();
2006
+ const extendedParams = this.extend(params, { 'status': 'INCOMPLETE' });
2007
+ return await this.fetchOrders(symbol, since, limit, extendedParams);
2008
+ }
2009
+ /**
2010
+ * @method
2011
+ * @name modetrade#fetchClosedOrders
2012
+ * @description fetches information on multiple orders made by the user
2013
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/get-orders
2014
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/get-algo-orders
2015
+ * @param {string} symbol unified market symbol of the market orders were made in
2016
+ * @param {int} [since] the earliest time in ms to fetch orders for
2017
+ * @param {int} [limit] the maximum number of order structures to retrieve
2018
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
2019
+ * @param {boolean} [params.trigger] whether the order is a stop/algo order
2020
+ * @param {boolean} [params.is_triggered] whether the order has been triggered (false by default)
2021
+ * @param {string} [params.side] 'buy' or 'sell'
2022
+ * @param {int} params.until timestamp in ms of the latest order to fetch
2023
+ * @param {boolean} [params.paginate] set to true if you want to fetch orders with pagination
2024
+ * @returns {Order[]} a list of [order structures]{@link https://docs.ccxt.com/#/?id=order-structure}
2025
+ */
2026
+ async fetchClosedOrders(symbol = undefined, since = undefined, limit = undefined, params = {}) {
2027
+ await this.loadMarkets();
2028
+ const extendedParams = this.extend(params, { 'status': 'COMPLETED' });
2029
+ return await this.fetchOrders(symbol, since, limit, extendedParams);
2030
+ }
2031
+ /**
2032
+ * @method
2033
+ * @name modetrade#fetchOrderTrades
2034
+ * @description fetch all the trades made from a single order
2035
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/get-all-trades-of-specific-order
2036
+ * @param {string} id order id
2037
+ * @param {string} symbol unified market symbol
2038
+ * @param {int} [since] the earliest time in ms to fetch trades for
2039
+ * @param {int} [limit] the maximum number of trades to retrieve
2040
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
2041
+ * @returns {object[]} a list of [trade structures]{@link https://docs.ccxt.com/#/?id=trade-structure}
2042
+ */
2043
+ async fetchOrderTrades(id, symbol = undefined, since = undefined, limit = undefined, params = {}) {
2044
+ await this.loadMarkets();
2045
+ let market = undefined;
2046
+ if (symbol !== undefined) {
2047
+ market = this.market(symbol);
2048
+ }
2049
+ const request = {
2050
+ 'oid': id,
2051
+ };
2052
+ const response = await this.v1PrivateGetOrderOidTrades(this.extend(request, params));
2053
+ //
2054
+ // {
2055
+ // "success": true,
2056
+ // "timestamp": 1702989203989,
2057
+ // "data": {
2058
+ // "rows": [{
2059
+ // "id": 2,
2060
+ // "symbol": "PERP_BTC_USDC",
2061
+ // "fee": 0.0001,
2062
+ // "fee_asset": "USDC",
2063
+ // "side": "BUY",
2064
+ // "order_id": 1,
2065
+ // "executed_price": 123,
2066
+ // "executed_quantity": 0.05,
2067
+ // "executed_timestamp": 1567382401000,
2068
+ // "is_maker": 1,
2069
+ // "realized_pnl": 123
2070
+ // }]
2071
+ // }
2072
+ // }
2073
+ //
2074
+ const data = this.safeDict(response, 'data', {});
2075
+ const trades = this.safeList(data, 'rows', []);
2076
+ return this.parseTrades(trades, market, since, limit, params);
2077
+ }
2078
+ /**
2079
+ * @method
2080
+ * @name modetrade#fetchMyTrades
2081
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/get-trades
2082
+ * @description fetch all trades made by the user
2083
+ * @param {string} symbol unified market symbol
2084
+ * @param {int} [since] the earliest time in ms to fetch trades for
2085
+ * @param {int} [limit] the maximum number of trades structures to retrieve
2086
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
2087
+ * @param {boolean} [params.paginate] set to true if you want to fetch trades with pagination
2088
+ * @param {int} params.until timestamp in ms of the latest trade to fetch
2089
+ * @returns {Trade[]} a list of [trade structures]{@link https://docs.ccxt.com/#/?id=trade-structure}
2090
+ */
2091
+ async fetchMyTrades(symbol = undefined, since = undefined, limit = undefined, params = {}) {
2092
+ await this.loadMarkets();
2093
+ let paginate = false;
2094
+ [paginate, params] = this.handleOptionAndParams(params, 'fetchMyTrades', 'paginate');
2095
+ if (paginate) {
2096
+ return await this.fetchPaginatedCallIncremental('fetchMyTrades', symbol, since, limit, params, 'page', 500);
2097
+ }
2098
+ let request = {};
2099
+ let market = undefined;
2100
+ if (symbol !== undefined) {
2101
+ market = this.market(symbol);
2102
+ request['symbol'] = market['id'];
2103
+ }
2104
+ if (since !== undefined) {
2105
+ request['start_t'] = since;
2106
+ }
2107
+ if (limit !== undefined) {
2108
+ request['size'] = limit;
2109
+ }
2110
+ else {
2111
+ request['size'] = 500;
2112
+ }
2113
+ [request, params] = this.handleUntilOption('end_t', request, params);
2114
+ const response = await this.v1PrivateGetTrades(this.extend(request, params));
2115
+ //
2116
+ // {
2117
+ // "success": true,
2118
+ // "timestamp": 1702989203989,
2119
+ // "data": {
2120
+ // "meta": {
2121
+ // "total": 9,
2122
+ // "records_per_page": 25,
2123
+ // "current_page": 1
2124
+ // },
2125
+ // "rows": [{
2126
+ // "id": 2,
2127
+ // "symbol": "PERP_BTC_USDC",
2128
+ // "fee": 0.0001,
2129
+ // "fee_asset": "USDC",
2130
+ // "side": "BUY",
2131
+ // "order_id": 1,
2132
+ // "executed_price": 123,
2133
+ // "executed_quantity": 0.05,
2134
+ // "executed_timestamp": 1567382401000,
2135
+ // "is_maker": 1,
2136
+ // "realized_pnl": 123
2137
+ // }]
2138
+ // }
2139
+ // }
2140
+ //
2141
+ const data = this.safeDict(response, 'data', {});
2142
+ const trades = this.safeList(data, 'rows', []);
2143
+ return this.parseTrades(trades, market, since, limit, params);
2144
+ }
2145
+ parseBalance(response) {
2146
+ const result = {
2147
+ 'info': response,
2148
+ };
2149
+ const balances = this.safeList(response, 'holding', []);
2150
+ for (let i = 0; i < balances.length; i++) {
2151
+ const balance = balances[i];
2152
+ const code = this.safeCurrencyCode(this.safeString(balance, 'token'));
2153
+ const account = this.account();
2154
+ account['total'] = this.safeString(balance, 'holding');
2155
+ account['frozen'] = this.safeString(balance, 'frozen');
2156
+ result[code] = account;
2157
+ }
2158
+ return this.safeBalance(result);
2159
+ }
2160
+ /**
2161
+ * @method
2162
+ * @name modetrade#fetchBalance
2163
+ * @description query for balance and get the amount of funds available for trading or funds locked in orders
2164
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/get-current-holding
2165
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
2166
+ * @returns {object} a [balance structure]{@link https://docs.ccxt.com/#/?id=balance-structure}
2167
+ */
2168
+ async fetchBalance(params = {}) {
2169
+ await this.loadMarkets();
2170
+ const response = await this.v1PrivateGetClientHolding(params);
2171
+ //
2172
+ // {
2173
+ // "success": true,
2174
+ // "timestamp": 1702989203989,
2175
+ // "data": {
2176
+ // "holding": [{
2177
+ // "updated_time": 1580794149000,
2178
+ // "token": "BTC",
2179
+ // "holding": -28.000752,
2180
+ // "frozen": 123,
2181
+ // "pending_short": -2000
2182
+ // }]
2183
+ // }
2184
+ // }
2185
+ //
2186
+ const data = this.safeDict(response, 'data');
2187
+ return this.parseBalance(data);
2188
+ }
2189
+ async getAssetHistoryRows(code = undefined, since = undefined, limit = undefined, params = {}) {
2190
+ await this.loadMarkets();
2191
+ const request = {};
2192
+ let currency = undefined;
2193
+ if (code !== undefined) {
2194
+ currency = this.currency(code);
2195
+ request['balance_token'] = currency['id'];
2196
+ }
2197
+ if (since !== undefined) {
2198
+ request['start_t'] = since;
2199
+ }
2200
+ if (limit !== undefined) {
2201
+ request['pageSize'] = limit;
2202
+ }
2203
+ const transactionType = this.safeString(params, 'type');
2204
+ params = this.omit(params, 'type');
2205
+ if (transactionType !== undefined) {
2206
+ request['type'] = transactionType;
2207
+ }
2208
+ const response = await this.v1PrivateGetAssetHistory(this.extend(request, params));
2209
+ //
2210
+ // {
2211
+ // "success": true,
2212
+ // "timestamp": 1702989203989,
2213
+ // "data": {
2214
+ // "meta": {
2215
+ // "total": 9,
2216
+ // "records_per_page": 25,
2217
+ // "current_page": 1
2218
+ // },
2219
+ // "rows": [{
2220
+ // "id": "230707030600002",
2221
+ // "tx_id": "0x4b0714c63cc7abae72bf68e84e25860b88ca651b7d27dad1e32bf4c027fa5326",
2222
+ // "side": "WITHDRAW",
2223
+ // "token": "USDC",
2224
+ // "amount": 555,
2225
+ // "fee": 123,
2226
+ // "trans_status": "FAILED",
2227
+ // "created_time": 1688699193034,
2228
+ // "updated_time": 1688699193096,
2229
+ // "chain_id": "986532"
2230
+ // }]
2231
+ // }
2232
+ // }
2233
+ //
2234
+ const data = this.safeDict(response, 'data', {});
2235
+ return [currency, this.safeList(data, 'rows', [])];
2236
+ }
2237
+ parseLedgerEntry(item, currency = undefined) {
2238
+ const currencyId = this.safeString(item, 'token');
2239
+ const code = this.safeCurrencyCode(currencyId, currency);
2240
+ currency = this.safeCurrency(currencyId, currency);
2241
+ const amount = this.safeNumber(item, 'amount');
2242
+ const side = this.safeString(item, 'token_side');
2243
+ const direction = (side === 'DEPOSIT') ? 'in' : 'out';
2244
+ const timestamp = this.safeInteger(item, 'created_time');
2245
+ const fee = this.parseTokenAndFeeTemp(item, 'fee_token', 'fee_amount');
2246
+ return this.safeLedgerEntry({
2247
+ 'id': this.safeString(item, 'id'),
2248
+ 'currency': code,
2249
+ 'account': this.safeString(item, 'account'),
2250
+ 'referenceAccount': undefined,
2251
+ 'referenceId': this.safeString(item, 'tx_id'),
2252
+ 'status': this.parseTransactionStatus(this.safeString(item, 'status')),
2253
+ 'amount': amount,
2254
+ 'before': undefined,
2255
+ 'after': undefined,
2256
+ 'fee': fee,
2257
+ 'direction': direction,
2258
+ 'timestamp': timestamp,
2259
+ 'datetime': this.iso8601(timestamp),
2260
+ 'type': this.parseLedgerEntryType(this.safeString(item, 'type')),
2261
+ 'info': item,
2262
+ }, currency);
2263
+ }
2264
+ parseLedgerEntryType(type) {
2265
+ const types = {
2266
+ 'BALANCE': 'transaction',
2267
+ 'COLLATERAL': 'transfer', // Funds moved between portfolios
2268
+ };
2269
+ return this.safeString(types, type, type);
2270
+ }
2271
+ /**
2272
+ * @method
2273
+ * @name modetrade#fetchLedger
2274
+ * @description fetch the history of changes, actions done by the user or operations that altered the balance of the user
2275
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/get-asset-history
2276
+ * @param {string} [code] unified currency code, default is undefined
2277
+ * @param {int} [since] timestamp in ms of the earliest ledger entry, default is undefined
2278
+ * @param {int} [limit] max number of ledger entries to return, default is undefined
2279
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
2280
+ * @returns {object} a [ledger structure]{@link https://docs.ccxt.com/#/?id=ledger}
2281
+ */
2282
+ async fetchLedger(code = undefined, since = undefined, limit = undefined, params = {}) {
2283
+ const currencyRows = await this.getAssetHistoryRows(code, since, limit, params);
2284
+ const currency = this.safeValue(currencyRows, 0);
2285
+ const rows = this.safeList(currencyRows, 1);
2286
+ return this.parseLedger(rows, currency, since, limit, params);
2287
+ }
2288
+ parseTransaction(transaction, currency = undefined) {
2289
+ // example in fetchLedger
2290
+ const code = this.safeString(transaction, 'token');
2291
+ let movementDirection = this.safeStringLower(transaction, 'token_side');
2292
+ if (movementDirection === 'withdraw') {
2293
+ movementDirection = 'withdrawal';
2294
+ }
2295
+ const fee = this.parseTokenAndFeeTemp(transaction, 'fee_token', 'fee_amount');
2296
+ const addressTo = this.safeString(transaction, 'target_address');
2297
+ const addressFrom = this.safeString(transaction, 'source_address');
2298
+ const timestamp = this.safeInteger(transaction, 'created_time');
2299
+ return {
2300
+ 'info': transaction,
2301
+ 'id': this.safeString2(transaction, 'id', 'withdraw_id'),
2302
+ 'txid': this.safeString(transaction, 'tx_id'),
2303
+ 'timestamp': timestamp,
2304
+ 'datetime': this.iso8601(timestamp),
2305
+ 'address': undefined,
2306
+ 'addressFrom': addressFrom,
2307
+ 'addressTo': addressTo,
2308
+ 'tag': this.safeString(transaction, 'extra'),
2309
+ 'tagFrom': undefined,
2310
+ 'tagTo': undefined,
2311
+ 'type': movementDirection,
2312
+ 'amount': this.safeNumber(transaction, 'amount'),
2313
+ 'currency': code,
2314
+ 'status': this.parseTransactionStatus(this.safeString(transaction, 'status')),
2315
+ 'updated': this.safeInteger(transaction, 'updated_time'),
2316
+ 'comment': undefined,
2317
+ 'internal': undefined,
2318
+ 'fee': fee,
2319
+ 'network': undefined,
2320
+ };
2321
+ }
2322
+ parseTransactionStatus(status) {
2323
+ const statuses = {
2324
+ 'NEW': 'pending',
2325
+ 'CONFIRMING': 'pending',
2326
+ 'PROCESSING': 'pending',
2327
+ 'COMPLETED': 'ok',
2328
+ 'CANCELED': 'canceled',
2329
+ };
2330
+ return this.safeString(statuses, status, status);
2331
+ }
2332
+ /**
2333
+ * @method
2334
+ * @name modetrade#fetchDeposits
2335
+ * @description fetch all deposits made to an account
2336
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/get-asset-history
2337
+ * @param {string} code unified currency code
2338
+ * @param {int} [since] the earliest time in ms to fetch deposits for
2339
+ * @param {int} [limit] the maximum number of deposits structures to retrieve
2340
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
2341
+ * @returns {object[]} a list of [transaction structures]{@link https://docs.ccxt.com/#/?id=transaction-structure}
2342
+ */
2343
+ async fetchDeposits(code = undefined, since = undefined, limit = undefined, params = {}) {
2344
+ const request = {
2345
+ 'side': 'DEPOSIT',
2346
+ };
2347
+ return await this.fetchDepositsWithdrawals(code, since, limit, this.extend(request, params));
2348
+ }
2349
+ /**
2350
+ * @method
2351
+ * @name modetrade#fetchWithdrawals
2352
+ * @description fetch all withdrawals made from an account
2353
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/get-asset-history
2354
+ * @param {string} code unified currency code
2355
+ * @param {int} [since] the earliest time in ms to fetch withdrawals for
2356
+ * @param {int} [limit] the maximum number of withdrawals structures to retrieve
2357
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
2358
+ * @returns {object[]} a list of [transaction structures]{@link https://docs.ccxt.com/#/?id=transaction-structure}
2359
+ */
2360
+ async fetchWithdrawals(code = undefined, since = undefined, limit = undefined, params = {}) {
2361
+ const request = {
2362
+ 'side': 'WITHDRAW',
2363
+ };
2364
+ return await this.fetchDepositsWithdrawals(code, since, limit, this.extend(request, params));
2365
+ }
2366
+ /**
2367
+ * @method
2368
+ * @name modetrade#fetchDepositsWithdrawals
2369
+ * @description fetch history of deposits and withdrawals
2370
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/get-asset-history
2371
+ * @param {string} [code] unified currency code for the currency of the deposit/withdrawals, default is undefined
2372
+ * @param {int} [since] timestamp in ms of the earliest deposit/withdrawal, default is undefined
2373
+ * @param {int} [limit] max number of deposit/withdrawals to return, default is undefined
2374
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
2375
+ * @returns {object} a list of [transaction structure]{@link https://docs.ccxt.com/#/?id=transaction-structure}
2376
+ */
2377
+ async fetchDepositsWithdrawals(code = undefined, since = undefined, limit = undefined, params = {}) {
2378
+ const request = {};
2379
+ const currencyRows = await this.getAssetHistoryRows(code, since, limit, this.extend(request, params));
2380
+ const currency = this.safeValue(currencyRows, 0);
2381
+ const rows = this.safeList(currencyRows, 1);
2382
+ //
2383
+ // {
2384
+ // "rows":[],
2385
+ // "meta":{
2386
+ // "total":0,
2387
+ // "records_per_page":25,
2388
+ // "current_page":1
2389
+ // },
2390
+ // "success":true
2391
+ // }
2392
+ //
2393
+ return this.parseTransactions(rows, currency, since, limit, params);
2394
+ }
2395
+ async getWithdrawNonce(params = {}) {
2396
+ const response = await this.v1PrivateGetWithdrawNonce(params);
2397
+ //
2398
+ // {
2399
+ // "success": true,
2400
+ // "timestamp": 1702989203989,
2401
+ // "data": {
2402
+ // "withdraw_nonce": 1
2403
+ // }
2404
+ // }
2405
+ //
2406
+ const data = this.safeDict(response, 'data', {});
2407
+ return this.safeNumber(data, 'withdraw_nonce');
2408
+ }
2409
+ hashMessage(message) {
2410
+ return '0x' + this.hash(message, keccak, 'hex');
2411
+ }
2412
+ signHash(hash, privateKey) {
2413
+ const signature = ecdsa(hash.slice(-64), privateKey.slice(-64), secp256k1, undefined);
2414
+ const r = signature['r'];
2415
+ const s = signature['s'];
2416
+ const v = this.intToBase16(this.sum(27, signature['v']));
2417
+ return '0x' + r.padStart(64, '0') + s.padStart(64, '0') + v;
2418
+ }
2419
+ signMessage(message, privateKey) {
2420
+ return this.signHash(this.hashMessage(message), privateKey.slice(-64));
2421
+ }
2422
+ /**
2423
+ * @method
2424
+ * @name modetrade#withdraw
2425
+ * @description make a withdrawal
2426
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/create-withdraw-request
2427
+ * @param {string} code unified currency code
2428
+ * @param {float} amount the amount to withdraw
2429
+ * @param {string} address the address to withdraw to
2430
+ * @param {string} tag
2431
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
2432
+ * @returns {object} a [transaction structure]{@link https://docs.ccxt.com/#/?id=transaction-structure}
2433
+ */
2434
+ async withdraw(code, amount, address, tag = undefined, params = {}) {
2435
+ await this.loadMarkets();
2436
+ this.checkAddress(address);
2437
+ if (code !== undefined) {
2438
+ code = code.toUpperCase();
2439
+ if (code !== 'USDC') {
2440
+ throw new NotSupported(this.id + ' withdraw() only support USDC');
2441
+ }
2442
+ }
2443
+ const currency = this.currency(code);
2444
+ const verifyingContractAddress = this.safeString(this.options, 'verifyingContractAddress');
2445
+ const chainId = this.safeString(params, 'chainId');
2446
+ const currencyNetworks = this.safeDict(currency, 'networks', {});
2447
+ const coinNetwork = this.safeDict(currencyNetworks, chainId, {});
2448
+ const coinNetworkId = this.safeNumber(coinNetwork, 'id');
2449
+ if (coinNetworkId === undefined) {
2450
+ throw new BadRequest(this.id + ' withdraw() require chainId parameter');
2451
+ }
2452
+ const withdrawNonce = await this.getWithdrawNonce(params);
2453
+ const nonce = this.nonce();
2454
+ const domain = {
2455
+ 'chainId': chainId,
2456
+ 'name': 'Orderly',
2457
+ 'verifyingContract': verifyingContractAddress,
2458
+ 'version': '1',
2459
+ };
2460
+ const messageTypes = {
2461
+ 'Withdraw': [
2462
+ { 'name': 'brokerId', 'type': 'string' },
2463
+ { 'name': 'chainId', 'type': 'uint256' },
2464
+ { 'name': 'receiver', 'type': 'address' },
2465
+ { 'name': 'token', 'type': 'string' },
2466
+ { 'name': 'amount', 'type': 'uint256' },
2467
+ { 'name': 'withdrawNonce', 'type': 'uint64' },
2468
+ { 'name': 'timestamp', 'type': 'uint64' },
2469
+ ],
2470
+ };
2471
+ const withdrawRequest = {
2472
+ 'brokerId': this.safeString(this.options, 'keyBrokerId', 'mode'),
2473
+ 'chainId': this.parseToInt(chainId),
2474
+ 'receiver': address,
2475
+ 'token': code,
2476
+ 'amount': amount.toString(),
2477
+ 'withdrawNonce': withdrawNonce,
2478
+ 'timestamp': nonce,
2479
+ };
2480
+ const msg = this.ethEncodeStructuredData(domain, messageTypes, withdrawRequest);
2481
+ const signature = this.signMessage(msg, this.privateKey);
2482
+ const request = {
2483
+ 'signature': signature,
2484
+ 'userAddress': address,
2485
+ 'verifyingContract': verifyingContractAddress,
2486
+ 'message': withdrawRequest,
2487
+ };
2488
+ params = this.omit(params, 'chainId');
2489
+ const response = await this.v1PrivatePostWithdrawRequest(this.extend(request, params));
2490
+ //
2491
+ // {
2492
+ // "success": true,
2493
+ // "timestamp": 1702989203989,
2494
+ // "data": {
2495
+ // "withdraw_id": 123
2496
+ // }
2497
+ // }
2498
+ //
2499
+ const data = this.safeDict(response, 'data', {});
2500
+ return this.parseTransaction(data, currency);
2501
+ }
2502
+ parseLeverage(leverage, market = undefined) {
2503
+ const leverageValue = this.safeInteger(leverage, 'max_leverage');
2504
+ return {
2505
+ 'info': leverage,
2506
+ 'symbol': market['symbol'],
2507
+ 'marginMode': undefined,
2508
+ 'longLeverage': leverageValue,
2509
+ 'shortLeverage': leverageValue,
2510
+ };
2511
+ }
2512
+ /**
2513
+ * @method
2514
+ * @name modetrade#fetchLeverage
2515
+ * @description fetch the set leverage for a market
2516
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/get-account-information
2517
+ * @param {string} symbol unified market symbol
2518
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
2519
+ * @returns {object} a [leverage structure]{@link https://docs.ccxt.com/#/?id=leverage-structure}
2520
+ */
2521
+ async fetchLeverage(symbol, params = {}) {
2522
+ await this.loadMarkets();
2523
+ const market = this.market(symbol);
2524
+ const response = await this.v1PrivateGetClientInfo(params);
2525
+ //
2526
+ // {
2527
+ // "success": true,
2528
+ // "timestamp": 1702989203989,
2529
+ // "data": {
2530
+ // "account_id": "<string>",
2531
+ // "email": "test@test.com",
2532
+ // "account_mode": "FUTURES",
2533
+ // "max_leverage": 20,
2534
+ // "taker_fee_rate": 123,
2535
+ // "maker_fee_rate": 123,
2536
+ // "futures_taker_fee_rate": 123,
2537
+ // "futures_maker_fee_rate": 123,
2538
+ // "maintenance_cancel_orders": true,
2539
+ // "imr_factor": {
2540
+ // "PERP_BTC_USDC": 123,
2541
+ // "PERP_ETH_USDC": 123,
2542
+ // "PERP_NEAR_USDC": 123
2543
+ // },
2544
+ // "max_notional": {
2545
+ // "PERP_BTC_USDC": 123,
2546
+ // "PERP_ETH_USDC": 123,
2547
+ // "PERP_NEAR_USDC": 123
2548
+ // }
2549
+ // }
2550
+ // }
2551
+ //
2552
+ const data = this.safeDict(response, 'data', {});
2553
+ return this.parseLeverage(data, market);
2554
+ }
2555
+ /**
2556
+ * @method
2557
+ * @name modetrade#setLeverage
2558
+ * @description set the level of leverage for a market
2559
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/update-leverage-setting
2560
+ * @param {int} [leverage] the rate of leverage
2561
+ * @param {string} [symbol] unified market symbol
2562
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
2563
+ * @returns {object} response from the exchange
2564
+ */
2565
+ async setLeverage(leverage, symbol = undefined, params = {}) {
2566
+ await this.loadMarkets();
2567
+ const isMinLeverage = leverage < 1;
2568
+ const isMaxLeverage = leverage > 50;
2569
+ if (isMinLeverage || isMaxLeverage) {
2570
+ throw new BadRequest(this.id + ' leverage should be between 1 and 50');
2571
+ }
2572
+ const request = {
2573
+ 'leverage': leverage,
2574
+ };
2575
+ return await this.v1PrivatePostClientLeverage(this.extend(request, params));
2576
+ }
2577
+ parsePosition(position, market = undefined) {
2578
+ //
2579
+ // {
2580
+ // "IMR_withdraw_orders": 0.1,
2581
+ // "MMR_with_orders": 0.05,
2582
+ // "average_open_price": 27908.14386047,
2583
+ // "cost_position": -139329.358492,
2584
+ // "est_liq_price": 117335.92899428,
2585
+ // "fee_24_h": 123,
2586
+ // "imr": 0.1,
2587
+ // "last_sum_unitary_funding": 70.38,
2588
+ // "mark_price": 27794.9,
2589
+ // "mmr": 0.05,
2590
+ // "pending_long_qty": 123,
2591
+ // "pending_short_qty": 123,
2592
+ // "pnl_24_h": 123,
2593
+ // "position_qty": -5,
2594
+ // "settle_price": 27865.8716984,
2595
+ // "symbol": "PERP_BTC_USDC",
2596
+ // "timestamp": 1685429350571,
2597
+ // "unsettled_pnl": 354.858492
2598
+ // }
2599
+ //
2600
+ const contract = this.safeString(position, 'symbol');
2601
+ market = this.safeMarket(contract, market);
2602
+ let size = this.safeString(position, 'position_qty');
2603
+ let side = undefined;
2604
+ if (Precise.stringGt(size, '0')) {
2605
+ side = 'long';
2606
+ }
2607
+ else {
2608
+ side = 'short';
2609
+ }
2610
+ const contractSize = this.safeString(market, 'contractSize');
2611
+ const markPrice = this.safeString(position, 'mark_price');
2612
+ const timestamp = this.safeInteger(position, 'timestamp');
2613
+ const entryPrice = this.safeString(position, 'average_open_price');
2614
+ const unrealisedPnl = this.safeString(position, 'unsettled_pnl');
2615
+ size = Precise.stringAbs(size);
2616
+ const notional = Precise.stringMul(size, markPrice);
2617
+ return this.safePosition({
2618
+ 'info': position,
2619
+ 'id': undefined,
2620
+ 'symbol': this.safeString(market, 'symbol'),
2621
+ 'timestamp': timestamp,
2622
+ 'datetime': this.iso8601(timestamp),
2623
+ 'lastUpdateTimestamp': undefined,
2624
+ 'initialMargin': undefined,
2625
+ 'initialMarginPercentage': undefined,
2626
+ 'maintenanceMargin': undefined,
2627
+ 'maintenanceMarginPercentage': undefined,
2628
+ 'entryPrice': this.parseNumber(entryPrice),
2629
+ 'notional': this.parseNumber(notional),
2630
+ 'leverage': undefined,
2631
+ 'unrealizedPnl': this.parseNumber(unrealisedPnl),
2632
+ 'contracts': this.parseNumber(size),
2633
+ 'contractSize': this.parseNumber(contractSize),
2634
+ 'marginRatio': undefined,
2635
+ 'liquidationPrice': this.safeNumber(position, 'est_liq_price'),
2636
+ 'markPrice': this.parseNumber(markPrice),
2637
+ 'lastPrice': undefined,
2638
+ 'collateral': undefined,
2639
+ 'marginMode': 'cross',
2640
+ 'marginType': undefined,
2641
+ 'side': side,
2642
+ 'percentage': undefined,
2643
+ 'hedged': undefined,
2644
+ 'stopLossPrice': undefined,
2645
+ 'takeProfitPrice': undefined,
2646
+ });
2647
+ }
2648
+ /**
2649
+ * @method
2650
+ * @name modetrade#fetchPosition
2651
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/get-one-position-info
2652
+ * @description fetch data on an open position
2653
+ * @param {string} symbol unified market symbol of the market the position is held in
2654
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
2655
+ * @returns {object} a [position structure]{@link https://docs.ccxt.com/#/?id=position-structure}
2656
+ */
2657
+ async fetchPosition(symbol, params = {}) {
2658
+ await this.loadMarkets();
2659
+ const market = this.market(symbol);
2660
+ const request = {
2661
+ 'symbol': market['id'],
2662
+ };
2663
+ const response = await this.v1PrivateGetPositionSymbol(this.extend(request, params));
2664
+ //
2665
+ // {
2666
+ // "success": true,
2667
+ // "timestamp": 1702989203989,
2668
+ // "data": {
2669
+ // "IMR_withdraw_orders": 0.1,
2670
+ // "MMR_with_orders": 0.05,
2671
+ // "average_open_price": 27908.14386047,
2672
+ // "cost_position": -139329.358492,
2673
+ // "est_liq_price": 117335.92899428,
2674
+ // "fee_24_h": 123,
2675
+ // "imr": 0.1,
2676
+ // "last_sum_unitary_funding": 70.38,
2677
+ // "mark_price": 27794.9,
2678
+ // "mmr": 0.05,
2679
+ // "pending_long_qty": 123,
2680
+ // "pending_short_qty": 123,
2681
+ // "pnl_24_h": 123,
2682
+ // "position_qty": -5,
2683
+ // "settle_price": 27865.8716984,
2684
+ // "symbol": "PERP_BTC_USDC",
2685
+ // "timestamp": 1685429350571,
2686
+ // "unsettled_pnl": 354.858492
2687
+ // }
2688
+ // }
2689
+ //
2690
+ const data = this.safeDict(response, 'data');
2691
+ return this.parsePosition(data, market);
2692
+ }
2693
+ /**
2694
+ * @method
2695
+ * @name modetrade#fetchPositions
2696
+ * @description fetch all open positions
2697
+ * @see https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/get-all-positions-info
2698
+ * @param {string[]} [symbols] list of unified market symbols
2699
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
2700
+ * @returns {object[]} a list of [position structure]{@link https://docs.ccxt.com/#/?id=position-structure}
2701
+ */
2702
+ async fetchPositions(symbols = undefined, params = {}) {
2703
+ await this.loadMarkets();
2704
+ const response = await this.v1PrivateGetPositions(params);
2705
+ //
2706
+ // {
2707
+ // "success": true,
2708
+ // "timestamp": 1702989203989,
2709
+ // "data": {
2710
+ // "current_margin_ratio_with_orders": 1.2385,
2711
+ // "free_collateral": 450315.09115,
2712
+ // "initial_margin_ratio": 0.1,
2713
+ // "initial_margin_ratio_with_orders": 0.1,
2714
+ // "maintenance_margin_ratio": 0.05,
2715
+ // "maintenance_margin_ratio_with_orders": 0.05,
2716
+ // "margin_ratio": 1.2385,
2717
+ // "open_margin_ratio": 1.2102,
2718
+ // "total_collateral_value": 489865.71329,
2719
+ // "total_pnl_24_h": 123,
2720
+ // "rows": [{
2721
+ // "IMR_withdraw_orders": 0.1,
2722
+ // "MMR_with_orders": 0.05,
2723
+ // "average_open_price": 27908.14386047,
2724
+ // "cost_position": -139329.358492,
2725
+ // "est_liq_price": 117335.92899428,
2726
+ // "fee_24_h": 123,
2727
+ // "imr": 0.1,
2728
+ // "last_sum_unitary_funding": 70.38,
2729
+ // "mark_price": 27794.9,
2730
+ // "mmr": 0.05,
2731
+ // "pending_long_qty": 123,
2732
+ // "pending_short_qty": 123,
2733
+ // "pnl_24_h": 123,
2734
+ // "position_qty": -5,
2735
+ // "settle_price": 27865.8716984,
2736
+ // "symbol": "PERP_BTC_USDC",
2737
+ // "timestamp": 1685429350571,
2738
+ // "unsettled_pnl": 354.858492
2739
+ // }]
2740
+ // }
2741
+ // }
2742
+ //
2743
+ const result = this.safeDict(response, 'data', {});
2744
+ const positions = this.safeList(result, 'rows', []);
2745
+ return this.parsePositions(positions, symbols);
2746
+ }
2747
+ nonce() {
2748
+ return this.milliseconds();
2749
+ }
2750
+ sign(path, section = 'public', method = 'GET', params = {}, headers = undefined, body = undefined) {
2751
+ const version = section[0];
2752
+ const access = section[1];
2753
+ const pathWithParams = this.implodeParams(path, params);
2754
+ let url = this.implodeHostname(this.urls['api'][access]);
2755
+ url += '/' + version + '/';
2756
+ params = this.omit(params, this.extractParams(path));
2757
+ params = this.keysort(params);
2758
+ if (access === 'public') {
2759
+ url += pathWithParams;
2760
+ if (Object.keys(params).length) {
2761
+ url += '?' + this.urlencode(params);
2762
+ }
2763
+ }
2764
+ else {
2765
+ this.checkRequiredCredentials();
2766
+ const isPostOrPut = method === 'POST' || method === 'PUT';
2767
+ const isOrder = path === 'algo/order' || path === 'order' || path === 'batch-order';
2768
+ if (isPostOrPut && isOrder) {
2769
+ const isSandboxMode = this.safeBool(this.options, 'sandboxMode', false);
2770
+ if (!isSandboxMode) {
2771
+ const brokerId = this.safeString(this.options, 'brokerId', 'CCXT');
2772
+ if (path === 'batch-order') {
2773
+ const ordersList = this.safeList(params, 'orders', []);
2774
+ for (let i = 0; i < ordersList.length; i++) {
2775
+ params['orders'][i]['order_tag'] = brokerId;
2776
+ }
2777
+ }
2778
+ else {
2779
+ params['order_tag'] = brokerId;
2780
+ }
2781
+ }
2782
+ params = this.keysort(params);
2783
+ }
2784
+ let auth = '';
2785
+ const ts = this.nonce().toString();
2786
+ url += pathWithParams;
2787
+ let apiKey = this.apiKey;
2788
+ if (apiKey.indexOf('ed25519:') < 0) {
2789
+ apiKey = 'ed25519:' + apiKey;
2790
+ }
2791
+ headers = {
2792
+ 'orderly-account-id': this.accountId,
2793
+ 'orderly-key': apiKey,
2794
+ 'orderly-timestamp': ts,
2795
+ };
2796
+ auth = ts + method + '/' + version + '/' + pathWithParams;
2797
+ if (method === 'POST' || method === 'PUT') {
2798
+ body = this.json(params);
2799
+ auth += body;
2800
+ headers['content-type'] = 'application/json';
2801
+ }
2802
+ else {
2803
+ if (Object.keys(params).length) {
2804
+ url += '?' + this.urlencode(params);
2805
+ auth += '?' + this.rawencode(params);
2806
+ }
2807
+ headers['content-type'] = 'application/x-www-form-urlencoded';
2808
+ if (method === 'DELETE') {
2809
+ body = '';
2810
+ }
2811
+ }
2812
+ let secret = this.secret;
2813
+ if (secret.indexOf('ed25519:') >= 0) {
2814
+ const parts = secret.split('ed25519:');
2815
+ secret = parts[1];
2816
+ }
2817
+ const signature = eddsa(this.encode(auth), this.base58ToBinary(secret), ed25519);
2818
+ headers['orderly-signature'] = this.urlencodeBase64(this.base64ToBinary(signature));
2819
+ }
2820
+ return { 'url': url, 'method': method, 'body': body, 'headers': headers };
2821
+ }
2822
+ handleErrors(httpCode, reason, url, method, headers, body, response, requestHeaders, requestBody) {
2823
+ if (!response) {
2824
+ return undefined; // fallback to default error handler
2825
+ }
2826
+ //
2827
+ // 400 Bad Request {"success":false,"code":-1012,"message":"Amount is required for buy market orders when margin disabled."}
2828
+ // {"code":"-1011","message":"The system is under maintenance.","success":false}
2829
+ //
2830
+ const success = this.safeBool(response, 'success');
2831
+ const errorCode = this.safeString(response, 'code');
2832
+ if (!success) {
2833
+ const feedback = this.id + ' ' + this.json(response);
2834
+ this.throwBroadlyMatchedException(this.exceptions['broad'], body, feedback);
2835
+ this.throwExactlyMatchedException(this.exceptions['exact'], errorCode, feedback);
2836
+ throw new ExchangeError(feedback);
2837
+ }
2838
+ return undefined;
2839
+ }
2840
+ }