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
package/dist/dexbot.js CHANGED
@@ -47,7 +47,7 @@ const __dirname = _esmDirname(__filename);
47
47
  * Runtime: dexbot start [bot] | stop | reload | restart | delete | status | pm2
48
48
  * Trading: dexbot test <bot> | drystart <bot> | reset {all|<bot>}
49
49
  * Config: dexbot bot | key | enable {all|<bot>} | disable {all|<bot>} | default | update
50
- * Analysis: dexbot order [<bot>] | export <bot> | credit [<bot>]
50
+ * Analysis: dexbot order [<bot>] | export <bot> | credit [<bot>] | rich <asset...>
51
51
  * dexbot tv <target> | dw <target> | pnl <account>
52
52
  * Files: dexbot clear | clear-orders | clear-market-adapter | clear-all
53
53
  *
@@ -122,6 +122,7 @@ const { setupGracefulShutdown, registerCleanup, unregisterCleanup } = require('.
122
122
  const { collectValidationIssues, loadSettingsFile, normalizeBotEntries, resolveRawBotEntries, saveSettingsFile, } = require('./modules/bot_settings');
123
123
  const { buildRuntimeScriptArgs } = require('./modules/launcher/runtime_entry');
124
124
  const { parseWorkerArgs, LAUNCHER_WORKER_COMMAND } = require('./modules/launcher/launch_modes');
125
+ const { detectRuntimeMode, runtimeControlHelp, runtimeModeLabel, checkStatusTarget, resolveStatusRoute } = require('./modules/launcher/runtime_mode');
125
126
  const { PATHS, getHomeProfilesDir, getRecalculateTriggerFile, printRelocationNotices } = require('./modules/paths');
126
127
  const credentialPolicy = require('./modules/credential_policy');
127
128
  const { Config } = require('./modules/config');
@@ -142,7 +143,7 @@ if (typeof credentialPolicy.checkPolicyFileSecurity === 'function')
142
143
  // Primary CLI driver that manages tracked bots and helper utilities such as key/bot editors.
143
144
  const PROFILES_BOTS_FILE = PATHS.PROFILES.BOTS_JSON;
144
145
  const PROFILES_DIR = PATHS.PROFILES_DIR;
145
- const CLI_COMMANDS = ['start', 'test', 'reset', 'default', 'disable', 'enable', 'drystart', 'key', 'bot', 'pm2', 'update', 'export', 'order', 'credit', 'tv', 'dw', 'pnl', 'git', 'clear', 'clear-orders', 'clear-market-adapter', 'clear-all', 'status', 'unlock', 'delete', 'stop', 'restart', 'reload', 'help'];
146
+ const CLI_COMMANDS = ['start', 'test', 'reset', 'default', 'disable', 'enable', 'drystart', 'key', 'bot', 'pm2', 'update', 'export', 'order', 'credit', 'rich', 'tv', 'dw', 'pnl', 'git', 'clear', 'clear-orders', 'clear-market-adapter', 'clear-all', 'status', 'unlock', 'delete', 'stop', 'restart', 'reload', 'help'];
146
147
  const COMMAND_ALIASES = { orders: 'order', keys: 'key', bots: 'bot', stat: 'status', stats: 'status', start: 'unlock', defaults: 'default', stp: 'stop', stopall: 'stop', restartall: 'restart', reloadall: 'reload' };
147
148
  const CLI_HELP_FLAGS = ['-h', '--help'];
148
149
  const CLI_EXAMPLES_FLAG = '--cli-examples';
@@ -157,6 +158,7 @@ const CLI_EXAMPLES = [
157
158
  { title: 'Export bot trades', command: 'dexbot export <bot>', notes: 'Write trades and settings to CSV/JSON.' },
158
159
  { title: 'Analyze order grids', command: 'dexbot order', notes: 'Spread, increment, and funds; --export for HTML.' },
159
160
  { title: 'Show live credit positions', command: 'dexbot credit', notes: 'Summed MPA + borrowed-credit positions per bot.' },
161
+ { title: 'Asset rich list', command: 'dexbot rich <asset> [<asset...>]', notes: 'Top holder(s) per asset from the chain asset holders API.' },
160
162
  { title: 'Chart a bot, pool, or pair', command: 'dexbot tv <bot|pool-id|AssetA/AssetB> --month 3', notes: 'OHLC chart; --feed charts MPA price-feed history.' },
161
163
  { title: 'PnL report', command: 'dexbot pnl <bot|account> --month 3 [--pair BASE/QUOTE]', notes: 'Self-contained HTML PnL report for the resolved bot or chain account.' }
162
164
  ];
@@ -185,17 +187,13 @@ const cliArgs = process.argv.slice(2);
185
187
  */
186
188
  function printCLIUsage() {
187
189
  // Grouped so the full command set stays scannable; descriptions are one line each
188
- // and the description column is derived, so alignment never drifts.
190
+ // and the description column is derived, so alignment never drifts. The
191
+ // Runtime group is phrased for the runtime that is actually up — the
192
+ // detection and the mode→instructions mapping are centralized in
193
+ // modules/launcher/runtime_mode.ts so `help` and `status` agree.
194
+ const runtime = detectRuntimeMode();
189
195
  const groups = [
190
- ['Runtime', [
191
- ['start [bot]', 'Start the bot runtime (flags below).'],
192
- ['stop', 'Stop the runtime.'],
193
- ['reload', 'Reload the runtime without re-unlocking keys.'],
194
- ['restart', 'Restart the runtime and re-unlock keys.'],
195
- ['delete', 'Stop and delete all runtime processes.'],
196
- ['status', 'Show runtime status.'],
197
- ['pm2 [<sub>]', 'Manage PM2 bots (see: dexbot pm2 help).'],
198
- ]],
196
+ ['Runtime', runtimeControlHelp(runtime.mode).map((entry) => [entry.command, entry.description])],
199
197
  ['Trading', [
200
198
  ['test <bot>', 'Run a bot live once.'],
201
199
  ['drystart <bot>', 'Run a bot without broadcasting.'],
@@ -213,6 +211,7 @@ function printCLIUsage() {
213
211
  ['order [<bot>]', 'Analyze persisted order grids; --export for HTML.'],
214
212
  ['export <bot>', 'Export bot trades/settings to CSV/JSON.'],
215
213
  ['credit [<bot>]', 'Show live MPA and borrowed-credit positions.'],
214
+ ['rich <asset...>', 'Per-asset rich list / top holder; --top N, --json.'],
216
215
  ['tv <target>', 'OHLC chart for <bot|pool-id|AssetA/AssetB>; --month N (default 3), --feed for MPA price-feed history.'],
217
216
  ['dw <target>', 'Dynamic-weight research chart; same targets and flags as tv.'],
218
217
  ['pnl <account>', 'HTML PnL report for a bot or account; --month N, optional --pair BASE/QUOTE filter.'],
@@ -226,6 +225,12 @@ function printCLIUsage() {
226
225
  ];
227
226
  const width = Math.max(...groups.flatMap(([, entries]) => entries.map(([cmd]) => cmd.length))) + 2;
228
227
  console.log('Usage: dexbot [command] [bot]');
228
+ console.log(`Active runtime: ${runtimeModeLabel(runtime)}`);
229
+ if (runtime.mode === 'none') {
230
+ console.log(' Standard start: dexbot start (monolithic)');
231
+ console.log(' Alternatives: dexbot start --isolated | dexbot pm2');
232
+ }
233
+ console.log();
229
234
  for (const [title, entries] of groups) {
230
235
  console.log(`${title}:`);
231
236
  for (const [cmd, desc] of entries) {
@@ -260,7 +265,7 @@ if (cliArgs.some(arg => CLI_HELP_FLAGS.includes(arg))) {
260
265
  // so the script prints its usage. Only scripts with offline help handling
261
266
  // belong here — forwarding to a script without it could misinterpret the
262
267
  // flag as input (e.g. a bot-name filter triggering live work).
263
- const HELP_OWNING_COMMANDS = new Set(['credit', 'tv', 'dw', 'pnl']);
268
+ const HELP_OWNING_COMMANDS = new Set(['credit', 'rich', 'tv', 'dw', 'pnl']);
264
269
  const requestedCommand = COMMAND_ALIASES[cliArgs[0]] ?? cliArgs[0];
265
270
  if (!HELP_OWNING_COMMANDS.has(requestedCommand)) {
266
271
  printCLIUsage();
@@ -1008,6 +1013,24 @@ async function handleCLICommands() {
1008
1013
  process.exit(result.status ?? 0);
1009
1014
  return true;
1010
1015
  }
1016
+ case 'rich': {
1017
+ const { spawnSync } = require('child_process');
1018
+ const scriptArgs = buildRuntimeScriptArgs({
1019
+ codeRoot: __dirname,
1020
+ scriptSegments: ['scripts', 'rich_list'],
1021
+ scriptArgs: cliArgs.slice(1),
1022
+ });
1023
+ const result = spawnSync(Config.EXEC_PATH, scriptArgs, {
1024
+ cwd: PATHS.PROJECT_ROOT,
1025
+ stdio: 'inherit',
1026
+ });
1027
+ if (result.error) {
1028
+ console.error(`rich: ${result.error.message}`);
1029
+ process.exit(1);
1030
+ }
1031
+ process.exit(result.status ?? 0);
1032
+ return true;
1033
+ }
1011
1034
  case 'git': {
1012
1035
  // Repository statistics analyzer (scripts/analyze-git.ts): parses
1013
1036
  // git history and writes analysis/charts/repo-stats.html. Git-only;
@@ -1145,60 +1168,18 @@ async function handleCLICommands() {
1145
1168
  process.exit(code);
1146
1169
  return true;
1147
1170
  };
1148
- const { spawnSync, execSync } = require('child_process');
1149
- const MONOLITHIC_PID_FILE = PATHS.PROFILES.MONOLITHIC_PID;
1150
- const MONOLITHIC_CRED_PID_FILE = PATHS.PROFILES.MONOLITHIC_CRED_PID;
1151
- const SUPERVISOR_SOCK = PATHS.PROFILES.SUPERVISOR_SOCK;
1152
- let unlockRunning = false;
1153
- if (storage.exists(MONOLITHIC_PID_FILE)) {
1154
- try {
1155
- const pid = Number(storage.readFile(MONOLITHIC_PID_FILE).trim());
1156
- if (Number.isInteger(pid) && pid > 0) {
1157
- try {
1158
- process.kill(pid, 0);
1159
- unlockRunning = true;
1160
- }
1161
- catch (err) {
1162
- if (getErrorCode(err) === 'EACCES') {
1163
- console.warn('[dexbot]', `process.kill(${pid}, 0) EACCES — process exists but permission denied`);
1164
- unlockRunning = true;
1165
- }
1166
- else if (getErrorCode(err) !== 'ESRCH') {
1167
- console.warn('[dexbot]', `process.kill(${pid}, 0) unexpected error: ${getErrorMessage(err)}`);
1168
- }
1169
- }
1170
- }
1171
- }
1172
- catch (_) { }
1173
- }
1174
- if (!unlockRunning && storage.exists(SUPERVISOR_SOCK)) {
1175
- unlockRunning = true;
1176
- }
1177
- // A credential daemon can outlive a `dexbot stop` (bots + market
1178
- // adapter stop; the daemon stays up for fast re-unlock). Report it
1179
- // too, so `stat` does not show "No DEXBot2 processes running." when
1180
- // only the daemon remains.
1181
- if (!unlockRunning && storage.exists(MONOLITHIC_CRED_PID_FILE)) {
1182
- try {
1183
- const pid = Number(storage.readFile(MONOLITHIC_CRED_PID_FILE).trim());
1184
- if (Number.isInteger(pid) && pid > 0) {
1185
- try {
1186
- process.kill(pid, 0);
1187
- unlockRunning = true;
1188
- }
1189
- catch (err) {
1190
- if (getErrorCode(err) === 'EACCES')
1191
- unlockRunning = true;
1192
- }
1193
- }
1194
- }
1195
- catch (_) { }
1196
- }
1197
- if (unlockRunning) {
1171
+ const { spawnSync } = require('child_process');
1172
+ // Centralized detection (modules/launcher/runtime_mode.ts), shared
1173
+ // with the mode-aware help output so the two cannot disagree.
1174
+ const runtimeInfo = detectRuntimeMode();
1175
+ // PM2 bots win over a stray credential daemon (resolveStatusRoute)
1176
+ // so the report matches the help banner; otherwise a live unlock
1177
+ // runtime is shown by delegating to `unlock status`.
1178
+ if (resolveStatusRoute(runtimeInfo) === 'unlock') {
1198
1179
  const unlockArgs = buildRuntimeScriptArgs({
1199
1180
  codeRoot: __dirname,
1200
1181
  scriptSegments: ['unlock'],
1201
- scriptArgs: ['status'],
1182
+ scriptArgs: ['status', ...(target ? [target] : [])],
1202
1183
  });
1203
1184
  const result = spawnSync(Config.EXEC_PATH, unlockArgs, {
1204
1185
  cwd: PATHS.PROJECT_ROOT,
@@ -1206,85 +1187,75 @@ async function handleCLICommands() {
1206
1187
  });
1207
1188
  return finish(result.status ?? 0);
1208
1189
  }
1209
- try {
1210
- const output = execSync('pm2 jlist 2>/dev/null', { encoding: 'utf8', timeout: 5000 }).toString().trim();
1211
- const jsonStart = output.indexOf('[');
1212
- if (jsonStart === -1) {
1213
- console.log('No DEXBot2 processes running.');
1214
- return finish(0);
1215
- }
1216
- const allProcs = JSON.parse(output.slice(jsonStart));
1217
- if (!Array.isArray(allProcs) || allProcs.length === 0) {
1218
- console.log('No DEXBot2 processes running.');
1219
- return finish(0);
1220
- }
1221
- const serviceNames = new Set(['dexbot-cred', 'dexbot-adapter', 'dexbot-update']);
1222
- const botNames = new Set();
1223
- try {
1224
- const { config } = loadSettingsFile(PROFILES_BOTS_FILE);
1225
- const entries = resolveRawBotEntries(config);
1226
- for (const b of entries) {
1227
- if (b.name)
1228
- botNames.add(b.name);
1229
- }
1230
- }
1231
- catch (_) { }
1232
- const dexbotProcs = allProcs.filter((p) => {
1233
- const name = String(p?.name || '');
1234
- return serviceNames.has(name) || botNames.has(name);
1235
- });
1236
- if (dexbotProcs.length === 0) {
1237
- console.log('No DEXBot2 processes running.');
1238
- return finish(0);
1239
- }
1240
- console.log('='.repeat(50));
1241
- console.log('DEXBot2 PM2 Processes');
1242
- console.log('='.repeat(50));
1243
- console.log('');
1244
- const fmtUptime = (p) => {
1245
- if (!p?.pm2_env?.pm_uptime)
1246
- return '-';
1247
- const ms = Date.now() - new Date(p.pm2_env.pm_uptime).getTime();
1248
- const s = Math.floor(Math.abs(ms) / 1000);
1249
- if (s < 60)
1250
- return `${s}s`;
1251
- const m = Math.floor(s / 60);
1252
- if (m < 60)
1253
- return `${m}m ${s % 60}s`;
1254
- const h = Math.floor(m / 60);
1255
- if (h < 24)
1256
- return `${h}h ${m % 60}m`;
1257
- const d = Math.floor(h / 24);
1258
- return `${d}d ${h % 24}h`;
1259
- };
1260
- const fmtMem = (p) => {
1261
- const bytes = p?.monit?.memory;
1262
- if (!bytes || bytes <= 0)
1263
- return '-';
1264
- if (bytes < 1024)
1265
- return `${bytes}B`;
1266
- if (bytes < 1024 * 1024)
1267
- return `${(bytes / 1024).toFixed(1)}KB`;
1268
- return `${(bytes / (1024 * 1024)).toFixed(1)}MB`;
1269
- };
1270
- const rows = dexbotProcs.map((p) => ({
1271
- pid: String(p?.pid || '-'),
1272
- name: String(p?.name || '-'),
1273
- status: String(p?.pm2_env?.status || '-'),
1274
- uptime: fmtUptime(p),
1275
- mem: fmtMem(p),
1276
- }));
1277
- const nameWidth = Math.max(...rows.map(r => r.name.length), 4);
1278
- const statusWidth = Math.max(...rows.map(r => r.status.length), 6);
1279
- const header = `${'PID'.padEnd(8)} ${'NAME'.padEnd(nameWidth)} ${'STATUS'.padEnd(statusWidth)} ${'UPTIME'.padEnd(12)} ${'MEMORY'}`;
1280
- console.log(header);
1281
- console.log('-'.repeat(header.length));
1282
- for (const r of rows) {
1283
- console.log(`${r.pid.padEnd(8)} ${r.name.padEnd(nameWidth)} ${r.status.padEnd(statusWidth)} ${r.uptime.padEnd(12)} ${r.mem}`);
1284
- }
1190
+ // status is whole-runtime on every surface. With no unlock bot
1191
+ // runtime up, validate and ignore a bot name here so the behavior
1192
+ // matches the `unlock status <bot>` path.
1193
+ const statusCheck = checkStatusTarget(target);
1194
+ if (statusCheck.error) {
1195
+ console.error(startupError(statusCheck.error));
1196
+ return finish(1);
1285
1197
  }
1286
- catch {
1198
+ if (statusCheck.notice) {
1199
+ console.warn(statusCheck.notice);
1200
+ }
1201
+ // No unlock runtime: render the PM2 table for this install's own
1202
+ // processes (already filtered by the shared detector).
1203
+ const dexbotProcs = runtimeInfo.pm2Processes;
1204
+ if (dexbotProcs.length === 0) {
1287
1205
  console.log('No DEXBot2 processes running.');
1206
+ return finish(0);
1207
+ }
1208
+ console.log('='.repeat(50));
1209
+ console.log('DEXBot2 PM2 Processes');
1210
+ console.log('='.repeat(50));
1211
+ console.log('');
1212
+ const fmtUptime = (p) => {
1213
+ if (!p?.pm2_env?.pm_uptime)
1214
+ return '-';
1215
+ const ms = Date.now() - new Date(p.pm2_env.pm_uptime).getTime();
1216
+ const s = Math.floor(Math.abs(ms) / 1000);
1217
+ if (s < 60)
1218
+ return `${s}s`;
1219
+ const m = Math.floor(s / 60);
1220
+ if (m < 60)
1221
+ return `${m}m ${s % 60}s`;
1222
+ const h = Math.floor(m / 60);
1223
+ if (h < 24)
1224
+ return `${h}h ${m % 60}m`;
1225
+ const d = Math.floor(h / 24);
1226
+ return `${d}d ${h % 24}h`;
1227
+ };
1228
+ const fmtMem = (p) => {
1229
+ const bytes = p?.monit?.memory;
1230
+ if (!bytes || bytes <= 0)
1231
+ return '-';
1232
+ if (bytes < 1024)
1233
+ return `${bytes}B`;
1234
+ if (bytes < 1024 * 1024)
1235
+ return `${(bytes / 1024).toFixed(1)}KB`;
1236
+ return `${(bytes / (1024 * 1024)).toFixed(1)}MB`;
1237
+ };
1238
+ const rows = dexbotProcs.map((p) => ({
1239
+ pid: String(p?.pid || '-'),
1240
+ name: String(p?.name || '-'),
1241
+ status: String(p?.pm2_env?.status || '-'),
1242
+ uptime: fmtUptime(p),
1243
+ mem: fmtMem(p),
1244
+ }));
1245
+ const nameWidth = Math.max(...rows.map(r => r.name.length), 4);
1246
+ const statusWidth = Math.max(...rows.map(r => r.status.length), 6);
1247
+ const header = `${'PID'.padEnd(8)} ${'NAME'.padEnd(nameWidth)} ${'STATUS'.padEnd(statusWidth)} ${'UPTIME'.padEnd(12)} ${'MEMORY'}`;
1248
+ console.log(header);
1249
+ console.log('-'.repeat(header.length));
1250
+ for (const r of rows) {
1251
+ console.log(`${r.pid.padEnd(8)} ${r.name.padEnd(nameWidth)} ${r.status.padEnd(statusWidth)} ${r.uptime.padEnd(12)} ${r.mem}`);
1252
+ }
1253
+ // Mixed state: surface a stray unlock-mode credential daemon too —
1254
+ // it holds unlocked keys in memory and would otherwise be invisible
1255
+ // here because PM2 wins the status route.
1256
+ if (runtimeInfo.credentialDaemonPid > 0) {
1257
+ console.log();
1258
+ console.log(`Credential daemon also running (unlock mode), PID ${runtimeInfo.credentialDaemonPid}.`);
1288
1259
  }
1289
1260
  return finish(0);
1290
1261
  }
@@ -79,14 +79,16 @@ function computeHuberWindowSlopePct(amaValues, index, lookbackBars, hub = AMA_SL
79
79
  return null;
80
80
  // Local linear fit needs the full window at both ends; reject non-positive
81
81
  // values anywhere in the window (Number(null) is 0) before fitting.
82
- const y = [];
83
- for (let k = index - bars; k <= index; k++) {
84
- const v = Number(amaValues[k]);
82
+ // The window is exactly [index - bars, index], so n is known up front and
83
+ // y can be pre-sized (no per-bar push growth).
84
+ const n = bars + 1;
85
+ const y = new Array(n);
86
+ for (let i = 0; i < n; i++) {
87
+ const v = Number(amaValues[index - bars + i]);
85
88
  if (!Number.isFinite(v) || v <= 0)
86
89
  return null;
87
- y.push(Math.log(v));
90
+ y[i] = Math.log(v);
88
91
  }
89
- const n = y.length;
90
92
  // Fill any missing field from the canonical constant. Production never passes
91
93
  // `hub` (the default is the constant), so this only guards a partial override
92
94
  // from silently skipping the IRLS loop or injecting NaN weights.
@@ -116,18 +118,31 @@ function computeHuberWindowSlopePct(amaValues, index, lookbackBars, hub = AMA_SL
116
118
  const a = sw === 0 ? 0 : (swy - b * swx) / sw;
117
119
  return { a, b };
118
120
  };
119
- let fit = wls(new Array(n).fill(1)); // OLS initialisation
121
+ // Scratch buffers are reused across the IRLS passes instead of allocating a
122
+ // fresh resid/abs/w triple (plus the sort copy) every iteration. This is the
123
+ // per-bar hot path of the dynamic-weight series, so the allocations dominate
124
+ // the fit arithmetic at the 16-bar window. The loops keep the original
125
+ // operation order so the returned slope is bit-for-bit unchanged.
126
+ const ones = new Array(n).fill(1);
127
+ const resid = new Array(n);
128
+ const abs = new Array(n);
129
+ const w = new Array(n);
130
+ let fit = wls(ones); // OLS initialisation
120
131
  for (let iter = 0; iter < hubIterations; iter++) {
121
- const resid = y.map((v, i) => v - (fit.a + fit.b * (i - xMean)));
122
- const abs = resid.map(Math.abs).sort((p, q) => p - q);
123
- const mid = abs.length >> 1;
124
- const mad = abs.length % 2 === 1 ? abs[mid] : (abs[mid - 1] + abs[mid]) / 2;
132
+ for (let i = 0; i < n; i++)
133
+ resid[i] = y[i] - (fit.a + fit.b * (i - xMean));
134
+ for (let i = 0; i < n; i++)
135
+ abs[i] = Math.abs(resid[i]);
136
+ abs.sort((p, q) => p - q);
137
+ const mid = n >> 1;
138
+ const mad = n % 2 === 1 ? abs[mid] : (abs[mid - 1] + abs[mid]) / 2;
125
139
  // Floor the robust scale. On piecewise-perfect data (a pure ramp, a
126
140
  // constant series) the MAD can collapse to ~0, which makes the Huber
127
141
  // weights degenerate and the weighted fit unstable; the floor is far
128
142
  // below any real per-bar signal (the reset gate is ~7e-5 here).
129
143
  const s = Math.max(1.4826 * mad, hubScaleFloor);
130
- const w = resid.map((r) => Math.min(1, (hubC * s) / Math.max(Math.abs(r), 1e-12)));
144
+ for (let i = 0; i < n; i++)
145
+ w[i] = Math.min(1, (hubC * s) / Math.max(Math.abs(resid[i]), 1e-12));
131
146
  const next = wls(w);
132
147
  const moved = Math.abs(next.b - fit.b);
133
148
  fit = next;
@@ -23,11 +23,15 @@
23
23
  import { getMarketCandles } from '../core/kibana_market_candles.js';
24
24
  import { path } from '../../modules/path_api.js';
25
25
  import { PATHS } from '../../modules/paths.js';
26
- import { toIntervalLabel, slugPart } from '../interval_utils.js';
26
+ import { toIntervalLabel } from '../interval_utils.js';
27
27
  import { buildFetchWindowsFromRange, runCachedWindows } from './window_cache.js';
28
+ import { canonicalPair, pairFolderSlug, sameAssets, swappedAssets, inverseSiblingOutPath, invertCandleSeries } from '../utils/pair_orientation.js';
28
29
  function bookCacheKey(assetA, assetB) {
29
- // Orientation matters: candles are B-per-A, so A/B and B/A are different series.
30
- return `${slugPart(assetA?.symbol || assetA?.id)}_${slugPart(assetB?.symbol || assetB?.id)}`;
30
+ // Canonical orientation: A/B and B/A share one cache family. Candles are
31
+ // B-per-A, so the stored series is always the canonical one and the
32
+ // fetcher inverts it back for an inverse request.
33
+ const pair = canonicalPair(assetA || {}, assetB || {});
34
+ return pairFolderSlug(pair.assetA, pair.assetB);
31
35
  }
32
36
  function bookOutputPath(assetA, assetB, intervalSeconds) {
33
37
  const label = toIntervalLabel(intervalSeconds);
@@ -43,11 +47,17 @@ function isBookChunkMatch(metaInput, requestKeyInput) {
43
47
  return false;
44
48
  if (meta.intervalSeconds !== requestKey.intervalSeconds)
45
49
  return false;
46
- if (meta.assetA?.id !== requestKey.assetA.id || meta.assetB?.id !== requestKey.assetB.id)
50
+ return sameAssets(meta, requestKey);
51
+ }
52
+ /** Same identity, opposite orientation (legacy pre-canonicalization shard). */
53
+ function isBookChunkMatchInverse(metaInput, requestKeyInput) {
54
+ const meta = metaInput;
55
+ const requestKey = requestKeyInput;
56
+ if (meta.source !== 'book' || requestKey.source !== 'book')
47
57
  return false;
48
- if (meta.assetA?.precision !== requestKey.assetA.precision || meta.assetB?.precision !== requestKey.assetB.precision)
58
+ if (meta.intervalSeconds !== requestKey.intervalSeconds)
49
59
  return false;
50
- return true;
60
+ return swappedAssets(meta, requestKey);
51
61
  }
52
62
  /**
53
63
  * OHLC order-book fill candles for an asset pair with chunk-level disk reuse.
@@ -64,13 +74,19 @@ async function fetchMarketCandlesSequentially(assetA, assetB, opts = {}) {
64
74
  if (!timeRange?.gte || !timeRange?.lte) {
65
75
  throw new Error('fetchMarketCandlesSequentially requires opts.timeRange { gte, lte }');
66
76
  }
67
- const pair = bookCacheKey(assetA, assetB);
68
- const outPath = opts.outPath || bookOutputPath(assetA, assetB, intervalSeconds);
77
+ // Canonical storage: fetch/query the lower-id leg as A, invert the result
78
+ // back if the caller asked for the other side. One cache family per pair.
79
+ const canon = canonicalPair(assetA, assetB);
80
+ const storeA = canon.assetA;
81
+ const storeB = canon.assetB;
82
+ const flipped = canon.flipped;
83
+ const pair = bookCacheKey(storeA, storeB);
84
+ const outPath = opts.outPath || bookOutputPath(storeA, storeB, intervalSeconds);
69
85
  const requestKey = {
70
86
  source: 'book',
71
87
  pair,
72
- assetA: { id: assetA.id, precision: assetA.precision, symbol: assetA.symbol },
73
- assetB: { id: assetB.id, precision: assetB.precision, symbol: assetB.symbol },
88
+ assetA: { id: storeA.id, precision: storeA.precision, symbol: storeA.symbol },
89
+ assetB: { id: storeB.id, precision: storeB.precision, symbol: storeB.symbol },
74
90
  intervalSeconds,
75
91
  };
76
92
  const plainWindows = buildFetchWindowsFromRange(timeRange, chunkMonths);
@@ -106,7 +122,7 @@ async function fetchMarketCandlesSequentially(assetA, assetB, opts = {}) {
106
122
  }
107
123
  catch (_) { /* progress must never fail the fetch */ }
108
124
  };
109
- const candles = await getMarketCandles(assetA, assetB, {
125
+ const candles = await getMarketCandles(storeA, storeB, {
110
126
  ...passthrough,
111
127
  ...(signal ? { signal } : {}),
112
128
  onPage,
@@ -115,11 +131,18 @@ async function fetchMarketCandlesSequentially(assetA, assetB, opts = {}) {
115
131
  });
116
132
  return sawPartial ? { candles, complete: false } : candles;
117
133
  };
118
- return runCachedWindows({
134
+ const merged = await runCachedWindows({
119
135
  windows,
120
136
  outPath,
121
137
  requestKey,
122
138
  isMatch: isBookChunkMatch,
139
+ inverse: (() => {
140
+ const invFolder = pairFolderSlug(storeB, storeA);
141
+ const legacy = inverseSiblingOutPath(outPath, storeA, storeB, `book_${invFolder}_${toIntervalLabel(intervalSeconds)}.json`);
142
+ return legacy
143
+ ? { outPath: legacy, isMatch: isBookChunkMatchInverse, toRequest: (c) => invertCandleSeries(c, { volume: 'scale' }) }
144
+ : undefined;
145
+ })(),
123
146
  metaForWindow: (window) => ({
124
147
  source: 'book',
125
148
  esSource: 'https://kibana.bitshares.dev (bitshares-*, op_type 4, fill_order)',
@@ -134,6 +157,7 @@ async function fetchMarketCandlesSequentially(assetA, assetB, opts = {}) {
134
157
  bucketMs: intervalSeconds * 1000,
135
158
  allowSubFetch: true,
136
159
  });
160
+ return flipped ? invertCandleSeries(merged, { volume: 'scale' }) : merged;
137
161
  }
138
162
  export { bookCacheKey, bookOutputPath, isBookChunkMatch, fetchMarketCandlesSequentially, };
139
163
  //# sourceMappingURL=fetch_book_data.js.map
@@ -38,6 +38,12 @@ declare function applyPrecisionOverrides(assetA: AssetRef, assetB: AssetRef, pre
38
38
  assetA: AssetRef;
39
39
  assetB: AssetRef;
40
40
  };
41
+ /**
42
+ * Locate a cached fetch context for a bot. Standard calls omit `dir` and both
43
+ * orientations are probed, so a bot whose pair is the inverse of the canonical
44
+ * folder still skips the blockchain connect. An explicit `dir` (tests, custom
45
+ * layouts) is scanned alone.
46
+ */
41
47
  declare function loadCachedFetchContext(bot: BotConfig, intervalSeconds: number, dir?: string): {
42
48
  poolId: string;
43
49
  assetA: AssetRef;