dexbot 1.6.14 → 1.6.16

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 (134) hide show
  1. package/CHANGELOG.md +60 -4
  2. package/README.md +4 -4
  3. package/analysis/README.md +6 -6
  4. package/analysis/tradingview/README.md +2 -1
  5. package/claw/README.md +7 -7
  6. package/claw/docs/DEXBOT2_TUNING_CHEAT_SHEET.md +3 -2
  7. package/claw/modules/chain_queries.ts +3 -3
  8. package/claw/package.json +1 -1
  9. package/claw/runtimes/openclaw-plugin/openclaw.plugin.json +1 -1
  10. package/claw/runtimes/openclaw-plugin/package.json +1 -1
  11. package/dist/analysis/ama_fitting/fetch_lp_candles.js +8 -1
  12. package/dist/analysis/ama_fitting/optimizer_high_resolution.d.ts +50 -1
  13. package/dist/analysis/ama_fitting/optimizer_high_resolution.js +33 -7
  14. package/dist/analysis/bot_fitting/backtest_bot_fitting.d.ts +8 -6
  15. package/dist/analysis/bot_fitting/backtest_bot_fitting.js +8 -10
  16. package/dist/analysis/bot_key_utils.js +9 -7
  17. package/dist/analysis/chart_utils.d.ts +9 -2
  18. package/dist/analysis/chart_utils.js +51 -1
  19. package/dist/analysis/tradingview/tradingview_uplot_chart_generator.d.ts +2 -1
  20. package/dist/analysis/tradingview/tradingview_uplot_chart_generator.js +99 -11
  21. package/dist/analysis/trend_detection/dynamic_weight_chart_generator.js +57 -4
  22. package/dist/analysis/trend_detection/kalman_chart_generator.js +1 -0
  23. package/dist/analysis/trend_detection/regime_chart_generator.js +1 -1
  24. package/dist/analysis/trend_detection/volatility_chart_generator.js +1 -0
  25. package/dist/dexbot.js +112 -141
  26. package/dist/market_adapter/core/strategies/dynamic_weight_series.js +26 -11
  27. package/dist/market_adapter/inputs/fetch_book_data.js +36 -12
  28. package/dist/market_adapter/inputs/fetch_lp_data.d.ts +6 -0
  29. package/dist/market_adapter/inputs/fetch_lp_data.js +65 -19
  30. package/dist/market_adapter/inputs/kibana_feed_source.js +45 -12
  31. package/dist/market_adapter/inputs/window_cache.d.ts +14 -1
  32. package/dist/market_adapter/inputs/window_cache.js +53 -17
  33. package/dist/market_adapter/lp_chart_core.js +1 -0
  34. package/dist/market_adapter/utils/chain.js +4 -3
  35. package/dist/market_adapter/utils/pair_orientation.d.ts +60 -0
  36. package/dist/market_adapter/utils/pair_orientation.js +142 -0
  37. package/dist/modules/account_bots.d.ts +40 -3
  38. package/dist/modules/account_bots.js +80 -37
  39. package/dist/modules/account_orders.d.ts +4 -1
  40. package/dist/modules/account_orders.js +4 -1
  41. package/dist/modules/bitshares-native/resolvers.js +3 -3
  42. package/dist/modules/bitshares-native/subscriptions.js +1 -1
  43. package/dist/modules/bitshares_client.d.ts +12 -10
  44. package/dist/modules/bitshares_client.js +12 -10
  45. package/dist/modules/bot_defaults.d.ts +13 -2
  46. package/dist/modules/bot_defaults.js +31 -2
  47. package/dist/modules/bot_settings.js +6 -3
  48. package/dist/modules/chain_keys.d.ts +5 -1
  49. package/dist/modules/chain_keys.js +5 -1
  50. package/dist/modules/chain_orders.d.ts +6 -2
  51. package/dist/modules/chain_orders.js +6 -2
  52. package/dist/modules/constants.d.ts +11 -0
  53. package/dist/modules/constants.js +31 -1
  54. package/dist/modules/credit_runtime.d.ts +35 -5
  55. package/dist/modules/credit_runtime.js +219 -38
  56. package/dist/modules/dexbot_class.d.ts +22 -2
  57. package/dist/modules/dexbot_class.js +61 -21
  58. package/dist/modules/dexbot_cow_runtime.js +19 -18
  59. package/dist/modules/dexbot_fill_runtime.d.ts +1 -1
  60. package/dist/modules/dexbot_fill_runtime.js +4 -7
  61. package/dist/modules/dexbot_maintenance_runtime.d.ts +7 -2
  62. package/dist/modules/dexbot_maintenance_runtime.js +52 -7
  63. package/dist/modules/dexbot_startup_runtime.js +3 -2
  64. package/dist/modules/dexbot_state_recovery.js +1 -1
  65. package/dist/modules/graceful_shutdown.d.ts +6 -2
  66. package/dist/modules/graceful_shutdown.js +6 -2
  67. package/dist/modules/launcher/bot_supervisor.js +4 -0
  68. package/dist/modules/launcher/launch_modes.js +6 -3
  69. package/dist/modules/launcher/monolithic_runtime.d.ts +39 -1
  70. package/dist/modules/launcher/monolithic_runtime.js +60 -1
  71. package/dist/modules/launcher/runtime_mode.d.ts +79 -0
  72. package/dist/modules/launcher/runtime_mode.js +223 -0
  73. package/dist/modules/market_adapter_whitelist.d.ts +8 -0
  74. package/dist/modules/market_adapter_whitelist.js +8 -0
  75. package/dist/modules/order/accounting.d.ts +15 -1
  76. package/dist/modules/order/accounting.js +15 -1
  77. package/dist/modules/order/async_lock.d.ts +9 -3
  78. package/dist/modules/order/async_lock.js +9 -3
  79. package/dist/modules/order/export.d.ts +0 -5
  80. package/dist/modules/order/export.js +2 -7
  81. package/dist/modules/order/format.d.ts +6 -4
  82. package/dist/modules/order/format.js +6 -4
  83. package/dist/modules/order/grid.d.ts +147 -19
  84. package/dist/modules/order/grid.js +321 -68
  85. package/dist/modules/order/grid_reconcile.js +4 -3
  86. package/dist/modules/order/grid_reconcile_internal.js +13 -18
  87. package/dist/modules/order/index.d.ts +4 -2
  88. package/dist/modules/order/index.js +1 -1
  89. package/dist/modules/order/logger_state.d.ts +1 -1
  90. package/dist/modules/order/logger_state.js +1 -1
  91. package/dist/modules/order/manager.d.ts +1 -0
  92. package/dist/modules/order/manager.js +5 -14
  93. package/dist/modules/order/sync_engine.d.ts +26 -11
  94. package/dist/modules/order/sync_engine.js +44 -20
  95. package/dist/modules/order/utils/math.d.ts +157 -47
  96. package/dist/modules/order/utils/math.js +236 -78
  97. package/dist/modules/order/utils/order.d.ts +64 -20
  98. package/dist/modules/order/utils/order.js +80 -24
  99. package/dist/modules/order/utils/system.d.ts +41 -11
  100. package/dist/modules/order/utils/system.js +43 -13
  101. package/dist/modules/order/utils/validate.d.ts +6 -3
  102. package/dist/modules/order/utils/validate.js +53 -60
  103. package/dist/modules/types.d.ts +7 -0
  104. package/dist/modules/utils/asset_symbols.d.ts +5 -5
  105. package/dist/modules/utils/asset_symbols.js +5 -5
  106. package/dist/modules/utils/errors.d.ts +2 -1
  107. package/dist/modules/utils/errors.js +2 -1
  108. package/dist/scripts/analyze-credit.js +44 -27
  109. package/dist/scripts/diagnose-pool-history.js +35 -30
  110. package/dist/scripts/pnl.js +1 -1
  111. package/dist/scripts/rich_list.d.ts +50 -0
  112. package/dist/scripts/rich_list.js +469 -0
  113. package/dist/scripts/run-tests.js +80 -4
  114. package/dist/unlock.d.ts +2 -1
  115. package/dist/unlock.js +53 -21
  116. package/docs/BITSHARES_ONBOARDING.md +7 -0
  117. package/docs/COW_INVARIANTS.md +7 -1
  118. package/docs/DEXBOT_COMPARISON.md +6 -6
  119. package/docs/EVOLUTION.md +11 -9
  120. package/docs/FUND_MOVEMENT_AND_ACCOUNTING.md +3 -3
  121. package/docs/GRID_RECALCULATION.md +18 -6
  122. package/docs/LIFECYCLE.md +8 -8
  123. package/docs/LOGGING.md +16 -1
  124. package/docs/MPA_CREDIT_USAGE.md +10 -5
  125. package/docs/ORDER_ENGINE_POST_1.0_RETROSPECTIVE.md +163 -13
  126. package/docs/README.md +1 -1
  127. package/docs/WORKFLOW.md +6 -5
  128. package/docs/architecture.md +56 -2
  129. package/docs/developer_guide.md +5 -1
  130. package/market_adapter/README.md +14 -7
  131. package/modules/README.md +1 -1
  132. package/package.json +4 -1
  133. package/scripts/README.md +59 -32
  134. package/tests/README.md +40 -2
