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
@@ -54,6 +54,7 @@
54
54
  * never touches a running bot's connection) and has no runtime dependencies.
55
55
  *
56
56
  * Usage:
57
+ * dexbot asset bts [options] # CLI shortcut (also: dexbot a bts)
57
58
  * node dist/scripts/bts_distribution.js # terminal + HTML
58
59
  * node dist/scripts/bts_distribution.js --json # JSON + HTML
59
60
  * node dist/scripts/bts_distribution.js --no-html # terminal only
@@ -68,6 +69,9 @@ import { pathToFileURL } from 'node:url';
68
69
  import { NODE_MANAGEMENT } from '../modules/constants.js';
69
70
  import { PATHS } from '../modules/paths.js';
70
71
  import { CLI_COLORS as colors } from '../modules/cli_colors.js';
72
+ import { CLI_TAGS, clearProgressLine, loadingLine, savedLine, section, summaryLine, taggedProgress, warningsBlock, } from '../modules/cli_progress.js';
73
+ import { scanIdRange, DENSE_SPACE_GAP } from '../modules/chain_scan.js';
74
+ import { toFileUrl } from '../analysis/chart_utils.js';
71
75
  // ---------------------------------------------------------------------------
72
76
  // Constants
73
77
  // ---------------------------------------------------------------------------
@@ -80,9 +84,15 @@ const NAME_BATCH = 50; // get_accounts ids per call
80
84
  const DEFAULT_DETAIL = 10;
81
85
  const DEFAULT_VESTING_MAX = 200_000;
82
86
  const DEFAULT_SCAN_GAP = 20_000;
87
+ const SYMBOL_BATCH = 500; // asset ids per symbol lookup
83
88
  const DEFAULT_HTML = path.join(PATHS.ANALYSIS.CHARTS_DIR, 'bts-distribution.html');
84
89
  const DEFAULT_HOLDERS = 100;
85
90
  const MAX_HOLDERS = 1000;
91
+ // Console tag for status lines, matching the `[Liquidity]` / `[TradingView]`
92
+ // style used by the other analysis entries.
93
+ const LOG_TAG = CLI_TAGS.assets;
94
+ const progress = taggedProgress(LOG_TAG);
95
+ const clearProgress = clearProgressLine;
86
96
  // asset_api (get_asset_holders) is allowlisted on only a few public nodes; this
87
97
  // list mirrors the one in scripts/rich_list.ts, which solves the same problem.
