@yuants/vendor-binance 0.7.8 → 0.8.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 (102) hide show
  1. package/dist/api/client.js +70 -0
  2. package/dist/api/client.js.map +1 -0
  3. package/dist/api/private-api.js +162 -0
  4. package/dist/api/private-api.js.map +1 -0
  5. package/dist/api/public-api.js +52 -0
  6. package/dist/api/public-api.js.map +1 -0
  7. package/dist/index.js +2 -0
  8. package/dist/index.js.map +1 -1
  9. package/dist/interest_rate.js +2 -2
  10. package/dist/interest_rate.js.map +1 -1
  11. package/dist/legacy_index.js +270 -237
  12. package/dist/legacy_index.js.map +1 -1
  13. package/dist/product.js +2 -2
  14. package/dist/product.js.map +1 -1
  15. package/dist/quote.js +4 -4
  16. package/dist/quote.js.map +1 -1
  17. package/dist/services/account-actions-with-credential.js +34 -0
  18. package/dist/services/account-actions-with-credential.js.map +1 -0
  19. package/dist/services/accounts/profile.js +22 -0
  20. package/dist/services/accounts/profile.js.map +1 -0
  21. package/dist/services/accounts/spot.js +44 -0
  22. package/dist/services/accounts/spot.js.map +1 -0
  23. package/dist/services/accounts/unified.js +56 -0
  24. package/dist/services/accounts/unified.js.map +1 -0
  25. package/dist/services/order-actions-with-credential.js +19 -0
  26. package/dist/services/order-actions-with-credential.js.map +1 -0
  27. package/dist/services/orders/cancelOrder.js +39 -0
  28. package/dist/services/orders/cancelOrder.js.map +1 -0
  29. package/dist/services/orders/listOrders.js +49 -0
  30. package/dist/services/orders/listOrders.js.map +1 -0
  31. package/dist/services/orders/order-utils.js +118 -0
  32. package/dist/services/orders/order-utils.js.map +1 -0
  33. package/dist/services/orders/submitOrder.js +70 -0
  34. package/dist/services/orders/submitOrder.js.map +1 -0
  35. package/lib/api/client.d.ts +16 -0
  36. package/lib/api/client.d.ts.map +1 -0
  37. package/lib/api/client.js +80 -0
  38. package/lib/api/client.js.map +1 -0
  39. package/lib/api/private-api.d.ts +477 -0
  40. package/lib/api/private-api.d.ts.map +1 -0
  41. package/lib/api/private-api.js +183 -0
  42. package/lib/api/private-api.js.map +1 -0
  43. package/lib/api/public-api.d.ts +150 -0
  44. package/lib/api/public-api.d.ts.map +1 -0
  45. package/lib/api/public-api.js +61 -0
  46. package/lib/api/public-api.js.map +1 -0
  47. package/lib/index.d.ts +2 -0
  48. package/lib/index.d.ts.map +1 -1
  49. package/lib/index.js +2 -0
  50. package/lib/index.js.map +1 -1
  51. package/lib/interest_rate.js +2 -2
  52. package/lib/interest_rate.js.map +1 -1
  53. package/lib/legacy_index.js +270 -237
  54. package/lib/legacy_index.js.map +1 -1
  55. package/lib/product.js +2 -2
  56. package/lib/product.js.map +1 -1
  57. package/lib/quote.js +4 -4
  58. package/lib/quote.js.map +1 -1
  59. package/lib/services/account-actions-with-credential.d.ts +2 -0
  60. package/lib/services/account-actions-with-credential.d.ts.map +1 -0
  61. package/lib/services/account-actions-with-credential.js +36 -0
  62. package/lib/services/account-actions-with-credential.js.map +1 -0
  63. package/lib/services/accounts/profile.d.ts +7 -0
  64. package/lib/services/accounts/profile.d.ts.map +1 -0
  65. package/lib/services/accounts/profile.js +26 -0
  66. package/lib/services/accounts/profile.js.map +1 -0
  67. package/lib/services/accounts/spot.d.ts +4 -0
  68. package/lib/services/accounts/spot.d.ts.map +1 -0
  69. package/lib/services/accounts/spot.js +48 -0
  70. package/lib/services/accounts/spot.js.map +1 -0
  71. package/lib/services/accounts/unified.d.ts +4 -0
  72. package/lib/services/accounts/unified.d.ts.map +1 -0
  73. package/lib/services/accounts/unified.js +60 -0
  74. package/lib/services/accounts/unified.js.map +1 -0
  75. package/lib/services/order-actions-with-credential.d.ts +2 -0
  76. package/lib/services/order-actions-with-credential.d.ts.map +1 -0
  77. package/lib/services/order-actions-with-credential.js +21 -0
  78. package/lib/services/order-actions-with-credential.js.map +1 -0
  79. package/lib/services/orders/cancelOrder.d.ts +4 -0
  80. package/lib/services/orders/cancelOrder.d.ts.map +1 -0
  81. package/lib/services/orders/cancelOrder.js +43 -0
  82. package/lib/services/orders/cancelOrder.js.map +1 -0
  83. package/lib/services/orders/listOrders.d.ts +4 -0
  84. package/lib/services/orders/listOrders.d.ts.map +1 -0
  85. package/lib/services/orders/listOrders.js +53 -0
  86. package/lib/services/orders/listOrders.js.map +1 -0
  87. package/lib/services/orders/order-utils.d.ts +12 -0
  88. package/lib/services/orders/order-utils.d.ts.map +1 -0
  89. package/lib/services/orders/order-utils.js +131 -0
  90. package/lib/services/orders/order-utils.js.map +1 -0
  91. package/lib/services/orders/submitOrder.d.ts +4 -0
  92. package/lib/services/orders/submitOrder.d.ts.map +1 -0
  93. package/lib/services/orders/submitOrder.js +74 -0
  94. package/lib/services/orders/submitOrder.js.map +1 -0
  95. package/package.json +1 -1
  96. package/temp/package-deps.json +22 -9
  97. package/dist/api.js +0 -248
  98. package/dist/api.js.map +0 -1
  99. package/lib/api.d.ts +0 -576
  100. package/lib/api.d.ts.map +0 -1
  101. package/lib/api.js +0 -256
  102. package/lib/api.js.map +0 -1