@@ -0,0 +1,469 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * BitShares per-asset rich list / top-holder reporter.
4
+ *
5
+ * Query one asset, or a list of assets, and print their top holders. Uses the
6
+ * chain's `asset_api` (`get_asset_holders`, `get_asset_holders_count`). Those
7
+ * endpoints are often restricted to allowlisted API users, so this script first
8
+ * tries candidate nodes that expose `asset_api` to anonymous logins.
9
+ *
10
+ * The returned holders are ordered by balance descending server-side (the
11
+ * account_balance_index `by_asset_balance` key sorts balance with
12
+ * `std::greater`), so page 0 is already the rich list.
13
+ *
14
+ * CLI: dexbot rich <ASSET> [ASSET...] [--top N]
15
+ * npm run richlist -- <ASSET> --top 10
16
+ */
17
+ import { pathToFileURL } from 'node:url';
18
+ import { normalizeAssetSymbol } from '../modules/utils/asset_symbols.js';
19
+ const DB_API = 0; // database_api is always registered first
20
+ const LOGIN_API = 1; // login_api (registered second)
21
+ const FALLBACK_HOLDER_LIMIT = 100; // used when the node does not report api_limit_get_asset_holders
22
+ const LOOKUP_PAGE = 100; // chunk size for lookup_asset_symbols / get_objects
23
+ /** Nodes known to expose `asset_api` to anonymous logins come first. */
24
+ const DEFAULT_NODES = [
25
+ 'wss://dex.iobanker.com/ws',
26
+ 'wss://api.dex.trading/',
27
+ 'wss://api.bitshares.dev/ws',
28
+ 'wss://cloud.xbts.io/ws',
29
+ 'wss://node.xbts.io/ws',
30
+ 'wss://api.bts.mobi/ws',
31
+ ];
32
+ class UsageError extends Error {
33
+ }
34
+ // ---------------------------------------------------------------------------
35
+ // CLI parsing
36
+ // ---------------------------------------------------------------------------
37
+ function printHelp() {
38
+ console.log(`BitShares per-asset rich list.
39
+
40
+ Usage:
41
+ dexbot rich <ASSET> [ASSET...] [--top N]
42
+
43
+ Examples:
44
+ dexbot rich <asset>
45
+ dexbot rich <asset-a> <asset-b> <asset-c> --top 10
46
+ dexbot rich <asset-a> <asset-b> --json
47
+
48
+ Flags:
49
+ --top N Holders per asset (default: node holder limit, usually 100)
50
+ --concurrency N Parallel asset queries (default 8)
51
+ --node URL Pin websocket node (repeatable)
52
+ --json JSON output
53
+ --csv CSV output
54
+ -h, --help This help`);
55
+ }
56
+ function parseArgs(argv) {
57
+ const VALUE_FLAGS = new Set(['--top', '--concurrency', '--node']);
58
+ // Normalise `--flag=value` into `--flag value` for the value-taking flags.
59
+ const args = [];
60
+ for (const a of argv) {
61
+ const eq = a.indexOf('=');
62
+ if (a.startsWith('--') && eq > 0 && VALUE_FLAGS.has(a.slice(0, eq))) {
63
+ args.push(a.slice(0, eq), a.slice(eq + 1));
64
+ }
65
+ else {
66
+ args.push(a);
67
+ }
68
+ }
69
+ const opts = {
70
+ assets: [],
71
+ top: 0, // 0 = unset; resolved after the node holder limit is known
72
+ topSet: false,
73
+ concurrency: 8,
74
+ nodes: [],
75
+ json: false,
76
+ csv: false,
77
+ help: false,
78
+ };
79
+ for (let i = 0; i < args.length; i++) {
80
+ const a = args[i];
81
+ const next = () => {
82
+ const v = args[++i];
83
+ if (v === undefined)
84
+ throw new UsageError(`Missing value for ${a}`);
85
+ return v;
86
+ };
87
+ if (a === '--top') {
88
+ opts.top = Number(next());
89
+ opts.topSet = true;
90
+ }
91
+ else if (a === '--concurrency')
92
+ opts.concurrency = Number(next());
93
+ else if (a === '--node')
94
+ opts.nodes.push(next());
95
+ else if (a === '--json')
96
+ opts.json = true;
97
+ else if (a === '--csv')
98
+ opts.csv = true;
99
+ else if (a === '-h' || a === '--help')
100
+ opts.help = true;
101
+ else if (a.startsWith('-'))
102
+ throw new UsageError(`Unknown flag: ${a}`);
103
+ else
104
+ opts.assets.push(a);
105
+ }
106
+ if (opts.topSet && (!Number.isInteger(opts.top) || opts.top < 1)) {
107
+ throw new UsageError('--top must be a positive integer');
108
+ }
109
+ if (!Number.isInteger(opts.concurrency) || opts.concurrency < 1)
110
+ throw new UsageError('--concurrency must be >= 1');
111
+ if (opts.assets.length === 0 && !opts.help) {
112
+ throw new UsageError('Provide at least one ASSET (or an asset list).');
113
+ }
114
+ if (opts.json && opts.csv)
115
+ throw new UsageError('--json and --csv are mutually exclusive');
116
+ return opts;
117
+ }
118
+ class RpcClient {
119
+ url;
120
+ _id = 0;
121
+ _pending = new Map();
122
+ _closed = false;
123
+ _ws = null;
124
+ constructor(url) {
125
+ this.url = url;
126
+ }
127
+ connect(timeoutMs = 15000) {
128
+ return new Promise((resolve, reject) => {
129
+ let settled = false;
130
+ const ws = new WebSocket(this.url);
131
+ this._ws = ws;
132
+ const timer = setTimeout(() => {
133
+ if (settled)
134
+ return;
135
+ settled = true;
136
+ try {
137
+ ws.close();
138
+ }
139
+ catch { /* ignore */ }
140
+ reject(new Error(`connect timeout: ${this.url}`));
141
+ }, timeoutMs);
142
+ ws.onopen = () => {
143
+ if (settled)
144
+ return;
145
+ settled = true;
146
+ clearTimeout(timer);
147
+ resolve();
148
+ };
149
+ ws.onerror = () => {
150
+ if (settled)
151
+ return;
152
+ settled = true;
153
+ clearTimeout(timer);
154
+ reject(new Error(`connect error: ${this.url}`));
155
+ };
156
+ ws.onclose = () => {
157
+ this._closed = true;
158
+ for (const { reject: rj } of this._pending.values())
159
+ rj(new Error('connection closed'));
160
+ this._pending.clear();
161
+ };
162
+ ws.onmessage = (ev) => {
163
+ let msg;
164
+ try {
165
+ msg = JSON.parse(typeof ev.data === 'string' ? ev.data : String(ev.data));
166
+ }
167
+ catch {
168
+ return;
169
+ }
170
+ const p = this._pending.get(msg.id);
171
+ if (!p)
172
+ return;
173
+ this._pending.delete(msg.id);
174
+ if (msg.error)
175
+ p.reject(new Error(msg.error.message || JSON.stringify(msg.error)));
176
+ else
177
+ p.resolve(msg.result);
178
+ };
179
+ });
180
+ }
181
+ call(api, method, args, timeoutMs = 30000) {
182
+ if (this._closed)
183
+ return Promise.reject(new Error('connection closed'));
184
+ return new Promise((resolve, reject) => {
185
+ const id = ++this._id;
186
+ const timer = setTimeout(() => {
187
+ if (!this._pending.has(id))
188
+ return;
189
+ this._pending.delete(id);
190
+ reject(new Error(`rpc timeout: ${method}`));
191
+ }, timeoutMs);
192
+ this._pending.set(id, {
193
+ resolve: (v) => { clearTimeout(timer); resolve(v); },
194
+ reject: (e) => { clearTimeout(timer); reject(e); },
195
+ });
196
+ this._ws?.send(JSON.stringify({ jsonrpc: '2.0', id, method: 'call', params: [api, method, args] }));
197
+ });
198
+ }
199
+ close() {
200
+ try {
201
+ this._ws?.close();
202
+ }
203
+ catch { /* ignore */ }
204
+ }
205
+ }
206
+ // ---------------------------------------------------------------------------
207
+ // Asset API session
208
+ // ---------------------------------------------------------------------------
209
+ /** Connect to the first node that grants anonymous access to `asset_api`. */
210
+ async function openSession(nodes) {
211
+ const errors = [];
212
+ for (const url of nodes) {
213
+ const client = new RpcClient(url);
214
+ try {
215
+ await client.connect();
216
+ await client.call(LOGIN_API, 'login', [null, null]);
217
+ const assetApi = await client.call(LOGIN_API, 'asset', []);
218
+ if (typeof assetApi !== 'number')
219
+ throw new Error('asset() did not return an API id');
220
+ const holderLimit = await readHolderLimit(client);
221
+ return { client, assetApi, dbApi: DB_API, holderLimit };
222
+ }
223
+ catch (e) {
224
+ errors.push(`${url}: ${e.message}`);
225
+ client.close();
226
+ }
227
+ }
228
+ throw new Error(`No node exposes asset_api.\n ${errors.join('\n ')}`);
229
+ }
230
+ /** Read the node's configured get_asset_holders page cap, falling back to 100. */
231
+ async function readHolderLimit(client) {
232
+ try {
233
+ const cfg = await client.call(LOGIN_API, 'get_config', []);
234
+ const limit = cfg?.api_limit_get_asset_holders;
235
+ if (typeof limit === 'number' && limit > 0)
236
+ return limit;
237
+ }
238
+ catch { /* older nodes omit it */ }
239
+ return FALLBACK_HOLDER_LIMIT;
240
+ }
241
+ // ---------------------------------------------------------------------------
242
+ // Chain queries
243
+ // ---------------------------------------------------------------------------
244
+ export async function lookupSymbols(session, symbolsOrIds) {
245
+ const out = [];
246
+ for (let i = 0; i < symbolsOrIds.length; i += LOOKUP_PAGE) {
247
+ const slice = symbolsOrIds.slice(i, i + LOOKUP_PAGE);
248
+ // `lookup_asset_symbols` is case-sensitive: a lowercase symbol returns
249
+ // null on-chain, so canonicalize before handing it to the node. Asset
250
+ // object ids ("1.3.x") pass through untouched. `request` keeps the
251
+ // caller's spelling so "asset not found" still echoes the input.
252
+ const refs = slice.map((s) => normalizeAssetSymbol(s));
253
+ const res = await session.client.call(session.dbApi, 'lookup_asset_symbols', [refs]);
254
+ res.forEach((a, j) => out.push(a
255
+ ? { request: slice[j], found: true, id: a.id, symbol: a.symbol, precision: a.precision }
256
+ : { request: slice[j], found: false }));
257
+ }
258
+ return out;
259
+ }
260
+ /**
261
+ * Look up symbols/ids. Legacy `bit*` MPA names were renamed on-chain without the
262
+ * `bit` prefix, so unresolved `bit*` names are retried stripped and reported as
263
+ * an alias.
264
+ */
265
+ export async function fetchAssets(session, symbolsOrIds) {
266
+ const out = await lookupSymbols(session, symbolsOrIds);
267
+ const retry = [];
268
+ out.forEach((a, i) => {
269
+ if (!a.found && /^bit./i.test(a.request) && a.request.length > 3)
270
+ retry.push(i);
271
+ });
272
+ if (retry.length > 0) {
273
+ const aliased = await lookupSymbols(session, retry.map((i) => out[i].request.slice(3)));
274
+ retry.forEach((i, j) => {
275
+ const a = aliased[j];
276
+ if (a.found)
277
+ out[i] = { ...a, request: out[i].request, alias: out[i].request };
278
+ });
279
+ }
280
+ return out;
281
+ }
282
+ async function fetchSupplies(session, assetIds) {
283
+ const map = new Map();
284
+ if (assetIds.length === 0)
285
+ return map;
286
+ // dynamic asset data id mirrors the asset instance: 1.3.N -> 2.3.N
287
+ const dynIds = assetIds.map((id) => `2.3.${id.split('.')[2]}`);
288
+ for (let i = 0; i < dynIds.length; i += LOOKUP_PAGE) {
289
+ const slice = dynIds.slice(i, i + LOOKUP_PAGE);
290
+ const objs = await session.client.call(session.dbApi, 'get_objects', [slice]);
291
+ slice.forEach((_, j) => {
292
+ const o = objs[j];
293
+ if (o)
294
+ map.set(assetIds[i + j], o.current_supply);
295
+ });
296
+ }
297
+ return map;
298
+ }
299
+ async function fetchTopHolders(session, asset, top) {
300
+ const holders = await session.client.call(session.assetApi, 'get_asset_holders', [asset.id, 0, top]);
301
+ const count = await session.client.call(session.assetApi, 'get_asset_holders_count', [asset.id]);
302
+ return { holders: holders || [], count };
303
+ }
304
+ /** Run `fn` over items with bounded concurrency, preserving order. */
305
+ async function mapPool(items, limit, fn) {
306
+ const results = new Array(items.length);
307
+ let next = 0;
308
+ async function worker() {
309
+ while (true) {
310
+ const i = next++;
311
+ if (i >= items.length)
312
+ return;
313
+ results[i] = await fn(items[i], i);
314
+ }
315
+ }
316
+ const workers = Array.from({ length: Math.min(limit, items.length) }, () => worker());
317
+ await Promise.all(workers);
318
+ return results;
319
+ }
320
+ // ---------------------------------------------------------------------------
321
+ // Formatting
322
+ // ---------------------------------------------------------------------------
323
+ function formatAmount(raw, precision) {
324
+ const s = BigInt(raw).toString().padStart(precision + 1, '0');
325
+ const intPart = s.slice(0, s.length - precision);
326
+ let frac = precision > 0 ? s.slice(s.length - precision) : '';
327
+ frac = frac.replace(/0+$/, '');
328
+ return intPart + (frac ? `.${frac}` : '');
329
+ }
330
+ function pct(raw, supply) {
331
+ if (!supply || supply === '0')
332
+ return '';
333
+ const bp = (BigInt(raw) * 10000n) / BigInt(supply); // basis points
334
+ return `${bp / 100n}.${(bp % 100n).toString().padStart(2, '0')}%`;
335
+ }
336
+ const NAME_MAX_WIDTH = 30;
337
+ /** Pick a stable name column width for one asset, capped for readability. */
338
+ function nameColumnWidth(holders) {
339
+ const longest = holders.reduce((m, h) => Math.max(m, h.name.length), 0);
340
+ return Math.min(longest, NAME_MAX_WIDTH);
341
+ }
342
+ function padName(name, width) {
343
+ if (name.length <= width)
344
+ return name.padEnd(width);
345
+ return name.slice(0, width - 1) + '\u2026';
346
+ }
347
+ function widthOf(values) {
348
+ return values.reduce((m, v) => Math.max(m, v.length), 0);
349
+ }
350
+ function printTable(rows) {
351
+ for (const r of rows) {
352
+ if (!r.found) {
353
+ console.error(`?? ${r.request}: asset not found`);
354
+ continue;
355
+ }
356
+ const head = `${r.symbol} (${r.id}) precision=${r.precision}` +
357
+ (r.count != null ? ` holders=${r.count}` : '') +
358
+ (r.supply ? ` supply=${formatAmount(r.supply, r.precision ?? 0)}` : '');
359
+ console.log(`\n${head}`);
360
+ if (r.holders.length === 0) {
361
+ console.log(' (no holders)');
362
+ continue;
363
+ }
364
+ console.log(` ${r.holders.length > 1 ? `top ${r.holders.length}` : 'top holder'}:`);
365
+ const nameWidth = nameColumnWidth(r.holders);
366
+ const rankWidth = String(r.holders.length).length;
367
+ const amounts = r.holders.map((h) => formatAmount(h.amount, r.precision ?? 0));
368
+ const shares = r.holders.map((h) => pct(h.amount, r.supply));
369
+ const amountWidth = widthOf(amounts);
370
+ const shareWidth = widthOf(shares);
371
+ r.holders.forEach((h, i) => {
372
+ const share = shares[i];
373
+ console.log(` #${String(i + 1).padStart(rankWidth)} ${padName(h.name, nameWidth)} ${amounts[i].padStart(amountWidth)}` +
374
+ (share ? ` ${share.padStart(shareWidth)}` : '') + ` ${h.account_id}`);
375
+ });
376
+ }
377
+ }
378
+ function printCsv(rows) {
379
+ console.log('symbol,asset_id,rank,account,account_id,amount,percent');
380
+ for (const r of rows) {
381
+ if (!r.found)
382
+ continue;
383
+ r.holders.forEach((h, i) => {
384
+ console.log([r.symbol, r.id, i + 1, h.name, h.account_id,
385
+ formatAmount(h.amount, r.precision ?? 0), pct(h.amount, r.supply)].join(','));
386
+ });
387
+ }
388
+ }
389
+ // ---------------------------------------------------------------------------
390
+ // Main
391
+ // ---------------------------------------------------------------------------
392
+ async function collectRows(session, assets, opts) {
393
+ const showProgress = !opts.json && !opts.csv && assets.length > 10;
394
+ let done = 0;
395
+ const queried = await mapPool(assets, opts.concurrency, async (asset) => {
396
+ const { holders, count } = await fetchTopHolders(session, asset, opts.top);
397
+ if (showProgress) {
398
+ done++;
399
+ process.stderr.write(`\r queried ${done}/${assets.length} assets ...`);
400
+ }
401
+ return { ...asset, holders, count };
402
+ });
403
+ if (showProgress)
404
+ process.stderr.write('\r');
405
+ const supplies = await fetchSupplies(session, queried.map((r) => r.id));
406
+ return queried.map((r) => ({ ...r, supply: supplies.get(r.id) || null }));
407
+ }
408
+ async function main() {
409
+ let opts;
410
+ try {
411
+ opts = parseArgs(process.argv.slice(2));
412
+ }
413
+ catch (e) {
414
+ if (e instanceof UsageError) {
415
+ console.error(`Error: ${e.message}\n`);
416
+ printHelp();
417
+ process.exit(2);
418
+ }
419
+ throw e;
420
+ }
421
+ if (opts.help) {
422
+ printHelp();
423
+ return;
424
+ }
425
+ const nodes = opts.nodes.length > 0 ? opts.nodes : DEFAULT_NODES;
426
+ const session = await openSession(nodes);
427
+ // Resolve --top against the node's advertised cap now that we know it.
428
+ if (!opts.topSet)
429
+ opts.top = session.holderLimit;
430
+ if (opts.top > session.holderLimit) {
431
+ if (!opts.json && !opts.csv) {
432
+ console.error(`Note: --top ${opts.top} clamped to node limit ${session.holderLimit}.`);
433
+ }
434
+ opts.top = session.holderLimit;
435
+ }
436
+ try {
437
+ const looked = await fetchAssets(session, opts.assets);
438
+ const assets = looked.filter((a) => {
439
+ if (!a.found) {
440
+ console.error(`Warning: asset not found: ${a.request}`);
441
+ return false;
442
+ }
443
+ if (a.alias)
444
+ console.error(`Note: ${a.alias} was renamed; using ${a.symbol}.`);
445
+ return a.found;
446
+ });
447
+ if (assets.length === 0) {
448
+ console.error('No valid assets.');
449
+ process.exit(1);
450
+ }
451
+ const rows = await collectRows(session, assets, opts);
452
+ if (opts.json)
453
+ console.log(JSON.stringify(rows, null, 2));
454
+ else if (opts.csv)
455
+ printCsv(rows);
456
+ else
457
+ printTable(rows);
458
+ }
459
+ finally {
460
+ session.client.close();
461
+ }
462
+ }
463
+ if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
464
+ main().catch((e) => {
465
+ console.error(`Error: ${e.message}`);
466
+ process.exit(1);
467
+ });
468
+ }
469
+ //# sourceMappingURL=rich_list.js.map
@@ -1,5 +1,6 @@
1
- import { globSync, mkdirSync, appendFileSync } from 'node:fs';
1
+ import { globSync, mkdirSync, appendFileSync, readFileSync, rmSync } from 'node:fs';
2
2
  import { spawn } from 'child_process';
