dexbot 1.6.18 → 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 (146) hide show
  1. package/CHANGELOG.md +68 -0
  2. package/README.md +24 -18
  3. package/analysis/README.md +117 -5
  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_dynamic_weight.js +2 -1
  9. package/dist/analysis/analyze_kalman.js +3 -2
  10. package/dist/analysis/analyze_liquidity.d.ts +17 -0
  11. package/dist/analysis/analyze_liquidity.js +336 -0
  12. package/dist/analysis/analyze_regime.js +3 -2
  13. package/dist/analysis/analyze_regime_windows.js +3 -2
  14. package/dist/analysis/analyze_volatility.js +3 -2
  15. package/dist/analysis/bot_key_utils.d.ts +44 -1
  16. package/dist/analysis/bot_key_utils.js +76 -7
  17. package/dist/analysis/fills_cache.js +2 -1
  18. package/dist/analysis/grid_correction_check.d.ts +1 -19
  19. package/dist/analysis/grid_correction_check.js +12 -30
  20. package/dist/analysis/liquidity/liquidity_chain.d.ts +66 -0
  21. package/dist/analysis/liquidity/liquidity_chain.js +178 -0
  22. package/dist/analysis/liquidity/liquidity_chart_generator.d.ts +19 -0
  23. package/dist/analysis/liquidity/liquidity_chart_generator.js +693 -0
  24. package/dist/analysis/liquidity/liquidity_depth_model.d.ts +166 -0
  25. package/dist/analysis/liquidity/liquidity_depth_model.js +258 -0
  26. package/dist/analysis/liquidity/liquidity_view.d.ts +53 -0
  27. package/dist/analysis/liquidity/liquidity_view.js +66 -0
  28. package/dist/analysis/resolve_bot_accounts.js +7 -6
  29. package/dist/analysis/trade_profitability.js +4 -2
  30. package/dist/analysis/tradingview/analyze_tradingview.js +2 -1
  31. package/dist/credential-daemon.js +13 -2
  32. package/dist/dexbot.js +77 -172
  33. package/dist/modules/account_bots.d.ts +11 -1
  34. package/dist/modules/account_orders.d.ts +12 -0
  35. package/dist/modules/account_orders.js +30 -0
  36. package/dist/modules/asset_lookup.d.ts +25 -0
  37. package/dist/modules/asset_lookup.js +75 -0
  38. package/dist/modules/bitshares-native/transport.js +24 -6
  39. package/dist/modules/bitshares_client.d.ts +24 -6
  40. package/dist/modules/bitshares_client.js +44 -7
  41. package/dist/modules/bot_settings.d.ts +2 -0
  42. package/dist/modules/chain_scan.d.ts +64 -0
  43. package/dist/modules/chain_scan.js +80 -0
  44. package/dist/modules/cli_colors.d.ts +5 -0
  45. package/dist/modules/cli_colors.js +7 -0
  46. package/dist/modules/cli_progress.d.ts +51 -0
  47. package/dist/modules/cli_progress.js +113 -0
  48. package/dist/modules/constants.d.ts +26 -3
  49. package/dist/modules/constants.js +63 -8
  50. package/dist/modules/dexbot_class.d.ts +1 -6
  51. package/dist/modules/dexbot_class.js +14 -0
  52. package/dist/modules/dexbot_cow_runtime.d.ts +97 -7
  53. package/dist/modules/dexbot_cow_runtime.js +634 -136
  54. package/dist/modules/dexbot_fill_runtime.js +132 -23
  55. package/dist/modules/dexbot_maintenance_runtime.js +242 -74
  56. package/dist/modules/dexbot_startup_runtime.js +32 -20
  57. package/dist/modules/dexbot_state_recovery.d.ts +33 -1
  58. package/dist/modules/dexbot_state_recovery.js +167 -5
  59. package/dist/modules/launcher/runtime_entry.d.ts +21 -1
  60. package/dist/modules/launcher/runtime_entry.js +36 -1
  61. package/dist/modules/liquidity_pool_ref.d.ts +32 -0
  62. package/dist/modules/liquidity_pool_ref.js +116 -0
  63. package/dist/modules/node_connect_policy.d.ts +8 -1
  64. package/dist/modules/node_connect_policy.js +18 -2
  65. package/dist/modules/node_flap_score.d.ts +87 -0
  66. package/dist/modules/node_flap_score.js +124 -0
  67. package/dist/modules/node_health_cache.d.ts +40 -3
  68. package/dist/modules/node_health_cache.js +66 -20
  69. package/dist/modules/node_manager.d.ts +101 -19
  70. package/dist/modules/node_manager.js +255 -65
  71. package/dist/modules/order/accounting.js +56 -7
  72. package/dist/modules/order/format.d.ts +24 -10
  73. package/dist/modules/order/format.js +30 -10
  74. package/dist/modules/order/grid.js +91 -13
  75. package/dist/modules/order/grid_reconcile.d.ts +5 -1
  76. package/dist/modules/order/grid_reconcile.js +333 -189
  77. package/dist/modules/order/grid_reconcile_internal.d.ts +38 -2
  78. package/dist/modules/order/grid_reconcile_internal.js +298 -100
  79. package/dist/modules/order/index.d.ts +19 -1
  80. package/dist/modules/order/lifecycle.d.ts +353 -0
  81. package/dist/modules/order/lifecycle.js +1109 -0
  82. package/dist/modules/order/manager.d.ts +273 -5
  83. package/dist/modules/order/manager.js +654 -48
  84. package/dist/modules/order/placement_lock.d.ts +102 -0
  85. package/dist/modules/order/placement_lock.js +222 -0
  86. package/dist/modules/order/strategy.js +13 -1
  87. package/dist/modules/order/sync_engine.d.ts +33 -1
  88. package/dist/modules/order/sync_engine.js +241 -73
  89. package/dist/modules/order/utils/math.d.ts +39 -1
  90. package/dist/modules/order/utils/math.js +83 -1
  91. package/dist/modules/order/utils/order.d.ts +181 -5
  92. package/dist/modules/order/utils/order.js +455 -9
  93. package/dist/modules/order/utils/system.d.ts +81 -1
  94. package/dist/modules/order/utils/system.js +331 -44
  95. package/dist/modules/order/utils/validate.d.ts +7 -1
  96. package/dist/modules/order/utils/validate.js +80 -3
  97. package/dist/modules/outcome.d.ts +54 -0
  98. package/dist/modules/outcome.js +51 -0
  99. package/dist/modules/settings_merge.d.ts +4 -0
  100. package/dist/modules/settings_merge.js +9 -0
  101. package/dist/modules/timing_policy.d.ts +82 -0
  102. package/dist/modules/timing_policy.js +244 -0
  103. package/dist/modules/types.d.ts +137 -0
  104. package/dist/modules/utils/asset_symbols.d.ts +13 -2
  105. package/dist/modules/utils/asset_symbols.js +21 -2
  106. package/dist/scripts/analysis_runner.d.ts +10 -0
  107. package/dist/scripts/analysis_runner.js +33 -0
  108. package/dist/scripts/analyze-credit.js +54 -17
  109. package/dist/scripts/analyze-orders.d.ts +4 -2
  110. package/dist/scripts/analyze-orders.js +115 -35
  111. package/dist/scripts/asset.d.ts +26 -0
  112. package/dist/scripts/asset.js +165 -0
  113. package/dist/scripts/bts_distribution.d.ts +1 -0
  114. package/dist/scripts/bts_distribution.js +194 -142
  115. package/dist/scripts/chart_command.d.ts +1 -1
  116. package/dist/scripts/chart_command.js +60 -86
  117. package/dist/scripts/chart_targets.d.ts +18 -0
  118. package/dist/scripts/chart_targets.js +48 -0
  119. package/dist/scripts/dw.js +1 -1
  120. package/dist/scripts/liquid.d.ts +24 -0
  121. package/dist/scripts/liquid.js +134 -0
  122. package/dist/scripts/rich_list.js +12 -16
  123. package/dist/scripts/test-credit-renewal.js +12 -5
  124. package/dist/scripts/token_distribution.d.ts +2 -0
  125. package/dist/scripts/token_distribution.js +132 -108
  126. package/dist/scripts/tv.js +1 -1
  127. package/dist/scripts/verify-browser-bundle.js +18 -0
  128. package/docs/COW_INVARIANTS.md +69 -10
  129. package/docs/DEXBOT_COMPARISON.md +6 -6
  130. package/docs/EVOLUTION.md +10 -8
  131. package/docs/FUND_MOVEMENT_AND_ACCOUNTING.md +1 -1
  132. package/docs/GRID_PRICE_INVARIANT.md +5 -2
  133. package/docs/GRID_RECONCILE.md +36 -11
  134. package/docs/LOGGING.md +50 -4
  135. package/docs/ORDER_ENGINE_POST_1.0_RETROSPECTIVE.md +177 -27
  136. package/docs/README.md +23 -2
  137. package/docs/WORKFLOW.md +12 -4
  138. package/docs/architecture.md +42 -5
  139. package/docs/developer_guide.md +25 -8
  140. package/modules/README.md +8 -1
  141. package/package.json +3 -1
  142. package/scripts/README.md +39 -7
  143. package/scripts/pdev.sh +14 -1
  144. package/scripts/pmain.sh +23 -8
  145. package/scripts/ptest.sh +7 -0
  146. package/tests/README.md +30 -1