@@ -4,7 +4,9 @@ import { Terminal } from '@yuants/protocol';
4
4
  import { addAccountTransferAddress } from '@yuants/transfer';
5
5
  import { decodePath, encodePath, formatTime } from '@yuants/utils';
6
6
  import { createHash } from 'crypto';
7
- import { client, isError } from './api';
7
+ import { getDefaultCredential, isApiError } from './api/client';
8
+ import { deleteUmOrder, getDepositAddress, getDepositHistory, getSpotAccountInfo, getSubAccountList, getUnifiedAccountBalance, getUnifiedUmAccount, getUnifiedUmOpenOrders, getWithdrawHistory, postAssetTransfer, postUmOrder, postWithdraw, } from './api/private-api';
9
+ import { getFutureOpenInterest } from './api/public-api';
8
10
  const terminal = Terminal.fromNodeEnv();
9
11
  const _mapSymbolToOpenInterest = {};
10
12
  const getOpenInterest = async (symbol) => {
@@ -15,7 +17,7 @@ const getOpenInterest = async (symbol) => {
15
17
  return cache.value;
16
18
  }
17
19
  }
18
- const data = await client.getFutureOpenInterest({ symbol });
20
+ const data = await getFutureOpenInterest({ symbol });
19
21
  const value = +data.openInterest || 0;
20
22
  _mapSymbolToOpenInterest[symbol] = { value, updated_at: Date.now() };
21
23
  return value;
@@ -89,6 +91,7 @@ const deriveClientOrderId = (order) => {
89
91
  });
90
92
  return `YUANTS${createHash('sha256').update(payload).digest('hex').slice(0, 24)}`;
91
93
  };
94
+ const isPublicOnly = process.env.PUBLIC_ONLY === 'true';
92
95
  // provideTicks(terminal, 'binance', (product_id) => {
