dexbot 1.7.0 → 1.7.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.
Files changed (105) hide show
  1. package/CHANGELOG.md +42 -0
  2. package/README.md +3 -3
  3. package/analysis/README.md +33 -13
  4. package/claw/package.json +1 -1
  5. package/claw/runtimes/openclaw-plugin/openclaw.plugin.json +1 -1
  6. package/claw/runtimes/openclaw-plugin/package.json +1 -1
  7. package/dist/analysis/account_resolver.js +5 -6
  8. package/dist/analysis/analyze_liquidity.js +15 -4
  9. package/dist/analysis/bot_key_utils.d.ts +44 -1
  10. package/dist/analysis/bot_key_utils.js +76 -7
  11. package/dist/analysis/fills_cache.js +2 -1
  12. package/dist/analysis/grid_correction_check.d.ts +1 -19
  13. package/dist/analysis/grid_correction_check.js +12 -30
  14. package/dist/analysis/liquidity/liquidity_chart_generator.js +190 -20
  15. package/dist/analysis/liquidity/liquidity_depth_model.d.ts +41 -16
  16. package/dist/analysis/liquidity/liquidity_depth_model.js +49 -21
  17. package/dist/analysis/liquidity/liquidity_view.d.ts +15 -0
  18. package/dist/analysis/liquidity/liquidity_view.js +11 -0
  19. package/dist/analysis/resolve_bot_accounts.js +7 -6
  20. package/dist/analysis/trade_profitability.js +2 -1
  21. package/dist/credential-daemon.js +13 -2
  22. package/dist/dexbot.js +4 -4
  23. package/dist/modules/account_bots.d.ts +11 -1
  24. package/dist/modules/account_orders.d.ts +12 -0
  25. package/dist/modules/account_orders.js +30 -0
  26. package/dist/modules/bitshares-native/transport.js +24 -6
  27. package/dist/modules/bitshares_client.d.ts +24 -6
  28. package/dist/modules/bitshares_client.js +44 -7
  29. package/dist/modules/bot_settings.d.ts +2 -0
  30. package/dist/modules/constants.d.ts +26 -3
  31. package/dist/modules/constants.js +63 -8
  32. package/dist/modules/dexbot_class.d.ts +1 -6
  33. package/dist/modules/dexbot_class.js +14 -0
  34. package/dist/modules/dexbot_cow_runtime.d.ts +97 -7
  35. package/dist/modules/dexbot_cow_runtime.js +620 -132
  36. package/dist/modules/dexbot_fill_runtime.js +124 -35
  37. package/dist/modules/dexbot_maintenance_runtime.js +226 -73
  38. package/dist/modules/dexbot_startup_runtime.js +32 -20
  39. package/dist/modules/dexbot_state_recovery.d.ts +33 -1
  40. package/dist/modules/dexbot_state_recovery.js +167 -5
  41. package/dist/modules/node_connect_policy.d.ts +8 -1
  42. package/dist/modules/node_connect_policy.js +18 -2
  43. package/dist/modules/node_flap_score.d.ts +87 -0
  44. package/dist/modules/node_flap_score.js +124 -0
  45. package/dist/modules/node_health_cache.d.ts +40 -3
  46. package/dist/modules/node_health_cache.js +66 -20
  47. package/dist/modules/node_manager.d.ts +101 -19
  48. package/dist/modules/node_manager.js +255 -65
  49. package/dist/modules/order/accounting.js +56 -7
  50. package/dist/modules/order/format.d.ts +24 -10
  51. package/dist/modules/order/format.js +30 -10
  52. package/dist/modules/order/grid.js +82 -11
  53. package/dist/modules/order/grid_reconcile.d.ts +5 -1
  54. package/dist/modules/order/grid_reconcile.js +333 -210
  55. package/dist/modules/order/grid_reconcile_internal.d.ts +38 -2
  56. package/dist/modules/order/grid_reconcile_internal.js +207 -87
  57. package/dist/modules/order/index.d.ts +4 -0
  58. package/dist/modules/order/lifecycle.d.ts +353 -0
  59. package/dist/modules/order/lifecycle.js +1109 -0
  60. package/dist/modules/order/manager.d.ts +271 -5
  61. package/dist/modules/order/manager.js +651 -48
  62. package/dist/modules/order/placement_lock.d.ts +102 -0
  63. package/dist/modules/order/placement_lock.js +222 -0
  64. package/dist/modules/order/strategy.js +13 -1
  65. package/dist/modules/order/sync_engine.d.ts +13 -1
  66. package/dist/modules/order/sync_engine.js +153 -53
  67. package/dist/modules/order/utils/math.d.ts +22 -1
  68. package/dist/modules/order/utils/math.js +54 -1
  69. package/dist/modules/order/utils/order.d.ts +75 -5
  70. package/dist/modules/order/utils/order.js +158 -9
  71. package/dist/modules/order/utils/system.d.ts +18 -0
  72. package/dist/modules/order/utils/system.js +84 -8
  73. package/dist/modules/order/utils/validate.d.ts +7 -1
  74. package/dist/modules/order/utils/validate.js +48 -2
  75. package/dist/modules/outcome.d.ts +54 -0
  76. package/dist/modules/outcome.js +51 -0
  77. package/dist/modules/settings_merge.d.ts +4 -0
  78. package/dist/modules/settings_merge.js +9 -0
  79. package/dist/modules/timing_policy.d.ts +82 -0
  80. package/dist/modules/timing_policy.js +244 -0
  81. package/dist/modules/types.d.ts +132 -0
  82. package/dist/scripts/analyze-credit.js +54 -17
  83. package/dist/scripts/analyze-orders.d.ts +4 -2
  84. package/dist/scripts/analyze-orders.js +113 -34
  85. package/dist/scripts/chart_targets.js +11 -15
  86. package/dist/scripts/test-credit-renewal.js +12 -5
  87. package/docs/COW_INVARIANTS.md +67 -8
  88. package/docs/DEXBOT_COMPARISON.md +6 -6
  89. package/docs/EVOLUTION.md +9 -8
  90. package/docs/FUND_MOVEMENT_AND_ACCOUNTING.md +1 -1
  91. package/docs/GRID_PRICE_INVARIANT.md +1 -1
  92. package/docs/GRID_RECONCILE.md +17 -12
  93. package/docs/LOGGING.md +25 -1
  94. package/docs/ORDER_ENGINE_POST_1.0_RETROSPECTIVE.md +175 -26
  95. package/docs/README.md +23 -2
  96. package/docs/WORKFLOW.md +9 -3
  97. package/docs/architecture.md +42 -5
  98. package/docs/developer_guide.md +25 -8
  99. package/modules/README.md +3 -0
  100. package/package.json +1 -1
  101. package/scripts/README.md +16 -0
  102. package/scripts/pdev.sh +14 -1
  103. package/scripts/pmain.sh +23 -8
  104. package/scripts/ptest.sh +7 -0
  105. package/tests/README.md +30 -1