@@ -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. */
@@ -0,0 +1,64 @@
1
+ /**
2
+ * CHAIN OBJECT-ID SCAN
3
+ *
4
+ * Some object types have no list API: legacy 1.15.x balance objects and 1.13.x
5
+ * vesting balances can only be enumerated by probing ids with `get_objects`,
6
+ * which answers `null` for holes. Ids are allocated monotonically and never
7
+ * reused, so a range is "dense-ish" — walking from 0 and stopping after `gap`
8
+ * consecutive misses finds every live object without knowing where the range
9
+ * ends.
10
+ *
11
+ * Two details keep that affordable on every report:
12
+ * - one large `get_objects` batch per round trip (1000 ids measurably beats
13
+ * 100 by ~4x because the per-call overhead dominates), with an automatic
14
+ * halving fallback for nodes that refuse big batches, and
15
+ * - no filtering here: callers filter the objects they need (e.g. by
16
+ * `balance.asset_id`), because the node exposes no per-asset index for
17
+ * these types.
18
+ *
19
+ * The module is pure (the reader is injected), so it stays browser-safe.
20
+ */
21
+ /** Ids per `get_objects` call; all public nodes accept this batch size. */
22
+ declare const DEFAULT_SCAN_BATCH = 1000;
23
+ /** Never fall below this when backing off, so a refusal cannot loop forever. */
24
+ declare const MIN_SCAN_BATCH = 100;
25
+ /**
26
+ * Trailing miss budget for the dense implementation spaces (2.3.x, 2.4.x, 1.3.x,
27
+ * 1.16.x): one object per asset / smart asset, ids never reused, holes measured
28
+ * in single digits. The legacy spaces (1.13.x vesting, 1.15.x balances) have
29
+ * real holes and keep the larger `--gap` default.
30
+ */
31
+ declare const DENSE_SPACE_GAP = 2000;
32
+ interface ChainScanOptions {
33
+ /** Database reader; `read('get_objects', [ids])` must resolve to `(T|null)[]`. */
34
+ read: (method: string, args: unknown[]) => Promise<unknown>;
35
+ /** Object id prefix, e.g. `'1.15'`. */
36
+ prefix: string;
37
+ /** Highest id to probe (the walk always starts at 0). */
38
+ maxId: number;
39
+ /** Stop after this many consecutive missing ids. */
40
+ gap: number;
41
+ /**
42
+ * Ids per call (default 1000). An explicit smaller value is honoured as-is;
43
+ * a node error halves the batch down to `MIN_SCAN_BATCH`.
44
+ */
45
+ batchSize?: number;
46
+ /** Called after each batch with the number of ids walked so far. */
47
+ onProgress?: (scanned: number) => void;
48
+ }
49
+ interface ChainScanResult<T> {
50
+ objects: T[];
51
+ /** Number of ids walked (exclusive upper bound of the scanned range). */
52
+ scannedTo: number;
53
+ /** True when the walk stopped at `maxId`, so the range may be truncated. */
54
+ hitLimit: boolean;
55
+ }
56
+ /**
57
+ * Walk `prefix.0 … prefix.maxId` in batches and collect every live object.
58
+ *
59
+ * @throws when `get_objects` keeps failing even at `MIN_SCAN_BATCH`.
60
+ */
61
+ declare function scanIdRange<T>(options: ChainScanOptions): Promise<ChainScanResult<T>>;
62
+ export { scanIdRange, DEFAULT_SCAN_BATCH, DENSE_SPACE_GAP, MIN_SCAN_BATCH };
63
+ export type { ChainScanOptions, ChainScanResult };
64
+ //# sourceMappingURL=chain_scan.d.ts.map
@@ -0,0 +1,80 @@
1
+ 'use strict';
2
+ /**
3
+ * CHAIN OBJECT-ID SCAN
4
+ *
5
+ * Some object types have no list API: legacy 1.15.x balance objects and 1.13.x
6
+ * vesting balances can only be enumerated by probing ids with `get_objects`,
7
+ * which answers `null` for holes. Ids are allocated monotonically and never
8
+ * reused, so a range is "dense-ish" — walking from 0 and stopping after `gap`
9
+ * consecutive misses finds every live object without knowing where the range
10
+ * ends.
11
+ *
12
+ * Two details keep that affordable on every report:
13
+ * - one large `get_objects` batch per round trip (1000 ids measurably beats
14
+ * 100 by ~4x because the per-call overhead dominates), with an automatic
15
+ * halving fallback for nodes that refuse big batches, and
16
+ * - no filtering here: callers filter the objects they need (e.g. by
17
+ * `balance.asset_id`), because the node exposes no per-asset index for
18
+ * these types.
19
+ *
20
+ * The module is pure (the reader is injected), so it stays browser-safe.
21
+ */
22
+ /** Ids per `get_objects` call; all public nodes accept this batch size. */
23
+ const DEFAULT_SCAN_BATCH = 1000;
24
+ /** Never fall below this when backing off, so a refusal cannot loop forever. */
25
+ const MIN_SCAN_BATCH = 100;
26
+ /**
27
+ * Trailing miss budget for the dense implementation spaces (2.3.x, 2.4.x, 1.3.x,
28
+ * 1.16.x): one object per asset / smart asset, ids never reused, holes measured
29
+ * in single digits. The legacy spaces (1.13.x vesting, 1.15.x balances) have
30
+ * real holes and keep the larger `--gap` default.
31
+ */
32
+ const DENSE_SPACE_GAP = 2_000;
33
+ /**
34
+ * Walk `prefix.0 … prefix.maxId` in batches and collect every live object.
35
+ *
36
+ * @throws when `get_objects` keeps failing even at `MIN_SCAN_BATCH`.
37
+ */
38
+ async function scanIdRange(options) {
39
+ const { read, prefix, maxId, gap, onProgress } = options;
40
+ let batch = Math.max(1, Math.floor(options.batchSize ?? DEFAULT_SCAN_BATCH));
41
+ const objects = [];
42
+ let index = 0;
43
+ let lastSeen = -1;
44
+ // `gap` means exactly "gap consecutive missing ids": after the last live
45
+ // object only ids up to lastSeen+gap are worth probing. Clipping the batch
46
+ // to that bound makes the walk independent of the batch size — without it a
47
+ // larger batch probes (and reports) further than the documented --gap.
48
+ while (index <= maxId) {
49
+ const limit = Math.min(maxId, lastSeen + gap);
50
+ if (index > limit)
51
+ break;
52
+ const count = Math.min(batch, limit - index + 1);
53
+ const ids = Array.from({ length: count }, (_, i) => `${prefix}.${index + i}`);
54
+ let page;
55
+ try {
56
+ page = await read('get_objects', [ids]);
57
+ }
58
+ catch (err) {
59
+ // A node may cap the batch size; retry the same window smaller
60
+ // instead of failing the whole report.
61
+ if (batch > MIN_SCAN_BATCH) {
62
+ batch = Math.max(MIN_SCAN_BATCH, Math.floor(batch / 2));
63
+ continue;
64
+ }
65
+ throw err;
66
+ }
67
+ for (let j = 0; j < count; j++) {
68
+ const obj = page?.[j];
69
+ if (obj) {
70
+ lastSeen = index + j;
71
+ objects.push(obj);
72
+ }
73
+ }
74
+ index += count;
75
+ onProgress?.(index);
76
+ }
77
+ return { objects, scannedTo: index, hitLimit: index > maxId };
78
+ }
79
+ export { scanIdRange, DEFAULT_SCAN_BATCH, DENSE_SPACE_GAP, MIN_SCAN_BATCH };
80
+ //# sourceMappingURL=chain_scan.js.map
@@ -35,4 +35,9 @@ export declare const CLI_COLORS: {
35
35
  readonly silver: "\u001B[38;5;250m";
36
36
  readonly greenBold: "\u001B[1;32m";
37
37
  };
