dexbot 1.6.11 → 1.6.13

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 (398) hide show
  1. package/CHANGELOG.md +67 -4
  2. package/README.md +2 -1
  3. package/analysis/README.md +11 -11
  4. package/analysis/ama_fitting/README.md +5 -3
  5. package/analysis/tradingview/README.md +1 -1
  6. package/analysis/trend_detection/DYNAMIC_WEIGHT_RESEARCH.md +3 -3
  7. package/claw/README.md +1 -0
  8. package/claw/docs/AI_BOT_LIBRARY_API.md +2 -0
  9. package/claw/docs/DEXBOT2_TUNING_CHEAT_SHEET.md +4 -3
  10. package/claw/modules/chain_broadcast.ts +7 -1
  11. package/claw/modules/claw_bridge.ts +0 -3
  12. package/claw/modules/claw_launcher.ts +1 -1
  13. package/claw/modules/claw_skill_md.ts +1 -0
  14. package/claw/modules/launcher_mode_detector.ts +9 -2
  15. package/claw/package.json +3 -3
  16. package/claw/runtimes/openclaw-plugin/openclaw.plugin.json +1 -1
  17. package/claw/runtimes/openclaw-plugin/package.json +1 -1
  18. package/claw/tsconfig.json +2 -2
  19. package/dist/analysis/account_resolver.d.ts +1 -1
  20. package/dist/analysis/account_resolver.js +2 -1
  21. package/dist/analysis/ama_fitting/analyze_ama_price_changes.js +3 -4
  22. package/dist/analysis/ama_fitting/analyze_lambda_vs_slow.js +3 -21
  23. package/dist/analysis/ama_fitting/calibrate_convergence_er.js +0 -1
  24. package/dist/analysis/ama_fitting/fetch_lp_candles.js +55 -45
  25. package/dist/analysis/ama_fitting/generate_unified_comparison_chart.d.ts +1 -8
  26. package/dist/analysis/ama_fitting/generate_unified_comparison_chart.js +45 -24
  27. package/dist/analysis/ama_fitting/optimizer_high_resolution.js +34 -78
  28. package/dist/analysis/analyze_args.d.ts +43 -0
  29. package/dist/analysis/analyze_args.js +61 -0
  30. package/dist/analysis/analyze_dynamic_weight.js +7 -17
  31. package/dist/analysis/analyze_kalman.js +7 -17
  32. package/dist/analysis/analyze_regime.js +7 -17
  33. package/dist/analysis/analyze_regime_windows.js +8 -18
  34. package/dist/analysis/analyze_risk_profile.js +8 -18
  35. package/dist/analysis/analyze_trade_heatmap.js +7 -17
  36. package/dist/analysis/analyze_volatility.js +7 -17
  37. package/dist/analysis/bot_fitting/backtest_ama_sweep.d.ts +221 -28
  38. package/dist/analysis/bot_fitting/backtest_ama_sweep.js +23 -103
  39. package/dist/analysis/bot_fitting/backtest_bot_fitting.d.ts +34 -4
  40. package/dist/analysis/bot_fitting/backtest_bot_fitting.js +32 -150
  41. package/dist/analysis/bot_fitting/backtest_lookback_drawdown.js +19 -34
  42. package/dist/analysis/bot_fitting/shared_utils.d.ts +77 -9
  43. package/dist/analysis/bot_fitting/shared_utils.js +125 -9
  44. package/dist/analysis/bot_key_utils.d.ts +18 -10
  45. package/dist/analysis/bot_key_utils.js +7 -4
  46. package/dist/analysis/bot_usage/discover_bot_accounts.js +44 -47
  47. package/dist/analysis/bot_usage/kibana_bot_queries.d.ts +1 -21
  48. package/dist/analysis/chain_pool.d.ts +3 -1
  49. package/dist/analysis/chart_ui.d.ts +35 -3
  50. package/dist/analysis/chart_ui.js +43 -4
  51. package/dist/analysis/chart_utils.d.ts +5 -5
  52. package/dist/analysis/chart_utils.js +9 -0
  53. package/dist/analysis/fills_cache.d.ts +4 -24
  54. package/dist/analysis/fills_cache.js +3 -84
  55. package/dist/analysis/fills_source.d.ts +33 -3
  56. package/dist/analysis/fills_source.js +83 -13
  57. package/dist/analysis/grid_correction_check.d.ts +2 -24
  58. package/dist/analysis/grid_correction_check.js +27 -69
  59. package/dist/analysis/math_utils.d.ts +91 -6
  60. package/dist/analysis/math_utils.js +119 -7
  61. package/dist/analysis/pnl_report.js +29 -29
  62. package/dist/analysis/price_sources.d.ts +30 -9
  63. package/dist/analysis/price_sources.js +28 -4
  64. package/dist/analysis/resolve_bot_accounts.js +2 -2
  65. package/dist/analysis/resolve_source.d.ts +1 -1
  66. package/dist/analysis/resolve_source.js +31 -10
  67. package/dist/analysis/trade_profitability.d.ts +29 -2
  68. package/dist/analysis/trade_profitability.js +73 -147
  69. package/dist/analysis/tradingview/analyze_tradingview.d.ts +3 -8
  70. package/dist/analysis/tradingview/analyze_tradingview.js +7 -6
  71. package/dist/analysis/tradingview/grid_reset_config.d.ts +41 -32
  72. package/dist/analysis/tradingview/grid_reset_config.js +23 -11
  73. package/dist/analysis/tradingview/grid_reset_sim.d.ts +75 -9
  74. package/dist/analysis/tradingview/grid_reset_sim.js +4 -3
  75. package/dist/analysis/tradingview/tradingview_uplot_chart_generator.d.ts +69 -4
  76. package/dist/analysis/tradingview/tradingview_uplot_chart_generator.js +20 -34
  77. package/dist/analysis/trend_detection/backtest_ama_slope_huber.d.ts +1 -101
  78. package/dist/analysis/trend_detection/backtest_ama_slope_huber.js +26 -84
  79. package/dist/analysis/trend_detection/dynamic_weight_chart_generator.d.ts +63 -1
  80. package/dist/analysis/trend_detection/dynamic_weight_chart_generator.js +16 -41
  81. package/dist/analysis/trend_detection/huber_scale_variants.d.ts +9 -3
  82. package/dist/analysis/trend_detection/huber_scale_variants.js +4 -4
  83. package/dist/analysis/trend_detection/kalman_chart_generator.d.ts +30 -1
  84. package/dist/analysis/trend_detection/kalman_chart_generator.js +3 -3
  85. package/dist/analysis/trend_detection/regime_chart_generator.d.ts +31 -1
  86. package/dist/analysis/trend_detection/regime_chart_generator.js +2 -2
  87. package/dist/analysis/trend_detection/volatility_chart_generator.d.ts +33 -1
  88. package/dist/analysis/trend_detection/volatility_chart_generator.js +9 -10
  89. package/dist/analysis/worker_pool.d.ts +38 -0
  90. package/dist/analysis/worker_pool.js +67 -0
  91. package/dist/credential-daemon.js +34 -20
  92. package/dist/dexbot.js +78 -59
  93. package/dist/market_adapter/ama_signal_runner.js +21 -54
  94. package/dist/market_adapter/candle_utils.d.ts +29 -19
  95. package/dist/market_adapter/candle_utils.js +35 -21
  96. package/dist/market_adapter/cli_args.d.ts +22 -0
  97. package/dist/market_adapter/cli_args.js +30 -0
  98. package/dist/market_adapter/core/asymmetric_bounds.d.ts +13 -4
  99. package/dist/market_adapter/core/config_normalizers.d.ts +3 -3
  100. package/dist/market_adapter/core/config_normalizers.js +11 -6
  101. package/dist/market_adapter/core/kibana_candles.d.ts +56 -19
  102. package/dist/market_adapter/core/kibana_candles.js +12 -9
  103. package/dist/market_adapter/core/kibana_client.d.ts +9 -1
  104. package/dist/market_adapter/core/kibana_client.js +8 -7
  105. package/dist/market_adapter/core/kibana_market_candles.d.ts +3 -2
  106. package/dist/market_adapter/core/market_adapter_service.d.ts +320 -153
  107. package/dist/market_adapter/core/market_adapter_service.js +228 -269
  108. package/dist/market_adapter/core/signals/hurst_analyzer.d.ts +1 -6
  109. package/dist/market_adapter/core/signals/hurst_analyzer.js +1 -1
  110. package/dist/market_adapter/core/signals/permutation_entropy_analyzer.d.ts +1 -11
  111. package/dist/market_adapter/core/signals/permutation_entropy_analyzer.js +1 -1
  112. package/dist/market_adapter/core/strategies/ama.d.ts +10 -2
  113. package/dist/market_adapter/core/strategies/ama.js +2 -4
  114. package/dist/market_adapter/core/strategies/ama_slope_model.d.ts +14 -1
  115. package/dist/market_adapter/core/strategies/ama_slope_model.js +12 -34
  116. package/dist/market_adapter/core/strategies/atr/calculator.d.ts +7 -2
  117. package/dist/market_adapter/core/strategies/atr/calculator.js +2 -34
  118. package/dist/market_adapter/core/strategies/collateral_manager.d.ts +7 -1
  119. package/dist/market_adapter/core/strategies/collateral_manager.js +0 -22
  120. package/dist/market_adapter/core/strategies/dynamic_weight_series.d.ts +34 -6
  121. package/dist/market_adapter/core/strategies/dynamic_weight_series.js +7 -7
  122. package/dist/market_adapter/core/strategies/regime_gate.d.ts +24 -15
  123. package/dist/market_adapter/core/strategies/regime_gate.js +4 -31
  124. package/dist/market_adapter/core/strategies/regime_interp.d.ts +7 -1
  125. package/dist/market_adapter/core/strategies/regime_interp.js +14 -22
  126. package/dist/market_adapter/core/strategies/volatility_shift.d.ts +11 -1
  127. package/dist/market_adapter/core/strategies/volatility_shift.js +0 -13
  128. package/dist/market_adapter/inputs/cex_candle_parsing.d.ts +29 -0
  129. package/dist/market_adapter/inputs/cex_candle_parsing.js +52 -0
  130. package/dist/market_adapter/inputs/fetch_book_data.d.ts +20 -4
  131. package/dist/market_adapter/inputs/fetch_book_data.js +3 -2
  132. package/dist/market_adapter/inputs/fetch_cex_synthetic_data.js +160 -332
  133. package/dist/market_adapter/inputs/fetch_lp_data.d.ts +50 -8
  134. package/dist/market_adapter/inputs/fetch_lp_data.js +47 -22
  135. package/dist/market_adapter/inputs/kibana_feed_source.d.ts +74 -15
  136. package/dist/market_adapter/inputs/kibana_feed_source.js +26 -41
  137. package/dist/market_adapter/inputs/kibana_source.d.ts +25 -3
  138. package/dist/market_adapter/inputs/kibana_source.js +2 -2
  139. package/dist/market_adapter/inputs/window_cache.d.ts +93 -156
  140. package/dist/market_adapter/inputs/window_cache.js +59 -184
  141. package/dist/market_adapter/interval_utils.d.ts +15 -5
  142. package/dist/market_adapter/interval_utils.js +16 -1
  143. package/dist/market_adapter/log_format.d.ts +6 -6
  144. package/dist/market_adapter/log_format.js +28 -21
  145. package/dist/market_adapter/lp_chart_core.d.ts +22 -1
  146. package/dist/market_adapter/lp_chart_core.js +26 -192
  147. package/dist/market_adapter/lp_chart_runner.d.ts +10 -7
  148. package/dist/market_adapter/lp_chart_runner.js +37 -19
  149. package/dist/market_adapter/lp_chart_strategy_loader.d.ts +41 -6
  150. package/dist/market_adapter/lp_chart_strategy_loader.js +8 -7
  151. package/dist/market_adapter/market_adapter.d.ts +94 -32
  152. package/dist/market_adapter/market_adapter.js +94 -120
  153. package/dist/market_adapter/utils/adapter_client.d.ts +3 -3
  154. package/dist/market_adapter/utils/adapter_client.js +2 -2
  155. package/dist/market_adapter/utils/atomic_write.d.ts +8 -1
  156. package/dist/market_adapter/utils/chain.d.ts +50 -41
  157. package/dist/market_adapter/utils/chain.js +40 -46
  158. package/dist/market_adapter/utils/data_discovery.d.ts +49 -2
  159. package/dist/market_adapter/utils/data_discovery.js +220 -27
  160. package/dist/market_adapter/utils/dynamic_grid_snapshot.d.ts +12 -3
  161. package/dist/market_adapter/utils/file_json_cache.d.ts +2 -2
  162. package/dist/market_adapter/utils/file_lock.d.ts +28 -14
  163. package/dist/market_adapter/utils/file_lock.js +36 -32
  164. package/dist/market_adapter/utils/month_shards.d.ts +84 -0
  165. package/dist/market_adapter/utils/month_shards.js +237 -0
  166. package/dist/market_adapter/utils/native_history.d.ts +9 -2
  167. package/dist/market_adapter/utils/native_history.js +35 -28
  168. package/dist/modules/account_bots.d.ts +578 -21
  169. package/dist/modules/account_bots.js +272 -267
  170. package/dist/modules/account_orders.d.ts +79 -30
  171. package/dist/modules/account_orders.js +60 -64
  172. package/dist/modules/authority_resolver.d.ts +12 -2
  173. package/dist/modules/authority_resolver.js +15 -8
  174. package/dist/modules/bitshares-native/chain_client.d.ts +39 -4
  175. package/dist/modules/bitshares-native/chain_client.js +59 -59
  176. package/dist/modules/bitshares-native/crypto/ecc.browser.js +11 -171
  177. package/dist/modules/bitshares-native/crypto/ecc.js +13 -176
  178. package/dist/modules/bitshares-native/crypto/ecc_core.d.ts +33 -0
  179. package/dist/modules/bitshares-native/crypto/ecc_core.js +213 -0
  180. package/dist/modules/bitshares-native/crypto/ecc_selector.d.ts +2 -1
  181. package/dist/modules/bitshares-native/crypto/ecc_selector.js +1 -13
  182. package/dist/modules/bitshares-native/index.d.ts +1 -1
  183. package/dist/modules/bitshares-native/lru_cache.d.ts +7 -7
  184. package/dist/modules/bitshares-native/resolvers.d.ts +18 -6
  185. package/dist/modules/bitshares-native/resolvers.js +22 -25
  186. package/dist/modules/bitshares-native/serial/lazy_buffer.d.ts +4 -0
  187. package/dist/modules/bitshares-native/serial/lazy_buffer.js +21 -0
  188. package/dist/modules/bitshares-native/serial/operations.d.ts +1 -1
  189. package/dist/modules/bitshares-native/serial/serializer.d.ts +16 -12
  190. package/dist/modules/bitshares-native/serial/serializer.js +7 -13
  191. package/dist/modules/bitshares-native/serial/types.d.ts +25 -16
  192. package/dist/modules/bitshares-native/serial/types.js +73 -108
  193. package/dist/modules/bitshares-native/signing_client.d.ts +32 -2
  194. package/dist/modules/bitshares-native/signing_client.js +19 -14
  195. package/dist/modules/bitshares-native/subscriptions.d.ts +81 -1
  196. package/dist/modules/bitshares-native/subscriptions.js +12 -12
  197. package/dist/modules/bitshares-native/transport.d.ts +5 -4
  198. package/dist/modules/bitshares-native/transport.js +5 -4
  199. package/dist/modules/bitshares-native/tx/builder.d.ts +40 -5
  200. package/dist/modules/bitshares-native/tx/builder.js +4 -3
  201. package/dist/modules/bitshares-native/tx/tx_cache.d.ts +4 -4
  202. package/dist/modules/bitshares_client.d.ts +45 -13
  203. package/dist/modules/bitshares_client.js +28 -19
  204. package/dist/modules/bot_defaults.d.ts +8 -16
  205. package/dist/modules/bot_defaults.js +1 -5
  206. package/dist/modules/bot_settings.d.ts +44 -10
  207. package/dist/modules/bot_settings.js +11 -8
  208. package/dist/modules/bots_file_lock.d.ts +6 -6
  209. package/dist/modules/bots_file_lock.js +16 -12
  210. package/dist/modules/broadcast_failure.d.ts +4 -3
  211. package/dist/modules/broadcast_failure.js +5 -4
  212. package/dist/modules/chain_keys.d.ts +42 -40
  213. package/dist/modules/chain_keys.js +53 -45
  214. package/dist/modules/chain_orders.d.ts +94 -60
  215. package/dist/modules/chain_orders.js +81 -118
  216. package/dist/modules/cli_start_onboarding.d.ts +2 -1
  217. package/dist/modules/constants.d.ts +534 -32
  218. package/dist/modules/constants.js +35 -48
  219. package/dist/modules/cr_planner.d.ts +41 -3
  220. package/dist/modules/cr_planner.js +1 -5
  221. package/dist/modules/credential_policy.d.ts +68 -8
  222. package/dist/modules/credential_policy.js +37 -31
  223. package/dist/modules/credential_session_cache.d.ts +2 -2
  224. package/dist/modules/credit_pricing.d.ts +43 -6
  225. package/dist/modules/credit_pricing.js +70 -60
  226. package/dist/modules/credit_runtime.d.ts +283 -118
  227. package/dist/modules/credit_runtime.js +500 -231
  228. package/dist/modules/crypto/browser_provider.js +6 -4
  229. package/dist/modules/crypto/node_provider.js +1 -1
  230. package/dist/modules/crypto/pure_scrypt.js +35 -4
  231. package/dist/modules/crypto/pure_secp256k1.js +72 -26
  232. package/dist/modules/crypto/sync.d.ts +9 -9
  233. package/dist/modules/crypto/sync.js +16 -9
  234. package/dist/modules/daemon_node_health.d.ts +4 -3
  235. package/dist/modules/dexbot_class.d.ts +213 -146
  236. package/dist/modules/dexbot_class.js +183 -86
  237. package/dist/modules/dexbot_cow_runtime.d.ts +160 -87
  238. package/dist/modules/dexbot_cow_runtime.js +302 -234
  239. package/dist/modules/dexbot_credential_client.d.ts +19 -18
  240. package/dist/modules/dexbot_credential_client.js +1 -2
  241. package/dist/modules/dexbot_fill_runtime.d.ts +63 -49
  242. package/dist/modules/dexbot_fill_runtime.js +53 -47
  243. package/dist/modules/dexbot_maintenance_runtime.d.ts +127 -71
  244. package/dist/modules/dexbot_maintenance_runtime.js +187 -147
  245. package/dist/modules/dexbot_startup_runtime.d.ts +11 -11
  246. package/dist/modules/dexbot_startup_runtime.js +56 -57
  247. package/dist/modules/dexbot_state_recovery.d.ts +13 -12
  248. package/dist/modules/dexbot_state_recovery.js +18 -13
  249. package/dist/modules/fund_registry.d.ts +4 -2
  250. package/dist/modules/fund_registry.js +10 -10
  251. package/dist/modules/general_settings.d.ts +30 -4
  252. package/dist/modules/graceful_shutdown.d.ts +3 -3
  253. package/dist/modules/graceful_shutdown.js +19 -13
  254. package/dist/modules/grid_price_source.d.ts +1 -1
  255. package/dist/modules/key_store.d.ts +14 -8
  256. package/dist/modules/key_store.js +7 -7
  257. package/dist/modules/launcher/adapter_requirement.d.ts +11 -5
  258. package/dist/modules/launcher/adapter_requirement.js +5 -5
  259. package/dist/modules/launcher/bot_supervisor.d.ts +106 -24
  260. package/dist/modules/launcher/bot_supervisor.js +12 -11
  261. package/dist/modules/launcher/credential_bootstrap.d.ts +1 -1
  262. package/dist/modules/launcher/credential_bootstrap.js +1 -1
  263. package/dist/modules/launcher/credential_daemon.d.ts +4 -4
  264. package/dist/modules/launcher/credential_daemon.js +2 -2
  265. package/dist/modules/launcher/credential_secret.d.ts +1 -1
  266. package/dist/modules/launcher/foreign_cred_daemon.d.ts +2 -2
  267. package/dist/modules/launcher/market_adapter_runtime.d.ts +61 -6
  268. package/dist/modules/launcher/market_adapter_watchdog.d.ts +10 -2
  269. package/dist/modules/launcher/market_adapter_watchdog.js +4 -0
  270. package/dist/modules/launcher/monolithic_runtime.d.ts +18 -16
  271. package/dist/modules/launcher/status_reporting.d.ts +22 -14
  272. package/dist/modules/launcher/supervisor_control.d.ts +8 -1
  273. package/dist/modules/launcher/supervisor_control.js +4 -2
  274. package/dist/modules/lazy_shim.d.ts +11 -0
  275. package/dist/modules/lazy_shim.js +16 -0
  276. package/dist/modules/market_adapter_whitelist.d.ts +9 -3
  277. package/dist/modules/market_adapter_whitelist.js +15 -3
  278. package/dist/modules/node_connect_policy.d.ts +3 -2
  279. package/dist/modules/node_failure_ledger.d.ts +6 -5
  280. package/dist/modules/node_manager.d.ts +41 -11
  281. package/dist/modules/node_manager.js +5 -4
  282. package/dist/modules/order/accounting.d.ts +53 -24
  283. package/dist/modules/order/accounting.js +16 -15
  284. package/dist/modules/order/async_lock.d.ts +5 -2
  285. package/dist/modules/order/async_lock.js +1 -1
  286. package/dist/modules/order/export.d.ts +7 -6
  287. package/dist/modules/order/export.js +1 -7
  288. package/dist/modules/order/format.d.ts +16 -7
  289. package/dist/modules/order/format.js +15 -3
  290. package/dist/modules/order/genesis_policy.d.ts +24 -21
  291. package/dist/modules/order/genesis_policy.js +17 -10
  292. package/dist/modules/order/grid.d.ts +144 -36
  293. package/dist/modules/order/grid.js +226 -145
  294. package/dist/modules/order/grid_reconcile.d.ts +21 -20
  295. package/dist/modules/order/grid_reconcile.js +13 -12
  296. package/dist/modules/order/grid_reconcile_internal.d.ts +85 -72
  297. package/dist/modules/order/grid_reconcile_internal.js +28 -19
  298. package/dist/modules/order/index.d.ts +264 -1
  299. package/dist/modules/order/logger.d.ts +46 -19
  300. package/dist/modules/order/logger.js +38 -49
  301. package/dist/modules/order/logger_state.d.ts +4 -4
  302. package/dist/modules/order/manager.d.ts +232 -191
  303. package/dist/modules/order/manager.js +110 -130
  304. package/dist/modules/order/processed_fill_store.d.ts +18 -3
  305. package/dist/modules/order/processed_fill_store.js +16 -27
  306. package/dist/modules/order/strategy.d.ts +21 -13
  307. package/dist/modules/order/strategy.js +5 -6
  308. package/dist/modules/order/sync_engine.d.ts +91 -33
  309. package/dist/modules/order/sync_engine.js +84 -53
  310. package/dist/modules/order/utils/math.d.ts +157 -102
  311. package/dist/modules/order/utils/math.js +77 -38
  312. package/dist/modules/order/utils/order.d.ts +310 -110
  313. package/dist/modules/order/utils/order.js +153 -126
  314. package/dist/modules/order/utils/slot.d.ts +1 -1
  315. package/dist/modules/order/utils/system.d.ts +156 -26
  316. package/dist/modules/order/utils/system.js +103 -71
  317. package/dist/modules/order/utils/validate.d.ts +89 -41
  318. package/dist/modules/order/utils/validate.js +26 -24
  319. package/dist/modules/order/utils/withPoolRef.d.ts +6 -4
  320. package/dist/modules/order/utils/withPoolRef.js +18 -12
  321. package/dist/modules/order/working_grid.d.ts +14 -13
  322. package/dist/modules/paths.d.ts +1 -4
  323. package/dist/modules/paths.js +33 -61
  324. package/dist/modules/process_discovery.d.ts +1 -1
  325. package/dist/modules/process_discovery.js +2 -1
  326. package/dist/modules/runtime.d.ts +7 -3
  327. package/dist/modules/runtime.js +4 -3
  328. package/dist/modules/runtime_settings.d.ts +14 -11
  329. package/dist/modules/runtime_settings.js +30 -24
  330. package/dist/modules/settings_merge.d.ts +29 -3
  331. package/dist/modules/settings_merge.js +56 -78
  332. package/dist/modules/socket_json_client.d.ts +9 -5
  333. package/dist/modules/storage/browser_adapter.d.ts +38 -53
  334. package/dist/modules/storage/browser_adapter.js +7 -30
  335. package/dist/modules/storage/index.d.ts +1 -1
  336. package/dist/modules/storage/node_adapter.d.ts +45 -27
  337. package/dist/modules/storage/node_adapter.js +9 -9
  338. package/dist/modules/storage/types.d.ts +5 -4
  339. package/dist/modules/types.d.ts +1090 -6
  340. package/dist/modules/types.js +4 -1
  341. package/dist/modules/utils/asset_symbols.d.ts +2 -1
  342. package/dist/modules/utils/asset_symbols.js +2 -1
  343. package/dist/modules/utils/errors.d.ts +9 -0
  344. package/dist/modules/utils/errors.js +24 -4
  345. package/dist/modules/utils/time_range.d.ts +1 -12
  346. package/dist/modules/utils/time_range.js +1 -1
  347. package/dist/modules/validate_profiles.js +53 -43
  348. package/dist/modules/version_notice.d.ts +23 -11
  349. package/dist/modules/version_notice.js +6 -4
  350. package/dist/pm2.d.ts +28 -27
  351. package/dist/pm2.js +61 -40
  352. package/dist/scripts/analyze-credit.d.ts +2 -1
  353. package/dist/scripts/analyze-credit.js +23 -24
  354. package/dist/scripts/analyze-git.js +2 -15
  355. package/dist/scripts/analyze-orders.d.ts +77 -9
  356. package/dist/scripts/analyze-orders.js +65 -40
  357. package/dist/scripts/chart_command.d.ts +27 -18
  358. package/dist/scripts/chart_command.js +15 -13
  359. package/dist/scripts/check_any.d.ts +22 -0
  360. package/dist/scripts/check_any.js +158 -0
  361. package/dist/scripts/diagnose-pool-history.js +13 -13
  362. package/dist/scripts/print_grid.js +0 -1
  363. package/dist/scripts/run-tests.js +39 -8
  364. package/dist/scripts/runner.js +1 -1
  365. package/dist/scripts/sync-version.js +0 -10
  366. package/dist/scripts/test-credit-renewal.js +30 -21
  367. package/dist/scripts/update.d.ts +10 -5
  368. package/dist/scripts/update.js +168 -103
  369. package/dist/scripts/update_dist_freshness.js +1 -0
  370. package/dist/scripts/update_layout.d.ts +61 -0
  371. package/dist/scripts/update_layout.js +180 -0
  372. package/dist/scripts/validate_bots.js +2 -1
  373. package/dist/scripts/verify-browser-bundle.js +8 -0
  374. package/dist/unlock.d.ts +28 -3
  375. package/dist/unlock.js +63 -35
  376. package/docs/BITSHARES_ONBOARDING.md +10 -12
  377. package/docs/COPY_ON_WRITE_MASTER_PLAN.md +2 -2
  378. package/docs/COW_INVARIANTS.md +13 -6
  379. package/docs/DEXBOT_COMPARISON.md +8 -8
  380. package/docs/EVOLUTION.md +10 -8
  381. package/docs/FUND_MOVEMENT_AND_ACCOUNTING.md +5 -5
  382. package/docs/GRID_RECALCULATION.md +4 -2
  383. package/docs/GRID_RECONCILE.md +14 -3
  384. package/docs/LIFECYCLE.md +11 -11
  385. package/docs/MPA_CREDIT_USAGE.md +22 -3
  386. package/docs/README.md +14 -3
  387. package/docs/WORKFLOW.md +1 -0
  388. package/docs/architecture.md +31 -19
  389. package/docs/developer_guide.md +6 -6
  390. package/market_adapter/README.md +12 -5
  391. package/modules/README.md +4 -1
  392. package/package.json +9 -9
  393. package/scripts/README.md +43 -38
  394. package/tests/README.md +25 -10
  395. package/dist/scripts/diagnose-kibana-candles.d.ts +0 -3
  396. package/dist/scripts/diagnose-kibana-candles.js +0 -82
  397. package/scripts/git-viewer.sh +0 -489
  398. package/scripts/postinstall.js +0 -16