3
+ import os from 'node:os';
3
4
  import path from 'node:path';
4
5
  // ── Intentionally uses process.env directly, NOT Config ───────────
5
6
  // This script runs BEFORE any module is loaded — Config's startup
@@ -67,13 +68,76 @@ const diagHits = [];
67
68
  // Per-test watchdog: a test that neither exits nor errors would otherwise
68
69
  // stall the whole sequential run. Override with DEXBOT_TEST_TIMEOUT_MS.
69
70
  const TEST_TIMEOUT_MS = Number(process.env.DEXBOT_TEST_TIMEOUT_MS || 240000);
71
+ // ── ESM-mock preload ──────────────────────────────────────────────
72
+ // Tests that call esmMockEntry() re-exec themselves with --import loader hooks
73
+ // when the hooks are not yet active. The runner can install those hooks up
74
+ // front, letting the test run in the process it was already spawned into and
75
+ // skipping one full node startup + module-graph load per file. Stage-based
76
+ // tests with multiple stages are excluded: their stages need per-stage worker
77
+ // isolation and the preload would defeat it. Single-stage runEsmMockStages()
78
+ // files are eligible, though: with the hooks already active the helper's
79
+ // in-process branch runs its one stage directly instead of spawning a worker.
80
+ const ESM_HOOKS_FILE = path.join(process.cwd(), 'dist', 'tests', 'helpers', 'esm_mock_hooks.mjs');
81
+ const ESM_ENV_ACTIVE = 'DEXBOT_ESM_MOCKS_ACTIVE';
82
+ const ESM_ENV_MANIFEST = 'DEXBOT_ESM_MOCK_MANIFEST';
83
+ const esmPreloadCache = new Map();
84
+ // Matches runEsmMockStages(['only-stage'], ...) — a single string literal in the
85
+ // stage array. Multi-stage calls (comma-separated literals,
86
+ // Object.keys(STAGES), a stage-list variable, or runEsmMockStagesGrouped/Shared)
87
+ // do not match, so only unambiguously single-stage files opt in.
88
+ const SINGLE_STAGE_CALL_RE = /runEsmMockStages\(\s*\[\s*(['"])[^'"]+\1\s*\]\s*,/g;
89
+ const STAGE_CALL_RE = /runEsmMockStages\(/g;
90
+ function isSingleStageTest(src) {
91
+ if (!src.includes('runEsmMockStages('))
92
+ return false;
93
+ const calls = src.match(STAGE_CALL_RE) || [];
94
+ const single = src.match(SINGLE_STAGE_CALL_RE) || [];
95
+ return calls.length > 0 && single.length === calls.length;
96
+ }
97
+ function needsEsmPreload(testFile) {
98
+ const cached = esmPreloadCache.get(testFile);
99
+ if (cached !== undefined)
100
+ return cached;
101
+ let needed = false;
102
+ try {
103
+ const src = readFileSync(testFile, 'utf8');
104
+ needed = (src.includes('esmMockEntry(') && !src.includes('runEsmMockStages'))
105
+ || isSingleStageTest(src);
106
+ }
107
+ catch {
108
+ needed = false;
109
+ }
110
+ esmPreloadCache.set(testFile, needed);
111
+ return needed;
112
+ }
70
113
  function runOne(testFile) {
71
114
  return new Promise((resolve) => {
72
115
  const start = performance.now();
73
- const child = spawn(process.execPath, [testFile], {
116
+ const preload = needsEsmPreload(testFile);
117
+ let manifestPath = null;
118
+ let childArgs = [testFile];
119
+ let childEnv = process.env;
120
+ if (preload) {
121
+ const manifestDir = path.join(logDir, 'esm-mock-manifests');
122
+ mkdirSync(manifestDir, { recursive: true });
123
+ const stamp = path.basename(testFile).replace(/\W+/g, '_');
124
+ manifestPath = path.join(manifestDir, `${stamp}-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2, 8)}.json`);
125
+ childArgs = ['--import', ESM_HOOKS_FILE, testFile];
126
+ childEnv = { ...process.env, [ESM_ENV_ACTIVE]: '1', [ESM_ENV_MANIFEST]: manifestPath };
127
+ }
128
+ const cleanupManifest = () => {
129
+ if (!manifestPath)
130
+ return;
131
+ try {
132
+ rmSync(manifestPath, { force: true });
133
+ }
134
+ catch { /* best effort */ }
135
+ manifestPath = null;
136
+ };
137
+ const child = spawn(process.execPath, childArgs, {
74
138
  stdio: ['inherit', 'pipe', 'pipe'],
75
139
  cwd: process.cwd(),
76
- env: process.env,
140
+ env: childEnv,
77
141
  });
78
142
  let timedOut = false;
79
143
  const killTimer = setTimeout(() => {
@@ -99,6 +163,7 @@ function runOne(testFile) {
99
163
  child.stderr.on('data', accumulate);
100
164
  child.on('close', (code, signal) => {
101
165
  clearTimeout(killTimer);
166
+ cleanupManifest();
102
167
  // Slice the buffered output into lines and scan for diagnostics.
103
168
  const seenForTest = new Set();
104
169
  for (const line of buffer.split(/\r?\n/)) {
@@ -126,6 +191,7 @@ function runOne(testFile) {
126
191
  });
127
192
  child.on('error', (err) => {
128
193
  clearTimeout(killTimer);
194
+ cleanupManifest();
129
195
  resolve({
130
196
  test: testFile,
131
197
  elapsedMs: performance.now() - start,
@@ -145,7 +211,17 @@ async function main() {
145
211
  let startTotal = performance.now();
146
212
  const concurrency = (() => {
147
213
  const raw = Number(process.env.DEXBOT_TEST_CONCURRENCY);
148
- return Number.isFinite(raw) && raw >= 1 ? Math.floor(raw) : 1;
214
+ if (Number.isFinite(raw) && raw >= 1)
215
+ return Math.floor(raw);
216
+ // Default to a small parallel fan-out. With one node process per test,
217
+ // sequential runs spend most of their wall time in per-process startup
218
+ // and module-graph evaluation. Files are isolated (per-PID tests/tmp),
219
+ // so bounded parallelism is safe. Set DEXBOT_TEST_CONCURRENCY=1 to force
220
+ // the old sequential behavior.
221
+ const cores = typeof os.availableParallelism === 'function'
222
+ ? os.availableParallelism()
223
+ : os.cpus().length;
224
+ return Math.max(1, Math.min(cores, 4));
149
225
  })();
150
226
  const toRun = [];
151
227
  for (const testFile of testFiles) {
package/dist/unlock.d.ts CHANGED
@@ -18,9 +18,10 @@ declare function runStartOnboardingIfNeeded({ allowInteractive, nonInteractiveFl
18
18
  allowInteractive?: boolean;
19
19
  nonInteractiveFlag?: string;
20
20
  }): Promise<boolean>;
21
- declare function main({ argv, startupGraceMs, exitAfterOnboarding, onboard }?: {
21
+ declare function main({ argv, startupGraceMs, isolatedPollIntervalMs, exitAfterOnboarding, onboard }?: {
22
22
  argv?: string[];
23
23
  startupGraceMs?: number;
24
+ isolatedPollIntervalMs?: number;
24
25
  exitAfterOnboarding?: boolean;
25
26
  onboard?: boolean;
26
27
  }): Promise<void>;