38
+ /**
39
+ * Wrap text in one of the {@link CLI_COLORS} codes and reset afterwards —
40
+ * the `c(colors.x, text)` helper each CLI used to declare locally.
41
+ */
42
+ export declare function colorize(code: string, text: string): string;
38
43
  //# sourceMappingURL=cli_colors.d.ts.map
@@ -39,4 +39,11 @@ export const CLI_COLORS = {
39
39
  silver: '\x1b[38;5;250m', // light silver grey (bots editor)
40
40
  greenBold: '\x1b[1;32m', // bold green (bots editor)
41
41
  };
42
+ /**
43
+ * Wrap text in one of the {@link CLI_COLORS} codes and reset afterwards —
44
+ * the `c(colors.x, text)` helper each CLI used to declare locally.
45
+ */
46
+ export function colorize(code, text) {
47
+ return `${code}${text}${CLI_COLORS.reset}`;
48
+ }
42
49
  //# sourceMappingURL=cli_colors.js.map
@@ -0,0 +1,51 @@
1
+ /** Console tags for the read-only analysis commands. */
2
+ export declare const CLI_TAGS: {
3
+ readonly assets: "[Assets]";
4
+ readonly liquidity: "[Liquidity]";
5
+ readonly tradingView: "[TradingView]";
6
+ readonly dynamicWeight: "[DynamicWeight]";
7
+ readonly pnl: "[PnL]";
8
+ readonly orders: "[Order]";
9
+ readonly volatility: "[Volatility]";
10
+ readonly regime: "[Regime]";
11
+ readonly regimeWindows: "[RegimeWindows]";
12
+ readonly kalman: "[Kalman]";
13
+ readonly derivatives: "[Derivatives]";
14
+ };
15
+ /**
16
+ * Write (or overwrite) a transient `[Tag] message` progress line.
17
+ * Silenced when `enabled` is false, when stderr is not a TTY, or in a browser.
18
+ */
19
+ export declare function progressLine(tag: string, enabled: boolean, message: string): void;
20
+ /** Erase the transient progress line before the report is printed. */
21
+ export declare function clearProgressLine(enabled?: boolean): void;
22
+ /**
23
+ * Bind {@link progressLine} to one tag, so a script keeps its short
24
+ * `progress(enabled, message)` call sites while the format stays centralized.
25
+ */
26
+ export declare function taggedProgress(tag: string): (enabled: boolean, message: string) => void;
27
+ /**
28
+ * Trailer printed after a report is written, e.g.
29
+ * `[Assets] ✓ Report saved. Open report: (file:///…/bts-distribution.html)`.
30
+ * The caller supplies the file URL (analysis/chart_utils.toFileUrl); `kind`
31
+ * overrides the lowercased label in the "Open …" half (`'PnL report'` →
32
+ * `'report'`).
33
+ */
34
+ export declare function savedLine(tag: string, label: string, fileUrl: string, kind?: string): string;
35
+ /** Persistent `[Tag] message` line, e.g. `[Assets] Loading … from chain...`. */
36
+ export declare function loadingLine(tag: string, message: string): string;
37
+ /**
38
+ * Report section header block (leading blank line included):
39
+ * `\n ── Title ─────────────────────────────…`, bold amber.
40
+ */
41
+ export declare function section(title: string, width?: number): string;
42
+ /** Warning bullets (` • text`, red), one per entry. */
43
+ export declare function warningLines(warnings: readonly string[]): string[];
44
+ /**
45
+ * A section header plus its warning bullets, or '' when there is nothing to
46
+ * report. Every report command prints warnings the same way through this.
47
+ */
48
+ export declare function warningsBlock(warnings: readonly string[], width?: number): string;
49
+ /** Muted trailing summary line, printed just above the saved-file link. */
50
+ export declare function summaryLine(text: string): string;
51
+ //# sourceMappingURL=cli_progress.d.ts.map
@@ -0,0 +1,113 @@
1
+ 'use strict';
2
+ /**
3
+ * modules/cli_progress.ts - Shared terminal status style for CLI commands.
4
+ *
5
+ * One place for the `[Tag] …` line style used by the analysis commands
6
+ * (`dexbot liq`, the chart exporters, the supply distribution analyzers):
7
+ *
8
+ * [Tag] Loading … caller's console.log (persistent)
9
+ * [Tag] scanning 1.13.x … progressLine() - transient, stderr, TTY only
10
+ * [Tag] ✓ Report saved … savedLine() (persistent, clickable file:// link)
11
+ *
12
+ * Keeping the tag, the clear-line escape and the trailer wording here is what
13
+ * keeps `dexbot liq`, `dexbot tv`, `dexbot asset` … visually identical instead
14
+ * of each script re-implementing the same three things.
15
+ *
16
+ * Browser-safe: no Node imports. The stderr access is guarded through
17
+ * modules/env.ts, so the module can be imported from browser-reachable code
18
+ * without touching `process`.
19
+ */
20
+ import { hasProcess } from './env.js';
21
+ import { CLI_COLORS, colorize } from './cli_colors.js';
22
+ /** Erase the current terminal line (progress updates overwrite in place). */
23
+ const CLEAR_LINE = '\r\x1b[2K';
24
+ /** Console tags for the read-only analysis commands. */
25
+ export const CLI_TAGS = {
26
+ assets: '[Assets]',
27
+ liquidity: '[Liquidity]',
28
+ tradingView: '[TradingView]',
29
+ dynamicWeight: '[DynamicWeight]',
30
+ pnl: '[PnL]',
31
+ orders: '[Order]',
32
+ volatility: '[Volatility]',
33
+ regime: '[Regime]',
34
+ regimeWindows: '[RegimeWindows]',
35
+ kalman: '[Kalman]',
36
+ derivatives: '[Derivatives]',
37
+ };
38
+ /** Width of the report section headers (`── Title ──…`). */
39
+ const SECTION_WIDTH = 70;
40
+ /** stderr when it is an interactive terminal, otherwise null (no-op logging). */
41
+ function progressStream() {
42
+ if (!hasProcess())
43
+ return null;
44
+ const stream = globalThis.process?.stderr;
45
+ return stream && stream.isTTY ? stream : null;
46
+ }
47
+ /**
48
+ * Write (or overwrite) a transient `[Tag] message` progress line.
49
+ * Silenced when `enabled` is false, when stderr is not a TTY, or in a browser.
50
+ */
51
+ export function progressLine(tag, enabled, message) {
52
+ if (!enabled)
53
+ return;
54
+ const stream = progressStream();
55
+ if (!stream)
56
+ return;
57
+ stream.write(`${CLEAR_LINE}${tag} ${message}`);
58
+ }
59
+ /** Erase the transient progress line before the report is printed. */
60
+ export function clearProgressLine(enabled = true) {
61
+ if (!enabled)
62
+ return;
63
+ const stream = progressStream();
64
+ if (stream)
65
+ stream.write(CLEAR_LINE);
66
+ }
67
+ /**
68
+ * Bind {@link progressLine} to one tag, so a script keeps its short
69
+ * `progress(enabled, message)` call sites while the format stays centralized.
70
+ */
71
+ export function taggedProgress(tag) {
72
+ return (enabled, message) => progressLine(tag, enabled, message);
73
+ }
74
+ /**
75
+ * Trailer printed after a report is written, e.g.
76
+ * `[Assets] ✓ Report saved. Open report: (file:///…/bts-distribution.html)`.
77
+ * The caller supplies the file URL (analysis/chart_utils.toFileUrl); `kind`
78
+ * overrides the lowercased label in the "Open …" half (`'PnL report'` →
79
+ * `'report'`).
80
+ */
81
+ export function savedLine(tag, label, fileUrl, kind) {
82
+ return `\n${tag} ✓ ${label} saved. Open ${kind ?? label.toLowerCase()}: (${fileUrl})`;
83
+ }
84
+ /** Persistent `[Tag] message` line, e.g. `[Assets] Loading … from chain...`. */
85
+ export function loadingLine(tag, message) {
86
+ return `${tag} ${message}`;
87
+ }
88
+ /**
89
+ * Report section header block (leading blank line included):
90
+ * `\n ── Title ─────────────────────────────…`, bold amber.
91
+ */
92
+ export function section(title, width = SECTION_WIDTH) {
93
+ const rule = '─'.repeat(Math.max(0, width - title.length));
94
+ return `\n${colorize(CLI_COLORS.bold + CLI_COLORS.yellow, ` ── ${title} ${rule}`)}`;
95
+ }
96
+ /** Warning bullets (` • text`, red), one per entry. */
97
+ export function warningLines(warnings) {
98
+ return warnings.map((text) => colorize(CLI_COLORS.sell, ` • ${text}`));
99
+ }
100
+ /**
101
+ * A section header plus its warning bullets, or '' when there is nothing to
102
+ * report. Every report command prints warnings the same way through this.
103
+ */
104
+ export function warningsBlock(warnings, width = SECTION_WIDTH) {
105
+ if (warnings.length === 0)
106
+ return '';
107
+ return [section('Warnings', width), ...warningLines(warnings)].join('\n');
108
+ }
109
+ /** Muted trailing summary line, printed just above the saved-file link. */
110
+ export function summaryLine(text) {
111
+ return colorize(CLI_COLORS.gray, ` ${text}`);
112
+ }
113
+ //# sourceMappingURL=cli_progress.js.map
@@ -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
  };
