@icgio/icg-exchanges-wrapper 1.17.28 → 1.18.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.
@@ -13,7 +13,14 @@ let first_time = {},
13
13
  // Track persistent WebSocket failures per pair to avoid repeated retries
14
14
  ws_failed_pairs = {}, // { "ExchangeName": { "PAIR": { count: number, last_failed: timestamp } } }
15
15
  WS_FAILURE_THRESHOLD = 3, // Skip WebSocket after 3 consecutive failures
16
- WS_RETRY_INTERVAL_MS = 5 * 60 * 1000 // Retry WebSocket after 5 minutes
16
+ WS_RETRY_INTERVAL_MS = 5 * 60 * 1000, // Retry WebSocket after 5 minutes
17
+ // After a WS reconnect, the post-reconnect dict starts empty. Force REST
18
+ // fallback for this window so trades dropped during the WS-down gap are
19
+ // backfilled. The flag on Exchange.ws.market.reconnected_at[pair] is set by
20
+ // exchange-ws.js on reconnect and cleared here after a successful REST call.
21
+ RECONNECT_RECOVERY_MS = 60 * 1000,
22
+ RECONNECT_BACKFILL_CAP_MS = 10 * 60 * 1000,
23
+ RECONNECT_BACKFILL_BUFFER_MS = 10 * 1000
17
24
 
18
25
  // ============ HELPER FUNCTIONS ============
19
26
 
@@ -490,6 +497,17 @@ const get_exchanges_market_history = function (Exchanges, exchange_pair_dict, ti
490
497
  // Wait for pair to become ready with retry logic
491
498
  const pair_start_time = Date.now()
492
499
  const check_pair_ready = () => {
500
+ // Check if the pair just reconnected — if so, force REST fallback so
501
+ // trades dropped during the WS-down window are backfilled. The post-
502
+ // reconnect dict is empty but `length >= 0` returns true, which would
503
+ // otherwise mask the gap. Resolve failed immediately to skip the 10s
504
+ // check_pair_ready timeout.
505
+ const reconnected_at = Exchange.ws?.market?.reconnected_at?.[pair]
506
+ if (reconnected_at && Date.now() - reconnected_at < RECONNECT_RECOVERY_MS) {
507
+ rate_resolve({ pair, failed: true })
508
+ return
509
+ }
510
+
493
511
  // Check if this specific pair is ready before calling market_history_ws
494
512
  const pair_ready =
495
513
  Exchange.ws.market.pair_status &&
@@ -541,10 +559,20 @@ const get_exchanges_market_history = function (Exchanges, exchange_pair_dict, ti
541
559
  console.log(`[WebSocket Fallback] ${Exchange_name} market history: Falling back to REST for ${failed_pairs.length} pair(s): ${failed_pairs.join(', ')}`)
542
560
  const rest_promises = failed_pairs.map((pair) => {
543
561
  return new Promise((rest_resolve) => {
544
- Exchange.market_history(pair, timeframe_in_ms, function (res) {
562
+ // After a reconnect, expand the REST timeframe to cover the WS-down
563
+ // gap so trades dropped while the socket was closed are backfilled.
564
+ const gap_from = Exchange.ws?.market?.reconnect_gap_from?.[pair]
565
+ const effective_timeframe = gap_from
566
+ ? Math.min(Math.max(timeframe_in_ms, Date.now() - gap_from + RECONNECT_BACKFILL_BUFFER_MS), RECONNECT_BACKFILL_CAP_MS)
567
+ : timeframe_in_ms
568
+ Exchange.market_history(pair, effective_timeframe, function (res) {
545
569
  if (res.success) {
546
570
  set_pair_data(exchange_pair_market_history, pair_exchange_market_history, Exchange_name, pair, res.body)
547
- console.log(`[REST Fallback] ${Exchange_name} ${pair} market history: Successfully fetched via REST`)
571
+ // Clear reconnect flags so subsequent polls resume using WS
572
+ if (Exchange.ws?.market?.reconnected_at) delete Exchange.ws.market.reconnected_at[pair]
573
+ if (Exchange.ws?.market?.reconnect_gap_from) delete Exchange.ws.market.reconnect_gap_from[pair]
574
+ const window_s = Math.round(effective_timeframe / 1000)
575
+ console.log(`[REST Fallback] ${Exchange_name} ${pair} market history: Successfully fetched via REST (${window_s}s window)`)
548
576
  } else {
549
577
  console.error(`[REST Fallback] getting market history failed for ${Exchange_name} ${pair}:`, res)
550
578
  delete_pair_data(exchange_pair_market_history, pair_exchange_market_history, Exchange_name, pair)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-exchanges-wrapper",
3
- "version": "1.17.28",
3
+ "version": "1.18.0",
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.41.1",
20
+ "@icgio/icg-exchanges": "^1.42.0",
21
21
  "@icgio/icg-utils": "^1.9.61",
22
22
  "lodash": "^4.17.23",
23
23
  "pg": "^8.17.2"