@yuants/vendor-okx 0.23.24 → 0.23.26

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 (139) hide show
  1. package/dist/account.js +16 -138
  2. package/dist/account.js.map +1 -1
  3. package/dist/accountInfos/earning.js +15 -0
  4. package/dist/accountInfos/earning.js.map +1 -0
  5. package/dist/accountInfos/funding.js +46 -0
  6. package/dist/accountInfos/funding.js.map +1 -0
  7. package/dist/accountInfos/index.js +7 -0
  8. package/dist/accountInfos/index.js.map +1 -0
  9. package/dist/accountInfos/loan.js +46 -0
  10. package/dist/accountInfos/loan.js.map +1 -0
  11. package/dist/accountInfos/strategy.js +56 -0
  12. package/dist/accountInfos/strategy.js.map +1 -0
  13. package/dist/accountInfos/trading.js +84 -0
  14. package/dist/accountInfos/trading.js.map +1 -0
  15. package/dist/accountInfos/types.js +2 -0
  16. package/dist/accountInfos/types.js.map +1 -0
  17. package/dist/api.js +444 -402
  18. package/dist/api.js.map +1 -1
  19. package/dist/index.js +5 -4
  20. package/dist/index.js.map +1 -1
  21. package/dist/loan-account.js +4 -45
  22. package/dist/loan-account.js.map +1 -1
  23. package/dist/market-order.js +1 -1
  24. package/dist/market-order.js.map +1 -1
  25. package/dist/order-actions.js +39 -0
  26. package/dist/order-actions.js.map +1 -0
  27. package/dist/order.js +3 -3
  28. package/dist/order.js.map +1 -1
  29. package/dist/orders/cancelOrder.js +13 -0
  30. package/dist/orders/cancelOrder.js.map +1 -0
  31. package/dist/orders/modifyOrder.js +58 -0
  32. package/dist/orders/modifyOrder.js.map +1 -0
  33. package/dist/orders/submitOrder.js +107 -0
  34. package/dist/orders/submitOrder.js.map +1 -0
  35. package/dist/public-api.js.map +1 -1
  36. package/dist/quote.js.map +1 -1
  37. package/dist/services.js +3 -2
  38. package/dist/services.js.map +1 -1
  39. package/dist/strategy-account.js +8 -61
  40. package/dist/strategy-account.js.map +1 -1
  41. package/dist/trade.js +6 -5
  42. package/dist/trade.js.map +1 -1
  43. package/dist/transfer.js +279 -0
  44. package/dist/transfer.js.map +1 -0
  45. package/dist/websocket.js.map +1 -1
  46. package/lib/account.d.ts +4 -2
  47. package/lib/account.d.ts.map +1 -1
  48. package/lib/account.js +15 -138
  49. package/lib/account.js.map +1 -1
  50. package/lib/accountInfos/earning.d.ts +4 -0
  51. package/lib/accountInfos/earning.d.ts.map +1 -0
  52. package/lib/accountInfos/earning.js +19 -0
  53. package/lib/accountInfos/earning.js.map +1 -0
  54. package/lib/accountInfos/funding.d.ts +4 -0
  55. package/lib/accountInfos/funding.d.ts.map +1 -0
  56. package/lib/accountInfos/funding.js +50 -0
  57. package/lib/accountInfos/funding.js.map +1 -0
  58. package/lib/accountInfos/index.d.ts +8 -0
  59. package/lib/accountInfos/index.d.ts.map +1 -0
  60. package/lib/accountInfos/index.js +16 -0
  61. package/lib/accountInfos/index.js.map +1 -0
  62. package/lib/accountInfos/loan.d.ts +4 -0
  63. package/lib/accountInfos/loan.d.ts.map +1 -0
  64. package/lib/accountInfos/loan.js +50 -0
  65. package/lib/accountInfos/loan.js.map +1 -0
  66. package/lib/accountInfos/strategy.d.ts +4 -0
  67. package/lib/accountInfos/strategy.d.ts.map +1 -0
  68. package/lib/accountInfos/strategy.js +60 -0
  69. package/lib/accountInfos/strategy.js.map +1 -0
  70. package/lib/accountInfos/trading.d.ts +5 -0
  71. package/lib/accountInfos/trading.d.ts.map +1 -0
  72. package/lib/accountInfos/trading.js +88 -0
  73. package/lib/accountInfos/trading.js.map +1 -0
  74. package/lib/accountInfos/types.d.ts +6 -0
  75. package/lib/accountInfos/types.d.ts.map +1 -0
  76. package/lib/accountInfos/types.js +3 -0
  77. package/lib/accountInfos/types.js.map +1 -0
  78. package/lib/api.d.ts +1252 -1264
  79. package/lib/api.d.ts.map +1 -1
  80. package/lib/api.js +477 -404
  81. package/lib/api.js.map +1 -1
  82. package/lib/index.d.ts +5 -4
  83. package/lib/index.d.ts.map +1 -1
  84. package/lib/index.js +5 -4
  85. package/lib/index.js.map +1 -1
  86. package/lib/loan-account.js +3 -44
  87. package/lib/loan-account.js.map +1 -1
  88. package/lib/market-order.d.ts.map +1 -1
  89. package/lib/market-order.js +1 -1
  90. package/lib/market-order.js.map +1 -1
  91. package/lib/order-actions.d.ts +2 -0
  92. package/lib/order-actions.d.ts.map +1 -0
  93. package/lib/order-actions.js +41 -0
  94. package/lib/order-actions.js.map +1 -0
  95. package/lib/order.js +2 -2
  96. package/lib/order.js.map +1 -1
  97. package/lib/orders/cancelOrder.d.ts +4 -0
  98. package/lib/orders/cancelOrder.d.ts.map +1 -0
  99. package/lib/orders/cancelOrder.js +17 -0
  100. package/lib/orders/cancelOrder.js.map +1 -0
  101. package/lib/orders/modifyOrder.d.ts +4 -0
  102. package/lib/orders/modifyOrder.d.ts.map +1 -0
  103. package/lib/orders/modifyOrder.js +62 -0
  104. package/lib/orders/modifyOrder.js.map +1 -0
  105. package/lib/orders/submitOrder.d.ts +4 -0
  106. package/lib/orders/submitOrder.d.ts.map +1 -0
  107. package/lib/orders/submitOrder.js +111 -0
  108. package/lib/orders/submitOrder.js.map +1 -0
  109. package/lib/public-api.js.map +1 -1
  110. package/lib/quote.d.ts +1 -1
  111. package/lib/quote.d.ts.map +1 -1
  112. package/lib/quote.js.map +1 -1
  113. package/lib/services.js +2 -1
  114. package/lib/services.js.map +1 -1
  115. package/lib/strategy-account.d.ts +2 -2
  116. package/lib/strategy-account.d.ts.map +1 -1
  117. package/lib/strategy-account.js +7 -60
  118. package/lib/strategy-account.js.map +1 -1
  119. package/lib/trade.js +6 -5
  120. package/lib/trade.js.map +1 -1
  121. package/lib/transfer.d.ts +2 -0
  122. package/lib/transfer.d.ts.map +1 -0
  123. package/lib/transfer.js +281 -0
  124. package/lib/transfer.js.map +1 -0
  125. package/lib/websocket.js.map +1 -1
  126. package/package.json +13 -12
  127. package/temp/package-deps.json +40 -29
  128. package/dist/legacy_index.js +0 -563
  129. package/dist/legacy_index.js.map +0 -1
  130. package/dist/logger.js +0 -91
  131. package/dist/logger.js.map +0 -1
  132. package/lib/legacy_index.d.ts +0 -2
  133. package/lib/legacy_index.d.ts.map +0 -1
  134. package/lib/legacy_index.js +0 -565
  135. package/lib/legacy_index.js.map +0 -1
  136. package/lib/logger.d.ts +0 -21
  137. package/lib/logger.d.ts.map +0 -1
  138. package/lib/logger.js +0 -98
  139. package/lib/logger.js.map +0 -1