88
98
  const HOLDER_NODES = [
@@ -104,6 +114,7 @@ balances. The remaining liquid supply (accounts, order books, blinded balances)
104
114
  is reported as a derived residual.
105
115
 
106
116
  Usage:
117
+ dexbot asset bts [options]
107
118
  node dist/scripts/bts_distribution.js [options]
108
119
 
109
120
  Options:
@@ -539,16 +550,17 @@ async function fetchObjects(session, ids) {
539
550
  return out;
540
551
  }
541
552
  /**
542
- * Paginate a `list_*` endpoint whose signature is `(limit, start_id)`.
553
+ * Paginate a `list_*`/`get_*_by_asset` endpoint. The caller owns the argument
554
+ * order (most take `(limit, start_id)`, `list_htlcs` takes `(start_id, limit)`).
543
555
  * Every page is de-duplicated by id, so an inclusive lower bound cannot loop.
544
556
  */
545
- async function listPages(session, method, limit, maxPages = 5000) {
557
+ async function listPages(fetchPage, limit, maxPages = 5000) {
546
558
  const seen = new Set();
547
559
  const out = [];
548
560
  let start = null;
549
561
  for (let page = 0; page < maxPages; page++) {
550
- const rows = await callDb(session, method, [limit, start]);
551
- if (!rows || rows.length === 0)
562
+ const rows = await fetchPage(start);
563
+ if (!Array.isArray(rows) || rows.length === 0)
552
564
  break;
553
565
  let fresh = 0;
554
566
  for (const row of rows) {
@@ -565,63 +577,20 @@ async function listPages(session, method, limit, maxPages = 5000) {
565
577
  return out;
566
578
  }
567
579
  /**
568
- * Enumerate every asset via `list_assets` (symbol-ordered, inclusive lower
569
- * bound). Assets are keyed by symbol, so pagination advances on the last
570
- * symbol and duplicates are ignored.
580
+ * Fetch asset objects by id/symbol in `SYMBOL_BATCH`-sized `get_assets` chunks.
581
+ * get_assets has no page limit, but keeping requests bounded survives every
582
+ * node and keeps a huge MPA set from becoming one oversized message.
571
583
  */
572
- async function listAllAssets(session) {
573
- const limit = Math.max(1, Math.min(session.config.api_limit_get_assets ?? 100, 1000));
574
- const seen = new Set();
584
+ async function fetchAssetObjects(session, refs) {
575
585
  const out = [];
576
- let lower = '';
577
- for (let page = 0; page < 5000; page++) {
578
- const rows = await callDb(session, 'list_assets', [lower, limit]);
579
- if (!Array.isArray(rows) || rows.length === 0)
580
- break;
581
- let fresh = 0;
582
- for (const row of rows) {
583
- if (!row || seen.has(row.id))
584
- continue;
585
- seen.add(row.id);
586
- out.push(row);
587
- fresh++;
588
- }
589
- if (rows.length < limit)
590
- break;
591
- const lastSymbol = rows[rows.length - 1]?.symbol;
592
- if (typeof lastSymbol !== 'string')
593
- break;
594
- if (fresh === 0)
595
- break;
596
- lower = lastSymbol;
586
+ for (let i = 0; i < refs.length; i += SYMBOL_BATCH) {
587
+ const rows = await callDb(session, 'get_assets', [refs.slice(i, i + SYMBOL_BATCH)]);
588
+ for (const row of rows || [])
589
+ if (row && typeof row.id === 'string')
590
+ out.push(row);
597
591
  }
598
592
  return out;
599
593
  }
600
- /**
601
- * Scan a dense-ish object id range (`prefix.N`) with a gap tolerance. Object
602
- * ids are allocated monotonically but are never reused, so a range contains
603
- * holes; the scan stops once it has walked `gap` ids past the last live object.
604
- */
605
- async function scanIdRange(session, prefix, maxId, gap, onProgress) {
606
- const objects = [];
607
- let index = 0;
608
- let lastSeen = -1;
609
- while (index <= maxId && index - lastSeen <= gap) {
610
- const count = Math.min(OBJECT_BATCH, maxId - index + 1);
611
- const ids = Array.from({ length: count }, (_, i) => `${prefix}.${index + i}`);
612
- const page = await callDb(session, 'get_objects', [ids]);
613
- for (let j = 0; j < count; j++) {
614
- const obj = page?.[j];
615
- if (obj) {
616
- lastSeen = index + j;
617
- objects.push(obj);
618
- }
619
- }
620
- index += count;
621
- onProgress?.(index);
622
- }
623
- return { objects, scannedTo: index, hitLimit: index > maxId && index - lastSeen <= gap };
624
- }
625
594
  async function resolveAccountNames(session, ids) {
626
595
  const names = new Map();
627
596
  const unique = [...new Set(ids.filter(Boolean))];
@@ -665,18 +634,6 @@ async function sumCallOrderCollateral(session, mpas) {
665
634
  // ---------------------------------------------------------------------------
666
635
  // Collection
667
636
  // ---------------------------------------------------------------------------
668
- function progress(enabled, message) {
669
- if (!enabled)
670
- return;
671
- if (!process.stderr.isTTY)
672
- return;
673
- process.stderr.write(`\r\x1b[2K ${message}`);
674
- }
675
- function clearProgress(enabled) {
676
- if (!enabled || !process.stderr.isTTY)
677
- return;
678
- process.stderr.write('\r\x1b[2K');
679
- }
680
637
  const COLORS = [
681
638
  '#5eb0ef', '#f0883e', '#3fb950', '#db61a2', '#a371f7', '#e3b341',
682
639
  '#39c5cf', '#ff7b72', '#7ee787', '#d2a8ff', '#ffa657', '#8b949e',
@@ -701,43 +658,80 @@ async function collect(session, opts) {
701
658
  const currentSupply = toBig(coreDyn.current_supply);
702
659
  const confidentialSupply = toBig(coreDyn.confidential_supply);
703
660
  const global = await callDb(session, 'get_dynamic_global_properties', []);
704
- // ---- All assets: MPAs, symbols, fee pools -------------------------------
705
- progress(showProgress, 'enumerating assets ...');
706
- const assets = await listAllAssets(session);
707
- const assetById = new Map(assets.map((a) => [a.id, a]));
708
- const symbolOf = (id) => assetById.get(id)?.symbol ?? id;
709
- progress(showProgress, `fetching dynamic data for ${assets.length} assets ...`);
710
- const dynamicIds = assets.map((a) => a.dynamic_asset_data_id).filter(Boolean);
711
- const dynamicRows = await fetchObjects(session, dynamicIds);
712
- const dynamicById = new Map();
713
- for (const dyn of dynamicRows)
714
- if (dyn)
715
- dynamicById.set(dyn.id, dyn);
716
- const mpaAssets = assets.filter((a) => typeof a.bitasset_data_id === 'string' && a.bitasset_data_id);
717
- const bitassetRows = await fetchObjects(session, mpaAssets.map((a) => a.bitasset_data_id));
718
- const bitassetById = new Map();
719
- bitassetRows.forEach((bit) => {
720
- if (bit)
721
- bitassetById.set(bit.id, bit);
661
+ // ---- Smart assets (2.4.x) and fee pools (2.3.x) -------------------------
662
+ // bitshares-core has no per-asset index for the legacy object spaces and no
663
+ // "list smart assets" call, but both implementation spaces are small and
664
+ // dense: 2.4.x holds one object per smart asset (with asset_id and
665
+ // options.short_backing_asset) and 2.3.x one tiny object per asset, whose
666
+ // fee_pool is documented as "in core asset". Walking those two spaces is
667
+ // far cheaper than list_assets (100 assets per page over ~6.5k assets) just
668
+ // to find the ~380 BTS-backed ones.
669
+ progress(showProgress, 'listing smart assets (2.4.x) ...');
670
+ const bitassetScan = await scanIdRange({
671
+ read: (method, args) => callDb(session, method, args),
672
+ prefix: '2.4',
673
+ maxId: 200_000,
674
+ gap: DENSE_SPACE_GAP,
675
+ onProgress: (n) => progress(showProgress, `listing smart assets (2.4.x) ... ${n} ids`),
676
+ });
677
+ const bitassets = bitassetScan.objects;
678
+ if (bitassetScan.hitLimit) {
679
+ warnings.push('Smart-asset scan hit its 200,000-id limit; MPA collateral totals may be incomplete.');
680
+ }
681
+ const bitassetById = new Map(bitassets.map((bit) => [bit.id, bit]));
682
+ const mpaIds = [...new Set(bitassets.map((bit) => bit.asset_id).filter(Boolean))];
683
+ // get_assets() returns extended_asset_object, i.e. the MPA metadata plus
684
+ // total_in_collateral / total_backing_collateral.
685
+ const mpaAssets = await fetchAssetObjects(session, mpaIds);
686
+ progress(showProgress, 'scanning asset dynamic data (2.3.x fee pools) ...');
687
+ const dynamicScan = await scanIdRange({
688
+ read: (method, args) => callDb(session, method, args),
689
+ prefix: '2.3',
690
+ maxId: 200_000,
691
+ gap: DENSE_SPACE_GAP,
692
+ onProgress: (n) => progress(showProgress, `scanning asset dynamic data (2.3.x) ... ${n} ids`),
722
693
  });
694
+ const dynamicRows = dynamicScan.objects;
695
+ if (dynamicScan.hitLimit) {
696
+ warnings.push('Asset dynamic-data scan hit its 200,000-id limit; fee-pool totals may be incomplete.');
697
+ }
698
+ const dynamicById = new Map(dynamicRows.map((dyn) => [dyn.id, dyn]));
699
+ // Symbols are looked up for the ids the report prints, never for every
700
+ // asset: 2.3.x/2.4.x carry ids only.
701
+ const symbolById = new Map([[coreId, core.symbol]]);
702
+ const symbolOf = (id) => symbolById.get(id) ?? id;
703
+ const resolveSymbols = async (ids) => {
704
+ const missing = [...new Set(ids.filter((id) => !!id && id.startsWith('1.3.') && !symbolById.has(id)))];
705
+ if (missing.length === 0)
706
+ return;
707
+ for (const row of await fetchAssetObjects(session, missing)) {
708
+ if (row.symbol)
709
+ symbolById.set(row.id, row.symbol);
710
+ }
711
+ };
712
+ for (const mpa of mpaAssets)
713
+ symbolById.set(mpa.id, mpa.symbol);
723
714
  const btsBackedMpas = mpaAssets.filter((a) => {
724
715
  const bit = a.bitasset_data_id ? bitassetById.get(a.bitasset_data_id) : undefined;
725
716
  return bit?.options?.short_backing_asset === coreId;
726
717
  });
727
718
  // ---- Fee pools (fee_pool is denominated in the core asset) --------------
728
719
  let feePoolTotal = 0n;
729
- const feePoolRows = [];
720
+ const feePoolCandidates = [];
730
721
  for (const dyn of dynamicRows) {
731
722
  if (!dyn)
732
723
  continue;
733
724
  const value = toBig(dyn.fee_pool);
734
725
  feePoolTotal += value;
735
- if (value > 0n) {
736
- const asset = assetById.get(dyn.id.replace(/^2\.3\./, '1.3.'));
737
- feePoolRows.push({ symbol: asset?.symbol ?? dyn.id, amount: value });
738
- }
739
- }
740
- feePoolRows.sort((a, b) => compareDesc(a.amount, b.amount));
726
+ if (value > 0n)
727
+ feePoolCandidates.push({ assetId: dyn.id.replace(/^2\.3\./, '1.3.'), amount: value });
728
+ }
729
+ // Thousands of assets have a (tiny) fee pool; only the printed rows need a
730
+ // symbol, so the top detail rows are resolved after sorting.
731
+ feePoolCandidates.sort((a, b) => compareDesc(a.amount, b.amount));
732
+ const feePoolTop = feePoolCandidates.slice(0, opts.detail);
733
+ await resolveSymbols(feePoolTop.map((row) => row.assetId));
734
+ const feePoolRows = feePoolTop.map((row) => ({ symbol: symbolOf(row.assetId), amount: row.amount }));
741
735
  // ---- Call-order collateral ---------------------------------------------
742
736
  let collateral;
743
737
  let collateralFromApi = core.total_in_collateral !== undefined && core.total_in_collateral !== null;
@@ -845,12 +839,15 @@ async function collect(session, opts) {
845
839
  // ---- Collateral bids (only meaningful after a global settlement) --------
846
840
  let bidCount = 0;
847
841
  let bidTotal = 0n;
842
+ // `limit` may not exceed the node's configured maximum, so use that value:
843
+ // fewer round trips per settled smart asset.
844
+ const bidLimit = Math.max(1, Math.min(session.config.api_limit_get_collateral_bids ?? 100, 1000));
848
845
  for (const mpa of globallySettled) {
849
846
  let start = 0;
850
847
  for (let page = 0; page < 100; page++) {
851
848
  let rows;
852
849
  try {
853
- rows = await callDb(session, 'get_collateral_bids', [mpa.id, 100, start]);
850
+ rows = await callDb(session, 'get_collateral_bids', [mpa.id, bidLimit, start]);
854
851
  }
855
852
  catch {
856
853
  break;
@@ -864,14 +861,14 @@ async function collect(session, opts) {
864
861
  bidTotal += amountOf(row.inv_swan_price.base);
865
862
  }
866
863
  start += rows.length;
867
- if (rows.length < 100)
864
+ if (rows.length < bidLimit)
868
865
  break;
869
866
  }
870
867
  }
871
868
  // ---- Tickets (voting / governance locks) --------------------------------
872
869
  progress(showProgress, 'listing governance tickets ...');
873
870
  const ticketLimit = Math.max(1, Math.min(session.config.api_limit_get_tickets ?? 100, 1000));
874
- const tickets = await listPages(session, 'list_tickets', ticketLimit);
871
+ const tickets = await listPages((start) => callDb(session, 'list_tickets', [ticketLimit, start]), ticketLimit);
875
872
  let ticketTotal = 0n;
876
873
  const ticketByType = new Map();
877
874
  const ticketByStatus = new Map();
@@ -900,7 +897,13 @@ async function collect(session, opts) {
900
897
  .slice(0, opts.detail);
901
898
  // ---- Vesting balances (1.13.x) ------------------------------------------
902
899
  progress(showProgress, 'scanning vesting balances (1.13.x) ...');
903
- const vestingScan = await scanIdRange(session, '1.13', opts.vestingMax, opts.gap, (n) => progress(showProgress, `scanning vesting balances (1.13.x) ... ${n}`));
900
+ const vestingScan = await scanIdRange({
901
+ read: (method, args) => callDb(session, method, args),
902
+ prefix: '1.13',
903
+ maxId: opts.vestingMax,
904
+ gap: opts.gap,
905
+ onProgress: (n) => progress(showProgress, `scanning vesting balances (1.13.x) ... ${n} ids`),
906
+ });
904
907
  let vestingTotal = 0n;
905
908
  const vestingByType = new Map();
906
909
  const vestingByOwner = new Map();
@@ -923,8 +926,16 @@ async function collect(session, opts) {
923
926
  .sort((a, b) => compareDesc(a.amount, b.amount))
924
927
  .slice(0, opts.detail);
925
928
  // ---- Legacy 1.15.x balance objects --------------------------------------
926
- progress(showProgress, 'scanning legacy balances (1.15.x) ...');
927
- const genesisScan = await scanIdRange(session, '1.15', 200_000, opts.gap);
929
+ // The node exposes no per-asset index for 1.15.x objects, so the walk is
930
+ // global and the requested asset is filtered out below.
931
+ progress(showProgress, 'scanning legacy balance objects (1.15.x) ...');
932
+ const genesisScan = await scanIdRange({
933
+ read: (method, args) => callDb(session, method, args),
934
+ prefix: '1.15',
935
+ maxId: 200_000,
936
+ gap: opts.gap,
937
+ onProgress: (n) => progress(showProgress, `scanning legacy balance objects (1.15.x) ... ${n} ids`),
938
+ });
928
939
  if (genesisScan.hitLimit) {
929
940
  warnings.push('Legacy 1.15.x scan hit its 200,000-id probe limit; the legacy bucket may be incomplete.');
930
941
  }
@@ -945,7 +956,9 @@ async function collect(session, opts) {
945
956
  // ---- Liquidity pools (1.19.x) -------------------------------------------
946
957
  progress(showProgress, 'listing liquidity pools ...');
947
958
  const poolLimit = Math.max(1, Math.min(session.config.api_limit_get_liquidity_pools ?? 100, 1000));
948
- const pools = await listPages(session, 'list_liquidity_pools', poolLimit);
959
+ const pools = await listPages((start) => callDb(session, 'list_liquidity_pools', [poolLimit, start]), poolLimit);
960
+ // The pool table prints both legs; resolve just those symbols.
961
+ await resolveSymbols(pools.flatMap((pool) => [pool.asset_a, pool.asset_b]));
949
962
  let poolTotal = 0n;
950
963
  let poolsWithBts = 0;
951
964
  const poolRows = [];
@@ -969,7 +982,21 @@ async function collect(session, opts) {
969
982
  // ---- SameT funds (1.20.x) -----------------------------------------------
970
983
  progress(showProgress, 'listing SameT funds ...');
971
984
  const sametLimit = Math.max(1, Math.min(session.config.api_limit_get_samet_funds ?? 100, 1000));
972
- const sametFunds = await listPages(session, 'list_samet_funds', sametLimit);
985
+ // `get_samet_funds_by_asset` returns only this asset's funds; the plain
986
+ // list_* fallback keeps older nodes working.
987
+ let sametFunds = [];
988
+ try {
989
+ sametFunds = await listPages((start) => callDb(session, 'get_samet_funds_by_asset', [coreId, sametLimit, start]), sametLimit);
990
+ }
991
+ catch {
992
+ try {
993
+ const all = await listPages((start) => callDb(session, 'list_samet_funds', [sametLimit, start]), sametLimit);
994
+ sametFunds = all.filter((fund) => fund.asset_type === coreId);
995
+ }
996
+ catch (e) {
997
+ warnings.push(`SameT funds unavailable: ${e.message}`);
998
+ }
999
+ }
973
1000
  let sametTotal = 0n;
974
1001
  let sametBtsCount = 0;
975
1002
  const sametRows = [];
@@ -985,7 +1012,20 @@ async function collect(session, opts) {
985
1012
  // ---- Credit offers / deals (1.21.x / 1.22.x) ----------------------------
986
1013
  progress(showProgress, 'listing credit offers and deals ...');
987
1014
  const creditLimit = Math.max(1, Math.min(session.config.api_limit_get_credit_offers ?? 100, 1000));
988
- const offers = await listPages(session, 'list_credit_offers', creditLimit);
1015
+ // Asset-scoped endpoints only return BTS rows; list_* stays as the fallback.
1016
+ let offers = [];
1017
+ try {
1018
+ offers = await listPages((start) => callDb(session, 'get_credit_offers_by_asset', [coreId, creditLimit, start]), creditLimit);
1019
+ }
1020
+ catch {
1021
+ try {
1022
+ const all = await listPages((start) => callDb(session, 'list_credit_offers', [creditLimit, start]), creditLimit);
1023
+ offers = all.filter((offer) => offer.asset_type === coreId);
1024
+ }
1025
+ catch (e) {
1026
+ warnings.push(`Credit offers unavailable: ${e.message}`);
1027
+ }
1028
+ }
989
1029
  let offerTotal = 0n;
990
1030
  let btsOfferCount = 0;
991
1031
  for (const offer of offers) {
@@ -994,7 +1034,19 @@ async function collect(session, opts) {
994
1034
  btsOfferCount++;
995
1035
  offerTotal += toBig(offer.current_balance);
996
1036
  }
997
- const deals = await listPages(session, 'list_credit_deals', creditLimit);
1037
+ let deals = [];
1038
+ try {
1039
+ deals = await listPages((start) => callDb(session, 'get_credit_deals_by_collateral_asset', [coreId, creditLimit, start]), creditLimit);
1040
+ }
1041
+ catch {
1042
+ try {
1043
+ const all = await listPages((start) => callDb(session, 'list_credit_deals', [creditLimit, start]), creditLimit);
1044
+ deals = all.filter((deal) => deal.collateral_asset === coreId);
1045
+ }
1046
+ catch (e) {
1047
+ warnings.push(`Credit deals unavailable: ${e.message}`);
1048
+ }
1049
+ }
998
1050
  let dealTotal = 0n;
999
1051
  let btsDealCount = 0;
1000
1052
  for (const deal of deals) {
@@ -1007,17 +1059,20 @@ async function collect(session, opts) {
1007
1059
  progress(showProgress, 'listing HTLCs ...');
1008
1060
  let htlcTotal = 0n;
1009
1061
  let htlcCount = 0;
1010
- for (let index = 0; index < 100_000; index += OBJECT_BATCH) {
1011
- const ids = Array.from({ length: OBJECT_BATCH }, (_, i) => `1.16.${index + i}`);
1012
- const rows = await fetchObjects(session, ids);
1013
- const live = rows.filter((r) => !!r);
1014
- for (const row of live) {
1015
- htlcCount++;
1016
- if (row.transfer?.asset_id === coreId)
1017
- htlcTotal += toBig(row.transfer.amount);
1018
- }
1019
- if (live.length === 0)
1020
- break;
1062
+ // `list_htlcs` is the paginated list for this space (the id walk it replaces
1063
+ // scanned 1.16.0..1.16.99,999 whenever any HTLC existed).
1064
+ let htlcs = [];
1065
+ const htlcLimit = Math.max(1, Math.min(session.config.api_limit_list_htlcs ?? 100, 1000));
1066
+ try {
1067
+ htlcs = await listPages((start) => callDb(session, 'list_htlcs', [start ?? '1.16.0', htlcLimit]), htlcLimit);
1068
+ }
1069
+ catch (e) {
1070
+ warnings.push(`HTLCs unavailable: ${e.message}`);
1071
+ }
1072
+ for (const htlc of htlcs) {
1073
+ htlcCount++;
1074
+ if (htlc.transfer?.asset_id === coreId)
1075
+ htlcTotal += toBig(htlc.transfer.amount);
1021
1076
  }
1022
1077
  // ---- Build buckets ------------------------------------------------------
1023
1078
  const named = (id, fallback) => {
@@ -1288,7 +1343,7 @@ async function collect(session, opts) {
1288
1343
  lockedPerCirculating,
1289
1344
  },
1290
1345
  details: {
1291
- assetCount: assets.length,
1346
+ assetCount: dynamicRows.length,
1292
1347
  mpaCount: btsBackedMpas.length,
1293
1348
  mpaBackedCollateral,
1294
1349
  collateralFromApi,
@@ -1403,30 +1458,27 @@ function renderTerminal(report, opts) {
1403
1458
  console.log(c(colors.gray, ` bars are scaled to the largest bucket; check the reconcile: `
1404
1459
  + `${money(report.identified + report.residual)} = supply`));
1405
1460
  // ---- Detail sections ----------------------------------------------------
1406
- const section = (title) => {
1407
- console.log();
1408
- console.log(c(colors.bold + colors.yellow, ` ── ${title} ${'─'.repeat(Math.max(0, 66 - title.length))}`));
1409
- };
1410
- section(`Settlement pools (${report.details.mpaCount} BTS-backed MPAs)`);
1461
+ const heading = (title) => { console.log(section(title, 66)); };
1462
+ heading(`Settlement pools (${report.details.mpaCount} BTS-backed MPAs)`);
1411
1463
  console.log(c(colors.gray, ' Global: ' + money(report.buckets.find((b) => b.key === 'settlementFund')?.amount ?? 0n))
1412
1464
  + c(colors.gray, ' · Individual: ')
1413
1465
  + c(colors.white, money(report.buckets.find((b) => b.key === 'individualSettlementFund')?.amount ?? 0n)));
1414
1466
  printMpaTable(report, opts.detail, money, c);
1415
1467
  printSettlementTable(report, money, c);
1416
- section('Governance tickets by lock type');
1468
+ heading('Governance tickets by lock type');
1417
1469
  printTypeTable(report.details.tickets.byType, supply, money, c);
1418
- section('Vesting balances by balance type');
1470
+ heading('Vesting balances by balance type');
1419
1471
  printTypeTable(report.details.vesting.byType, supply, money, c);
1420
1472
  if (report.details.vesting.hitLimit) {
1421
1473
  console.log(c(colors.sell, ` warning: vesting scan reached --vesting-max ${opts.vestingMax}; totals may be incomplete`));
1422
1474
  }
1423
- section('Top governance ticket holders');
1475
+ heading('Top governance ticket holders');
1424
1476
  printHolderTable(report.details.tickets.top, money, c);
1425
- section('Top vesting balance owners');
1477
+ heading('Top vesting balance owners');
1426
1478
  printHolderTable(report.details.vesting.top, money, c);
1427
- section('Liquidity pools with BTS');
1479
+ heading('Liquidity pools with BTS');
1428
1480
  printPoolTable(report, opts.detail, money, c);
1429
- section(`Top liquid holders (rich list, ${report.details.holders.count} fetched)`);
1481
+ heading(`Top liquid holders (rich list, ${report.details.holders.count} fetched)`);
1430
1482
  const holders = report.details.holders;
1431
1483
  if (holders.top.length === 0) {
1432
1484
  console.log(c(colors.gray, ' (asset_api unavailable — no holder data)'));
@@ -1475,20 +1527,20 @@ function renderTerminal(report, opts) {
1475
1527
  }
1476
1528
  }
1477
1529
  }
1478
- section('Credit & SameT');
1530
+ heading('Credit & SameT');
1479
1531
  const credit = report.details.credit;
1480
1532
  console.log(` credit offers ${padStart(money(credit.offerTotal), 20)} ${c(colors.gray, `${credit.btsOfferCount}/${credit.offerCount} offers in ${report.asset.symbol}`)}`);
1481
1533
  console.log(` credit collateral ${padStart(money(credit.dealTotal), 20)} ${c(colors.gray, `${credit.btsDealCount}/${credit.dealCount} deals collateralised in ${report.asset.symbol}`)}`);
1482
1534
  console.log(` SameT funds ${padStart(money(report.details.samet.total), 20)} ${c(colors.gray, `${report.details.samet.btsCount}/${report.details.samet.count} funds in ${report.asset.symbol}`)}`);
1483
- section('Asset fee pools (held in BTS)');
1535
+ heading('Asset fee pools (held in BTS)');
1484
1536
  console.log(` total ${padStart(money(report.details.feePools.total), 20)}`);
1485
1537
  for (const row of report.details.feePools.top) {
1486
1538
  console.log(` ${padEnd(row.symbol.slice(0, 22), 22)} ${padStart(money(row.amount), 20)}`);
1487
1539
  }
1488
- section('Legacy 1.15.x balances');
1540
+ heading('Legacy 1.15.x balances');
1489
1541
  console.log(` claimable objects ${padStart(money(report.details.genesis.total), 20)} ${c(colors.gray, `${report.details.genesis.count} object(s), ${report.details.genesis.claimableCount} without vesting policy`)}`);
1490
1542
  // ---- Final comparison against circulating supply ----------------------
1491
- section('Circulating supply comparison');
1543
+ heading('Circulating supply comparison');
1492
1544
  const circ = report.circulating;
1493
1545
  const circWidth = 24;
1494
1546
  console.log(` ${padEnd('Total supply (current_supply)', 36)} ${padStart(money(supply), circWidth)} `
@@ -1525,18 +1577,14 @@ function renderTerminal(report, opts) {
1525
1577
  + `${padStart(sharePercent(bucket.amount, circ.derived), pctWidth)}`);
1526
1578
  }
1527
1579
  console.log(c(colors.gray, ` ${'─'.repeat(labelWidth + amountWidth + pctWidth * 2 + 3)}`));
1528
- console.log(c(colors.gray, ' Float = supply − identified/dormant. BitShares has no protocol-level '
1529
- + '"circulating" field; this is a derived float.'));
1530
1580
  if (report.details.warnings.length > 0) {
1531
- section('Warnings');
1532
- for (const warning of report.details.warnings)
1533
- console.log(c(colors.sell, ` ! ${warning}`));
1581
+ console.log(warningsBlock(report.details.warnings, 66));
1534
1582
  }
1535
1583
  console.log();
1536
- if (opts.html)
1537
- console.log(c(colors.brightGreen, ` HTML report: ${opts.html}`));
1538
- console.log(c(colors.gray, ' Collateral: api_helper_indexes total_in_collateral; rest via list_* / get_objects scans.'));
1539
- console.log();
1584
+ // Same trailing pattern as `dexbot asset <token>`: one muted summary line,
1585
+ // then the shared `[Assets] Report saved … file://` link from the caller.
1586
+ console.log(summaryLine('float = supply − identified/dormant (no protocol-level "circulating" field); '
1587
+ + 'collateral via api_helper_indexes total_in_collateral'));
1540
1588
  }
1541
1589
  function printMpaTable(report, detail, money, c) {
1542
1590
  const rows = report.details.mpas.slice(0, detail);
@@ -2046,7 +2094,7 @@ ${warningHtml}
2046
2094
 
2047
2095
  <footer>
2048
2096
  <p><strong>Residual</strong> = current supply − all identified holdings. It contains account balances (2.5.x), limit-order escrow (1.7.x), blinded balances (${money(report.supply.confidential)} BTS) and any object type not listed above. It is derived, not enumerated.</p>
2049
- <p>Sources: bitshares-core 7.0.x database_api — <code>list_assets</code>, <code>get_objects</code>, <code>list_tickets</code>, <code>list_liquidity_pools</code>, <code>list_samet_funds</code>, <code>list_credit_offers</code>, <code>list_credit_deals</code>, <code>list_htlcs</code>, <code>get_collateral_bids</code>${report.details.collateralFromApi ? ', <code>total_in_collateral</code>' : ''}${holders.sourceNode ? ', <code>asset_api::get_asset_holders</code>' : ''}. Read-only; no keys, no transactions. Exchange tags are name heuristics, not verified identities.</p>
2097
+ <p>Sources: bitshares-core 7.0.x database_api — <code>get_objects</code> (1.13.x vesting, 1.15.x balances, 2.3.x dynamic data, 2.4.x smart assets), <code>get_assets</code>, <code>list_tickets</code>, <code>list_liquidity_pools</code>, <code>get_samet_funds_by_asset</code>, <code>get_credit_offers_by_asset</code>, <code>get_credit_deals_by_collateral_asset</code>, <code>list_htlcs</code>, <code>get_collateral_bids</code>${report.details.collateralFromApi ? ', <code>total_in_collateral</code>' : ''}${holders.sourceNode ? ', <code>asset_api::get_asset_holders</code>' : ''}. Read-only; no keys, no transactions. Exchange tags are name heuristics, not verified identities.</p>
2050
2098
  <p>Generated by <code>scripts/bts_distribution.ts</code> · block ${esc(formatInteger(BigInt(report.blockNum)))} · ${esc(report.blockTime)} UTC.</p>
2051
2099
  </footer>
2052
2100
  </body>
@@ -2062,7 +2110,7 @@ async function main() {
2062
2110
  }
2063
2111
  catch (e) {
2064
2112
  if (e instanceof UsageError) {
2065
- console.error(`Error: ${e.message}\n`);
2113
+ console.error(`${LOG_TAG} Error: ${e.message}\n`);
2066
2114
  printHelp();
2067
2115
  process.exit(2);
2068
2116
  }
@@ -2072,6 +2120,8 @@ async function main() {
2072
2120
  printHelp();
2073
2121
  return;
2074
2122
  }
2123
+ if (!opts.json)
2124
+ console.log(loadingLine(LOG_TAG, 'Loading BTS supply distribution from chain...'));
2075
2125
  const nodes = opts.nodes.length > 0 ? opts.nodes : [...NODE_MANAGEMENT.DEFAULT_NODES];
2076
2126
  const session = await openSession(nodes);
2077
2127
  try {
@@ -2087,6 +2137,8 @@ async function main() {
2087
2137
  }
2088
2138
  else {
2089
2139
  renderTerminal(report, opts);
2140
+ if (opts.html)
2141
+ console.log(savedLine(LOG_TAG, 'Report', toFileUrl(opts.html)));
2090
2142
  }
2091
2143
  }
2092
2144
  finally {
@@ -2095,7 +2147,7 @@ async function main() {
2095
2147
  }
2096
2148
  if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
2097
2149
  main().catch((e) => {
2098
- console.error(`\nError: ${e.message}`);
2150
+ console.error(`\n${LOG_TAG} Error: ${e.message}`);
2099
2151
  process.exit(1);
2100
2152
  });
2101
2153
  }
@@ -22,6 +22,7 @@ interface FeedCtx {
22
22
  kind: string;
23
23
  legs: MpaBackingCtx[];
24
24
  }
25
+ import { isPoolIdTarget } from './chart_targets.js';
25
26
  declare const FEED_STALE_WARN_AGE_MS: number;
26
27
  /** Chart command identity: 'tv' → TradingView exporter, 'dw' → dynamic-weight research chart. */
27
28
  type ChartCmd = 'tv' | 'dw';
@@ -50,7 +51,6 @@ declare function parseArgs(argv: string[], cmd?: ChartCmd): {
50
51
  source: string;
51
52
  help: boolean;
52
53
  };
53
- declare function isPoolIdTarget(target: string): boolean;
54
54
  declare function run(cmd: ChartCmd): Promise<void>;
55
55
  export { parseArgs, isPoolIdTarget, resolveMpaBacking, pickFeedContext, activateFeedIfCovered, feedAgeMs, feedName, run, FEED_STALE_WARN_AGE_MS };
56
56
  export type { ChartCmd, ChartRenderer };