@icgio/icg-exchanges-wrapper 1.21.46 → 1.22.1

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.
@@ -187,7 +187,7 @@ module.exports = class Orders {
187
187
  limits
188
188
  if (_.includes(['hkbitex'], exchange_base)) {
189
189
  limits = HKBITEX_LIMITS
190
- } else if (_.includes(['binance', 'gate', 'gate'], exchange_base)) {
190
+ } else if (_.includes(['binance', 'binancefutures', 'gate'], exchange_base)) {
191
191
  limits = MEDIUM_LIMITS
192
192
  } else if (_.includes(['bitfinex', 'hashkeyglobal', 'kucoin', 'lbank', 'okx', 'phemex', 'xt'], exchange_base)) {
193
193
  limits = SMALL_LIMITS
@@ -524,24 +524,32 @@ module.exports = class Orders {
524
524
  this.delete_order_info(exchange, pair, order.type, order.order_id, order.order_type + ' cancel-order-success')
525
525
  }, this.delete_finished_orders_timeout)
526
526
  } else if (!res.success && res.error === 'order-not-open' && original_status !== 'CANCELED') {
527
- // console.log('%s ORDER CANNOT BE CANCELED: %j', original_status, order)
528
- this.update_order_info(
529
- exchange,
530
- pair,
531
- order.type,
532
- order.order_id,
533
- {
534
- status: 'F-FILLED',
535
- amount: 0,
536
- f_amount: order.amount,
537
- f_percentage: 1,
538
- close_time: Date.now(),
539
- },
540
- order.order_type + ' cannot-be-canceled',
541
- )
542
- setTimeout(() => {
543
- this.delete_order_info(exchange, pair, order.type, order.order_id, order.order_type + ' cannot-be-canceled')
544
- }, this.delete_finished_orders_timeout)
527
+ // Not-open filled: query the actual terminal state and book it via the standard
528
+ // reconciler. A failed query books CANCELED keeping known fills — never fabricate a fill.
529
+ this.Exchanges[exchange].query_order(pair, order.order_id, (query_res) => {
530
+ if (query_res.success) {
531
+ this.process_query_order({ [exchange]: { [pair]: { query_order: [query_res.body] } } })
532
+ return
533
+ }
534
+ console.error('cancel not-open, query_order failed: %s %s %s %j', exchange, pair, order.order_id, query_res)
535
+ this.update_order_info(
536
+ exchange,
537
+ pair,
538
+ order.type,
539
+ order.order_id,
540
+ {
541
+ status: 'CANCELED',
542
+ f_amount: order.f_amount || 0,
543
+ f_percentage: order.t_amount > 0 ? (order.f_amount || 0) / order.t_amount : 0,
544
+ close_time: Date.now(),
545
+ event_hrtime: process.hrtime(),
546
+ },
547
+ order.order_type + ' cannot-be-canceled query-failed',
548
+ )
549
+ setTimeout(() => {
550
+ this.delete_order_info(exchange, pair, order.type, order.order_id, order.order_type + ' cannot-be-canceled query-failed')
551
+ }, this.delete_finished_orders_timeout)
552
+ })
545
553
  } else {
546
554
  console.error('cancel order:', exchange, pair, order, res)
547
555
  }
@@ -1144,6 +1152,18 @@ module.exports = class Orders {
1144
1152
  }
1145
1153
  }