@@ -86,8 +86,11 @@
86
86
  * DEFAULT_NODES: List of BitShares nodes for redundancy
87
87
  * HEALTH_CHECK_INTERVAL_MS, HEALTH_CHECK_TIMEOUT_MS, MAX_PING_MS
88
88
  * BLACKLIST_THRESHOLD: Failures before node is blacklisted
89
+ * FAILURE_STRIKE_DECAY_MS: How long a live-transport strike lasts
90
+ * FLAP_WINDOW_MS / FLAP_HALF_LIFE_MS / FLAP_PENALTY_MS_PER_DROP /
91
+ * FLAP_PENALTY_MAX_MS: time-decayed connection-churn penalty
89
92
  * EXPECTED_CHAIN_ID: BitShares mainnet chain ID validation
90
- * SELECTION_STRATEGY: Node selection algorithm (latency-based)
93
+ * SELECTION_STRATEGY: Node selection algorithm (latency + churn penalty)
91
94
  *
92
95
  * 13. UPDATER - Version checking and update notification
93
96
  * ACTIVE, REPOSITORY_URL, BRANCH, SCHEDULE
@@ -139,6 +142,7 @@ import { BUILD_DIR } from './utils/build_dir.js';
139
142
  import { readGeneralSettings } from './general_settings.js';
