@yuants/vendor-okx 0.16.9 → 0.17.0

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