1146
1154
  function get_balances(Exchanges, interval_dict) {
1155
+ // push tier: observables fire on account events; the poll loops below remain the watchdog
1156
+ for (let exchange in Exchanges) {
1157
+ if (!Exchanges[exchange].balance_ws_observable) {
1158
+ continue
1159
+ }
1160
+ if (Exchanges[exchange].ws_status().trading === 'n-opened') {
1161
+ Exchanges[exchange].ws_account()
1162
+ }
1163
+ Exchanges[exchange].balance_ws_observable((res) => {
1164
+ balances_handler(exchange, res)
1165
+ })
1166
+ }
1147
1167
  for (let exchange in Exchanges) {
1148
1168
  if (Exchanges[exchange].balance_ws) {
1149
1169
  setInterval(() => {
@@ -1174,6 +1194,18 @@ module.exports = class Orders {
1174
1194
  }
1175
1195
  }
1176
1196
  function get_positions(Exchanges, interval_dict) {
1197
+ // push tier (see get_balances)
1198
+ for (let exchange in Exchanges) {
1199
+ if (!Exchanges[exchange].position_all_ws_observable) {
1200
+ continue
1201
+ }
1202
+ if (Exchanges[exchange].ws_status().trading === 'n-opened') {
1203
+ Exchanges[exchange].ws_account()
1204
+ }
1205
+ Exchanges[exchange].position_all_ws_observable((res) => {
1206
+ position_all_handler(exchange, res)
1207
+ })
1208
+ }
1177
1209
  for (let exchange in Exchanges) {
1178
1210
  if (Exchanges[exchange].position_all_ws) {
1179
1211
  if (Exchanges[exchange].ws_status().trading === 'n-opened') Exchanges[exchange].ws_account()
@@ -1302,8 +1334,7 @@ module.exports = class Orders {
1302
1334
  else live_ids.add(String(order.order_id))
1303
1335
  })
1304
1336
  const unexpected_ids = new Set(Array.from(live_ids).filter((order_id) => !state.orders_by_id.has(order_id)))
1305
- const unexpected_ids_changed =
1306
- unexpected_ids.size !== state.unexpected_ids.size || Array.from(unexpected_ids).some((order_id) => !state.unexpected_ids.has(order_id))
1337
+ const unexpected_ids_changed = unexpected_ids.size !== state.unexpected_ids.size || Array.from(unexpected_ids).some((order_id) => !state.unexpected_ids.has(order_id))
1307
1338
  if (unexpected_ids.size > 0 && unexpected_ids_changed) {
1308
1339
  console.error('unexpected orders during original cleanup: %s %s count=%s order_ids=%j', exchange, pair, unexpected_ids.size, Array.from(unexpected_ids).slice(0, 10))
1309
1340
  }
@@ -1410,6 +1441,20 @@ module.exports = class Orders {
1410
1441
  }, interval_dict[exchange].open_orders)
1411
1442
  })
1412
1443
  }
1444
+ // push tier (see get_balances)
1445
+ for (let exchange in Exchanges) {
1446
+ if (!Exchanges[exchange].open_orders_ws_observable) {
1447
+ continue
1448
+ }
1449
+ if (Exchanges[exchange].ws_status().trading === 'n-opened') {
1450
+ Exchanges[exchange].ws_account()
1451
+ }
1452
+ for (let pair of exchange_pairs[exchange]) {
1453
+ Exchanges[exchange].open_orders_ws_observable(pair, (res) => {
1454
+ open_orders_handler(exchange, pair, res)
1455
+ })
1456
+ }
1457
+ }
1413
1458
  for (let exchange in Exchanges) {
1414
1459
  if (Exchanges[exchange].get_all_open_orders_ws) {
1415
1460
  if (Exchanges[exchange].ws_status().trading === 'n-opened') Exchanges[exchange].ws_account()
@@ -1540,6 +1585,23 @@ module.exports = class Orders {
1540
1585
  }
1541
1586
  }
1542
1587
  }
1588
+ // push tier (see get_balances)
1589
+ for (let exchange in Exchanges) {
1590
+ if (_.includes(this.no_trades_function_exchanges, exchange)) {
1591
+ continue
1592
+ }
1593
+ if (!Exchanges[exchange].trades_ws_observable) {
1594
+ continue
1595
+ }
1596
+ if (Exchanges[exchange].ws_status().trading === 'n-opened') {
1597
+ Exchanges[exchange].ws_account()
1598
+ }
1599
+ for (let pair of exchange_pairs[exchange]) {
1600
+ Exchanges[exchange].trades_ws_observable(pair, TRADES_DEFAULT_MS, (res) => {
1601
+ trades_handler(exchange, pair, res)
1602
+ })
1603
+ }
1604
+ }
1543
1605
  for (let exchange in Exchanges) {
1544
1606
  if (_.includes(this.no_trades_function_exchanges, exchange)) {
1545
1607
  } else if (Exchanges[exchange].get_all_trades_ws) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-exchanges-wrapper",
3
- "version": "1.21.46",
3
+ "version": "1.22.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -17,7 +17,7 @@
17
17
  },
18
18
  "homepage": "https://github.com/icgio/icg-exchanges-wrapper#readme",
19
19
  "dependencies": {
20
- "@icgio/icg-exchanges": "^1.47.0",
20
+ "@icgio/icg-exchanges": "^1.48.0",
21
21
  "@icgio/icg-utils": "^1.9.64",
22
22
  "lodash": "^4.17.23",
23
23
  "pg": "^8.17.2"