140
143
  import { mergeSettings, buildNodesView } from './settings_merge.js';
141
144
  import { getErrorMessage } from './utils/errors.js';
145
+ import { DEFAULT_FLAP_HALF_LIFE_MS, DEFAULT_FLAP_PENALTY_MAX_MS, DEFAULT_FLAP_PENALTY_MS_PER_DROP, DEFAULT_FLAP_WINDOW_MS, } from './node_flap_score.js';
142
146
  const ORDER_TYPES = Object.freeze({
143
147
  SELL: 'sell',
144
148
  BUY: 'buy',
@@ -300,6 +304,42 @@ let TIMING = {
300
304
  // bound (FILL_BROADCAST_DEFER_MAX_MS, derived below) are expressed in
301
305
  // terms of it, so they cannot be tuned out of agreement.
302
306
  BROADCAST_STALE_CLEAR_MS: 120000, // 120 seconds
307
+ // PENDING_BROADCAST_TTL_MS: a CREATE recorded as pending (uncertain
308
+ // broadcast) protects its slot/price from a duplicate CREATE. The entry is
309
+ // normally cleared when the batch resolves; if that resolution is lost
310
+ // (crash, hung poll) a stale entry must not block creates forever. After
311
+ // this TTL it is dropped and the next caught-up read re-verifies the level
312
+ // against the chain instead.
313
+ PENDING_BROADCAST_TTL_MS: 30 * 60 * 1000, // 30 minutes
314
+ // BATCH_INFLIGHT_STALE_MS / RECOVERY_INFLIGHT_STALE_MS: leak watchdogs for
315
+ // the bot-level pipeline counters. A parked fill queue for minutes with no
316
+ // progress (2026-11-03: _batchInFlight stuck ~2.5 min, queue depth 10) must
317
+ // be surfaced and, when nothing actually holds placement, cleared so the
318
+ // pipeline can recover. Generous: a legitimate create-group reconcile can
319
+ // take minutes. Never force-clear while placement is still held (a live
320
+ // batch owns it).
321
+ BATCH_INFLIGHT_STALE_MS: 10 * 60 * 1000,
322
+ RECOVERY_INFLIGHT_STALE_MS: 10 * 60 * 1000,
323
+ // PLACEMENT_STALE_CLEAR_MS: a placement lease (COW / reconcile / drain)
324
+ // has no natural deadline — a hung chain call or a missed release can hold
325
+ // the exclusive region forever. Fills and rebalances defer while it is
326
+ // held, so the watchdog force-releases a region held far longer than any
327
+ // legitimate path. Sized above the longest observed reconcile (~8 min) and
328
+ // the COW batch ceiling; unlike the broadcast refcount there is no
329
+ // "re-arm on progress" signal, so err generous.
330
+ PLACEMENT_STALE_CLEAR_MS: 15 * 60 * 1000,
331
+ // CHAIN_SIZE_APPLIED_MARKER_TTL_MS: a sync-pass-1 "size already applied"
332
+ // marker is only meaningful until the matching fill history arrives. Stale
333
+ // markers are ignored/dropped so they cannot suppress an unrelated later
334
+ // fill of the same order.
335
+ CHAIN_SIZE_APPLIED_MARKER_TTL_MS: 5 * 60 * 1000,
336
+ // STRUCTURAL_REBUILD_DEFER_MAX_MS: fill-driven rebalances defer while a
337
+ // structural rebuild runs. A hung rebuild (e.g. a 10-minute reconcile) must
338
+ // not defer them silently for its whole duration: after this bound, log at
339
+ // warn every tick so the stall is visible to operators. The deferral
340
+ // itself is kept (correctness beats availability: planning mid-rebuild is
341
+ // exactly the duplicate class), but it is no longer silent.
342
+ STRUCTURAL_REBUILD_DEFER_MAX_MS: 3 * 60 * 1000,
303
343
  // BROADCAST_DEFER_SAFETY_MARGIN_MS: headroom added to
304
344
  // BROADCAST_STALE_CLEAR_MS when deriving FILL_BROADCAST_DEFER_MAX_MS, so a
305
345
  // region that is still considered live by the stale-clear watchdog cannot
@@ -1010,15 +1050,30 @@ let NODE_MANAGEMENT = {
1010
1050
  BLACKLIST_THRESHOLD: 3, // Failures before blacklist
1011
1051
  BLACKLIST_COOLDOWN_MS: 24 * 60 * 60 * 1000, // 24 hours before retrying blacklisted nodes
1012
1052
  FAILURE_REPORT_COOLDOWN_MS: 1000, // Min ms between failure count increments (prevents rapid-fire blacklisting)
1013
- // Consecutive successful health probes required before a node whose last
1014
- // failure came from the live transport (not a health check) gets its
1015
- // failure ledger reset. Prevents a single "is it up?" probe from erasing
1016
- // live-transport strikes, so flapping nodes still reach BLACKLIST_THRESHOLD.
1017
- LIVE_FAILURE_HEALTH_SUCCESS_STREAK: 2,
1053
+ // Strike decay: a live-transport failure counts as a strike, and strikes
1054
+ // fade out over this window instead of being erased by a successful health
1055
+ // probe. With a 4h probe interval and a once-a-day flapper, the old
1056
+ // success-streak reset wiped every strike before it could reach
1057
+ // BLACKLIST_THRESHOLD (every failure logged "attempt 1/3"), so a node that
1058
+ // drops its socket daily was selected forever. A strike is fully forgiven
1059
+ // only after this long without a new failure.
1060
+ FAILURE_STRIKE_DECAY_MS: 24 * 60 * 60 * 1000, // 24 hours
1061
+ // Flapping (connection-churn) scoring used by node selection: a node ranks
1062
+ // on `latency + penalty`, where the penalty is the time-decayed weight of
1063
+ // its recent transport drops. Zero recent drops means ping alone decides.
1064
+ // Values come from modules/node_flap_score.ts so the ranking maths and the
1065
+ // defaults here can never drift apart.
1066
+ FLAP_WINDOW_MS: DEFAULT_FLAP_WINDOW_MS, // only the trailing day counts
1067
+ FLAP_HALF_LIFE_MS: DEFAULT_FLAP_HALF_LIFE_MS, // a drop's weight halves every 12h
1068
+ FLAP_PENALTY_MS_PER_DROP: DEFAULT_FLAP_PENALTY_MS_PER_DROP, // +5s effective latency per fresh drop
1069
+ FLAP_PENALTY_MAX_MS: DEFAULT_FLAP_PENALTY_MAX_MS, // cap so a degraded node stays a last resort
1018
1070
  // Expected chain ID (BitShares mainnet)
1019
1071
  EXPECTED_CHAIN_ID: '4018d7844c78f6a6c41c6a552b898022310fc5dec06da467ee7905a8dad512c8',
1020
- // Selection strategy
1021
- SELECTION_STRATEGY: 'latency' // latency-based selection
1072
+ // Selection strategy. 'latency' means latency + the time-decayed flap
1073
+ // penalty (see FLAP_* above): the fastest node wins only while it is also
1074
+ // stable, which is what stops a cheap Cloudflare endpoint that drops every
1075
+ // few hours from outranking a slightly slower endpoint that never drops.
1076
+ SELECTION_STRATEGY: 'latency'
1022
1077
  };
1023
1078
  // Pipeline timeout configuration
1024
1079
  let PIPELINE_TIMING = {