@@ -12,6 +12,10 @@
12
12
  * Inversion swaps bid/ask roles: a bid for the old base is an ask for the new
13
13
  * base. `depthMode` selects the asset the cumulative depth is measured in,
14
14
  * relative to the current orientation ('quote' or 'base').
15
+ *
16
+ * The same transform carries the two spread bands the chart shades: the pool's
17
+ * fee-adjusted edges and the book's best bid/ask. Inversion flips both, so the
18
+ * bands stay on the correct sides instead of mirroring the wrong way round.
15
19
  */
16
20
  import type { DepthSeries, PriceWindow } from './liquidity_depth_model.js';
17
21
  interface DepthView {
@@ -21,6 +25,14 @@ interface DepthView {
21
25
  bookAsk: number[];
22
26
  poolAsk: number[];
23
27
  poolPrice: number | null;
28
+ /** Fee-adjusted pool bid edge in the current price orientation. */
29
+ poolBidPrice: number | null;
30
+ /** Fee-adjusted pool ask edge in the current price orientation. */
31
+ poolAskPrice: number | null;
32
+ /** Best book bid (the low edge of the order-book spread band). */
33
+ bookBidPrice: number | null;
34
+ /** Best book ask (the high edge of the order-book spread band). */
35
+ bookAskPrice: number | null;
24
36
  mid: number | null;
25
37
  min: number;
26
38
  max: number;
@@ -31,6 +43,9 @@ interface DepthViewOptions {
31
43
  poolPrice: number | null;
32
44
  mid: number | null;
33
45
  window: PriceWindow;
46
+ /** Best book bid/ask, so the book spread band follows the orientation. */
47
+ bookBestBid?: number | null;
48
+ bookBestAsk?: number | null;
34
49
  }
35
50
  declare function buildDepthView(series: DepthSeries, options: DepthViewOptions): DepthView;
36
51
  export { buildDepthView };
@@ -9,6 +9,10 @@ function buildDepthView(series, options) {
9
9
  bookAsk: useQuote ? series.bookAsk : series.bookAskBase,
10
10
  poolAsk: useQuote ? series.poolAsk : series.poolAskBase,
11
11
  poolPrice: options.poolPrice,
12
+ poolBidPrice: series.poolBidPrice,
13
+ poolAskPrice: series.poolAskPrice,
14
+ bookBidPrice: options.bookBestBid != null && options.bookBestBid > 0 ? options.bookBestBid : null,
15
+ bookAskPrice: options.bookBestAsk != null && options.bookBestAsk > 0 ? options.bookBestAsk : null,
12
16
  mid: options.mid,
13
17
  min: options.window.min,
14
18
  max: options.window.max,
@@ -46,6 +50,13 @@ function buildDepthView(series, options) {
46
50
  bookAsk,
47
51
  poolAsk,
48
52
  poolPrice: options.poolPrice != null ? 1 / options.poolPrice : null,
53
+ // Inversion swaps the sides, so the pool's bid edge becomes 1/askEdge.
54
+ poolBidPrice: series.poolAskPrice != null ? 1 / series.poolAskPrice : null,
55
+ poolAskPrice: series.poolBidPrice != null ? 1 / series.poolBidPrice : null,
56
+ // The best book bid becomes the inverted market's best ask, and vice
57
+ // versa, so the spread band survives an orientation flip intact.
58
+ bookBidPrice: options.bookBestAsk != null && options.bookBestAsk > 0 ? 1 / options.bookBestAsk : null,
59
+ bookAskPrice: options.bookBestBid != null && options.bookBestBid > 0 ? 1 / options.bookBestBid : null,
49
60
  mid: options.mid != null ? 1 / options.mid : null,
50
61
  min: validRange ? qCenter * (1 - range) : 1 / options.window.max,
51
62
  max: validRange ? qCenter * (1 + range) : 1 / options.window.min,
@@ -20,7 +20,7 @@
20
20
  * shared client on exit. Run standalone only — never import/call from a
21
21
  * long-running process holding a live connection.
22
22
  */
23
- import { loadBotSettings, computeBotKey, persistBotAccountId } from './bot_key_utils.js';
23
+ import { loadBotSettings, computeBotKey, persistBotAccountId, matchBotEntryTarget, isAccountIdRef } from './bot_key_utils.js';
24
24
  import { setGlobalConsoleLevel, getGlobalConsoleLevel } from '../modules/order/logger.js';
25
25
  import { sleep } from '../modules/order/utils/system.js';
26
26
  import { getErrorMessage, getErrorCode } from '../modules/utils/errors.js';
@@ -90,7 +90,7 @@ async function resolveWithTimeout(chainOrders, ref, timeoutMs) {
90
90
  chainOrders.resolveAccountId(ref),
91
91
  sleep(timeoutMs).then(() => { throw timeoutErr; }),
92
92
  ]);
93
- if (id && /^1\.2\.\d+$/.test(String(id)))
93
+ if (id && isAccountIdRef(id))
94
94
  return { id: String(id), reason: 'resolved' };
95
95
  return { id: null, reason: 'not-found' };
96
96
  }
@@ -108,8 +108,9 @@ async function main() {
108
108
  }
109
109
  let targets = entries.map((bot, index) => ({ bot, index, key: computeBotKey(bot, index) }));
110
110
  if (opts.botKey) {
111
- const want = String(opts.botKey).toLowerCase();
112
- targets = targets.filter((t) => t.key === want || String(t.bot?.name ?? '').toLowerCase() === want);
111
+ // Shared `<bot>` rule set (analysis/bot_key_utils): bot key or display
112
+ // name, case/separator-insensitive.
113
+ targets = targets.filter((t) => matchBotEntryTarget(t.bot, opts.botKey, t.key) !== null);
113
114
  if (!targets.length) {
114
115
  console.error(`Error: bot '${opts.botKey}' not found in profiles/bots.json.`);
115
116
  process.exit(1);
@@ -141,13 +142,13 @@ async function main() {
141
142
  for (const t of targets) {
142
143
  const name = t.bot?.name ?? t.key;
143
144
  const prefRaw = t.bot?.preferredAccount != null ? String(t.bot.preferredAccount) : '';
144
- const stored = /^1\.2\.\d+$/.test(String(t.bot?.accountId ?? '')) ? String(t.bot.accountId) : null;
145
+ const stored = isAccountIdRef(t.bot?.accountId) ? String(t.bot.accountId) : null;
145
146
  if (!prefRaw) {
146
147
  results.push({ key: t.key, name, status: 'skipped', detail: 'no preferredAccount' });
147
148
  console.log(`- ${t.key}: skipped (no preferredAccount)`);
148
149
  continue;
149
150
  }
150
- if (/^1\.2\.\d+$/.test(prefRaw)) {
151
+ if (isAccountIdRef(prefRaw)) {
151
152
  if (stored !== prefRaw) {
152
153
  const wrote = opts.dryRun ? true : persistBotAccountId(t.key, prefRaw);
153
154
  const status = !wrote ? 'failed' : (opts.dryRun ? 'would-update' : 'updated');
@@ -4,6 +4,7 @@ import fs from 'node:fs';
4
4
  import path from 'node:path';
5
5
  import { pathToFileURL } from 'node:url';
6
6
  import { resolveAccountRef } from './account_resolver.js';
7
+ import { isAccountIdRef } from './bot_key_utils.js';
7
8
  import { BTS_ID, assetPrec as getPrec, assetSymbol, resolveAssetPrecisions, classifyFill, matchesAssetRef, } from './fills_source.js';
8
9
  import { writePnlReport } from './pnl_report.js';
9
10
  import { percentile } from './math_utils.js';
@@ -1075,7 +1076,7 @@ async function run(argv = process.argv.slice(2)) {
1075
1076
  const opts = parseArgs(argv);
1076
1077
  let accountId = opts.accountId;
1077
1078
  let resolvedBotName = null;
1078
- if (!/^1\.2\.\d+$/.test(String(accountId))) {
1079
+ if (!isAccountIdRef(accountId)) {
1079
1080
  // The Kibana query below filters on the 1.2.x account_id field, so a
1080
1081
  // name must always resolve first (a raw name would silently return
1081
1082
  // zero fills). Shared resolver: a local bot profile is checked first
@@ -785,8 +785,19 @@ function refreshNodeList() {
785
785
  try {
786
786
  const bestNodes = orderNodesForSettings(settings);
787
787
  if (bestNodes && bestNodes.length > 0) {
788
- _nativeNodeList = bestNodes;
789
- _nativeChainClient.setNodes(bestNodes);
788
+ // Never evict the node this daemon is currently connected to.
789
+ // setNodes() replaces the transport candidate list, and
790
+ // transport.connect() only keeps its socket when that list
791
+ // still contains the active node — so a refresh built from the
792
+ // health cache (which omits nodes that are momentarily
793
+ // unhealthy) used to force a full teardown on the next
794
+ // broadcast. Appending it as the last candidate is enough.
795
+ const activeNode = _nativeChainClient.transport?.getNodeUrl?.();
796
+ const nodes = activeNode && !bestNodes.includes(activeNode)
797
+ ? [...bestNodes, activeNode]
798
+ : bestNodes;
799
+ _nativeNodeList = nodes;
800
+ _nativeChainClient.setNodes(nodes);
790
801
  daemonLogger.log?.(`[credential-daemon] Node list refreshed: using best ${bestNodes.length} nodes from cache.`);
791
802
  }
792
803
  }
package/dist/dexbot.js CHANGED
@@ -47,7 +47,7 @@ const __dirname = _esmDirname(__filename);
47
47
  * Runtime: dexbot start [bot] | stop | reload | restart | delete | status | pm2
48
48
  * Trading: dexbot test <bot> | drystart <bot> | reset {all|<bot>}
49
49
  * Config: dexbot bot | key | enable {all|<bot>} | disable {all|<bot>} | default | update
50
- * Analysis: dexbot order [<bot>] | export <bot> | credit [<bot>] | rich <asset...>
50
+ * Analysis: dexbot order [<bot|account>] | export <bot> | credit [<bot|account>] | rich <asset...>
51
51
  * dexbot tv <target> | dw <target> | liquid <target> | pnl <account>
52
52
  * dexbot asset <bts|symbol> (shorthand: dexbot a <bts|symbol>)
53
53
  * Files: dexbot clear | clear-orders | clear-market-adapter | clear-all
@@ -158,7 +158,7 @@ const CLI_EXAMPLES = [
158
158
  { title: 'Update DEXBot2', command: 'dexbot update', notes: 'Pull, install, and restart active bots.' },
159
159
  { title: 'Export bot trades', command: 'dexbot export <bot>', notes: 'Write trades and settings to CSV/JSON.' },
160
160
  { title: 'Analyze order grids', command: 'dexbot order', notes: 'Spread, increment, and funds; --export for HTML.' },
161
- { title: 'Show live credit positions', command: 'dexbot credit', notes: 'Summed MPA + borrowed-credit positions per bot.' },
161
+ { title: 'Show live credit positions', command: 'dexbot credit [<bot|account>]', notes: 'Summed MPA + borrowed-credit positions per bot (all credit bots by default).' },
162
162
  { title: 'Asset rich list', command: 'dexbot rich <asset> [<asset...>]', notes: 'Top holder(s) per asset from the chain asset holders API.' },
163
163
  { title: 'Chart a bot, pool, or pair', command: 'dexbot tv <bot|pool|AssetA/AssetB> --month 3', notes: 'OHLC chart; pool = id (133) or pool token (LP share asset); --feed charts MPA price-feed history.' },
164
164
  { title: 'Liquidity depth chart', command: 'dexbot liquid <bot|pool|AssetA/AssetB>', notes: 'Order-book + AMM pool cumulative depth; pool = id or pool token (aliases: liq, liqu, liquidity).' },
@@ -214,13 +214,13 @@ function printCLIUsage() {
214
214
  // data export, then market research, then accounting/chain context. Keep the
215
215
  // README "Bot Management" block in the same order so the two cannot drift.
216
216
  ['Analysis', [
217
- ['order [<bot>]', 'Analyze persisted order grids; --export for HTML.'],
217
+ ['order [<bot|account>]', 'Analyze persisted order grids for a bot, an account (name or 1.2.x id), or all bots; --export for HTML.'],
218
218
  ['export <bot>', 'Export bot trades/settings to CSV/JSON.'],
219
219
  ['tv <target>', 'OHLC chart for <bot|pool|AssetA/AssetB> (pool = id or pool token); --month N (default 3), --feed for MPA price-feed history.'],
220
220
  ['dw <target>', 'Dynamic-weight research chart; same targets and flags as tv.'],
221
221
  ['liquid <target>', 'Liquidity depth chart for <bot|pool|AssetA/AssetB> (pool = id or pool token); --range, --depth, --steps (aliases: liq, liqu, liquidity).'],
222
222
  ['pnl <account>', 'HTML PnL report for a bot or account; --month N, optional --pair BASE/QUOTE filter.'],
223
- ['credit [<bot>]', 'Show live MPA and borrowed-credit positions.'],
223
+ ['credit [<bot|account>]', 'Show live MPA and borrowed-credit positions for a bot, an account (name or 1.2.x id), or every credit-enabled bot.'],
224
224
  ['asset <bts|symbol>', 'Supply distribution: bts = core asset, bare symbol/id = issued asset; --json, --html (shorthand: a, e.g. dexbot a bts).'],
225
225
  ['rich <asset...>', 'Per-asset rich list / top holder; --top N, --json.'],
226
226
  ]],
@@ -151,6 +151,12 @@ declare function loadGeneralSettings(): {
151
151
  DUST_HEALTH_CHECK_INTERVAL_MS: number;
152
152
  DUST_CANCEL_TIMEOUT_MS: number;
153
153
  BROADCAST_STALE_CLEAR_MS: number;
154
+ PENDING_BROADCAST_TTL_MS: number;
155
+ BATCH_INFLIGHT_STALE_MS: number;
156
+ RECOVERY_INFLIGHT_STALE_MS: number;
157
+ PLACEMENT_STALE_CLEAR_MS: number;
158
+ CHAIN_SIZE_APPLIED_MARKER_TTL_MS: number;
159
+ STRUCTURAL_REBUILD_DEFER_MAX_MS: number;
154
160
  BROADCAST_DEFER_SAFETY_MARGIN_MS: number;
155
161
  FILL_BROADCAST_DEFER_MAX_MS: number;
156
162
  FILL_TOTALS_RETRY_BASE_MS: number;
@@ -361,7 +367,11 @@ declare function loadGeneralSettings(): {
361
367
  BLACKLIST_THRESHOLD: number;
362
368
  BLACKLIST_COOLDOWN_MS: number;
363
369
  FAILURE_REPORT_COOLDOWN_MS: number;
364
- LIVE_FAILURE_HEALTH_SUCCESS_STREAK: number;
370
+ FAILURE_STRIKE_DECAY_MS: number;
371
+ FLAP_WINDOW_MS: number;
372
+ FLAP_HALF_LIFE_MS: number;
373
+ FLAP_PENALTY_MS_PER_DROP: number;
374
+ FLAP_PENALTY_MAX_MS: number;
365
375
  EXPECTED_CHAIN_ID: string;
366
376
  SELECTION_STRATEGY: string;
367
377
  };
@@ -298,6 +298,18 @@ declare class AccountOrders {
298
298
  type: string;
299
299
  fillsAt: number;
300
300
  } | null;
301
+ /**
302
+ * Load the persisted sync-pass-1 "size already applied" markers.
303
+ * Stored in the free-form debugInputs blob (no order-row schema change).
304
+ * Returns a sanitized `{ orderId: { filledInt, at } }` map (empty when
305
+ * absent/malformed); freshness/TTL enforcement happens manager-side.
306
+ * @param {boolean} forceReload - If true, reload from disk
307
+ * @returns {Object}
308
+ */
309
+ loadChainSizeAppliedFills(forceReload?: boolean): Record<string, {
310
+ filledInt: number;
311
+ at: number;
312
+ }>;
301
313
  /**
302
314
  * Erase a persisted last-market-fill row that restoreLastMarketFill
303
315
  * rejected (TTL expiry / malformed). Mirrors clearPersistedLastFillPivot:
@@ -589,6 +589,36 @@ class AccountOrders {
589
589
  this._ensureLoaded(forceReload);
590
590
  return normalizeLastMarketFill(this.data && this.data.lastMarketFill);
591
591
  }
592
+ /**
593
+ * Load the persisted sync-pass-1 "size already applied" markers.
594
+ * Stored in the free-form debugInputs blob (no order-row schema change).
595
+ * Returns a sanitized `{ orderId: { filledInt, at } }` map (empty when
596
+ * absent/malformed); freshness/TTL enforcement happens manager-side.
597
+ * @param {boolean} forceReload - If true, reload from disk
598
+ * @returns {Object}
599
+ */
600
+ loadChainSizeAppliedFills(forceReload = false) {
601
+ this._ensureLoaded(forceReload);
602
+ const out = {};
603
+ const stored = this.data && this.data.debugInputs
604
+ ? this.data.debugInputs.chainSizeAppliedFills
605
+ : null;
606
+ if (!stored || typeof stored !== 'object')
607
+ return out;
608
+ let count = 0;
609
+ for (const [id, raw] of Object.entries(stored)) {
610
+ if (!id || count >= 500)
611
+ continue;
612
+ const e = raw;
613
+ const filledInt = Math.round(Number(e?.filledInt));
614
+ const at = Number(e?.at);
615
+ if (!Number.isFinite(filledInt) || filledInt <= 0 || !Number.isFinite(at))
616
+ continue;
617
+ out[id] = { filledInt, at };
618
+ count++;
619
+ }
620
+ return out;
621
+ }
592
622
  /**
593
623
  * Erase a persisted last-market-fill row that restoreLastMarketFill
594
624
  * rejected (TTL expiry / malformed). Mirrors clearPersistedLastFillPivot:
@@ -139,11 +139,24 @@ function createTransport(config = {}) {
139
139
  }
140
140
  return false;
141
141
  }
142
- function setStatus(newStatus) {
142
+ /**
143
+ * Publish a transport status change.
144
+ *
145
+ * `displayNode` exists so teardown paths can name the node they are
146
+ * closing *after* the active-node field has been cleared. Before this, an
147
+ * intentional disconnect logged `(node=null)`, which made 120 of 172
148
+ * closes in a live log run unattributable — the single biggest hole in
149
+ * connection diagnostics.
150
+ *
151
+ * `onStatusChange` is never handed the display value: it reads the live
152
+ * transport state via getNodeUrl(), which is null after disconnect().
153
+ */
154
+ function setStatus(newStatus, displayNode) {
143
155
  if (status !== newStatus) {
144
156
  const prevStatus = status;
145
157
  status = newStatus;
146
- transportLogger.info(`status change: ${prevStatus} -> ${newStatus} (node=${nodeUrl})`);
158
+ const logNode = displayNode === undefined ? nodeUrl : displayNode;
159
+ transportLogger.info(`status change: ${prevStatus} -> ${newStatus} (node=${logNode})`);
147
160
  if (onStatusChange) {
148
161
  try {
149
162
  onStatusChange(newStatus, nodeUrl);
@@ -277,7 +290,7 @@ function createTransport(config = {}) {
277
290
  }
278
291
  });
279
292
  }
280
- function setupMessageHandler(socket) {
293
+ function setupMessageHandler(socket, socketUrl) {
281
294
  socket.onmessage = (raw) => {
282
295
  let msg;
283
296
  try {
@@ -344,7 +357,11 @@ function createTransport(config = {}) {
344
357
  };
345
358
  socket.onerror = (evt) => {
346
359
  const msg = evt && evt.message ? evt.message : 'WebSocket connection error';
347
- transportLogger.warn(`WebSocket error on ${nodeUrl}: ${msg}`);
360
+ // Name the socket's own node, not the currently active one: a
361
+ // superseded socket can still emit an error after a switch, and
362
+ // logging it against `nodeUrl` blamed the wrong node (or logged
363
+ // `on null`).
364
+ transportLogger.warn(`WebSocket error on ${socketUrl}: ${msg}`);
348
365
  };
349
366
  }
350
367
  function _onConnected(socket, url, idx, wasReconnect) {
@@ -367,7 +384,7 @@ function createTransport(config = {}) {
367
384
  reconnectAttempts = 0;
368
385
  // A successful connection clears the deprioritization for this node.
369
386
  failedNodes.delete(url);
370
- setupMessageHandler(socket);
387
+ setupMessageHandler(socket, url);
371
388
  return (async () => {
372
389
  if (validateNode) {
373
390
  await validateNode();
@@ -566,8 +583,9 @@ function createTransport(config = {}) {
566
583
  catch (_) { }
567
584
  }
568
585
  connectingSockets.clear();
586
+ const closingNode = nodeUrl;
569
587
  nodeUrl = null;
570
- setStatus('closed');
588
+ setStatus('closed', closingNode);
571
589
  }
572
590
  /**
573
591
  * Force a reconnect on the active connection, preferring a different node.
@@ -29,11 +29,12 @@
29
29
  * 10. getNodeManager() - Get the NodeManager instance
30
30
  * 11. getNodeStats() - Get node health statistics
31
31
  * 12. getNodeSummary() - Get node summary
32
- * 13. getConnectionError() - Get last connection error
33
- * 14. onReconnect(callback) - Register reconnect callback
34
- * 15. withTimeout(promise, timeoutMs, options) - Wrap promise with timeout (re-exported from order/utils/timeout.js)
35
- * 16. _assessFailover() - Internal failover assessment
36
- * 17. _internal - @internal Internal state (connected flag) — test-only, do not rely on in production
32
+ * 13. getActiveNodeUrl() - Node currently in use (for failure attribution)
33
+ * 14. getConnectionError() - Get last connection error
34
+ * 15. onReconnect(callback) - Register reconnect callback
35
+ * 16. withTimeout(promise, timeoutMs, options) - Wrap promise with timeout (re-exported from order/utils/timeout.js)
36
+ * 17. _assessFailover() - Internal failover assessment
37
+ * 18. _internal - @internal Internal state (connected flag) — test-only, do not rely on in production
37
38
  * ===============================================================================
38
39
  */
39
40
  import NodeManager from './node_manager.js';
@@ -145,12 +146,28 @@ declare function getNodeStats(): {
145
146
  failureCount: number;
146
147
  lastCheckTime: string | null;
147
148
  lastErrorMessage: string | null;
149
+ drops24h: number;
150
+ dropsPerHour: number;
151
+ flapPenaltyMs: number;
148
152
  }[] | undefined;
153
+ /**
154
+ * The node the transport is currently connected to (null when unknown).
155
+ *
156
+ * Use this — not `nodeManager.getBestNode()` — when attributing a failure to a
157
+ * node: the best node is the lowest-ranked *candidate* and need not be the one
158
+ * that served the request.
159
+ *
160
+ * Deliberately does NOT call ensureInitialized(): this is called from failure
161
+ * and logging paths, and lazily creating the whole client stack just to return
162
+ * null would be worse than reporting null.
163
+ */
164
+ declare function getActiveNodeUrl(): string | null;
149
165
  declare function getNodeSummary(): {
150
166
  monitoring: boolean;
151
167
  counts: Record<string, number>;
152
168
  bestNode: string | null;
153
169
  avgLatency: number | null;
170
+ totalDrops24h: number;
154
171
  } | undefined;
155
172
  declare function getConnectionError(): unknown;
156
173
  /** @internal Exposed for unit tests only — do not rely on in production code. */
@@ -172,11 +189,12 @@ declare const _default: {
172
189
  getNodeManager: typeof getNodeManager;
173
190
  getNodeStats: typeof getNodeStats;
174
191
  getNodeSummary: typeof getNodeSummary;
192
+ getActiveNodeUrl: typeof getActiveNodeUrl;
175
193
  getConnectionError: typeof getConnectionError;
176
194
  _internal: {
177
195
  readonly connected: boolean;
178
196
  };
179
197
  };
180
198
  export default _default;
181
- export { _lazyBitShares as BitShares, createAccountClient, waitForConnected, getConnectionStatus, disconnectClient, reconnectForCycle, setSuppressConnectionLog, isSuppressConnectionLog, onReconnect, withTimeout, assessFailover as _assessFailover, getNodeManager, getNodeStats, getNodeSummary, getConnectionError, _internal };
199
+ export { _lazyBitShares as BitShares, createAccountClient, waitForConnected, getConnectionStatus, disconnectClient, reconnectForCycle, setSuppressConnectionLog, isSuppressConnectionLog, onReconnect, withTimeout, assessFailover as _assessFailover, getNodeManager, getNodeStats, getNodeSummary, getActiveNodeUrl, getConnectionError, _internal };
182
200
  //# sourceMappingURL=bitshares_client.d.ts.map
@@ -29,11 +29,12 @@
29
29
  * 10. getNodeManager() - Get the NodeManager instance
30
30
  * 11. getNodeStats() - Get node health statistics
31
31
  * 12. getNodeSummary() - Get node summary
32
- * 13. getConnectionError() - Get last connection error
33
- * 14. onReconnect(callback) - Register reconnect callback
34
- * 15. withTimeout(promise, timeoutMs, options) - Wrap promise with timeout (re-exported from order/utils/timeout.js)
35
- * 16. _assessFailover() - Internal failover assessment
36
- * 17. _internal - @internal Internal state (connected flag) — test-only, do not rely on in production
32
+ * 13. getActiveNodeUrl() - Node currently in use (for failure attribution)
33
+ * 14. getConnectionError() - Get last connection error
34
+ * 15. onReconnect(callback) - Register reconnect callback
35
+ * 16. withTimeout(promise, timeoutMs, options) - Wrap promise with timeout (re-exported from order/utils/timeout.js)
36
+ * 17. _assessFailover() - Internal failover assessment
37
+ * 18. _internal - @internal Internal state (connected flag) — test-only, do not rely on in production
37
38
  * ===============================================================================
38
39
  */
39
40
  import { TIMING, NODE_MANAGEMENT, NATIVE_CLIENT } from './constants.js';
@@ -330,6 +331,28 @@ async function restartBitsharesConnection(serverList, reason = 'startup') {
330
331
  : [];
331
332
  if (servers.length === 0)
332
333
  return false;
334
+ // Keep an already-healthy socket. The transport's own connect() no-op only
335
+ // applies when its candidate list still contains the active node, and this
336
+ // function used to bypass that by disconnecting first — so every
337
+ // maintenance/refresh cycle tore down a working connection (logs showed
338
+ // 88 such socket cycles in 18 days, each costing a re-login, a
339
+ // re-subscribe and a safety-net sync). If the node we are on is still an
340
+ // acceptable target, re-point the candidate list and keep the socket.
341
+ //
342
+ // Never keep a blacklisted node: that would defeat the post-connect switch
343
+ // in handleConnectionStatus('connected'). Wedged-session recovery does not
344
+ // depend on this path — the transport's forceReconnect (api-id recovery and
345
+ // the fill-channel watchdog) is unaffected by the skip.
346
+ const activeNode = _nativeClient?.transport?.getNodeUrl?.() ?? null;
347
+ const alreadyConnected = _nativeClient?.isConnected?.() ?? false;
348
+ const activeNodeUsable = !!activeNode && !(nodeManager?.isBlacklisted(activeNode) ?? false);
349
+ if (alreadyConnected && activeNode && activeNodeUsable && servers.includes(activeNode)) {
350
+ _nativeClient.setNodes(servers);
351
+ if (!suppressConnectionLog) {
352
+ logger.info(`${reason}: keeping existing connection to ${activeNode}`);
353
+ }
354
+ return true;
355
+ }
333
356
  try {
334
357
  reconnectInProgress = true;
335
358
  connected = false;
@@ -711,6 +734,20 @@ function getNodeStats() {
711
734
  ensureInitialized();
712
735
  return nodeManager?.getStats();
713
736
  }
737
+ /**
738
+ * The node the transport is currently connected to (null when unknown).
739
+ *
740
+ * Use this — not `nodeManager.getBestNode()` — when attributing a failure to a
741
+ * node: the best node is the lowest-ranked *candidate* and need not be the one
742
+ * that served the request.
743
+ *
744
+ * Deliberately does NOT call ensureInitialized(): this is called from failure
745
+ * and logging paths, and lazily creating the whole client stack just to return
746
+ * null would be worse than reporting null.
747
+ */
748
+ function getActiveNodeUrl() {
749
+ return _nativeClient?.transport?.getNodeUrl?.() ?? null;
750
+ }
714
751
  function getNodeSummary() {
715
752
  ensureInitialized();
716
753
  return nodeManager?.getSummary();
@@ -722,7 +759,7 @@ function getConnectionError() {
722
759
  const _internal = {
723
760
  get connected() { return connected; },
724
761
  };
725
- const _default = { BitShares: _lazyBitShares, createAccountClient, waitForConnected, getConnectionStatus, disconnectClient, reconnectForCycle, setSuppressConnectionLog, isSuppressConnectionLog, onReconnect, withTimeout, _assessFailover: assessFailover, getNodeManager, getNodeStats, getNodeSummary, getConnectionError, _internal };
762
+ const _default = { BitShares: _lazyBitShares, createAccountClient, waitForConnected, getConnectionStatus, disconnectClient, reconnectForCycle, setSuppressConnectionLog, isSuppressConnectionLog, onReconnect, withTimeout, _assessFailover: assessFailover, getNodeManager, getNodeStats, getNodeSummary, getActiveNodeUrl, getConnectionError, _internal };
726
763
  export default _default;
727
- export { _lazyBitShares as BitShares, createAccountClient, waitForConnected, getConnectionStatus, disconnectClient, reconnectForCycle, setSuppressConnectionLog, isSuppressConnectionLog, onReconnect, withTimeout, assessFailover as _assessFailover, getNodeManager, getNodeStats, getNodeSummary, getConnectionError, _internal };
764
+ export { _lazyBitShares as BitShares, createAccountClient, waitForConnected, getConnectionStatus, disconnectClient, reconnectForCycle, setSuppressConnectionLog, isSuppressConnectionLog, onReconnect, withTimeout, assessFailover as _assessFailover, getNodeManager, getNodeStats, getNodeSummary, getActiveNodeUrl, getConnectionError, _internal };
728
765
  //# sourceMappingURL=bitshares_client.js.map
@@ -20,6 +20,8 @@ export interface BotEntry extends UnknownRecord {
20
20
  botKey?: string;
21
21
  botIndex?: number;
22
22
  preferredAccount?: string;
23
+ /** Chain account id (1.2.x) stamped next to `preferredAccount` (see analysis/account_resolver.ts). */
24
+ accountId?: string;
23
25
  coreOrders?: UnknownRecord;
24
26
  edgeOrders?: number | UnknownRecord;
25
27
  /** @deprecated legacy key, migrated to `coreOrders` at read time. */
@@ -85,8 +85,11 @@
85
85
  * DEFAULT_NODES: List of BitShares nodes for redundancy
86
86
  * HEALTH_CHECK_INTERVAL_MS, HEALTH_CHECK_TIMEOUT_MS, MAX_PING_MS
87
87
  * BLACKLIST_THRESHOLD: Failures before node is blacklisted
88
+ * FAILURE_STRIKE_DECAY_MS: How long a live-transport strike lasts
89
+ * FLAP_WINDOW_MS / FLAP_HALF_LIFE_MS / FLAP_PENALTY_MS_PER_DROP /
90
+ * FLAP_PENALTY_MAX_MS: time-decayed connection-churn penalty
88
91
  * EXPECTED_CHAIN_ID: BitShares mainnet chain ID validation
89
- * SELECTION_STRATEGY: Node selection algorithm (latency-based)
92
+ * SELECTION_STRATEGY: Node selection algorithm (latency + churn penalty)
90
93
  *
91
94
  * 13. UPDATER - Version checking and update notification
92
95
  * ACTIVE, REPOSITORY_URL, BRANCH, SCHEDULE
@@ -224,6 +227,12 @@ declare let TIMING: {
224
227
  DUST_HEALTH_CHECK_INTERVAL_MS: number;
225
228
  DUST_CANCEL_TIMEOUT_MS: number;
226
229
  BROADCAST_STALE_CLEAR_MS: number;
230
+ PENDING_BROADCAST_TTL_MS: number;
231
+ BATCH_INFLIGHT_STALE_MS: number;
232
+ RECOVERY_INFLIGHT_STALE_MS: number;
233
+ PLACEMENT_STALE_CLEAR_MS: number;
234
+ CHAIN_SIZE_APPLIED_MARKER_TTL_MS: number;
235
+ STRUCTURAL_REBUILD_DEFER_MAX_MS: number;
227
236
  BROADCAST_DEFER_SAFETY_MARGIN_MS: number;
228
237
  FILL_BROADCAST_DEFER_MAX_MS: number;
229
238
  FILL_TOTALS_RETRY_BASE_MS: number;
@@ -369,7 +378,11 @@ declare let NODE_MANAGEMENT: {
369
378
  BLACKLIST_THRESHOLD: number;
370
379
  BLACKLIST_COOLDOWN_MS: number;
371
380
  FAILURE_REPORT_COOLDOWN_MS: number;
372
- LIVE_FAILURE_HEALTH_SUCCESS_STREAK: number;
381
+ FAILURE_STRIKE_DECAY_MS: number;
382
+ FLAP_WINDOW_MS: number;
383
+ FLAP_HALF_LIFE_MS: number;
384
+ FLAP_PENALTY_MS_PER_DROP: number;
385
+ FLAP_PENALTY_MAX_MS: number;
373
386
  EXPECTED_CHAIN_ID: string;
374
387
  SELECTION_STRATEGY: string;
375
388
  };
@@ -770,6 +783,12 @@ declare function buildDefaultGeneralSettings(): {
770
783
  DUST_HEALTH_CHECK_INTERVAL_MS: number;
771
784
  DUST_CANCEL_TIMEOUT_MS: number;
772
785
  BROADCAST_STALE_CLEAR_MS: number;
786
+ PENDING_BROADCAST_TTL_MS: number;
787
+ BATCH_INFLIGHT_STALE_MS: number;
788
+ RECOVERY_INFLIGHT_STALE_MS: number;
789
+ PLACEMENT_STALE_CLEAR_MS: number;
790
+ CHAIN_SIZE_APPLIED_MARKER_TTL_MS: number;
791
+ STRUCTURAL_REBUILD_DEFER_MAX_MS: number;
773
792
  BROADCAST_DEFER_SAFETY_MARGIN_MS: number;
774
793
  FILL_BROADCAST_DEFER_MAX_MS: number;
775
794
  FILL_TOTALS_RETRY_BASE_MS: number;
@@ -980,7 +999,11 @@ declare function buildDefaultGeneralSettings(): {
980
999
  BLACKLIST_THRESHOLD: number;
981
1000
  BLACKLIST_COOLDOWN_MS: number;
982
1001
  FAILURE_REPORT_COOLDOWN_MS: number;
983
- LIVE_FAILURE_HEALTH_SUCCESS_STREAK: number;
1002
+ FAILURE_STRIKE_DECAY_MS: number;
1003
+ FLAP_WINDOW_MS: number;
1004
+ FLAP_HALF_LIFE_MS: number;
1005
+ FLAP_PENALTY_MS_PER_DROP: number;
1006
+ FLAP_PENALTY_MAX_MS: number;
984
1007
  EXPECTED_CHAIN_ID: string;
985
1008
  SELECTION_STRATEGY: string;
986
1009
  };