93
96
  // const [instType, symbol] = decodePath(product_id);
94
97
  // if (instType === 'usdt-future') {
@@ -120,263 +123,293 @@ const deriveClientOrderId = (order) => {
120
123
  // }
121
124
  // return EMPTY;
122
125
  // });
123
- (async () => {
124
- const spotAccountInfo = await client.getSpotAccountInfo();
125
- if (isError(spotAccountInfo)) {
126
- throw new Error(spotAccountInfo.msg);
127
- }
128
- const uid = spotAccountInfo.uid;
129
- const SPOT_ACCOUNT_ID = `binance/${uid}/spot/usdt`;
130
- const UNIFIED_ACCOUNT_ID = `binance/${uid}/unified/usdt`;
131
- {
132
- // unified accountInfo
133
- provideAccountInfoService(terminal, UNIFIED_ACCOUNT_ID, async () => {
134
- const [accountResult, umAccountResult] = await Promise.all([
135
- client.getUnifiedAccountBalance(),
136
- client.getUnifiedUmAccount(),
137
- ]);
138
- if (isError(accountResult)) {
139
- throw new Error(accountResult.msg);
140
- }
141
- const usdtAssets = accountResult.find((v) => v.asset === 'USDT');
142
- if (!usdtAssets) {
143
- throw new Error('USDT not found');
144
- }
145
- if (isError(umAccountResult)) {
146
- throw new Error(umAccountResult.msg);
147
- }
148
- const usdtUmAssets = umAccountResult.assets.find((v) => v.asset === 'USDT');
149
- if (!usdtUmAssets) {
150
- throw new Error('um USDT not found');
151
- }
152
- const equity = +usdtAssets.totalWalletBalance + +usdtAssets.umUnrealizedPNL;
153
- const free = equity - +usdtUmAssets.initialMargin;
154
- const positions = umAccountResult.positions
155
- .filter((v) => +v.positionAmt !== 0)
156
- .map((v) => {
126
+ if (isPublicOnly) {
127
+ console.info('PUBLIC_ONLY=1, skip Binance legacy services');
128
+ }
129
+ else {
130
+ const credential = getDefaultCredential();
131
+ (async () => {
132
+ const spotAccountInfo = await getSpotAccountInfo(credential);
133
+ if (isApiError(spotAccountInfo)) {
134
+ throw new Error(spotAccountInfo.msg);
135
+ }
136
+ const uid = spotAccountInfo.uid;
137
+ const SPOT_ACCOUNT_ID = `binance/${uid}/spot/usdt`;
138
+ const UNIFIED_ACCOUNT_ID = `binance/${uid}/unified/usdt`;
139
+ {
140
+ // unified accountInfo
141
+ provideAccountInfoService(terminal, UNIFIED_ACCOUNT_ID, async () => {
142
+ const [accountResult, umAccountResult] = await Promise.all([
143
+ getUnifiedAccountBalance(credential),
144
+ getUnifiedUmAccount(credential),
145
+ ]);
146
+ if (isApiError(accountResult)) {
147
+ throw new Error(accountResult.msg);
148
+ }
149
+ const usdtAssets = accountResult.find((v) => v.asset === 'USDT');
150
+ if (!usdtAssets) {
151
+ throw new Error('USDT not found');
152
+ }
153
+ if (isApiError(umAccountResult)) {
154
+ throw new Error(umAccountResult.msg);
155
+ }
156
+ const usdtUmAssets = umAccountResult.assets.find((v) => v.asset === 'USDT');
157
+ if (!usdtUmAssets) {
158
+ throw new Error('um USDT not found');
159
+ }
160
+ const equity = +usdtAssets.totalWalletBalance + +usdtAssets.umUnrealizedPNL;
161
+ const free = equity - +usdtUmAssets.initialMargin;
162
+ const positions = umAccountResult.positions
163
+ .filter((v) => +v.positionAmt !== 0)
164
+ .map((v) => {
165
+ return {
166
+ position_id: `${v.symbol}/${v.positionSide}`,
167
+ datasource_id: 'BINANCE',
168
+ product_id: encodePath('usdt-future', v.symbol),
169
+ direction: v.positionSide,
170
+ volume: +v.positionAmt,
171
+ free_volume: +v.positionAmt,
172
+ position_price: +v.entryPrice,
173
+ closable_price: +v.entryPrice + +v.unrealizedProfit / +v.positionAmt,
174
+ floating_profit: +v.unrealizedProfit,
175
+ valuation: +v.positionAmt * (+v.entryPrice + +v.unrealizedProfit / +v.positionAmt),
176
+ };
177
+ });
157
178
  return {
158
- position_id: `${v.symbol}/${v.positionSide}`,
159
- datasource_id: 'BINANCE',
160
- product_id: encodePath('usdt-future', v.symbol),
161
- direction: v.positionSide,
162
- volume: +v.positionAmt,
163
- free_volume: +v.positionAmt,
164
- position_price: +v.entryPrice,
165
- closable_price: +v.entryPrice + +v.unrealizedProfit / +v.positionAmt,
166
- floating_profit: +v.unrealizedProfit,
167
- valuation: +v.positionAmt * (+v.entryPrice + +v.unrealizedProfit / +v.positionAmt),
179
+ money: {
180
+ currency: 'USDT',
181
+ equity,
182
+ free,
183
+ },
184
+ positions,
168
185
  };
169
- });
170
- return {
171
- money: {
172
- currency: 'USDT',
173
- equity,
174
- free,
175
- },
176
- positions,
177
- };
178
- }, { auto_refresh_interval: 1000 });
179
- addAccountMarket(terminal, { account_id: UNIFIED_ACCOUNT_ID, market_id: 'BINANCE/UNIFIED' });
180
- }
181
- {
182
- // spot account info
183
- provideAccountInfoService(terminal, SPOT_ACCOUNT_ID, async () => {
184
- const spotAccountResult = await client.getSpotAccountInfo({ omitZeroBalances: true });
185
- if (isError(spotAccountResult)) {
186
- throw new Error(spotAccountResult.msg);
187
- }
188
- const usdtAssets = spotAccountResult.balances.find((v) => v.asset === 'USDT');
189
- return {
190
- money: {
191
- currency: 'USDT',
192
- equity: +((usdtAssets === null || usdtAssets === void 0 ? void 0 : usdtAssets.free) || 0),
193
- free: +((usdtAssets === null || usdtAssets === void 0 ? void 0 : usdtAssets.free) || 0),
194
- },
195
- positions: [],
196
- };
197
- });
198
- addAccountMarket(terminal, { account_id: SPOT_ACCOUNT_ID, market_id: 'BINANCE/SPOT' });
199
- }
200
- // transfer
201
- {
202
- // spot -> unified
203
- const SPOT_UNIFIED_NETWORK_ID = `binance/${uid}/spot/unified`;
204
- addAccountTransferAddress({
205
- terminal,
206
- account_id: SPOT_ACCOUNT_ID,
207
- network_id: SPOT_UNIFIED_NETWORK_ID,
208
- currency: 'USDT',
209
- address: `unified`,
210
- onApply: {
211
- INIT: async (order) => {
212
- const transferResult = await client.postAssetTransfer({
213
- type: 'MAIN_PORTFOLIO_MARGIN',
214
- asset: 'USDT',
215
- amount: order.current_amount,
216
- });
217
- if (isError(transferResult)) {
218
- return { state: 'INIT', message: transferResult.msg };
219
- }
220
- return { state: 'COMPLETE', transaction_id: '' + transferResult.tranId };
221
- },
222
- },
223
- onEval: async (order) => {
224
- return { state: 'COMPLETE', received_amount: order.current_amount };
225
- },
226
- });
227
- // unified -> spot
228
- addAccountTransferAddress({
229
- terminal,
230
- account_id: UNIFIED_ACCOUNT_ID,
231
- network_id: SPOT_UNIFIED_NETWORK_ID,
232
- currency: 'USDT',
233
- address: `spot`,
234
- onApply: {
235
- INIT: async (order) => {
236
- const transferResult = await client.postAssetTransfer({
237
- type: 'PORTFOLIO_MARGIN_MAIN',
238
- asset: 'USDT',
239
- amount: order.current_amount,
240
- });
241
- if (isError(transferResult)) {
242
- return { state: 'INIT', message: transferResult.msg };
186
+ }, { auto_refresh_interval: 1000 });
187
+ addAccountMarket(terminal, { account_id: UNIFIED_ACCOUNT_ID, market_id: 'BINANCE/UNIFIED' });
188
+ }
189
+ {
190
+ // spot account info
191
+ provideAccountInfoService(terminal, SPOT_ACCOUNT_ID, async () => {
192
+ const spotAccountResult = await getSpotAccountInfo(credential, { omitZeroBalances: true });
193
+ if (isApiError(spotAccountResult)) {
194
+ throw new Error(spotAccountResult.msg);
195
+ }
196
+ const usdtAssets = spotAccountResult.balances.find((v) => v.asset === 'USDT');
197
+ const positions = spotAccountResult.balances
198
+ .filter((balance) => balance.asset !== 'USDT')
199
+ .map((balance) => {
200
+ const volume = +balance.free + +balance.locked;
201
+ if (!volume) {
202
+ return undefined;
243
203
  }
244
- return { state: 'COMPLETE', transaction_id: '' + transferResult.tranId };
245
- },
246
- },
247
- onEval: async (order) => {
248
- return { state: 'COMPLETE', received_amount: order.current_amount };
249
- },
250
- });
251
- const subAccountsResult = await client.getSubAccountList();
252
- const isMain = !isError(subAccountsResult);
253
- // main -> sub
254
- // TODO...
255
- // blockchain
256
- if (isMain) {
257
- const depositAddressResult = await client.getDepositAddress({ coin: 'USDT', network: 'TRX' });
204
+ const position = {
205
+ position_id: `spot/${balance.asset}`,
206
+ datasource_id: 'BINANCE',
207
+ product_id: encodePath('spot', `${balance.asset}USDT`),
208
+ direction: 'LONG',
209
+ volume,
210
+ free_volume: +balance.free,
211
+ position_price: 0,
212
+ closable_price: 0,
213
+ floating_profit: 0,
214
+ valuation: 0,
215
+ };
216
+ return position;
217
+ })
218
+ .filter((position) => Boolean(position));
219
+ return {
220
+ money: {
221
+ currency: 'USDT',
222
+ equity: +((usdtAssets === null || usdtAssets === void 0 ? void 0 : usdtAssets.free) || 0) + +((usdtAssets === null || usdtAssets === void 0 ? void 0 : usdtAssets.locked) || 0),
223
+ free: +((usdtAssets === null || usdtAssets === void 0 ? void 0 : usdtAssets.free) || 0),
224
+ },
225
+ positions,
226
+ };
227
+ }, {
228
+ auto_refresh_interval: 5000,
229
+ });
230
+ addAccountMarket(terminal, { account_id: SPOT_ACCOUNT_ID, market_id: 'BINANCE/SPOT' });
231
+ }
232
+ // transfer
233
+ {
234
+ // spot -> unified
235
+ const SPOT_UNIFIED_NETWORK_ID = `binance/${uid}/spot/unified`;
258
236
  addAccountTransferAddress({
259
237
  terminal,
260
238
  account_id: SPOT_ACCOUNT_ID,
261
- network_id: 'TRC20',
239
+ network_id: SPOT_UNIFIED_NETWORK_ID,
262
240
  currency: 'USDT',
263
- address: depositAddressResult.address,
241
+ address: `unified`,
264
242
  onApply: {
265
243
  INIT: async (order) => {
266
- const transferResult = await client.postWithdraw({
267
- coin: 'USDT',
268
- network: 'TRX',
269
- address: order.current_rx_address,
244
+ const transferResult = await postAssetTransfer(credential, {
245
+ type: 'MAIN_PORTFOLIO_MARGIN',
246
+ asset: 'USDT',
270
247
  amount: order.current_amount,
271
248
  });
272
- if (isError(transferResult)) {
273
- return { state: 'ERROR', message: transferResult.msg };
249
+ if (isApiError(transferResult)) {
250
+ return { state: 'INIT', message: transferResult.msg };
274
251
  }
275
- const wdId = transferResult.id;
276
- return { state: 'PENDING', context: wdId };
252
+ return { state: 'COMPLETE', transaction_id: '' + transferResult.tranId };
277
253
  },
278
- PENDING: async (order) => {
279
- const wdId = order.current_tx_context;
280
- const withdrawResult = await client.getWithdrawHistory({ coin: 'USDT' });
281
- const record = withdrawResult === null || withdrawResult === void 0 ? void 0 : withdrawResult.find((v) => v.id === wdId);
282
- const txId = record === null || record === void 0 ? void 0 : record.txId;
283
- if (!txId) {
284
- return { state: 'PENDING', context: wdId };
254
+ },
255
+ onEval: async (order) => {
256
+ return { state: 'COMPLETE', received_amount: order.current_amount };
257
+ },
258
+ });
259
+ // unified -> spot
260
+ addAccountTransferAddress({
261
+ terminal,
262
+ account_id: UNIFIED_ACCOUNT_ID,
263
+ network_id: SPOT_UNIFIED_NETWORK_ID,
264
+ currency: 'USDT',
265
+ address: `spot`,
266
+ onApply: {
267
+ INIT: async (order) => {
268
+ const transferResult = await postAssetTransfer(credential, {
269
+ type: 'PORTFOLIO_MARGIN_MAIN',
270
+ asset: 'USDT',
271
+ amount: order.current_amount,
272
+ });
273
+ if (isApiError(transferResult)) {
274
+ return { state: 'INIT', message: transferResult.msg };
285
275
  }
286
- return { state: 'COMPLETE', transaction_id: txId };
276
+ return { state: 'COMPLETE', transaction_id: '' + transferResult.tranId };
287
277
  },
288
278
  },
289
279
  onEval: async (order) => {
290
- var _a;
291
- const checkResult = await client.getDepositHistory({
292
- coin: 'USDT',
293
- txId: order.current_transaction_id,
294
- });
295
- if (((_a = checkResult === null || checkResult === void 0 ? void 0 : checkResult[0]) === null || _a === void 0 ? void 0 : _a.status) !== 1) {
296
- return { state: 'PENDING' };
297
- }
298
- const received_amount = +checkResult[0].amount;
299
- return { state: 'COMPLETE', received_amount };
280
+ return { state: 'COMPLETE', received_amount: order.current_amount };
300
281
  },
301
282
  });
283
+ const subAccountsResult = await getSubAccountList(credential);
284
+ const isMain = !isApiError(subAccountsResult);
285
+ // main -> sub
286
+ // TODO...
287
+ // blockchain
288
+ if (isMain) {
289
+ const depositAddressResult = await getDepositAddress(credential, { coin: 'USDT', network: 'TRX' });
290
+ addAccountTransferAddress({
291
+ terminal,
292
+ account_id: SPOT_ACCOUNT_ID,
293
+ network_id: 'TRC20',
294
+ currency: 'USDT',
295
+ address: depositAddressResult.address,
296
+ onApply: {
297
+ INIT: async (order) => {
298
+ const transferResult = await postWithdraw(credential, {
299
+ coin: 'USDT',
300
+ network: 'TRX',
301
+ address: order.current_rx_address,
302
+ amount: order.current_amount,
303
+ });
304
+ if (isApiError(transferResult)) {
305
+ return { state: 'ERROR', message: transferResult.msg };
306
+ }
307
+ const wdId = transferResult.id;
308
+ return { state: 'PENDING', context: wdId };
309
+ },
310
+ PENDING: async (order) => {
311
+ const wdId = order.current_tx_context;
312
+ const withdrawResult = await getWithdrawHistory(credential, { coin: 'USDT' });
313
+ const record = withdrawResult === null || withdrawResult === void 0 ? void 0 : withdrawResult.find((v) => v.id === wdId);
314
+ const txId = record === null || record === void 0 ? void 0 : record.txId;
315
+ if (!txId) {
316
+ return { state: 'PENDING', context: wdId };
317
+ }
318
+ return { state: 'COMPLETE', transaction_id: txId };
319
+ },
320
+ },
321
+ onEval: async (order) => {
322
+ var _a;
323
+ const checkResult = await getDepositHistory(credential, {
324
+ coin: 'USDT',
325
+ txId: order.current_transaction_id,
326
+ });
327
+ if (((_a = checkResult === null || checkResult === void 0 ? void 0 : checkResult[0]) === null || _a === void 0 ? void 0 : _a.status) !== 1) {
328
+ return { state: 'PENDING' };
329
+ }
330
+ const received_amount = +checkResult[0].amount;
331
+ return { state: 'COMPLETE', received_amount };
332
+ },
333
+ });
334
+ }
302
335
  }
303
- }
304
- // order related
305
- {
306
- providePendingOrdersService(terminal, UNIFIED_ACCOUNT_ID, async () => {
307
- const openOrders = await client.getUnifiedUmOpenOrders();
308
- return openOrders.map((order) => {
309
- var _a;
310
- const order_direction = (_a = mapBinanceSideToYuantsDirection(order.side, order.positionSide)) !== null && _a !== void 0 ? _a : 'OPEN_LONG';
311
- return {
312
- order_id: `${order.orderId}`,
313
- account_id: UNIFIED_ACCOUNT_ID,
314
- product_id: encodePath('usdt-future', order.symbol),
315
- order_type: mapBinanceOrderTypeToYuants(order.type),
316
- order_direction,
317
- volume: +order.origQty,
318
- traded_volume: +order.executedQty,
319
- price: order.price === undefined ? undefined : +order.price,
320
- submit_at: order.time,
321
- updated_at: new Date(order.updateTime).toISOString(),
322
- order_status: order.status,
323
- };
336
+ // order related
337
+ {
338
+ providePendingOrdersService(terminal, UNIFIED_ACCOUNT_ID, async () => {
339
+ const openOrders = await getUnifiedUmOpenOrders(credential);
340
+ return openOrders.map((order) => {
341
+ var _a;
342
+ const order_direction = (_a = mapBinanceSideToYuantsDirection(order.side, order.positionSide)) !== null && _a !== void 0 ? _a : 'OPEN_LONG';
343
+ return {
344
+ order_id: `${order.orderId}`,
345
+ account_id: UNIFIED_ACCOUNT_ID,
346
+ product_id: encodePath('usdt-future', order.symbol),
347
+ order_type: mapBinanceOrderTypeToYuants(order.type),
348
+ order_direction,
349
+ volume: +order.origQty,
350
+ traded_volume: +order.executedQty,
351
+ price: order.price === undefined ? undefined : +order.price,
352
+ submit_at: order.time,
353
+ updated_at: new Date(order.updateTime).toISOString(),
354
+ order_status: order.status,
355
+ };
356
+ });
357
+ }, { auto_refresh_interval: 1000 });
358
+ terminal.server.provideService('SubmitOrder', {
359
+ required: ['account_id'],
360
+ properties: {
361
+ account_id: { const: UNIFIED_ACCOUNT_ID },
362
+ },
363
+ }, async (msg) => {
364
+ console.info(formatTime(Date.now()), 'SubmitOrder', msg.req);
365
+ const order = msg.req;
366
+ const [instType, symbol] = decodePath(order.product_id);
367
+ if (instType === 'usdt-future') {
368
+ const params = {
369
+ symbol,
370
+ side: mapOrderDirectionToSide(order.order_direction),
371
+ positionSide: mapOrderDirectionToPosSide(order.order_direction),
372
+ type: mapOrderTypeToOrdType(order.order_type),
373
+ timeInForce: order.order_type === 'MAKER' ? 'GTX' : order.order_type === 'LIMIT' ? 'GTC' : undefined,
374
+ quantity: order.volume,
375
+ price: order.price,
376
+ newClientOrderId: deriveClientOrderId(order),
377
+ };
378
+ console.info(formatTime(Date.now()), 'SubmitOrder', 'params', JSON.stringify(params));
379
+ const orderResult = await postUmOrder(credential, params);
380
+ if (isApiError(orderResult)) {
381
+ return { res: { code: orderResult.code, message: orderResult.msg } };
382
+ }
383
+ return { res: { code: 0, message: 'OK', order_id: orderResult.orderId } };
384
+ }
385
+ return { res: { code: 400, message: `unsupported type: ${instType}` } };
324
386
  });
325
- }, { auto_refresh_interval: 1000 });
326
- terminal.server.provideService('SubmitOrder', {
327
- required: ['account_id'],
328
- properties: {
329
- account_id: { const: UNIFIED_ACCOUNT_ID },
330
- },
331
- }, async (msg) => {
332
- console.info(formatTime(Date.now()), 'SubmitOrder', msg.req);
333
- const order = msg.req;
334
- const [instType, symbol] = decodePath(order.product_id);
335
- if (instType === 'usdt-future') {
336
- const params = {
387
+ }
388
+ {
389
+ terminal.server.provideService('CancelOrder', {
390
+ required: ['account_id', 'order_id', 'product_id'],
391
+ properties: {
392
+ account_id: { const: UNIFIED_ACCOUNT_ID },
393
+ },
394
+ }, async (msg) => {
395
+ const order = msg.req;
396
+ if (!order.order_id) {
397
+ return { res: { code: 400, message: 'order_id is required' } };
398
+ }
399
+ const [instType, symbol] = decodePath(order.product_id);
400
+ if (instType !== 'usdt-future') {
401
+ return { res: { code: 400, message: `unsupported type: ${instType}` } };
402
+ }
403
+ const cancelResult = await deleteUmOrder(credential, {
337
404
  symbol,
338
- side: mapOrderDirectionToSide(order.order_direction),
339
- positionSide: mapOrderDirectionToPosSide(order.order_direction),
340
- type: mapOrderTypeToOrdType(order.order_type),
341
- timeInForce: order.order_type === 'MAKER' ? 'GTX' : order.order_type === 'LIMIT' ? 'GTC' : undefined,
342
- quantity: order.volume,
343
- price: order.price,
344
- newClientOrderId: deriveClientOrderId(order),
345
- };
346
- console.info(formatTime(Date.now()), 'SubmitOrder', 'params', JSON.stringify(params));
347
- const orderResult = await client.postUmOrder(params);
348
- if (isError(orderResult)) {
349
- return { res: { code: orderResult.code, message: orderResult.msg } };
405
+ orderId: order.order_id,
406
+ });
407
+ if (isApiError(cancelResult)) {
408
+ return { res: { code: cancelResult.code, message: cancelResult.msg } };
350
409
  }
351
- return { res: { code: 0, message: 'OK', order_id: orderResult.orderId } };
352
- }
353
- return { res: { code: 400, message: `unsupported type: ${instType}` } };
354
- });
355
- }
356
- {
357
- terminal.server.provideService('CancelOrder', {
358
- required: ['account_id', 'order_id', 'product_id'],
359
- properties: {
360
- account_id: { const: UNIFIED_ACCOUNT_ID },
361
- },
362
- }, async (msg) => {
363
- const order = msg.req;
364
- if (!order.order_id) {
365
- return { res: { code: 400, message: 'order_id is required' } };
366
- }
367
- const [instType, symbol] = decodePath(order.product_id);
368
- if (instType !== 'usdt-future') {
369
- return { res: { code: 400, message: `unsupported type: ${instType}` } };
370
- }
371
- const cancelResult = await client.deleteUmOrder({
372
- symbol,
373
- orderId: order.order_id,
410
+ return { res: { code: 0, message: 'OK' } };
374
411
  });
375
- if (isError(cancelResult)) {
376
- return { res: { code: cancelResult.code, message: cancelResult.msg } };
377
- }
378
- return { res: { code: 0, message: 'OK' } };
379
- });
380
- }
381
- })();
412
+ }
413
+ })();
414
+ }
382
415
  //# sourceMappingURL=legacy_index.js.map