@@ -1,563 +0,0 @@
1
- import { Terminal } from '@yuants/protocol';
2
- import { addAccountTransferAddress } from '@yuants/transfer';
3
- import { decodePath, encodePath, formatTime, roundToStep } from '@yuants/utils';
4
- import { defer, filter, firstValueFrom, from, map, mergeMap, repeat, retry, shareReplay } from 'rxjs';
5
- import { accountConfig$, tradingAccountId$ } from './account';
6
- import { client } from './api';
7
- import { productService } from './product';
8
- import { getFundingRate, getInterestRateLoanQuota } from './public-api';
9
- import { spotMarketTickers$ } from './quote';
10
- const terminal = Terminal.fromNodeEnv();
11
- console.info(formatTime(Date.now()), 'Terminal', terminal.terminalInfo.terminal_id);
12
- const resOfAssetCurrencies = defer(() => client.getAssetCurrencies()).pipe(repeat({ delay: 3600000 }), retry({ delay: 10000 }), shareReplay(1));
13
- resOfAssetCurrencies.subscribe(); // make it hot
14
- const memoizeMap = (fn) => {
15
- const cache = {};
16
- return ((...params) => { var _a; var _b; return ((_a = cache[_b = encodePath(params)]) !== null && _a !== void 0 ? _a : (cache[_b] = fn(...params))); });
17
- };
18
- const fundingRate$ = memoizeMap((product_id) => defer(() => getFundingRate({ instId: decodePath(product_id)[1] })).pipe(mergeMap((x) => x.data), repeat({ delay: 5000 }), retry({ delay: 5000 }), shareReplay(1)));
19
- const interestRateLoanQuota$ = defer(() => getInterestRateLoanQuota()).pipe(repeat({ delay: 60000 }), retry({ delay: 60000 }), shareReplay(1));
20
- const interestRateByCurrency$ = memoizeMap((currency) => interestRateLoanQuota$.pipe(mergeMap((x) => from(x.data || []).pipe(mergeMap((x) => x.basic), filter((x) => x.ccy === currency), map((x) => +x.rate))), shareReplay(1)));
21
- // provideTicks(terminal, 'OKX', (product_id) => {
22
- // const [instType, instId] = decodePath(product_id);
23
- // if (instType === 'SWAP') {
24
- // return defer(async () => {
25
- // const products = await firstValueFrom(usdtSwapProducts$);
26
- // const theProduct = products.find((x) => x.product_id === product_id);
27
- // if (!theProduct) throw `No Found ProductID ${product_id}`;
28
- // const theTicker$ = swapMarketTickers$.pipe(
29
- // map((x) => x[instId]),
30
- // shareReplay(1),
31
- // );
32
- // return [of(theProduct), theTicker$, fundingRate$(product_id), swapOpenInterest$] as const;
33
- // }).pipe(
34
- // catchError(() => EMPTY),
35
- // mergeMap((x) =>
36
- // combineLatest(x).pipe(
37
- // map(([theProduct, ticker, fundingRate, swapOpenInterest]): ITick => {
38
- // return {
39
- // datasource_id: 'OKX',
40
- // product_id,
41
- // updated_at: Date.now(),
42
- // settlement_scheduled_at: +fundingRate.fundingTime,
43
- // price: +ticker.last,
44
- // ask: +ticker.askPx,
45
- // bid: +ticker.bidPx,
46
- // volume: +ticker.lastSz,
47
- // interest_rate_for_long: -+fundingRate.fundingRate,
48
- // interest_rate_for_short: +fundingRate.fundingRate,
49
- // open_interest: swapOpenInterest.get(instId),
50
- // };
51
- // }),
52
- // ),
53
- // ),
54
- // );
55
- // }
56
- // if (instType === 'MARGIN') {
57
- // return defer(async () => {
58
- // const products = await firstValueFrom(marginProducts$);
59
- // const theProduct = products.find((x) => x.product_id === product_id);
60
- // if (!theProduct) throw `No Found ProductID ${product_id}`;
61
- // const theTicker$ = spotMarketTickers$.pipe(
62
- // map((x) => x[instId]),
63
- // shareReplay(1),
64
- // );
65
- // return [
66
- // of(theProduct),
67
- // theTicker$,
68
- // interestRateByCurrency$(theProduct.base_currency!),
69
- // interestRateByCurrency$(theProduct.quote_currency!),
70
- // ] as const;
71
- // }).pipe(
72
- // catchError(() => EMPTY),
73
- // mergeMap((x) =>
74
- // combineLatest(x).pipe(
75
- // map(
76
- // ([theProduct, ticker, interestRateForBase, interestRateForQuote]): ITick => ({
77
- // datasource_id: 'OKX',
78
- // product_id,
79
- // updated_at: Date.now(),
80
- // price: +ticker.last,
81
- // volume: +ticker.lastSz,
82
- // // 在下一个整点扣除利息 See 如何计算利息 https://www.okx.com/zh-hans/help/how-to-calculate-borrowing-interest
83
- // settlement_scheduled_at: new Date().setMinutes(0, 0, 0) + 3600_000,
84
- // interest_rate_for_long: -interestRateForQuote / 24,
85
- // interest_rate_for_short: -interestRateForBase / 24,
86
- // }),
87
- // ),
88
- // ),
89
- // ),
90
- // );
91
- // }
92
- // return EMPTY;
93
- // });
94
- defer(async () => {
95
- const account_config = await firstValueFrom(accountConfig$);
96
- console.info(formatTime(Date.now()), 'AccountConfig', JSON.stringify(account_config));
97
- const { mainUid, uid } = account_config.data[0];
98
- const isMainAccount = mainUid === uid;
99
- const TRADING_ACCOUNT_ID = `okx/${uid}/trading`;
100
- const FUNDING_ACCOUNT_ID = `okx/${uid}/funding/USDT`;
101
- const EARNING_ACCOUNT_ID = `okx/${uid}/earning/USDT`;
102
- // BLOCK_CHAIN: only available for main account
103
- if (isMainAccount) {
104
- const depositAddressRes = await client.getAssetDepositAddress({ ccy: 'USDT' });
105
- console.info(formatTime(Date.now()), 'DepositAddress', JSON.stringify(depositAddressRes.data));
106
- const addresses = depositAddressRes.data.filter((v) => v.chain === 'USDT-TRC20' && v.to === '6');
107
- for (const address of addresses) {
108
- addAccountTransferAddress({
109
- terminal,
110
- account_id: FUNDING_ACCOUNT_ID,
111
- network_id: 'TRC20',
112
- currency: 'USDT',
113
- address: address.addr,
114
- onApply: {
115
- INIT: async (order) => {
116
- var _a, _b;
117
- if (!order.current_amount ||
118
- order.current_amount < 3 // 最小提币额度
119
- ) {
120
- return { state: 'ERROR', message: 'Amount too small' };
121
- }
122
- const res = await firstValueFrom(resOfAssetCurrencies);
123
- const theRes = (_a = res.data) === null || _a === void 0 ? void 0 : _a.find((x) => x.ccy === 'USDT' && x.chain === 'USDT-TRC20');
124
- const _fee = theRes === null || theRes === void 0 ? void 0 : theRes.minFee;
125
- if (!_fee)
126
- return { state: 'ERROR', message: 'Currency Info not found, cannot get fee' };
127
- const fee = +_fee;
128
- const amt = Math.floor(order.current_amount - fee);
129
- const transferResult = await client.postAssetWithdrawal({
130
- amt: `${amt}`,
131
- ccy: 'USDT',
132
- chain: 'USDT-TRC20',
133
- fee: `${fee}`,
134
- dest: '4',
135
- toAddr: order.current_rx_address,
136
- });
137
- if (transferResult.code !== '0') {
138
- return { state: 'INIT', message: transferResult.msg };
139
- }
140
- const wdId = (_b = transferResult.data[0]) === null || _b === void 0 ? void 0 : _b.wdId;
141
- return { state: 'AWAIT_TX_ID', context: wdId };
142
- },
143
- AWAIT_TX_ID: async (transferOrder) => {
144
- var _a, _b;
145
- const wdId = transferOrder.current_tx_context;
146
- const withdrawalHistory = await client.getAssetWithdrawalHistory({ wdId });
147
- const txId = (_b = (_a = withdrawalHistory.data) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.txId;
148
- if (!txId) {
149
- return { state: 'AWAIT_TX_ID', context: wdId };
150
- }
151
- return { state: 'COMPLETE', transaction_id: txId };
152
- },
153
- },
154
- onEval: async (transferOrder) => {
155
- const checkResult = await client.getAssetDepositHistory({
156
- ccy: 'USDT',
157
- txId: transferOrder.current_transaction_id,
158
- type: '4',
159
- });
160
- if (checkResult.code !== '0') {
161
- return {
162
- state: 'INIT',
163
- };
164
- }
165
- if (checkResult.data[0].state !== '2') {
166
- return { state: 'PENDING' };
167
- }
168
- const received_amount = +checkResult.data[0].amt;
169
- return { state: 'COMPLETE', received_amount };
170
- },
171
- });
172
- }
173
- }
174
- // Funding-Trading
175
- {
176
- const FUNDING_TRADING_NETWORK_ID = `OKX/${uid}/Funding-Trading`;
177
- addAccountTransferAddress({
178
- terminal,
179
- account_id: FUNDING_ACCOUNT_ID,
180
- network_id: FUNDING_TRADING_NETWORK_ID,
181
- currency: 'USDT',
182
- address: 'funding',
183
- onApply: {
184
- INIT: async (order) => {
185
- const transferResult = await client.postAssetTransfer({
186
- type: '0',
187
- ccy: 'USDT',
188
- amt: `${order.current_amount}`,
189
- from: '6',
190
- to: '18',
191
- });
192
- if (transferResult.code !== '0') {
193
- return { state: 'INIT', message: transferResult.msg };
194
- }
195
- const transaction_id = transferResult.data[0].transId;
196
- return { state: 'COMPLETE', transaction_id };
197
- },
198
- },
199
- onEval: async (transferOrder) => {
200
- return { state: 'COMPLETE', received_amount: transferOrder.current_amount };
201
- },
202
- });
203
- addAccountTransferAddress({
204
- terminal,
205
- account_id: TRADING_ACCOUNT_ID,
206
- network_id: FUNDING_TRADING_NETWORK_ID,
207
- currency: 'USDT',
208
- address: 'trading',
209
- onApply: {
210
- INIT: async (order) => {
211
- const transferResult = await client.postAssetTransfer({
212
- type: '0',
213
- ccy: order.currency,
214
- amt: `${order.current_amount}`,
215
- from: '18',
216
- to: '6',
217
- });
218
- if (transferResult.code !== '0') {
219
- return { state: 'INIT', message: transferResult.msg };
220
- }
221
- const transaction_id = transferResult.data[0].transId;
222
- return { state: 'COMPLETE', transaction_id };
223
- },
224
- },
225
- onEval: async (transferOrder) => {
226
- return { state: 'COMPLETE', received_amount: transferOrder.current_amount };
227
- },
228
- });
229
- }
230
- // Funding-Earning
231
- {
232
- const FUNDING_EARNING_NETWORK_ID = `OKX/${uid}/Funding-Earning`;
233
- addAccountTransferAddress({
234
- terminal,
235
- account_id: FUNDING_ACCOUNT_ID,
236
- network_id: FUNDING_EARNING_NETWORK_ID,
237
- currency: 'USDT',
238
- address: 'funding',
239
- onApply: {
240
- INIT: async (order) => {
241
- const transferResult = await client.postFinanceSavingsPurchaseRedempt({
242
- ccy: 'USDT',
243
- amt: `${order.current_amount}`,
244
- side: 'purchase',
245
- rate: '0.01',
246
- });
247
- if (transferResult.code !== '0') {
248
- return { state: 'INIT', message: transferResult.msg };
249
- }
250
- return { state: 'COMPLETE', transaction_id: 'ok' };
251
- },
252
- },
253
- onEval: async (transferOrder) => {
254
- return { state: 'COMPLETE', received_amount: transferOrder.current_amount };
255
- },
256
- });
257
- addAccountTransferAddress({
258
- terminal,
259
- account_id: EARNING_ACCOUNT_ID,
260
- network_id: FUNDING_EARNING_NETWORK_ID,
261
- currency: 'USDT',
262
- address: 'earning',
263
- onApply: {
264
- INIT: async (order) => {
265
- const transferResult = await client.postFinanceSavingsPurchaseRedempt({
266
- ccy: 'USDT',
267
- amt: `${order.current_amount}`,
268
- side: 'redempt',
269
- rate: '0.01',
270
- });
271
- if (transferResult.code !== '0') {
272
- return { state: 'INIT', message: transferResult.msg };
273
- }
274
- return { state: 'COMPLETE', transaction_id: 'ok' };
275
- },
276
- },
277
- onEval: async (transferOrder) => {
278
- return { state: 'COMPLETE', received_amount: transferOrder.current_amount };
279
- },
280
- });
281
- }
282
- // SubAccount
283
- {
284
- const getSubAccountNetworkId = (subUid) => `OKX/${mainUid}/SubAccount/${subUid}`;
285
- if (isMainAccount) {
286
- const subAcctsRes = await client.getSubAccountList();
287
- for (const item of subAcctsRes.data || []) {
288
- addAccountTransferAddress({
289
- terminal,
290
- account_id: FUNDING_ACCOUNT_ID,
291
- network_id: getSubAccountNetworkId(item.uid),
292
- currency: 'USDT',
293
- address: 'main',
294
- onApply: {
295
- INIT: async (order) => {
296
- const transferResult = await client.postAssetTransfer({
297
- type: '1',
298
- ccy: 'USDT',
299
- amt: `${order.current_amount}`,
300
- from: '6',
301
- to: '6',
302
- subAcct: item.subAcct,
303
- });
304
- if (transferResult.code !== '0') {
305
- return { state: 'INIT', message: transferResult.msg };
306
- }
307
- const transaction_id = transferResult.data[0].transId;
308
- return { state: 'COMPLETE', transaction_id };
309
- },
310
- },
311
- onEval: async (order) => {
312
- // ISSUE: OKX API Issue: transId is incorrect or transId does not match with ‘ type’
313
- // const checkResult = await client.getAssetTransferState({ transId: order.current_transaction_id });
314
- // const received_amount = checkResult?.data?.[0]?.amt;
315
- // if (!received_amount) {
316
- // return { state: 'INIT', message: checkResult.msg };
317
- // }
318
- // return { state: 'COMPLETE', received_amount: +received_amount };
319
- return { state: 'COMPLETE', received_amount: order.current_amount };
320
- },
321
- });
322
- }
323
- }
324
- // SubAccount API
325
- else {
326
- addAccountTransferAddress({
327
- terminal,
328
- account_id: FUNDING_ACCOUNT_ID,
329
- network_id: getSubAccountNetworkId(uid),
330
- currency: 'USDT',
331
- address: 'sub',
332
- onApply: {
333
- INIT: async (order) => {
334
- const transferResult = await client.postAssetTransfer({
335
- type: '3',
336
- ccy: 'USDT',
337
- amt: `${order.current_amount}`,
338
- from: '6',
339
- to: '6',
340
- });
341
- if (transferResult.code !== '0') {
342
- return { state: 'INIT', message: transferResult.msg };
343
- }
344
- const transaction_id = transferResult.data[0].transId;
345
- return { state: 'COMPLETE', transaction_id };
346
- },
347
- },
348
- onEval: async (order) => {
349
- // ISSUE: OKX API Issue: transId is incorrect or transId does not match with ‘ type’
350
- // const checkResult = await client.getAssetTransferState({ transId: order.current_transaction_id });
351
- // const received_amount = checkResult?.data?.[0]?.amt;
352
- // if (!received_amount) {
353
- // return { state: 'INIT', message: checkResult.msg };
354
- // }
355
- // return { state: 'COMPLETE', received_amount: +received_amount };
356
- return { state: 'COMPLETE', received_amount: order.current_amount };
357
- },
358
- });
359
- }
360
- }
361
- }).subscribe();
362
- defer(async () => {
363
- const tradingAccountId = await firstValueFrom(tradingAccountId$);
364
- terminal.server.provideService('SubmitOrder', {
365
- required: ['account_id'],
366
- properties: {
367
- account_id: { const: tradingAccountId },
368
- },
369
- }, async (msg) => {
370
- var _a, _b, _c;
371
- console.info(formatTime(Date.now()), 'SubmitOrder', JSON.stringify(msg));
372
- const order = msg.req;
373
- const [instType, instId] = decodePath(order.product_id);
374
- const mapOrderDirectionToSide = (direction) => {
375
- switch (direction) {
376
- case 'OPEN_LONG':
377
- case 'CLOSE_SHORT':
378
- return 'buy';
379
- case 'OPEN_SHORT':
380
- case 'CLOSE_LONG':
381
- return 'sell';
382
- }
383
- throw new Error(`Unknown direction: ${direction}`);
384
- };
385
- const mapOrderDirectionToPosSide = (direction) => {
386
- switch (direction) {
387
- case 'OPEN_LONG':
388
- case 'CLOSE_LONG':
389
- return 'long';
390
- case 'CLOSE_SHORT':
391
- case 'OPEN_SHORT':
392
- return 'short';
393
- }
394
- throw new Error(`Unknown direction: ${direction}`);
395
- };
396
- const mapOrderTypeToOrdType = (order_type) => {
397
- switch (order_type) {
398
- case 'LIMIT':
399
- return 'limit';
400
- case 'MARKET':
401
- return 'market';
402
- case 'MAKER':
403
- return 'post_only';
404
- }
405
- throw new Error(`Unknown order type: ${order_type}`);
406
- };
407
- // 交易数量,表示要购买或者出售的数量。
408
- // 当币币/币币杠杆以限价买入和卖出时,指交易货币数量。
409
- // 当币币杠杆以市价买入时,指计价货币的数量。
410
- // 当币币杠杆以市价卖出时,指交易货币的数量。
411
- // 对于币币市价单,单位由 tgtCcy 决定
412
- // 当交割、永续、期权买入和卖出时,指合约张数。
413
- const mapOrderVolumeToSz = async (order) => {
414
- if (instType === 'SWAP') {
415
- return order.volume;
416
- }
417
- if (instType === 'MARGIN') {
418
- if (order.order_type === 'LIMIT') {
419
- return order.volume;
420
- }
421
- if (order.order_type === 'MAKER') {
422
- return order.volume;
423
- }
424
- if (order.order_type === 'MARKET') {
425
- if (order.order_direction === 'OPEN_SHORT' || order.order_direction === 'CLOSE_LONG') {
426
- return order.volume;
427
- }
428
- //
429
- const price = await firstValueFrom(spotMarketTickers$.pipe(map((x) => mapOrderDirectionToPosSide(order.order_direction) === 'long'
430
- ? +x[instId].askPx
431
- : +x[instId].bidPx)));
432
- if (!price) {
433
- throw new Error(`invalid tick: ${price}`);
434
- }
435
- console.info(formatTime(Date.now()), 'SubmitOrder', 'price', price);
436
- const theProduct = await firstValueFrom(productService.mapProductIdToProduct$.pipe(map((x) => x.get(order.product_id))));
437
- if (!theProduct) {
438
- throw new Error(`Unknown product: ${order.position_id}`);
439
- }
440
- return roundToStep(order.volume * price, theProduct.volume_step);
441
- }
442
- return 0;
443
- }
444
- if (instType === 'SPOT') {
445
- return order.volume;
446
- }
447
- throw new Error(`Unknown instType: ${instType}`);
448
- };
449
- const params = {
450
- instId,
451
- tdMode: instType === 'SPOT' ? 'cash' : 'cross',
452
- side: mapOrderDirectionToSide(order.order_direction),
453
- posSide: instType === 'MARGIN' || instType === 'SPOT'
454
- ? 'net'
455
- : mapOrderDirectionToPosSide(order.order_direction),
456
- ordType: mapOrderTypeToOrdType(order.order_type),
457
- sz: (await mapOrderVolumeToSz(order)).toString(),
458
- tgtCcy: instType === 'SPOT' && order.order_type === 'MARKET' ? 'base_ccy' : undefined,
459
- reduceOnly: instType === 'MARGIN' && ['CLOSE_LONG', 'CLOSE_SHORT'].includes((_a = order.order_direction) !== null && _a !== void 0 ? _a : '')
460
- ? 'true'
461
- : undefined,
462
- px: order.order_type === 'LIMIT' || order.order_type === 'MAKER' ? order.price.toString() : undefined,
463
- ccy: instType === 'MARGIN' ? 'USDT' : undefined,
464
- };
465
- console.info(formatTime(Date.now()), 'SubmitOrder', 'params', JSON.stringify(params));
466
- const res = await client.postTradeOrder(params);
467
- if (res.code === '0' && ((_c = (_b = res.data) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.ordId)) {
468
- return {
469
- res: {
470
- code: 0,
471
- message: 'OK',
472
- data: {
473
- order_id: res.data[0].ordId,
474
- },
475
- },
476
- };
477
- }
478
- return { res: { code: +res.code, message: res.msg } };
479
- });
480
- terminal.server.provideService('ModifyOrder', {
481
- required: ['account_id'],
482
- properties: {
483
- account_id: { const: tradingAccountId },
484
- },
485
- }, async (msg) => {
486
- console.info(formatTime(Date.now()), 'ModifyOrder', JSON.stringify(msg));
487
- const order = msg.req;
488
- const [instType, instId] = decodePath(order.product_id);
489
- const params = {
490
- instId,
491
- ordId: order.order_id, // 使用现有订单ID
492
- };
493
- // 如果需要修改价格
494
- if (order.price !== undefined) {
495
- params.newPx = order.price.toString();
496
- }
497
- // 如果需要修改数量
498
- if (order.volume !== undefined) {
499
- // 处理数量修改,类似于 SubmitOrder 中的逻辑
500
- if (instType === 'SWAP') {
501
- params.newSz = order.volume.toString();
502
- }
503
- else if (instType === 'SPOT') {
504
- params.newSz = order.volume.toString();
505
- }
506
- else if (instType === 'MARGIN') {
507
- if (order.order_type === 'LIMIT') {
508
- params.newSz = order.volume.toString();
509
- }
510
- if (order.order_type === 'MAKER') {
511
- params.newSz = order.volume.toString();
512
- }
513
- if (order.order_type === 'MARKET') {
514
- // 对于市价单,可能需要根据当前价格计算新的数量
515
- const price = await firstValueFrom(spotMarketTickers$.pipe(map((x) => order.order_direction === 'OPEN_LONG' || order.order_direction === 'CLOSE_SHORT'
516
- ? +x[instId].askPx
517
- : +x[instId].bidPx)));
518
- if (!price) {
519
- throw new Error(`invalid tick: ${price}`);
520
- }
521
- console.info(formatTime(Date.now()), 'ModifyOrder', 'price', price);
522
- const theProduct = await firstValueFrom(productService.mapProductIdToProduct$.pipe(map((x) => x.get(order.product_id))));
523
- if (!theProduct) {
524
- throw new Error(`Unknown product: ${order.position_id}`);
525
- }
526
- params.newSz = roundToStep(order.volume * price, theProduct.volume_step).toString();
527
- }
528
- }
529
- else {
530
- throw new Error(`Unknown instType: ${instType}`);
531
- }
532
- }
533
- console.info(formatTime(Date.now()), 'ModifyOrder', 'params', JSON.stringify(params));
534
- const res = await client.postTradeAmendOrder(params);
535
- if (res.code !== '0') {
536
- return {
537
- res: {
538
- code: +res.code,
539
- message: res.msg,
540
- },
541
- };
542
- }
543
- return { res: { code: 0, message: 'OK' } };
544
- });
545
- terminal.server.provideService('CancelOrder', {
546
- required: ['account_id'],
547
- properties: {
548
- account_id: { const: tradingAccountId },
549
- },
550
- }, (msg) => defer(async () => {
551
- const order = msg.req;
552
- const [instType, instId] = decodePath(order.product_id);
553
- const res = await client.postTradeCancelOrder({
554
- instId,
555
- ordId: order.order_id,
556
- });
557
- if (res.code !== '0') {
558
- return { res: { code: +res.code, message: res.msg } };
559
- }
560
- return { res: { code: 0, message: 'OK' } };
561
- }));
562
- }).subscribe();
563
- //# source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rder } from '@yuants/data-order';\nimport { Terminal } from '@yuants/protocol';\nimport { addAccountTransferAddress } from '@yuants/transfer';\nimport { decodePath, encodePath, formatTime, roundToStep } from '@yuants/utils';\nimport { defer, filter, firstValueFrom, from, map, mergeMap, repeat, retry, shareReplay } from 'rxjs';\nimport { accountConfig$, tradingAccountId$ } from './account';\nimport { client } from './api';\nimport { productService } from './product';\nimport { getFundingRate, getInterestRateLoanQuota } from './public-api';\nimport { spotMarketTickers$ } from './quote';\n\nconst terminal = Terminal.fromNodeEnv();\n\nconsole.info(formatTime(Date.now()), 'Terminal', terminal.terminalInfo.terminal_id);\n\nconst resOfAssetCurrencies = defer(() => client.getAssetCurrencies()).pipe(\n repeat({ delay: 3600_000 }),\n retry({ delay: 10_000 }),\n shareReplay(1),\n);\n\nresOfAssetCurrencies.subscribe(); // make it hot\n\nconst memoizeMap = <T extends (...params: any[]) => any>(fn: T): T => {\n const cache: Record<string, any> = {};\n return ((...params: any[]) => (cache[encodePath(params)] ??= fn(...params))) as T;\n};\n\nconst fundingRate$ = memoizeMap((product_id: string) =>\n defer(() => getFundingRate({ instId: decodePath(product_id)[1] })).pipe(\n mergeMap((x) => x.data),\n repeat({ delay: 5000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n ),\n);\n\nconst interestRateLoanQuota$ = defer(() => getInterestRateLoanQuota()).pipe(\n repeat({ delay: 60_000 }),\n retry({ delay: 60_000 }),\n shareReplay(1),\n);\n\nconst interestRateByCurrency$ = memoizeMap((currency: string) =>\n interestRateLoanQuota$.pipe(\n mergeMap((x) =>\n from(x.data || []).pipe(\n mergeMap((x) => x.basic),\n filter((x) => x.ccy === currency),\n map((x) => +x.rate),\n ),\n ),\n shareReplay(1),\n ),\n);\n\n// provideTicks(terminal, 'OKX', (product_id) => {\n// const [instType, instId] = decodePath(product_id);\n// if (instType === 'SWAP') {\n// return defer(async () => {\n// const products = await firstValueFrom(usdtSwapProducts$);\n// const theProduct = products.find((x) => x.product_id === product_id);\n// if (!theProduct) throw `No Found ProductID ${product_id}`;\n// const theTicker$ = swapMarketTickers$.pipe(\n// map((x) => x[instId]),\n// shareReplay(1),\n// );\n// return [of(theProduct), theTicker$, fundingRate$(product_id), swapOpenInterest$] as const;\n// }).pipe(\n// catchError(() => EMPTY),\n// mergeMap((x) =>\n// combineLatest(x).pipe(\n// map(([theProduct, ticker, fundingRate, swapOpenInterest]): ITick => {\n// return {\n// datasource_id: 'OKX',\n// product_id,\n// updated_at: Date.now(),\n// settlement_scheduled_at: +fundingRate.fundingTime,\n// price: +ticker.last,\n// ask: +ticker.askPx,\n// bid: +ticker.bidPx,\n// volume: +ticker.lastSz,\n// interest_rate_for_long: -+fundingRate.fundingRate,\n// interest_rate_for_short: +fundingRate.fundingRate,\n// open_interest: swapOpenInterest.get(instId),\n// };\n// }),\n// ),\n// ),\n// );\n// }\n// if (instType === 'MARGIN') {\n// return defer(async () => {\n// const products = await firstValueFrom(marginProducts$);\n// const theProduct = products.find((x) => x.product_id === product_id);\n// if (!theProduct) throw `No Found ProductID ${product_id}`;\n// const theTicker$ = spotMarketTickers$.pipe(\n// map((x) => x[instId]),\n// shareReplay(1),\n// );\n// return [\n// of(theProduct),\n// theTicker$,\n// interestRateByCurrency$(theProduct.base_currency!),\n// interestRateByCurrency$(theProduct.quote_currency!),\n// ] as const;\n// }).pipe(\n// catchError(() => EMPTY),\n// mergeMap((x) =>\n// combineLatest(x).pipe(\n// map(\n// ([theProduct, ticker, interestRateForBase, interestRateForQuote]): ITick => ({\n// datasource_id: 'OKX',\n// product_id,\n// updated_at: Date.now(),\n// price: +ticker.last,\n// volume: +ticker.lastSz,\n// // 在下一个整点扣除利息 See 如何计算利息 https://www.okx.com/zh-hans/help/how-to-calculate-borrowing-interest\n// settlement_scheduled_at: new Date().setMinutes(0, 0, 0) + 3600_000,\n// interest_rate_for_long: -interestRateForQuote / 24,\n// interest_rate_for_short: -interestRateForBase / 24,\n// }),\n// ),\n// ),\n// ),\n// );\n// }\n// return EMPTY;\n// });\n\ndefer(async () => {\n const account_config = await firstValueFrom(accountConfig$);\n console.info(formatTime(Date.now()), 'AccountConfig', JSON.stringify(account_config));\n const { mainUid, uid } = account_config.data[0];\n const isMainAccount = mainUid === uid;\n\n const TRADING_ACCOUNT_ID = `okx/${uid}/trading`;\n const FUNDING_ACCOUNT_ID = `okx/${uid}/funding/USDT`;\n const EARNING_ACCOUNT_ID = `okx/${uid}/earning/USDT`;\n\n // BLOCK_CHAIN: only available for main account\n if (isMainAccount) {\n const depositAddressRes = await client.getAssetDepositAddress({ ccy: 'USDT' });\n console.info(formatTime(Date.now()), 'DepositAddress', JSON.stringify(depositAddressRes.data));\n const addresses = depositAddressRes.data.filter((v) => v.chain === 'USDT-TRC20' && v.to === '6');\n for (const address of addresses) {\n addAccountTransferAddress({\n terminal,\n account_id: FUNDING_ACCOUNT_ID,\n network_id: 'TRC20',\n currency: 'USDT',\n address: address.addr,\n onApply: {\n INIT: async (order) => {\n if (\n !order.current_amount ||\n order.current_amount < 3 // 最小提币额度\n ) {\n return { state: 'ERROR', message: 'Amount too small' };\n }\n const res = await firstValueFrom(resOfAssetCurrencies);\n const theRes = res.data?.find((x) => x.ccy === 'USDT' && x.chain === 'USDT-TRC20');\n const _fee = theRes?.minFee;\n if (!_fee) return { state: 'ERROR', message: 'Currency Info not found, cannot get fee' };\n const fee = +_fee;\n const amt = Math.floor(order.current_amount - fee);\n const transferResult = await client.postAssetWithdrawal({\n amt: `${amt}`,\n ccy: 'USDT',\n chain: 'USDT-TRC20',\n fee: `${fee}`,\n dest: '4', // 链上提币\n toAddr: order.current_rx_address!,\n });\n if (transferResult.code !== '0') {\n return { state: 'INIT', message: transferResult.msg };\n }\n const wdId = transferResult.data[0]?.wdId;\n return { state: 'AWAIT_TX_ID', context: wdId };\n },\n AWAIT_TX_ID: async (transferOrder) => {\n const wdId = transferOrder.current_tx_context;\n const withdrawalHistory = await client.getAssetWithdrawalHistory({ wdId });\n const txId = withdrawalHistory.data?.[0]?.txId;\n if (!txId) {\n return { state: 'AWAIT_TX_ID', context: wdId };\n }\n return { state: 'COMPLETE', transaction_id: txId };\n },\n },\n onEval: async (transferOrder) => {\n const checkResult = await client.getAssetDepositHistory({\n ccy: 'USDT',\n txId: transferOrder.current_transaction_id,\n type: '4',\n });\n\n if (checkResult.code !== '0') {\n return {\n state: 'INIT',\n };\n }\n\n if (checkResult.data[0].state !== '2') {\n return { state: 'PENDING' };\n }\n const received_amount = +checkResult.data[0].amt;\n return { state: 'COMPLETE', received_amount };\n },\n });\n }\n }\n\n // Funding-Trading\n {\n const FUNDING_TRADING_NETWORK_ID = `OKX/${uid}/Funding-Trading`;\n addAccountTransferAddress({\n terminal,\n account_id: FUNDING_ACCOUNT_ID,\n network_id: FUNDING_TRADING_NETWORK_ID,\n currency: 'USDT',\n address: 'funding',\n onApply: {\n INIT: async (order) => {\n const transferResult = await client.postAssetTransfer({\n type: '0',\n ccy: 'USDT',\n amt: `${order.current_amount}`,\n from: '6',\n to: '18',\n });\n if (transferResult.code !== '0') {\n return { state: 'INIT', message: transferResult.msg };\n }\n const transaction_id = transferResult.data[0].transId;\n return { state: 'COMPLETE', transaction_id };\n },\n },\n onEval: async (transferOrder) => {\n return { state: 'COMPLETE', received_amount: transferOrder.current_amount };\n },\n });\n addAccountTransferAddress({\n terminal,\n account_id: TRADING_ACCOUNT_ID,\n network_id: FUNDING_TRADING_NETWORK_ID,\n currency: 'USDT',\n address: 'trading',\n onApply: {\n INIT: async (order) => {\n const transferResult = await client.postAssetTransfer({\n type: '0',\n ccy: order.currency,\n amt: `${order.current_amount}`,\n from: '18',\n to: '6',\n });\n if (transferResult.code !== '0') {\n return { state: 'INIT', message: transferResult.msg };\n }\n const transaction_id = transferResult.data[0].transId;\n return { state: 'COMPLETE', transaction_id };\n },\n },\n onEval: async (transferOrder) => {\n return { state: 'COMPLETE', received_amount: transferOrder.current_amount };\n },\n });\n }\n // Funding-Earning\n {\n const FUNDING_EARNING_NETWORK_ID = `OKX/${uid}/Funding-Earning`;\n addAccountTransferAddress({\n terminal,\n account_id: FUNDING_ACCOUNT_ID,\n network_id: FUNDING_EARNING_NETWORK_ID,\n currency: 'USDT',\n address: 'funding',\n onApply: {\n INIT: async (order) => {\n const transferResult = await client.postFinanceSavingsPurchaseRedempt({\n ccy: 'USDT',\n amt: `${order.current_amount}`,\n side: 'purchase',\n rate: '0.01',\n });\n if (transferResult.code !== '0') {\n return { state: 'INIT', message: transferResult.msg };\n }\n return { state: 'COMPLETE', transaction_id: 'ok' };\n },\n },\n onEval: async (transferOrder) => {\n return { state: 'COMPLETE', received_amount: transferOrder.current_amount };\n },\n });\n addAccountTransferAddress({\n terminal,\n account_id: EARNING_ACCOUNT_ID,\n network_id: FUNDING_EARNING_NETWORK_ID,\n currency: 'USDT',\n address: 'earning',\n onApply: {\n INIT: async (order) => {\n const transferResult = await client.postFinanceSavingsPurchaseRedempt({\n ccy: 'USDT',\n amt: `${order.current_amount}`,\n side: 'redempt',\n rate: '0.01',\n });\n if (transferResult.code !== '0') {\n return { state: 'INIT', message: transferResult.msg };\n }\n return { state: 'COMPLETE', transaction_id: 'ok' };\n },\n },\n onEval: async (transferOrder) => {\n return { state: 'COMPLETE', received_amount: transferOrder.current_amount };\n },\n });\n }\n\n // SubAccount\n {\n const getSubAccountNetworkId = (subUid: string) => `OKX/${mainUid}/SubAccount/${subUid}`;\n if (isMainAccount) {\n const subAcctsRes = await client.getSubAccountList();\n for (const item of subAcctsRes.data || []) {\n addAccountTransferAddress({\n terminal,\n account_id: FUNDING_ACCOUNT_ID,\n network_id: getSubAccountNetworkId(item.uid),\n currency: 'USDT',\n address: 'main',\n onApply: {\n INIT: async (order) => {\n const transferResult = await client.postAssetTransfer({\n type: '1',\n ccy: 'USDT',\n amt: `${order.current_amount}`,\n from: '6',\n to: '6',\n subAcct: item.subAcct,\n });\n if (transferResult.code !== '0') {\n return { state: 'INIT', message: transferResult.msg };\n }\n const transaction_id = transferResult.data[0].transId;\n return { state: 'COMPLETE', transaction_id };\n },\n },\n onEval: async (order) => {\n // ISSUE: OKX API Issue: transId is incorrect or transId does not match with ‘ type’\n // const checkResult = await client.getAssetTransferState({ transId: order.current_transaction_id });\n // const received_amount = checkResult?.data?.[0]?.amt;\n // if (!received_amount) {\n // return { state: 'INIT', message: checkResult.msg };\n // }\n // return { state: 'COMPLETE', received_amount: +received_amount };\n\n return { state: 'COMPLETE', received_amount: order.current_amount };\n },\n });\n }\n }\n // SubAccount API\n else {\n addAccountTransferAddress({\n terminal,\n account_id: FUNDING_ACCOUNT_ID,\n network_id: getSubAccountNetworkId(uid),\n currency: 'USDT',\n address: 'sub',\n onApply: {\n INIT: async (order) => {\n const transferResult = await client.postAssetTransfer({\n type: '3',\n ccy: 'USDT',\n amt: `${order.current_amount}`,\n from: '6',\n to: '6',\n });\n if (transferResult.code !== '0') {\n return { state: 'INIT', message: transferResult.msg };\n }\n const transaction_id = transferResult.data[0].transId;\n return { state: 'COMPLETE', transaction_id };\n },\n },\n onEval: async (order) => {\n // ISSUE: OKX API Issue: transId is incorrect or transId does not match with ‘ type’\n // const checkResult = await client.getAssetTransferState({ transId: order.current_transaction_id });\n // const received_amount = checkResult?.data?.[0]?.amt;\n // if (!received_amount) {\n // return { state: 'INIT', message: checkResult.msg };\n // }\n // return { state: 'COMPLETE', received_amount: +received_amount };\n return { state: 'COMPLETE', received_amount: order.current_amount };\n },\n });\n }\n }\n}).subscribe();\n\ndefer(async () => {\n const tradingAccountId = await firstValueFrom(tradingAccountId$);\n terminal.server.provideService<IOrder, { order_id?: string }>(\n 'SubmitOrder',\n {\n required: ['account_id'],\n properties: {\n account_id: { const: tradingAccountId },\n },\n },\n async (msg) => {\n console.info(formatTime(Date.now()), 'SubmitOrder', JSON.stringify(msg));\n const order = msg.req;\n const [instType, instId] = decodePath(order.product_id);\n\n const mapOrderDirectionToSide = (direction?: string) => {\n switch (direction) {\n case 'OPEN_LONG':\n case 'CLOSE_SHORT':\n return 'buy';\n case 'OPEN_SHORT':\n case 'CLOSE_LONG':\n return 'sell';\n }\n throw new Error(`Unknown direction: ${direction}`);\n };\n const mapOrderDirectionToPosSide = (direction?: string) => {\n switch (direction) {\n case 'OPEN_LONG':\n case 'CLOSE_LONG':\n return 'long';\n case 'CLOSE_SHORT':\n case 'OPEN_SHORT':\n return 'short';\n }\n throw new Error(`Unknown direction: ${direction}`);\n };\n const mapOrderTypeToOrdType = (order_type?: string) => {\n switch (order_type) {\n case 'LIMIT':\n return 'limit';\n case 'MARKET':\n return 'market';\n case 'MAKER':\n return 'post_only';\n }\n throw new Error(`Unknown order type: ${order_type}`);\n };\n\n // 交易数量,表示要购买或者出售的数量。\n // 当币币/币币杠杆以限价买入和卖出时,指交易货币数量。\n // 当币币杠杆以市价买入时,指计价货币的数量。\n // 当币币杠杆以市价卖出时,指交易货币的数量。\n // 对于币币市价单,单位由 tgtCcy 决定\n // 当交割、永续、期权买入和卖出时,指合约张数。\n const mapOrderVolumeToSz = async (order: IOrder) => {\n if (instType === 'SWAP') {\n return order.volume;\n }\n if (instType === 'MARGIN') {\n if (order.order_type === 'LIMIT') {\n return order.volume;\n }\n if (order.order_type === 'MAKER') {\n return order.volume;\n }\n if (order.order_type === 'MARKET') {\n if (order.order_direction === 'OPEN_SHORT' || order.order_direction === 'CLOSE_LONG') {\n return order.volume;\n }\n //\n const price = await firstValueFrom(\n spotMarketTickers$.pipe(\n map((x) =>\n mapOrderDirectionToPosSide(order.order_direction) === 'long'\n ? +x[instId].askPx\n : +x[instId].bidPx,\n ),\n ),\n );\n if (!price) {\n throw new Error(`invalid tick: ${price}`);\n }\n console.info(formatTime(Date.now()), 'SubmitOrder', 'price', price);\n const theProduct = await firstValueFrom(\n productService.mapProductIdToProduct$.pipe(map((x) => x.get(order.product_id))),\n );\n if (!theProduct) {\n throw new Error(`Unknown product: ${order.position_id}`);\n }\n return roundToStep(order.volume * price, theProduct.volume_step!);\n }\n\n return 0;\n }\n\n if (instType === 'SPOT') {\n return order.volume;\n }\n\n throw new Error(`Unknown instType: ${instType}`);\n };\n\n const params = {\n instId,\n tdMode: instType === 'SPOT' ? 'cash' : 'cross',\n side: mapOrderDirectionToSide(order.order_direction),\n posSide:\n instType === 'MARGIN' || instType === 'SPOT'\n ? 'net'\n : mapOrderDirectionToPosSide(order.order_direction),\n ordType: mapOrderTypeToOrdType(order.order_type),\n sz: (await mapOrderVolumeToSz(order)).toString(),\n tgtCcy: instType === 'SPOT' && order.order_type === 'MARKET' ? 'base_ccy' : undefined,\n reduceOnly:\n instType === 'MARGIN' && ['CLOSE_LONG', 'CLOSE_SHORT'].includes(order.order_direction ?? '')\n ? 'true'\n : undefined,\n px:\n order.order_type === 'LIMIT' || order.order_type === 'MAKER' ? order.price!.toString() : undefined,\n ccy: instType === 'MARGIN' ? 'USDT' : undefined,\n };\n console.info(formatTime(Date.now()), 'SubmitOrder', 'params', JSON.stringify(params));\n const res = await client.postTradeOrder(params);\n if (res.code === '0' && res.data?.[0]?.ordId) {\n return {\n res: {\n code: 0,\n message: 'OK',\n data: {\n order_id: res.data[0].ordId,\n },\n },\n };\n }\n return { res: { code: +res.code, message: res.msg } };\n },\n );\n\n terminal.server.provideService<IOrder>(\n 'ModifyOrder',\n {\n required: ['account_id'],\n properties: {\n account_id: { const: tradingAccountId },\n },\n },\n async (msg) => {\n console.info(formatTime(Date.now()), 'ModifyOrder', JSON.stringify(msg));\n const order = msg.req;\n const [instType, instId] = decodePath(order.product_id);\n\n const params: any = {\n instId,\n ordId: order.order_id, // 使用现有订单ID\n };\n\n // 如果需要修改价格\n if (order.price !== undefined) {\n params.newPx = order.price.toString();\n }\n\n // 如果需要修改数量\n if (order.volume !== undefined) {\n // 处理数量修改,类似于 SubmitOrder 中的逻辑\n if (instType === 'SWAP') {\n params.newSz = order.volume.toString();\n } else if (instType === 'SPOT') {\n params.newSz = order.volume.toString();\n } else if (instType === 'MARGIN') {\n if (order.order_type === 'LIMIT') {\n params.newSz = order.volume.toString();\n }\n if (order.order_type === 'MAKER') {\n params.newSz = order.volume.toString();\n }\n if (order.order_type === 'MARKET') {\n // 对于市价单,可能需要根据当前价格计算新的数量\n const price = await firstValueFrom(\n spotMarketTickers$.pipe(\n map((x) =>\n order.order_direction === 'OPEN_LONG' || order.order_direction === 'CLOSE_SHORT'\n ? +x[instId].askPx\n : +x[instId].bidPx,\n ),\n ),\n );\n if (!price) {\n throw new Error(`invalid tick: ${price}`);\n }\n console.info(formatTime(Date.now()), 'ModifyOrder', 'price', price);\n const theProduct = await firstValueFrom(\n productService.mapProductIdToProduct$.pipe(map((x) => x.get(order.product_id))),\n );\n if (!theProduct) {\n throw new Error(`Unknown product: ${order.position_id}`);\n }\n params.newSz = roundToStep(order.volume * price, theProduct.volume_step!).toString();\n }\n } else {\n throw new Error(`Unknown instType: ${instType}`);\n }\n }\n\n console.info(formatTime(Date.now()), 'ModifyOrder', 'params', JSON.stringify(params));\n\n const res = await client.postTradeAmendOrder(params);\n if (res.code !== '0') {\n return {\n res: {\n code: +res.code,\n message: res.msg,\n },\n };\n }\n\n return { res: { code: 0, message: 'OK' } };\n },\n );\n\n terminal.server.provideService<IOrder>(\n 'CancelOrder',\n {\n required: ['account_id'],\n properties: {\n account_id: { const: tradingAccountId },\n },\n },\n (msg) =>\n defer(async () => {\n const order = msg.req;\n const [instType, instId] = decodePath(order.product_id);\n const res = await client.postTradeCancelOrder({\n instId,\n ordId: order.order_id,\n });\n if (res.code !== '0') {\n return { res: { code: +res.code, message: res.msg } };\n }\n return { res: { code: 0, message: 'OK' } };\n }),\n );\n}).subscribe();\n"]}