@@ -27,6 +27,7 @@ import path from 'node:path';
27
27
  import { getStorage } from '../modules/storage/index.js';
28
28
  import { PATHS } from '../modules/paths.js';
29
29
  import { writeJsonAtomic } from '../market_adapter/utils/atomic_write.js';
30
+ import { shardKeyForTimestamp, shardBoundsForKey, shardKeysForRange, shardPathFor, unionQueriedRanges, compactCoverage, mergeSpans, subtractRanges, } from '../market_adapter/utils/month_shards.js';
30
31
  import { fetchAllFills } from './fills_source.js';
31
32
  const storage = getStorage();
32
33
  const { readJSON } = storage;
@@ -35,27 +36,6 @@ const TAIL_SETTLE_LAG_MS = 6 * 3600 * 1000;
35
36
  /** Upper bound on spans persisted per shard (keeps coverage bookkeeping small). */
36
37
  const MAX_COVERAGE_SPANS = 64;
37
38
  const SHARD_EXT = '.json';
38
- const SHARD_MARKER = '.shard_';
39
- function shardKeyForTimestamp(tsMs) {
40
- const d = new Date(Number(tsMs));
41
- return `${d.getUTCFullYear()}-${String(d.getUTCMonth() + 1).padStart(2, '0')}`;
42
- }
43
- function shardBoundsForKey(key) {
44
- const parts = String(key).split('-').map(Number);
45
- const start = Date.UTC(parts[0], parts[1] - 1, 1);
46
- const end = parts[1] === 12 ? Date.UTC(parts[0] + 1, 0, 1) : Date.UTC(parts[0], parts[1], 1);
47
- return { start, end };
48
- }
49
- function shardKeysForRange(gteMs, lteMs) {
50
- const keys = [];
51
- const cursor = new Date(Date.UTC(new Date(gteMs).getUTCFullYear(), new Date(gteMs).getUTCMonth(), 1));
52
- const last = new Date(lteMs);
53
- while (cursor <= last) {
54
- keys.push(shardKeyForTimestamp(cursor.getTime()));
55
- cursor.setUTCMonth(cursor.getUTCMonth() + 1);
56
- }
57
- return keys;
58
- }
59
39
  function accountFileBase(accountId) {
60
40
  const s = String(accountId ?? '').trim();
61
41
  if (/^1\.2\.\d+$/.test(s))
@@ -65,10 +45,6 @@ function accountFileBase(accountId) {
65
45
  function basePathFor(cacheDir, accountId) {
66
46
  return path.join(cacheDir, 'fills', `${accountFileBase(accountId)}${SHARD_EXT}`);
67
47
  }
68
- function shardPathFor(basePath, shardKey) {
69
- const parsed = path.parse(basePath);
70
- return path.join(parsed.dir, `${parsed.name}${SHARD_MARKER}${shardKey}${parsed.ext}`);
71
- }
72
48
  function readShard(shardFile, accountId) {
73
49
  try {
74
50
  const parsed = readJSON(shardFile);
@@ -91,49 +67,13 @@ function readShard(shardFile, accountId) {
91
67
  }
92
68
  /** Sort, merge overlapping/adjacent same-`at` spans, cap the list length. */
93
69
  function normalizeQueried(ranges) {
94
- const clean = (ranges || [])
95
- .filter((q) => q && Number.isFinite(Number(q.gte)) && Number.isFinite(Number(q.lte)) && Number(q.lte) >= Number(q.gte))
96
- .map((q) => ({
97
- gte: Number(q.gte),
98
- lte: Number(q.lte),
99
- at: Number.isFinite(Number(q.at)) ? Number(q.at) : null,
100
- }))
101
- .sort((a, b) => a.gte - b.gte || a.lte - b.lte);
102
- const merged = [];
103
- for (const q of clean) {
104
- const top = merged[merged.length - 1];
105
- if (top && q.gte <= top.lte + 1 && top.at === q.at) {
106
- if (q.lte > top.lte)
107
- top.lte = q.lte;
108
- }
109
- else {
110
- merged.push({ ...q });
111
- }
112
- }
113
- if (merged.length > MAX_COVERAGE_SPANS)
114
- merged.splice(0, merged.length - MAX_COVERAGE_SPANS);
115
- return merged;
70
+ return compactCoverage(unionQueriedRanges(ranges || [], 1), MAX_COVERAGE_SPANS);
116
71
  }
117
72
  /** Record a freshly queried span, dropping older spans it fully supersedes. */
118
73
  function addCoverage(queried, span, atMs) {
119
74
  const kept = queried.filter((q) => !(q.gte >= span.gte && q.lte <= span.lte));
120
75
  return normalizeQueried([...kept, { gte: span.gte, lte: span.lte, at: atMs }]);
121
76
  }
122
- function mergeSpans(spans) {
123
- const sorted = [...spans].sort((a, b) => a.gte - b.gte || a.lte - b.lte);
124
- const merged = [];
125
- for (const s of sorted) {
126
- const top = merged[merged.length - 1];
127
- if (top && s.gte <= top.lte + 1) {
128
- if (s.lte > top.lte)
129
- top.lte = s.lte;
130
- }
131
- else {
132
- merged.push({ ...s });
133
- }
134
- }
135
- return merged;
136
- }
137
77
  /**
138
78
  * Portions of queried spans that are safe to reuse: the part up to
139
79
  * `at - TAIL_SETTLE_LAG_MS`. A span with an unknown `at` certifies nothing.
@@ -149,27 +89,6 @@ function trustedSegments(queried, nowMs) {
149
89
  }
150
90
  return mergeSpans(segs);
151
91
  }
152
- /** `span` minus the union of `covered` (both as inclusive ms ranges). */
153
- function subtractRanges(span, covered) {
154
- let parts = [{ ...span }];
155
- for (const c of mergeSpans(covered)) {
156
- const next = [];
157
- for (const p of parts) {
158
- if (c.lte < p.gte || c.gte > p.lte) {
159
- next.push(p);
160
- continue;
161
- }
162
- if (c.gte > p.gte)
163
- next.push({ gte: p.gte, lte: Math.min(p.lte, c.gte - 1) });
164
- if (c.lte < p.lte)
165
- next.push({ gte: Math.max(p.gte, c.lte + 1), lte: p.lte });
166
- }
167
- parts = next.filter(p => p.lte >= p.gte);
168
- if (parts.length === 0)
169
- break;
170
- }
171
- return parts;
172
- }
173
92
  function fillKey(f) {
174
93
  return `${f.blockNum}:${f.opNum}`;
175
94
  }
@@ -250,5 +169,5 @@ async function fetchFillsCached(config, accountId, gteIso, lteIso, options = {})
250
169
  }
251
170
  return [...collected.values()].sort(compareFills);
252
171
  }
253
- export { fetchFillsCached, shardKeyForTimestamp, shardBoundsForKey, shardKeysForRange, basePathFor, shardPathFor, normalizeQueried, addCoverage, trustedSegments, subtractRanges, mergeFills, fillKey, TAIL_SETTLE_LAG_MS, MAX_COVERAGE_SPANS, };
172
+ export { fetchFillsCached, shardKeyForTimestamp, shardBoundsForKey, shardKeysForRange, basePathFor, shardPathFor, trustedSegments, subtractRanges, mergeFills, fillKey, TAIL_SETTLE_LAG_MS, MAX_COVERAGE_SPANS, };
254
173
  //# sourceMappingURL=fills_cache.js.map
@@ -13,17 +13,47 @@ interface FillRecord {
13
13
  receives: AssetAmount;
14
14
  fee: AssetAmount;
15
15
  isMaker: boolean;
16
- sort: any[];
16
+ sort: unknown[];
17
17
  }
18
18
  declare function assetSymbol(id: string): string;
19
19
  declare function assetPrec(id: string): number | undefined;
20
20
  declare function toReal(amount: number, assetId: string): number;
21
+ /**
22
+ * A fill needs strictly positive, finite amounts on both legs. A zero base
23
+ * amount would produce an infinite price (quoteAmount / 0) that poisons
24
+ * downstream inventory lots and PnL aggregates.
25
+ */
26
+ declare function isValidFillAmounts(baseAmount: number, quoteAmount: number): boolean;
27
+ /** Match a user-supplied pair leg against a chain asset id or its symbol. */
28
+ declare function matchesAssetRef(ref: string, assetId: string): boolean;
29
+ interface ClassifiedFill {
30
+ direction: 'buy' | 'sell';
31
+ baseAsset: string;
32
+ quoteAsset: string;
33
+ baseAmount: number;
34
+ quoteAmount: number;
35
+ price: number;
36
+ }
37
+ /**
38
+ * Resolve a raw fill's buy/sell direction, base/quote assets and real amounts.
39
+ *
40
+ * Direction/base/quote rules (single home for trade_profitability and
41
+ * grid_correction_check, which previously carried verbatim copies):
42
+ * - BTS on the pays side → buy the received asset, quote = BTS.
43
+ * - BTS on the receives side → sell the paid asset, quote = BTS.
44
+ * - any other cross-pair → lower asset id is the base; sell when pays < receives.
45
+ *
46
+ * Returns `null` when an asset precision is unknown or either real amount is
47
+ * not strictly positive/finite. Callers own the skip counting and any extra
48
+ * validation (e.g. the market-fee asset check in trade_profitability).
49
+ */
50
+ declare function classifyFill(f: FillRecord): ClassifiedFill | null;
21
51
  /**
22
52
  * Collect all unique non-BTS asset IDs from fills, resolve unknown precisions
23
53
  * from the blockchain, and populate the runtime cache.
24
54
  */
25
55
  declare function resolveAssetPrecisions(fills: FillRecord[]): Promise<void>;
26
56
  /** Fetch every fill_order for an account in [gte, lte], paginated via search_after. */
27
- declare function fetchAllFills(config: any, accountId: string, gte: string, lte: string): Promise<FillRecord[]>;
28
- export { BTS_ID, assetSymbol, assetPrec, toReal, resolveAssetPrecisions, fetchAllFills, FillRecord, };
57
+ declare function fetchAllFills(config: Record<string, unknown>, accountId: string, gte: string, lte: string): Promise<FillRecord[]>;
58
+ export { BTS_ID, assetSymbol, assetPrec, toReal, isValidFillAmounts, matchesAssetRef, classifyFill, resolveAssetPrecisions, fetchAllFills, FillRecord, ClassifiedFill, };
29
59
  //# sourceMappingURL=fills_source.d.ts.map
@@ -8,6 +8,7 @@
8
8
  * of this (43-entry ASSETS table, toReal/assetPrec helpers, on-chain precision
9
9
  * resolution, query builder and paginated fetch); all of it lives here now.
10
10
  */
11
+ import { getErrorMessage } from '../modules/utils/errors.js';
11
12
  import * as KC from '../market_adapter/core/kibana_client.js';
12
13
  import { withReadOnlyClient } from './chain_pool.js';
13
14
  const { kibanaSearch, DEFAULT_CONFIG: BASE_CONFIG } = KC;
@@ -75,6 +76,74 @@ function toReal(amount, assetId) {
75
76
  return NaN;
76
77
  return amount / Math.pow(10, p);
77
78
  }
79
+ /**
80
+ * A fill needs strictly positive, finite amounts on both legs. A zero base
81
+ * amount would produce an infinite price (quoteAmount / 0) that poisons
82
+ * downstream inventory lots and PnL aggregates.
83
+ */
84
+ function isValidFillAmounts(baseAmount, quoteAmount) {
85
+ return Number.isFinite(baseAmount) && Number.isFinite(quoteAmount)
86
+ && baseAmount > 0 && quoteAmount > 0;
87
+ }
88
+ /** Match a user-supplied pair leg against a chain asset id or its symbol. */
89
+ function matchesAssetRef(ref, assetId) {
90
+ const want = String(ref).trim().toUpperCase();
91
+ if (!want)
92
+ return false;
93
+ return assetId.toUpperCase() === want || assetSymbol(assetId).toUpperCase() === want;
94
+ }
95
+ /**
96
+ * Resolve a raw fill's buy/sell direction, base/quote assets and real amounts.
97
+ *
98
+ * Direction/base/quote rules (single home for trade_profitability and
99
+ * grid_correction_check, which previously carried verbatim copies):
100
+ * - BTS on the pays side → buy the received asset, quote = BTS.
101
+ * - BTS on the receives side → sell the paid asset, quote = BTS.
102
+ * - any other cross-pair → lower asset id is the base; sell when pays < receives.
103
+ *
104
+ * Returns `null` when an asset precision is unknown or either real amount is
105
+ * not strictly positive/finite. Callers own the skip counting and any extra
106
+ * validation (e.g. the market-fee asset check in trade_profitability).
107
+ */
108
+ function classifyFill(f) {
109
+ const pAsset = f.pays.asset_id;
110
+ const rAsset = f.receives.asset_id;
111
+ let direction;
112
+ let baseAsset;
113
+ let quoteAsset;
114
+ let baseAmount;
115
+ let quoteAmount;
116
+ if (pAsset === BTS_ID && rAsset !== BTS_ID) {
117
+ direction = 'buy';
118
+ baseAsset = rAsset;
119
+ quoteAsset = BTS_ID;
120
+ baseAmount = toReal(f.receives.amount, rAsset);
121
+ quoteAmount = toReal(f.pays.amount, BTS_ID);
122
+ }
123
+ else if (rAsset === BTS_ID && pAsset !== BTS_ID) {
124
+ direction = 'sell';
125
+ baseAsset = pAsset;
126
+ quoteAsset = BTS_ID;
127
+ baseAmount = toReal(f.pays.amount, pAsset);
128
+ quoteAmount = toReal(f.receives.amount, BTS_ID);
129
+ }
130
+ else {
131
+ // Non-BTS cross-pair: consistent ordering (lower asset id = base).
132
+ const baseForCheck = pAsset < rAsset ? pAsset : rAsset;
133
+ const quoteForCheck = pAsset < rAsset ? rAsset : pAsset;
134
+ if (assetPrec(baseForCheck) === undefined || assetPrec(quoteForCheck) === undefined)
135
+ return null;
136
+ const isSell = pAsset < rAsset;
137
+ direction = isSell ? 'sell' : 'buy';
138
+ baseAsset = isSell ? pAsset : rAsset;
139
+ quoteAsset = isSell ? rAsset : pAsset;
140
+ baseAmount = toReal(isSell ? f.pays.amount : f.receives.amount, baseAsset);
141
+ quoteAmount = toReal(isSell ? f.receives.amount : f.pays.amount, quoteAsset);
142
+ }
143
+ if (!isValidFillAmounts(baseAmount, quoteAmount))
144
+ return null;
145
+ return { direction, baseAsset, quoteAsset, baseAmount, quoteAmount, price: quoteAmount / baseAmount };
146
+ }
78
147
  /**
79
148
  * Collect all unique non-BTS asset IDs from fills, resolve unknown precisions
80
149
  * from the blockchain, and populate the runtime cache.
@@ -112,7 +181,7 @@ async function resolveAssetPrecisions(fills) {
112
181
  });
113
182
  }
114
183
  catch (e) {
115
- console.warn(` [warn] Asset resolution failed: ${e.message}. Fills with unknown assets will be skipped.`);
184
+ console.warn(` [warn] Asset resolution failed: ${getErrorMessage(e)}. Fills with unknown assets will be skipped.`);
116
185
  }
117
186
  }
118
187
  function buildFillQuery(accountId, gte, lte, size) {
@@ -159,31 +228,32 @@ async function fetchAllFills(config, accountId, gte, lte) {
159
228
  const hits = result?.hits?.hits ?? [];
160
229
  if (!hits.length)
161
230
  break;
162
- for (const hit of hits) {
231
+ for (const rawHit of hits) {
232
+ const hit = rawHit;
163
233
  const src = hit?._source;
164
234
  const op = src?.operation_history?.op_object;
165
235
  if (!op || !op.pays || !op.receives)
166
236
  continue;
167
237
  fills.push({
168
- time: src.block_data?.block_time ?? '',
169
- blockNum: src.block_data?.block_num ?? 0,
238
+ time: String(src.block_data?.block_time ?? ''),
239
+ blockNum: Number(src.block_data?.block_num ?? 0),
170
240
  opNum: Number(src.operation_id_num ?? 0),
171
- orderId: op.order_id ?? '',
172
- accountId: op.account_id ?? '',
173
- pays: { amount: Number(op.pays.amount ?? 0), asset_id: op.pays.asset_id ?? '' },
174
- receives: { amount: Number(op.receives.amount ?? 0), asset_id: op.receives.asset_id ?? '' },
175
- fee: { amount: Number(op.fee?.amount ?? 0), asset_id: op.fee?.asset_id ?? '' },
176
- isMaker: op.is_maker ?? false,
177
- sort: hit.sort,
241
+ orderId: String(op.order_id ?? ''),
242
+ accountId: String(op.account_id ?? ''),
243
+ pays: { amount: Number(op.pays.amount ?? 0), asset_id: String(op.pays.asset_id ?? '') },
244
+ receives: { amount: Number(op.receives.amount ?? 0), asset_id: String(op.receives.asset_id ?? '') },
245
+ fee: { amount: Number(op.fee?.amount ?? 0), asset_id: String(op.fee?.asset_id ?? '') },
246
+ isMaker: Boolean(op.is_maker ?? false),
247
+ sort: hit.sort ?? [],
178
248
  });
179
249
  }
180
250
  if (hits.length < pageSize)
181
251
  break;
182
- searchAfter = hits[hits.length - 1].sort;
252
+ searchAfter = hits[hits.length - 1].sort ?? null;
183
253
  if (!Array.isArray(searchAfter))
184
254
  break;
185
255
  }
186
256
  return fills;
187
257
  }
188
- export { BTS_ID, assetSymbol, assetPrec, toReal, resolveAssetPrecisions, fetchAllFills, };
258
+ export { BTS_ID, assetSymbol, assetPrec, toReal, isValidFillAmounts, matchesAssetRef, classifyFill, resolveAssetPrecisions, fetchAllFills, };
189
259
  //# sourceMappingURL=fills_source.js.map
@@ -1,9 +1,5 @@
1
1
  #!/usr/bin/env node
2
2
  import { FillRecord } from './fills_source.js';
3
- interface OrderUpdate {
4
- orderId: string;
5
- sequence: number;
6
- }
7
3
  interface TradeFill {
8
4
  time: string;
9
5
  orderId: string;
@@ -29,19 +25,6 @@ interface Violation {
29
25
  halfInc: number;
30
26
  threshold: number;
31
27
  }
32
- interface AggregatedOrder {
33
- orderId: string;
34
- direction: 'buy' | 'sell';
35
- baseAsset: string;
36
- quoteAsset: string;
37
- baseAmount: number;
38
- quoteAmount: number;
39
- price: number;
40
- time: string;
41
- sequence: number;
42
- fillCount: number;
43
- isMaker: boolean;
44
- }
45
28
  /**
46
29
  * LAST-FILL-GUARD helper — pivot ± halfIncrement (replaces price-tolerance).
47
30
  * last fill @x with increment i: BUY < x*(1 - i/2/100), SELL > x*(1 + i/2/100)
@@ -54,7 +37,7 @@ interface AggregatedOrder {
54
37
  * @param {number|any} incrementPercent - Grid increment percent (e.g. 0.5). If not finite/<=0 falls back to DEFAULT_CONFIG.
55
38
  * @returns {{blocked: boolean, pivot: number|null, halfInc: number, threshold: number|null}}
56
39
  */
57
- declare function isLastFillGuardBlocked(price: any, type: any, lastPrice: any, lastType: any, incrementPercent: any): {
40
+ declare function isLastFillGuardBlocked(price: unknown, type: unknown, lastPrice: unknown, lastType: unknown, incrementPercent: unknown): {
58
41
  blocked: boolean;
59
42
  pivot: number | null;
60
43
  halfInc?: number;
@@ -64,10 +47,5 @@ declare function classifyFills(fills: FillRecord[]): {
64
47
  trades: TradeFill[];
65
48
  skipped: number;
66
49
  };
67
- declare function aggregateByOrder(trades: TradeFill[], updates?: OrderUpdate[]): AggregatedOrder[];
68
- declare function detectViolations(items: (TradeFill | AggregatedOrder)[], includeCrossPair: boolean, incrementPercent: number): {
69
- violations: Violation[];
70
- checkedTransitions: number;
71
- };
72
- export { isLastFillGuardBlocked, classifyFills, aggregateByOrder, detectViolations, TradeFill, Violation, AggregatedOrder };
50
+ export { isLastFillGuardBlocked, classifyFills, TradeFill, Violation };
73
51
  //# sourceMappingURL=grid_correction_check.d.ts.map
@@ -31,13 +31,14 @@
31
31
  * node dist/analysis/grid_correction_check.js --bot-key <bot-key> --hours 720 --increment 0.5
32
32
  * node dist/analysis/grid_correction_check.js --list-bots
33
33
  */
34
+ import { getErrorMessage, getErrorField } from '../modules/utils/errors.js';
34
35
  import fs from 'node:fs';
35
36
  import { pathToFileURL } from 'node:url';
36
37
  import * as KC from '../market_adapter/core/kibana_client.js';
37
38
  import * as C from '../modules/constants.js';
38
39
  import { loadBotSettings, computeBotKey } from './bot_key_utils.js';
39
40
  import { resolveBotAccount } from './account_resolver.js';
40
- import { BTS_ID, assetPrec, assetSymbol, fetchAllFills, resolveAssetPrecisions, toReal, } from './fills_source.js';
41
+ import { classifyFill, assetSymbol, fetchAllFills, resolveAssetPrecisions, } from './fills_source.js';
41
42
  const { kibanaSearch, DEFAULT_CONFIG: BASE_CONFIG } = KC;
42
43
  const OP_LIMIT_ORDER_UPDATE = 77;
43
44
  // ─── LAST-FILL-GUARD helper (1:1 with dexbot_cow_runtime.ts) ─────────────────
@@ -62,7 +63,7 @@ function isLastFillGuardBlocked(price, type, lastPrice, lastType, incrementPerce
62
63
  const pivot = Number(lastPrice);
63
64
  let inc = Number(incrementPercent);
64
65
  if (!Number.isFinite(inc) || inc <= 0) {
65
- inc = Number(C?.DEFAULT_CONFIG?.incrementPercent ?? 0.5);
66
+ inc = Number(C.DEFAULT_CONFIG?.incrementPercent ?? 0.5);
66
67
  }
67
68
  if (!Number.isFinite(inc) || inc <= 0)
68
69
  return { blocked: false, pivot: null };
@@ -82,7 +83,7 @@ function resolveIncrementPercent(botMeta, override) {
82
83
  const fromBot = Number(botMeta?.incrementPercent);
83
84
  if (Number.isFinite(fromBot) && fromBot > 0)
84
85
  return fromBot;
85
- const fromDefault = Number(C?.DEFAULT_CONFIG?.incrementPercent ?? 0.5);
86
+ const fromDefault = Number(C.DEFAULT_CONFIG?.incrementPercent ?? 0.5);
86
87
  if (Number.isFinite(fromDefault) && fromDefault > 0)
87
88
  return fromDefault;
88
89
  return 0.5;
@@ -111,7 +112,7 @@ Options:
111
112
  --refresh-account Force re-resolution of preferredAccount and update the
112
113
  stored accountId when it changed (default: reuse the
113
114
  stored accountId with no chain lookup)
114
- --increment <pct> Grid increment percent (default: from bot config or ${Number(C?.DEFAULT_CONFIG?.incrementPercent ?? 0.5)})
115
+ --increment <pct> Grid increment percent (default: from bot config or ${Number(C.DEFAULT_CONFIG?.incrementPercent ?? 0.5)})
115
116
  --tolerance <pct> Deprecated alias for --increment (kept for compat, prefer --increment)
116
117
  --per-fill Check at fill granularity (default: per-order aggregated)
117
118
  --include-cross-pair Check consecutive fills across different pairs (default: same pair only)
@@ -259,7 +260,7 @@ function resolveTimeRange(opts) {
259
260
  // ─── Account resolution errors ────────────────────────────────────────────────
260
261
  /** Map a shared account-resolver failure onto this tool's operator-facing hint. */
261
262
  function reportAccountFailure(resolved, opts) {
262
- switch (resolved.reason) {
263
+ switch (resolved?.reason) {
263
264
  case 'bot-not-found':
264
265
  console.error(`Error: bot key '${opts.botKey}' not found in profiles/bots.json and no --account provided.`);
265
266
  console.error('Use --list-bots to see available keys, or pass --account <1.2.x> explicitly.');
@@ -306,7 +307,7 @@ async function fetchAllOrderUpdates(config, accountId, gte, lte) {
306
307
  if (!hits.length)
307
308
  break;
308
309
  for (const hit of hits) {
309
- const source = hit?._source || {};
310
+ const source = (hit?._source || {});
310
311
  const op = source.operation_history?.op_object || {};
311
312
  const orderId = op.order;
312
313
  if (!orderId)
@@ -316,77 +317,34 @@ async function fetchAllOrderUpdates(config, accountId, gte, lte) {
316
317
  updates.push({ orderId, sequence: blockNum * 1e6 + opNum });
317
318
  }
318
319
  const last = hits[hits.length - 1];
319
- searchAfter = last?.sort;
320
+ searchAfter = last?.sort ?? null;
320
321
  if (!searchAfter || hits.length < pageSize)
321
322
  break;
322
323
  }
323
324
  return updates;
324
325
  }
325
326
  // ─── Fill classification ─────────────────────────────────────────────────────
327
+ // Direction/base/quote resolution is shared with trade_profitability via
328
+ // fills_source.classifyFill; this wrapper only owns the skip count and the
329
+ // TradeFill shape (no market-fee fields — grid checks prices, not PnL).
326
330
  function classifyFills(fills) {
327
331
  const trades = [];
328
332
  let skipped = 0;
329
333
  for (const f of fills) {
330
- const pAsset = f.pays.asset_id;
331
- const rAsset = f.receives.asset_id;
332
- let direction;
333
- let baseAsset;
334
- let quoteAsset;
335
- let baseAmount;
336
- let quoteAmount;
337
- let price;
338
- if (pAsset === BTS_ID && rAsset !== BTS_ID) {
339
- direction = 'buy';
340
- baseAsset = rAsset;
341
- quoteAsset = BTS_ID;
342
- baseAmount = toReal(f.receives.amount, rAsset);
343
- quoteAmount = toReal(f.pays.amount, BTS_ID);
344
- if (!Number.isFinite(baseAmount) || !Number.isFinite(quoteAmount) || baseAmount <= 0 || quoteAmount <= 0) {
345
- skipped++;
346
- continue;
347
- }
348
- price = quoteAmount / baseAmount;
349
- }
350
- else if (rAsset === BTS_ID && pAsset !== BTS_ID) {
351
- direction = 'sell';
352
- baseAsset = pAsset;
353
- quoteAsset = BTS_ID;
354
- baseAmount = toReal(f.pays.amount, pAsset);
355
- quoteAmount = toReal(f.receives.amount, BTS_ID);
356
- if (!Number.isFinite(baseAmount) || !Number.isFinite(quoteAmount) || baseAmount <= 0 || quoteAmount <= 0) {
357
- skipped++;
358
- continue;
359
- }
360
- price = quoteAmount / baseAmount;
361
- }
362
- else {
363
- const baseForCheck = pAsset < rAsset ? pAsset : rAsset;
364
- const quoteForCheck = pAsset < rAsset ? rAsset : pAsset;
365
- if (assetPrec(baseForCheck) === undefined || assetPrec(quoteForCheck) === undefined) {
366
- skipped++;
367
- continue;
368
- }
369
- const isSell = pAsset < rAsset;
370
- direction = isSell ? 'sell' : 'buy';
371
- baseAsset = isSell ? pAsset : rAsset;
372
- quoteAsset = isSell ? rAsset : pAsset;
373
- baseAmount = toReal(isSell ? f.pays.amount : f.receives.amount, baseAsset);
374
- quoteAmount = toReal(isSell ? f.receives.amount : f.pays.amount, quoteAsset);
375
- if (!Number.isFinite(baseAmount) || !Number.isFinite(quoteAmount) || baseAmount <= 0 || quoteAmount <= 0) {
376
- skipped++;
377
- continue;
378
- }
379
- price = quoteAmount / baseAmount;
334
+ const c = classifyFill(f);
335
+ if (!c) {
336
+ skipped++;
337
+ continue;
380
338
  }
381
339
  trades.push({
382
340
  time: f.time,
383
341
  orderId: f.orderId,
384
- direction,
385
- baseAsset,
386
- quoteAsset,
387
- baseAmount,
388
- quoteAmount,
389
- price,
342
+ direction: c.direction,
343
+ baseAsset: c.baseAsset,
344
+ quoteAsset: c.quoteAsset,
345
+ baseAmount: c.baseAmount,
346
+ quoteAmount: c.quoteAmount,
347
+ price: c.price,
390
348
  isMaker: f.isMaker,
391
349
  sequence: f.blockNum * 1e6 + f.opNum,
392
350
  });
@@ -668,7 +626,7 @@ async function main() {
668
626
  const { gte, lte, label } = resolveTimeRange(opts);
669
627
  console.log(`\nLast-fill guard check — bot-key: ${opts.botKey}`);
670
628
  console.log(`Range: ${label}`);
671
- const resolvedAccount = await resolveBotAccount(opts.botKey, {
629
+ const resolvedAccount = await resolveBotAccount(String(opts.botKey), {
672
630
  overrideAccount: opts.account,
673
631
  refresh: opts.refreshAccount,
674
632
  });
@@ -721,19 +679,19 @@ async function main() {
721
679
  }
722
680
  const { violations, checkedTransitions } = detectViolations(items, opts.includeCrossPair, incrementPercent);
723
681
  const ordersForReport = opts.perFill ? null : items;
724
- printReport(trades, ordersForReport, violations, checkedTransitions, skipped, label, opts.botKey, accountId, botMeta, opts.perFill, opts.includeCrossPair, incrementPercent, gte, lte);
682
+ printReport(trades, ordersForReport, violations, checkedTransitions, skipped, label, String(opts.botKey), accountId, botMeta, opts.perFill, opts.includeCrossPair, incrementPercent, gte, lte);
725
683
  if (opts.json)
726
- exportJson(opts.json, violations, trades, label, opts.botKey, accountId, incrementPercent);
684
+ exportJson(opts.json, violations, trades, label, String(opts.botKey), accountId, incrementPercent);
727
685
  if (opts.csv)
728
686
  exportCsv(opts.csv, violations);
729
687
  process.exit(violations.length > 0 ? 2 : 0);
730
688
  }
731
- export { isLastFillGuardBlocked, classifyFills, aggregateByOrder, detectViolations };
689
+ export { isLastFillGuardBlocked, classifyFills };
732
690
  if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
733
691
  main().catch(e => {
734
- console.error('\n[fatal]', e?.message ?? e);
692
+ console.error('\n[fatal]', getErrorMessage(e));
735
693
  if (process.env.DEBUG)
736
- console.error(e?.stack);
694
+ console.error(getErrorField(e, 'stack'));
737
695
  process.exit(1);
738
696
  });
739
697
  }
@@ -6,15 +6,100 @@ import { getCandleClose, getCandleTimestamp, normalizeCandle } from '../market_a
6
6
  * re-exported here so analysis tooling shares one logic path with the
7
7
  * live adapter and the browser-embedded chart scripts.
8
8
  */
9
- declare function range(min: number, max: number, step: number, decimals?: any): number[];
10
- declare function calcStdDev(arr: any): number;
9
+ declare function range(min: number, max: number, step: number, decimals?: number): number[];
10
+ declare function calcStdDev(arr: number[]): number;
11
+ /**
12
+ * Median of a numeric array (average of the two middle values for even
13
+ * lengths). Returns `null` for an empty array. Shared by the analysis
14
+ * backtests instead of each keeping a local copy.
15
+ */
16
+ declare function median(values: number[]): number | null;
17
+ interface PercentileOptions {
18
+ /** 'linear' interpolates between the two bracketing ranks (R-7, default);
19
+ * 'nearest' picks a single rank. */
20
+ interpolation?: 'linear' | 'nearest';
21
+ /** For 'nearest': round the rank (default) or floor it. */
22
+ rounding?: 'round' | 'floor';
23
+ /** Skip the internal sort when the input is already ascending. */
24
+ sorted?: boolean;
25
+ /** Value returned for an empty input (default null). */
26
+ empty?: number | null;
27
+ }
28
+ /**
29
+ * Percentile of a numeric array.
30
+ *
31
+ * Single home for the four copies previously kept by optimizer_high_resolution
32
+ * (fractional q, linear), backtest_ama_slope_huber (nearest/round),
33
+ * trade_profitability (pre-sorted, linear) and backtest_lookback_drawdown
34
+ * (nearest/floor). The options preserve each caller's exact ranking rule so
35
+ * centralizing does not silently change any reported number.
36
+ *
37
+ * @param values numeric array (copied + sorted unless `sorted` is true)
38
+ * @param p percentile position in [0, 100]
39
+ */
40
+ declare function percentile(values: number[], p: number, opts: PercentileOptions & {
41
+ empty: number;
42
+ }): number;
43
+ declare function percentile(values: number[], p: number, opts?: PercentileOptions): number | null;
44
+ interface FmtNumOptions {
45
+ /** Text returned for a value that is not finite. */
46
+ fallback?: string;
47
+ /** When true, only a real finite `number` counts (null/undefined fall back);
48
+ * when false (default), values are coerced with `Number()` first, so
49
+ * `null` formats as `0`. */
50
+ strict?: boolean;
51
+ }
52
+ /**
53
+ * Fixed-decimal number formatting with a non-finite fallback.
54
+ *
55
+ * Single home for the identical `fmt(x, d)` copies in backtest_ama_slope_huber
56
+ * and backtest_lookback_drawdown, plus the strict/padded variant in
57
+ * bot_fitting/shared_utils (`fmt(x, d, { fallback: ' n/a', strict: true })`).
58
+ */
59
+ declare function fmtNum(x: number | null | undefined, d?: number, { fallback, strict }?: FmtNumOptions): string;
60
+ /**
61
+ * Evenly spaced geometric progression from `min` to `max` (endpoints forced).
62
+ * Values are optionally snapped to `quantum` and clamped back into the range,
63
+ * rounded to `decimals`, deduped and sorted ascending. Single home for the
64
+ * helper previously copied into analyze_lambda_vs_slow and
65
+ * optimizer_high_resolution (which used different rounding precision).
66
+ */
67
+ declare function geometricRange(min: number, max: number, count: number, { decimals, quantum }?: {
68
+ decimals?: number | undefined;
69
+ quantum?: number | null | undefined;
70
+ }): number[];
11
71
  /**
12
72
  * Parse a candle JSON file with format detection:
13
73
  * flat array → {candles: [...]} → {data: [...]}
14
74
  */
15
- declare function loadCandleFile(filePath: any): {
16
- candles: any;
17
- meta: any;
75
+ export interface CandleFile {
76
+ candles: Record<string, unknown>[];
77
+ meta: Record<string, unknown> | null;
78
+ }
79
+ declare function loadCandleFile(filePath: string): CandleFile;
80
+ /** Normalized OHLCV candle (millisecond-free epoch seconds, like the adapter). */
81
+ type Candle = {
82
+ time: number;
83
+ open: number;
84
+ high: number;
85
+ low: number;
86
+ close: number;
87
+ volume: number;
88
+ };
89
+ /**
90
+ * Load candle records from a single JSON file or a directory of monthly JSON
91
+ * shards.
92
+ *
93
+ * Directory entries are filtered to `*.json` (manifests skipped) and sorted;
94
+ * shards are merged and deduped by candle time (a later file overwrites an
95
+ * earlier duplicate). Returns the merged candles, the first non-null `meta` and
96
+ * the file count. Single home for the loader previously copied into
97
+ * backtest_ama_slope_huber.
98
+ */
99
+ declare function loadCandleSeries(input: string): {
100
+ candles: Candle[];
101
+ meta: Record<string, unknown> | null;
102
+ files: number;
18
103
  };
19
- export { range, calcStdDev, getCandleClose, getCandleTimestamp, normalizeCandle, loadCandleFile, };
104
+ export { range, calcStdDev, geometricRange, median, percentile, fmtNum, getCandleClose, getCandleTimestamp, normalizeCandle, loadCandleFile, loadCandleSeries, };
20
105
  //# sourceMappingURL=math_utils.d.ts.map