dexbot 1.4.10 → 1.4.12
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.
- package/README.md +6 -5
- package/dist/analysis/chart_utils.d.ts.map +1 -1
- package/dist/analysis/chart_utils.js +9 -18
- package/dist/analysis/chart_utils.js.map +1 -1
- package/dist/analysis/math_utils.d.ts.map +1 -1
- package/dist/analysis/math_utils.js +6 -16
- package/dist/analysis/math_utils.js.map +1 -1
- package/dist/analysis/tradingview/tradingview_uplot_chart_generator.d.ts +1 -1
- package/dist/analysis/tradingview/tradingview_uplot_chart_generator.d.ts.map +1 -1
- package/dist/analysis/tradingview/tradingview_uplot_chart_generator.js +31 -38
- package/dist/analysis/tradingview/tradingview_uplot_chart_generator.js.map +1 -1
- package/dist/analysis/trend_detection/hurst_analyzer.js +7 -10
- package/dist/analysis/trend_detection/hurst_analyzer.js.map +1 -1
- package/dist/analysis/trend_detection/kalman_trend_analyzer.js +10 -14
- package/dist/analysis/trend_detection/kalman_trend_analyzer.js.map +1 -1
- package/dist/analysis/trend_detection/kalman_velocity_smoothing.js +10 -15
- package/dist/analysis/trend_detection/kalman_velocity_smoothing.js.map +1 -1
- package/dist/analysis/trend_detection/permutation_entropy_analyzer.js +7 -10
- package/dist/analysis/trend_detection/permutation_entropy_analyzer.js.map +1 -1
- package/dist/bot.d.ts.map +1 -1
- package/dist/bot.js +4 -12
- package/dist/bot.js.map +1 -1
- package/dist/credential-daemon.d.ts +0 -1
- package/dist/credential-daemon.d.ts.map +1 -1
- package/dist/credential-daemon.js +3 -123
- package/dist/credential-daemon.js.map +1 -1
- package/dist/dexbot.d.ts +0 -87
- package/dist/dexbot.d.ts.map +1 -1
- package/dist/dexbot.js +54 -23
- package/dist/dexbot.js.map +1 -1
- package/dist/market_adapter/ama_signal_runner.js +8 -9
- package/dist/market_adapter/ama_signal_runner.js.map +1 -1
- package/dist/market_adapter/candle_utils.js +1 -7
- package/dist/market_adapter/candle_utils.js.map +1 -1
- package/dist/market_adapter/core/asymmetric_bounds.js +1 -4
- package/dist/market_adapter/core/asymmetric_bounds.js.map +1 -1
- package/dist/market_adapter/core/config_normalizers.js +5 -9
- package/dist/market_adapter/core/config_normalizers.js.map +1 -1
- package/dist/market_adapter/core/kibana_candles.js +8 -15
- package/dist/market_adapter/core/kibana_candles.js.map +1 -1
- package/dist/market_adapter/core/kibana_client.js +7 -13
- package/dist/market_adapter/core/kibana_client.js.map +1 -1
- package/dist/market_adapter/core/kibana_market_candles.js +6 -12
- package/dist/market_adapter/core/kibana_market_candles.js.map +1 -1
- package/dist/market_adapter/core/market_adapter_service.d.ts +2 -0
- package/dist/market_adapter/core/market_adapter_service.d.ts.map +1 -1
- package/dist/market_adapter/core/market_adapter_service.js +100 -101
- package/dist/market_adapter/core/market_adapter_service.js.map +1 -1
- package/dist/market_adapter/core/strategies/ama.js +5 -10
- package/dist/market_adapter/core/strategies/ama.js.map +1 -1
- package/dist/market_adapter/core/strategies/ama_slope_model.js +21 -24
- package/dist/market_adapter/core/strategies/ama_slope_model.js.map +1 -1
- package/dist/market_adapter/core/strategies/atr/calculator.js +3 -5
- package/dist/market_adapter/core/strategies/atr/calculator.js.map +1 -1
- package/dist/market_adapter/core/strategies/collateral_manager.js +4 -6
- package/dist/market_adapter/core/strategies/collateral_manager.js.map +1 -1
- package/dist/market_adapter/core/strategies/regime_gate.js +15 -22
- package/dist/market_adapter/core/strategies/regime_gate.js.map +1 -1
- package/dist/market_adapter/index.d.ts +23 -23
- package/dist/market_adapter/index.d.ts.map +1 -1
- package/dist/market_adapter/index.js +24 -82
- package/dist/market_adapter/index.js.map +1 -1
- package/dist/market_adapter/inputs/fetch_cex_synthetic_data.js +2 -1
- package/dist/market_adapter/inputs/fetch_cex_synthetic_data.js.map +1 -1
- package/dist/market_adapter/inputs/fetch_lp_data.d.ts.map +1 -1
- package/dist/market_adapter/inputs/fetch_lp_data.js +59 -98
- package/dist/market_adapter/inputs/fetch_lp_data.js.map +1 -1
- package/dist/market_adapter/inputs/kibana_source.js +11 -17
- package/dist/market_adapter/inputs/kibana_source.js.map +1 -1
- package/dist/market_adapter/interval_utils.js +1 -2
- package/dist/market_adapter/interval_utils.js.map +1 -1
- package/dist/market_adapter/log_format.js +1 -7
- package/dist/market_adapter/log_format.js.map +1 -1
- package/dist/market_adapter/lp_chart_core.js +20 -22
- package/dist/market_adapter/lp_chart_core.js.map +1 -1
- package/dist/market_adapter/lp_chart_runner.d.ts +1 -1
- package/dist/market_adapter/lp_chart_runner.d.ts.map +1 -1
- package/dist/market_adapter/lp_chart_runner.js +41 -57
- package/dist/market_adapter/lp_chart_runner.js.map +1 -1
- package/dist/market_adapter/lp_chart_strategy_loader.js +30 -34
- package/dist/market_adapter/lp_chart_strategy_loader.js.map +1 -1
- package/dist/market_adapter/market_adapter.d.ts +2 -2
- package/dist/market_adapter/market_adapter.d.ts.map +1 -1
- package/dist/market_adapter/market_adapter.js +163 -222
- package/dist/market_adapter/market_adapter.js.map +1 -1
- package/dist/market_adapter/merge_lp_data.js +17 -18
- package/dist/market_adapter/merge_lp_data.js.map +1 -1
- package/dist/market_adapter/test_helpers.js +3 -14
- package/dist/market_adapter/test_helpers.js.map +1 -1
- package/dist/market_adapter/utils/adapter_client.js +7 -16
- package/dist/market_adapter/utils/adapter_client.js.map +1 -1
- package/dist/market_adapter/utils/atomic_write.js +3 -5
- package/dist/market_adapter/utils/atomic_write.js.map +1 -1
- package/dist/market_adapter/utils/chain.d.ts.map +1 -1
- package/dist/market_adapter/utils/chain.js +9 -17
- package/dist/market_adapter/utils/chain.js.map +1 -1
- package/dist/market_adapter/utils/data_discovery.js +8 -10
- package/dist/market_adapter/utils/data_discovery.js.map +1 -1
- package/dist/market_adapter/utils/dynamic_grid_snapshot.d.ts.map +1 -1
- package/dist/market_adapter/utils/dynamic_grid_snapshot.js +11 -14
- package/dist/market_adapter/utils/dynamic_grid_snapshot.js.map +1 -1
- package/dist/market_adapter/utils/file_lock.js +14 -19
- package/dist/market_adapter/utils/file_lock.js.map +1 -1
- package/dist/market_adapter/utils/native_history.js +8 -14
- package/dist/market_adapter/utils/native_history.js.map +1 -1
- package/dist/market_adapter/utils/paths.js +3 -6
- package/dist/market_adapter/utils/paths.js.map +1 -1
- package/dist/modules/account_bots.d.ts +1 -1
- package/dist/modules/account_bots.d.ts.map +1 -1
- package/dist/modules/account_bots.js +90 -97
- package/dist/modules/account_bots.js.map +1 -1
- package/dist/modules/account_orders.d.ts +0 -9
- package/dist/modules/account_orders.d.ts.map +1 -1
- package/dist/modules/account_orders.js +26 -121
- package/dist/modules/account_orders.js.map +1 -1
- package/dist/modules/authority_resolver.js +12 -18
- package/dist/modules/authority_resolver.js.map +1 -1
- package/dist/modules/bitshares-native/chain_client.js +16 -21
- package/dist/modules/bitshares-native/chain_client.js.map +1 -1
- package/dist/modules/bitshares-native/crypto/ecc.browser.js +12 -72
- package/dist/modules/bitshares-native/crypto/ecc.browser.js.map +1 -1
- package/dist/modules/bitshares-native/crypto/ecc.js +13 -40
- package/dist/modules/bitshares-native/crypto/ecc.js.map +1 -1
- package/dist/modules/bitshares-native/crypto/ecc_selector.d.ts.map +1 -1
- package/dist/modules/bitshares-native/crypto/ecc_selector.js +5 -6
- package/dist/modules/bitshares-native/crypto/ecc_selector.js.map +1 -1
- package/dist/modules/bitshares-native/index.d.ts +8 -8
- package/dist/modules/bitshares-native/index.d.ts.map +1 -1
- package/dist/modules/bitshares-native/index.js +11 -68
- package/dist/modules/bitshares-native/index.js.map +1 -1
- package/dist/modules/bitshares-native/lru_cache.js +3 -6
- package/dist/modules/bitshares-native/lru_cache.js.map +1 -1
- package/dist/modules/bitshares-native/resolvers.js +9 -11
- package/dist/modules/bitshares-native/resolvers.js.map +1 -1
- package/dist/modules/bitshares-native/serial/chain_constants.js +3 -28
- package/dist/modules/bitshares-native/serial/chain_constants.js.map +1 -1
- package/dist/modules/bitshares-native/serial/index.d.ts +4 -4
- package/dist/modules/bitshares-native/serial/index.d.ts.map +1 -1
- package/dist/modules/bitshares-native/serial/index.js +5 -46
- package/dist/modules/bitshares-native/serial/index.js.map +1 -1
- package/dist/modules/bitshares-native/serial/operations.d.ts +98 -98
- package/dist/modules/bitshares-native/serial/operations.d.ts.map +1 -1
- package/dist/modules/bitshares-native/serial/operations.js +372 -473
- package/dist/modules/bitshares-native/serial/operations.js.map +1 -1
- package/dist/modules/bitshares-native/serial/serializer.js +3 -8
- package/dist/modules/bitshares-native/serial/serializer.js.map +1 -1
- package/dist/modules/bitshares-native/serial/types.js +9 -74
- package/dist/modules/bitshares-native/serial/types.js.map +1 -1
- package/dist/modules/bitshares-native/signing_client.d.ts.map +1 -1
- package/dist/modules/bitshares-native/signing_client.js +11 -48
- package/dist/modules/bitshares-native/signing_client.js.map +1 -1
- package/dist/modules/bitshares-native/subscriptions.d.ts.map +1 -1
- package/dist/modules/bitshares-native/subscriptions.js +24 -27
- package/dist/modules/bitshares-native/subscriptions.js.map +1 -1
- package/dist/modules/bitshares-native/transport.d.ts.map +1 -1
- package/dist/modules/bitshares-native/transport.js +14 -26
- package/dist/modules/bitshares-native/transport.js.map +1 -1
- package/dist/modules/bitshares-native/tx/builder.js +29 -72
- package/dist/modules/bitshares-native/tx/builder.js.map +1 -1
- package/dist/modules/bitshares-native/tx/tx_cache.js +12 -21
- package/dist/modules/bitshares-native/tx/tx_cache.js.map +1 -1
- package/dist/modules/bitshares_client.d.ts +21 -0
- package/dist/modules/bitshares_client.d.ts.map +1 -1
- package/dist/modules/bitshares_client.js +32 -86
- package/dist/modules/bitshares_client.js.map +1 -1
- package/dist/modules/bot_settings.d.ts.map +1 -1
- package/dist/modules/bot_settings.js +30 -49
- package/dist/modules/bot_settings.js.map +1 -1
- package/dist/modules/bots_file_lock.d.ts.map +1 -1
- package/dist/modules/bots_file_lock.js +5 -13
- package/dist/modules/bots_file_lock.js.map +1 -1
- package/dist/modules/broadcast_failure.js +1 -3
- package/dist/modules/broadcast_failure.js.map +1 -1
- package/dist/modules/chain_keys.d.ts +4 -9
- package/dist/modules/chain_keys.d.ts.map +1 -1
- package/dist/modules/chain_keys.js +77 -182
- package/dist/modules/chain_keys.js.map +1 -1
- package/dist/modules/chain_orders.d.ts +2 -2
- package/dist/modules/chain_orders.d.ts.map +1 -1
- package/dist/modules/chain_orders.js +81 -144
- package/dist/modules/chain_orders.js.map +1 -1
- package/dist/modules/config.d.ts.map +1 -1
- package/dist/modules/config.js +23 -28
- package/dist/modules/config.js.map +1 -1
- package/dist/modules/constants.d.ts +4 -14
- package/dist/modules/constants.d.ts.map +1 -1
- package/dist/modules/constants.js +46 -78
- package/dist/modules/constants.js.map +1 -1
- package/dist/modules/cr_planner.js +15 -26
- package/dist/modules/cr_planner.js.map +1 -1
- package/dist/modules/credential_policy.d.ts +3 -6
- package/dist/modules/credential_policy.d.ts.map +1 -1
- package/dist/modules/credential_policy.js +62 -153
- package/dist/modules/credential_policy.js.map +1 -1
- package/dist/modules/credential_runtime.d.ts +1 -1
- package/dist/modules/credential_runtime.d.ts.map +1 -1
- package/dist/modules/credential_runtime.js +34 -41
- package/dist/modules/credential_runtime.js.map +1 -1
- package/dist/modules/credential_session_cache.d.ts +1 -1
- package/dist/modules/credential_session_cache.d.ts.map +1 -1
- package/dist/modules/credential_session_cache.js +5 -42
- package/dist/modules/credential_session_cache.js.map +1 -1
- package/dist/modules/credit_runtime.d.ts.map +1 -1
- package/dist/modules/credit_runtime.js +126 -162
- package/dist/modules/credit_runtime.js.map +1 -1
- package/dist/modules/crypto/browser_provider.d.ts +1 -1
- package/dist/modules/crypto/browser_provider.d.ts.map +1 -1
- package/dist/modules/crypto/browser_provider.js +7 -11
- package/dist/modules/crypto/browser_provider.js.map +1 -1
- package/dist/modules/crypto/index.d.ts +6 -6
- package/dist/modules/crypto/index.d.ts.map +1 -1
- package/dist/modules/crypto/index.js +12 -30
- package/dist/modules/crypto/index.js.map +1 -1
- package/dist/modules/crypto/node_provider.d.ts +1 -1
- package/dist/modules/crypto/node_provider.d.ts.map +1 -1
- package/dist/modules/crypto/node_provider.js +3 -5
- package/dist/modules/crypto/node_provider.js.map +1 -1
- package/dist/modules/crypto/provider.js +1 -2
- package/dist/modules/crypto/pure_ripemd160.js +1 -3
- package/dist/modules/crypto/pure_ripemd160.js.map +1 -1
- package/dist/modules/crypto/pure_scrypt.js +1 -4
- package/dist/modules/crypto/pure_scrypt.js.map +1 -1
- package/dist/modules/crypto/pure_secp256k1.d.ts +1 -1
- package/dist/modules/crypto/pure_secp256k1.d.ts.map +1 -1
- package/dist/modules/crypto/pure_secp256k1.js +1 -16
- package/dist/modules/crypto/pure_secp256k1.js.map +1 -1
- package/dist/modules/crypto/sync.d.ts.map +1 -1
- package/dist/modules/crypto/sync.js +16 -17
- package/dist/modules/crypto/sync.js.map +1 -1
- package/dist/modules/daemon_node_health.js +14 -16
- package/dist/modules/daemon_node_health.js.map +1 -1
- package/dist/modules/dexbot_class.d.ts +19 -14
- package/dist/modules/dexbot_class.d.ts.map +1 -1
- package/dist/modules/dexbot_class.js +127 -164
- package/dist/modules/dexbot_class.js.map +1 -1
- package/dist/modules/dexbot_cow_runtime.d.ts +28 -27
- package/dist/modules/dexbot_cow_runtime.d.ts.map +1 -1
- package/dist/modules/dexbot_cow_runtime.js +55 -45
- package/dist/modules/dexbot_cow_runtime.js.map +1 -1
- package/dist/modules/dexbot_credential_client.d.ts +6 -2
- package/dist/modules/dexbot_credential_client.d.ts.map +1 -1
- package/dist/modules/dexbot_credential_client.js +21 -31
- package/dist/modules/dexbot_credential_client.js.map +1 -1
- package/dist/modules/dexbot_fill_runtime.d.ts +23 -23
- package/dist/modules/dexbot_fill_runtime.d.ts.map +1 -1
- package/dist/modules/dexbot_fill_runtime.js +119 -77
- package/dist/modules/dexbot_fill_runtime.js.map +1 -1
- package/dist/modules/dexbot_maintenance_runtime.d.ts +46 -38
- package/dist/modules/dexbot_maintenance_runtime.d.ts.map +1 -1
- package/dist/modules/dexbot_maintenance_runtime.js +81 -81
- package/dist/modules/dexbot_maintenance_runtime.js.map +1 -1
- package/dist/modules/dexbot_startup_runtime.d.ts +3 -3
- package/dist/modules/dexbot_startup_runtime.d.ts.map +1 -1
- package/dist/modules/dexbot_startup_runtime.js +43 -81
- package/dist/modules/dexbot_startup_runtime.js.map +1 -1
- package/dist/modules/dexbot_state_recovery.d.ts +11 -11
- package/dist/modules/dexbot_state_recovery.js +32 -78
- package/dist/modules/dexbot_state_recovery.js.map +1 -1
- package/dist/modules/env.js +3 -7
- package/dist/modules/env.js.map +1 -1
- package/dist/modules/fund_registry.d.ts.map +1 -1
- package/dist/modules/fund_registry.js +12 -24
- package/dist/modules/fund_registry.js.map +1 -1
- package/dist/modules/general_settings.js +10 -16
- package/dist/modules/general_settings.js.map +1 -1
- package/dist/modules/graceful_shutdown.d.ts.map +1 -1
- package/dist/modules/graceful_shutdown.js +12 -19
- package/dist/modules/graceful_shutdown.js.map +1 -1
- package/dist/modules/key_store.d.ts.map +1 -1
- package/dist/modules/key_store.js +27 -65
- package/dist/modules/key_store.js.map +1 -1
- package/dist/modules/launcher/bot_supervisor.d.ts +1 -1
- package/dist/modules/launcher/bot_supervisor.d.ts.map +1 -1
- package/dist/modules/launcher/bot_supervisor.js +89 -118
- package/dist/modules/launcher/bot_supervisor.js.map +1 -1
- package/dist/modules/launcher/child_env.js +1 -5
- package/dist/modules/launcher/child_env.js.map +1 -1
- package/dist/modules/launcher/credential_bootstrap.js +22 -31
- package/dist/modules/launcher/credential_bootstrap.js.map +1 -1
- package/dist/modules/launcher/credential_daemon.js +37 -75
- package/dist/modules/launcher/credential_daemon.js.map +1 -1
- package/dist/modules/launcher/credential_secret.js +2 -37
- package/dist/modules/launcher/credential_secret.js.map +1 -1
- package/dist/modules/launcher/foreign_cred_daemon.js +25 -31
- package/dist/modules/launcher/foreign_cred_daemon.js.map +1 -1
- package/dist/modules/launcher/headless_password.js +10 -12
- package/dist/modules/launcher/headless_password.js.map +1 -1
- package/dist/modules/launcher/launch_modes.js +3 -9
- package/dist/modules/launcher/launch_modes.js.map +1 -1
- package/dist/modules/launcher/market_adapter_runtime.d.ts +1 -1
- package/dist/modules/launcher/market_adapter_runtime.d.ts.map +1 -1
- package/dist/modules/launcher/market_adapter_runtime.js +25 -35
- package/dist/modules/launcher/market_adapter_runtime.js.map +1 -1
- package/dist/modules/launcher/market_adapter_watchdog.d.ts.map +1 -1
- package/dist/modules/launcher/market_adapter_watchdog.js +42 -45
- package/dist/modules/launcher/market_adapter_watchdog.js.map +1 -1
- package/dist/modules/launcher/monolithic_runtime.d.ts +3 -2
- package/dist/modules/launcher/monolithic_runtime.d.ts.map +1 -1
- package/dist/modules/launcher/monolithic_runtime.js +97 -138
- package/dist/modules/launcher/monolithic_runtime.js.map +1 -1
- package/dist/modules/launcher/runtime_entry.d.ts +1 -11
- package/dist/modules/launcher/runtime_entry.d.ts.map +1 -1
- package/dist/modules/launcher/runtime_entry.js +12 -15
- package/dist/modules/launcher/runtime_entry.js.map +1 -1
- package/dist/modules/launcher/status_reporting.d.ts +1 -1
- package/dist/modules/launcher/status_reporting.d.ts.map +1 -1
- package/dist/modules/launcher/status_reporting.js +15 -36
- package/dist/modules/launcher/status_reporting.js.map +1 -1
- package/dist/modules/launcher/supervisor_control.js +4 -9
- package/dist/modules/launcher/supervisor_control.js.map +1 -1
- package/dist/modules/logger.d.ts +1 -1
- package/dist/modules/logger.d.ts.map +1 -1
- package/dist/modules/logger.js +3 -38
- package/dist/modules/logger.js.map +1 -1
- package/dist/modules/market_adapter_whitelist.js +9 -18
- package/dist/modules/market_adapter_whitelist.js.map +1 -1
- package/dist/modules/node_failure_ledger.js +1 -3
- package/dist/modules/node_failure_ledger.js.map +1 -1
- package/dist/modules/node_health_cache.d.ts.map +1 -1
- package/dist/modules/node_health_cache.js +15 -25
- package/dist/modules/node_health_cache.js.map +1 -1
- package/dist/modules/node_manager.d.ts +1 -1
- package/dist/modules/node_manager.d.ts.map +1 -1
- package/dist/modules/node_manager.js +44 -43
- package/dist/modules/node_manager.js.map +1 -1
- package/dist/modules/order/accounting.d.ts.map +1 -1
- package/dist/modules/order/accounting.js +102 -118
- package/dist/modules/order/accounting.js.map +1 -1
- package/dist/modules/order/async_lock.d.ts +3 -15
- package/dist/modules/order/async_lock.d.ts.map +1 -1
- package/dist/modules/order/async_lock.js +6 -26
- package/dist/modules/order/async_lock.js.map +1 -1
- package/dist/modules/order/export.js +34 -75
- package/dist/modules/order/export.js.map +1 -1
- package/dist/modules/order/format.d.ts +1 -3
- package/dist/modules/order/format.d.ts.map +1 -1
- package/dist/modules/order/format.js +4 -24
- package/dist/modules/order/format.js.map +1 -1
- package/dist/modules/order/grid.d.ts +8 -8
- package/dist/modules/order/grid.d.ts.map +1 -1
- package/dist/modules/order/grid.js +254 -266
- package/dist/modules/order/grid.js.map +1 -1
- package/dist/modules/order/grid_reconcile.d.ts.map +1 -1
- package/dist/modules/order/grid_reconcile.js +77 -94
- package/dist/modules/order/grid_reconcile.js.map +1 -1
- package/dist/modules/order/grid_reconcile_internal.d.ts.map +1 -1
- package/dist/modules/order/grid_reconcile_internal.js +117 -149
- package/dist/modules/order/grid_reconcile_internal.js.map +1 -1
- package/dist/modules/order/index.d.ts.map +1 -1
- package/dist/modules/order/index.js +11 -45
- package/dist/modules/order/index.js.map +1 -1
- package/dist/modules/order/logger.d.ts +0 -8
- package/dist/modules/order/logger.d.ts.map +1 -1
- package/dist/modules/order/logger.js +28 -141
- package/dist/modules/order/logger.js.map +1 -1
- package/dist/modules/order/logger_state.d.ts.map +1 -1
- package/dist/modules/order/logger_state.js +3 -4
- package/dist/modules/order/logger_state.js.map +1 -1
- package/dist/modules/order/manager.d.ts +10 -79
- package/dist/modules/order/manager.d.ts.map +1 -1
- package/dist/modules/order/manager.js +131 -247
- package/dist/modules/order/manager.js.map +1 -1
- package/dist/modules/order/processed_fill_store.js +2 -7
- package/dist/modules/order/processed_fill_store.js.map +1 -1
- package/dist/modules/order/strategy.js +23 -25
- package/dist/modules/order/strategy.js.map +1 -1
- package/dist/modules/order/sync_engine.d.ts +27 -0
- package/dist/modules/order/sync_engine.d.ts.map +1 -1
- package/dist/modules/order/sync_engine.js +232 -181
- package/dist/modules/order/sync_engine.js.map +1 -1
- package/dist/modules/order/utils/math.d.ts +1 -25
- package/dist/modules/order/utils/math.d.ts.map +1 -1
- package/dist/modules/order/utils/math.js +49 -172
- package/dist/modules/order/utils/math.js.map +1 -1
- package/dist/modules/order/utils/order.d.ts +46 -2
- package/dist/modules/order/utils/order.d.ts.map +1 -1
- package/dist/modules/order/utils/order.js +165 -143
- package/dist/modules/order/utils/order.js.map +1 -1
- package/dist/modules/order/utils/system.d.ts +3 -1
- package/dist/modules/order/utils/system.d.ts.map +1 -1
- package/dist/modules/order/utils/system.js +152 -211
- package/dist/modules/order/utils/system.js.map +1 -1
- package/dist/modules/order/utils/timeout.js +1 -4
- package/dist/modules/order/utils/timeout.js.map +1 -1
- package/dist/modules/order/utils/validate.js +91 -142
- package/dist/modules/order/utils/validate.js.map +1 -1
- package/dist/modules/order/utils/withPoolRef.js +21 -61
- package/dist/modules/order/utils/withPoolRef.js.map +1 -1
- package/dist/modules/order/working_grid.js +6 -9
- package/dist/modules/order/working_grid.js.map +1 -1
- package/dist/modules/path_api.d.ts.map +1 -1
- package/dist/modules/path_api.js +8 -11
- package/dist/modules/path_api.js.map +1 -1
- package/dist/modules/paths.d.ts.map +1 -1
- package/dist/modules/paths.js +61 -67
- package/dist/modules/paths.js.map +1 -1
- package/dist/modules/process_discovery.d.ts.map +1 -1
- package/dist/modules/process_discovery.js +14 -23
- package/dist/modules/process_discovery.js.map +1 -1
- package/dist/modules/runtime.js +7 -11
- package/dist/modules/runtime.js.map +1 -1
- package/dist/modules/runtime_settings.d.ts.map +1 -1
- package/dist/modules/runtime_settings.js +14 -16
- package/dist/modules/runtime_settings.js.map +1 -1
- package/dist/modules/settings_merge.js +1 -3
- package/dist/modules/settings_merge.js.map +1 -1
- package/dist/modules/socket_json_client.js +3 -5
- package/dist/modules/socket_json_client.js.map +1 -1
- package/dist/modules/storage/browser_adapter.js +1 -4
- package/dist/modules/storage/browser_adapter.js.map +1 -1
- package/dist/modules/storage/index.d.ts +3 -2
- package/dist/modules/storage/index.d.ts.map +1 -1
- package/dist/modules/storage/index.js +5 -8
- package/dist/modules/storage/index.js.map +1 -1
- package/dist/modules/storage/node_adapter.d.ts +3 -4
- package/dist/modules/storage/node_adapter.d.ts.map +1 -1
- package/dist/modules/storage/node_adapter.js +12 -16
- package/dist/modules/storage/node_adapter.js.map +1 -1
- package/dist/modules/storage/types.js +1 -2
- package/dist/modules/types.d.ts +0 -6
- package/dist/modules/types.d.ts.map +1 -1
- package/dist/modules/types.js +1 -2
- package/dist/modules/types.js.map +1 -1
- package/dist/modules/utils/base58check.js +8 -15
- package/dist/modules/utils/base58check.js.map +1 -1
- package/dist/modules/utils/build_dir.js +3 -7
- package/dist/modules/utils/build_dir.js.map +1 -1
- package/dist/modules/utils/errors.js +1 -5
- package/dist/modules/utils/errors.js.map +1 -1
- package/dist/modules/utils/math_utils.d.ts +1 -1
- package/dist/modules/utils/math_utils.d.ts.map +1 -1
- package/dist/modules/utils/math_utils.js +2 -7
- package/dist/modules/utils/math_utils.js.map +1 -1
- package/dist/modules/validate_profiles.d.ts.map +1 -1
- package/dist/modules/validate_profiles.js +18 -25
- package/dist/modules/validate_profiles.js.map +1 -1
- package/dist/pm2.d.ts +4 -70
- package/dist/pm2.d.ts.map +1 -1
- package/dist/pm2.js +106 -132
- package/dist/pm2.js.map +1 -1
- package/dist/scripts/analyze-git.js +13 -17
- package/dist/scripts/analyze-git.js.map +1 -1
- package/dist/scripts/analyze-orders.d.ts.map +1 -1
- package/dist/scripts/analyze-orders.js +48 -63
- package/dist/scripts/analyze-orders.js.map +1 -1
- package/dist/scripts/diagnose-kibana-candles.d.ts.map +1 -1
- package/dist/scripts/diagnose-kibana-candles.js +2 -1
- package/dist/scripts/diagnose-kibana-candles.js.map +1 -1
- package/dist/scripts/diagnose-pool-history.d.ts.map +1 -1
- package/dist/scripts/diagnose-pool-history.js +2 -1
- package/dist/scripts/diagnose-pool-history.js.map +1 -1
- package/dist/scripts/divergence-calc.d.ts.map +1 -1
- package/dist/scripts/divergence-calc.js +2 -2
- package/dist/scripts/divergence-calc.js.map +1 -1
- package/dist/scripts/fix-err-message.js +17 -22
- package/dist/scripts/fix-err-message.js.map +1 -1
- package/dist/scripts/generate_lp_chart.d.ts.map +1 -1
- package/dist/scripts/generate_lp_chart.js +7 -15
- package/dist/scripts/generate_lp_chart.js.map +1 -1
- package/dist/scripts/generate_mainnet_corpus_report.d.ts.map +1 -1
- package/dist/scripts/generate_mainnet_corpus_report.js +3 -2
- package/dist/scripts/generate_mainnet_corpus_report.js.map +1 -1
- package/dist/scripts/generate_market_adapter_whitelist.d.ts.map +1 -1
- package/dist/scripts/generate_market_adapter_whitelist.js +18 -24
- package/dist/scripts/generate_market_adapter_whitelist.js.map +1 -1
- package/dist/scripts/native_release_gates.js +15 -19
- package/dist/scripts/native_release_gates.js.map +1 -1
- package/dist/scripts/print_grid.d.ts.map +1 -1
- package/dist/scripts/print_grid.js +2 -2
- package/dist/scripts/print_grid.js.map +1 -1
- package/dist/scripts/run-tests.js +12 -17
- package/dist/scripts/run-tests.js.map +1 -1
- package/dist/scripts/runner.d.ts.map +1 -1
- package/dist/scripts/runner.js +18 -20
- package/dist/scripts/runner.js.map +1 -1
- package/dist/scripts/sync-version.js +2 -2
- package/dist/scripts/sync-version.js.map +1 -1
- package/dist/scripts/test-credit-renewal.d.ts.map +1 -1
- package/dist/scripts/test-credit-renewal.js +4 -3
- package/dist/scripts/test-credit-renewal.js.map +1 -1
- package/dist/scripts/update.js +93 -95
- package/dist/scripts/update.js.map +1 -1
- package/dist/scripts/validate_bots.d.ts.map +1 -1
- package/dist/scripts/validate_bots.js +2 -2
- package/dist/scripts/validate_bots.js.map +1 -1
- package/dist/scripts/verify-browser-bundle.js +12 -17
- package/dist/scripts/verify-browser-bundle.js.map +1 -1
- package/dist/unlock.d.ts +0 -30
- package/dist/unlock.d.ts.map +1 -1
- package/dist/unlock.js +258 -222
- package/dist/unlock.js.map +1 -1
- package/package.json +5 -8
- package/scripts/README.md +1 -1
- package/scripts/create-bot-symlinks.sh +2 -2
- package/dist/modules/utils/fs_utils.d.ts +0 -18
- package/dist/modules/utils/fs_utils.d.ts.map +0 -1
- package/dist/modules/utils/fs_utils.js +0 -32
- package/dist/modules/utils/fs_utils.js.map +0 -1
- package/dist/scripts/migrate_bot_keys.d.ts +0 -3
- package/dist/scripts/migrate_bot_keys.d.ts.map +0 -1
- package/dist/scripts/migrate_bot_keys.js +0 -225
- package/dist/scripts/migrate_bot_keys.js.map +0 -1
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* modules/order/manager.ts - OrderManager Engine
|
|
4
3
|
*
|
|
@@ -14,62 +13,24 @@
|
|
|
14
13
|
* SECTION 3: ORDER MANAGER CLASS
|
|
15
14
|
* ===============================================================================
|
|
16
15
|
*/
|
|
17
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
18
|
-
if (k2 === undefined) k2 = k;
|
|
19
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
20
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
21
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
22
|
-
}
|
|
23
|
-
Object.defineProperty(o, k2, desc);
|
|
24
|
-
}) : (function(o, m, k, k2) {
|
|
25
|
-
if (k2 === undefined) k2 = k;
|
|
26
|
-
o[k2] = m[k];
|
|
27
|
-
}));
|
|
28
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
29
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
30
|
-
}) : function(o, v) {
|
|
31
|
-
o["default"] = v;
|
|
32
|
-
});
|
|
33
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
34
|
-
var ownKeys = function(o) {
|
|
35
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
36
|
-
var ar = [];
|
|
37
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
38
|
-
return ar;
|
|
39
|
-
};
|
|
40
|
-
return ownKeys(o);
|
|
41
|
-
};
|
|
42
|
-
return function (mod) {
|
|
43
|
-
if (mod && mod.__esModule) return mod;
|
|
44
|
-
var result = {};
|
|
45
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
46
|
-
__setModuleDefault(result, mod);
|
|
47
|
-
return result;
|
|
48
|
-
};
|
|
49
|
-
})();
|
|
50
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
51
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
52
|
-
};
|
|
53
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
54
|
-
exports.COWRebalanceEngine = exports.OrderManager = void 0;
|
|
55
16
|
// ===============================================================================
|
|
56
17
|
// SECTION 1: EXTERNAL DEPENDENCIES
|
|
57
18
|
// ===============================================================================
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
19
|
+
import { persistGridSnapshot, deepFreeze, cloneMap, withBlockchainRetry } from './utils/system.js';
|
|
20
|
+
import { withTimeout } from './utils/timeout.js';
|
|
21
|
+
import { WorkingGrid } from './working_grid.js';
|
|
22
|
+
import Logger from './logger.js';
|
|
23
|
+
import AsyncLock from './async_lock.js';
|
|
24
|
+
import Accountant from './accounting.js';
|
|
25
|
+
import StrategyEngine from './strategy.js';
|
|
26
|
+
import SyncEngine from './sync_engine.js';
|
|
27
|
+
import { calculateCurrentSpread, checkSpreadCondition, checkGridHealth } from './grid.js';
|
|
28
|
+
import * as Format from './format.js';
|
|
29
|
+
import { ORDER_TYPES, ORDER_STATES, REBALANCE_STATES, DEFAULT_CONFIG, TIMING, LOG_LEVEL, PIPELINE_TIMING, COW_PERFORMANCE, COW_ACTIONS } from '../constants.js';
|
|
30
|
+
import { getMinAbsoluteOrderSize, computeChainFundTotals, hasValidAccountTotals, resolveConfigValueWithRegistry, isExplicitZeroAllocation, floatToBlockchainInt } from './utils/math.js';
|
|
31
|
+
import { validateOrder, validateGridForPersistence, validateWorkingGridFunds, checkFundDrift, reconcileGrid, optimizeRebalanceActions, projectTargetToWorkingGrid, buildStateUpdates, buildAbortedResult, buildSuccessResult, evaluateCommit } from './utils/validate.js';
|
|
32
|
+
import { resolveSpreadOrderSide, parseSlotIndex } from './utils/order.js';
|
|
33
|
+
import { getErrorMessage } from '../utils/errors.js';
|
|
73
34
|
const { toFiniteNumber } = Format;
|
|
74
35
|
// ===============================================================================
|
|
75
36
|
// SECTION 2: COW REBALANCE ENGINE
|
|
@@ -107,17 +68,17 @@ const { toFiniteNumber } = Format;
|
|
|
107
68
|
* @returns {string|null} Drop reason for the log, or null to keep the action
|
|
108
69
|
*/
|
|
109
70
|
function stalePlacementDropReason(action, planBoundary) {
|
|
110
|
-
const isPlacement = action?.type ===
|
|
71
|
+
const isPlacement = action?.type === COW_ACTIONS.CREATE || action?.type === COW_ACTIONS.UPDATE;
|
|
111
72
|
if (!isPlacement)
|
|
112
73
|
return null;
|
|
113
|
-
const targetSlot =
|
|
74
|
+
const targetSlot = parseSlotIndex(action?.newGridId ?? action?.id);
|
|
114
75
|
if (targetSlot === null)
|
|
115
76
|
return null;
|
|
116
|
-
const kindWord = action.type ===
|
|
117
|
-
if (action?.order?.type ===
|
|
77
|
+
const kindWord = action.type === COW_ACTIONS.UPDATE ? 'update' : 'create';
|
|
78
|
+
if (action?.order?.type === ORDER_TYPES.BUY && targetSlot > planBoundary) {
|
|
118
79
|
return `BUY ${kindWord} for slot ${action.id}: slot ${targetSlot} > plan boundary ${planBoundary}`;
|
|
119
80
|
}
|
|
120
|
-
if (action?.order?.type ===
|
|
81
|
+
if (action?.order?.type === ORDER_TYPES.SELL && targetSlot < planBoundary) {
|
|
121
82
|
return `SELL ${kindWord} for slot ${action.id}: slot ${targetSlot} < plan boundary ${planBoundary}`;
|
|
122
83
|
}
|
|
123
84
|
return null;
|
|
@@ -174,7 +135,7 @@ class COWRebalanceEngine {
|
|
|
174
135
|
}
|
|
175
136
|
async execute({ masterGrid, gridVersion, boundaryIdx, funds, fills = [], excludeIds = new Set() }) {
|
|
176
137
|
const startTime = Date.now();
|
|
177
|
-
const workingGrid = new
|
|
138
|
+
const workingGrid = new WorkingGrid(masterGrid, { baseVersion: gridVersion });
|
|
178
139
|
const strategyParams = {
|
|
179
140
|
frozenMasterGrid: masterGrid,
|
|
180
141
|
config: this.config,
|
|
@@ -186,14 +147,14 @@ class COWRebalanceEngine {
|
|
|
186
147
|
};
|
|
187
148
|
const { targetGrid, boundaryIdx: targetBoundary } = this.strategy.calculateTargetGrid(strategyParams);
|
|
188
149
|
let dustThresholdPercent = this.config?.gridLimits?.PARTIAL_DUST_THRESHOLD_PERCENTAGE;
|
|
189
|
-
const reconcileResult =
|
|
150
|
+
const reconcileResult = reconcileGrid(masterGrid, targetGrid, targetBoundary, {
|
|
190
151
|
logger: (msg, level) => this.logger?.log(msg, level),
|
|
191
152
|
dustThresholdPercent
|
|
192
153
|
});
|
|
193
154
|
if (reconcileResult.aborted) {
|
|
194
|
-
return
|
|
155
|
+
return buildAbortedResult(reconcileResult.reason);
|
|
195
156
|
}
|
|
196
|
-
const optimizedActions =
|
|
157
|
+
const optimizedActions = optimizeRebalanceActions(reconcileResult.actions, masterGrid);
|
|
197
158
|
// Stale-placement guard: drop placements crossing the plan's own
|
|
198
159
|
// boundary (see stalePlacementDropReason) — defer them to the next
|
|
199
160
|
// cycle instead of filling immediately.
|
|
@@ -216,28 +177,28 @@ class COWRebalanceEngine {
|
|
|
216
177
|
optimizedActions.length = 0;
|
|
217
178
|
optimizedActions.push(...guarded);
|
|
218
179
|
}
|
|
219
|
-
|
|
180
|
+
projectTargetToWorkingGrid(workingGrid, targetGrid, { actions: optimizedActions });
|
|
220
181
|
const precisions = {
|
|
221
182
|
buyPrecision: this.assets?.assetB?.precision,
|
|
222
183
|
sellPrecision: this.assets?.assetA?.precision
|
|
223
184
|
};
|
|
224
|
-
const fundCheck =
|
|
185
|
+
const fundCheck = validateWorkingGridFunds(workingGrid, funds, precisions, this.assets);
|
|
225
186
|
if (!fundCheck.isValid) {
|
|
226
187
|
this.logger?.log(`[COW] Fund validation failed: ${fundCheck.reason}`, 'warn');
|
|
227
|
-
return
|
|
188
|
+
return buildAbortedResult(fundCheck.reason);
|
|
228
189
|
}
|
|
229
190
|
if (workingGrid.isStale()) {
|
|
230
191
|
const reason = workingGrid.getStaleReason() || 'Master grid changed during planning';
|
|
231
192
|
this.logger?.log(`[COW] Rebalance plan invalidated: ${reason}`, 'warn');
|
|
232
|
-
return
|
|
193
|
+
return buildAbortedResult(reason);
|
|
233
194
|
}
|
|
234
|
-
const stateUpdates =
|
|
195
|
+
const stateUpdates = buildStateUpdates(optimizedActions, masterGrid);
|
|
235
196
|
const duration = Date.now() - startTime;
|
|
236
|
-
if (duration >
|
|
197
|
+
if (duration > COW_PERFORMANCE.MAX_REBALANCE_PLANNING_MS) {
|
|
237
198
|
this.logger?.log(`[COW] Rebalance planning took ${duration}ms`, 'warn');
|
|
238
199
|
}
|
|
239
200
|
this.logger?.log(`[COW] Plan: Actions=${optimizedActions.length}, StateUpdates=${stateUpdates.length}`, 'info');
|
|
240
|
-
return
|
|
201
|
+
return buildSuccessResult({
|
|
241
202
|
actions: optimizedActions,
|
|
242
203
|
stateUpdates,
|
|
243
204
|
workingGrid,
|
|
@@ -246,7 +207,6 @@ class COWRebalanceEngine {
|
|
|
246
207
|
});
|
|
247
208
|
}
|
|
248
209
|
}
|
|
249
|
-
exports.COWRebalanceEngine = COWRebalanceEngine;
|
|
250
210
|
// ===============================================================================
|
|
251
211
|
// SECTION 3: ORDER MANAGER CLASS
|
|
252
212
|
// ===============================================================================
|
|
@@ -290,7 +250,7 @@ class OrderManager {
|
|
|
290
250
|
return this._ordersByTypeCache;
|
|
291
251
|
}
|
|
292
252
|
const byType = {};
|
|
293
|
-
for (const key of [
|
|
253
|
+
for (const key of [ORDER_TYPES.BUY, ORDER_TYPES.SELL, ORDER_TYPES.SPREAD]) {
|
|
294
254
|
byType[key] = new Set();
|
|
295
255
|
}
|
|
296
256
|
for (const [id, o] of this.orders) {
|
|
@@ -314,7 +274,7 @@ class OrderManager {
|
|
|
314
274
|
return this._ordersByStateCache;
|
|
315
275
|
}
|
|
316
276
|
const byState = {};
|
|
317
|
-
for (const key of [
|
|
277
|
+
for (const key of [ORDER_STATES.VIRTUAL, ORDER_STATES.ACTIVE, ORDER_STATES.PARTIAL]) {
|
|
318
278
|
byState[key] = new Set();
|
|
319
279
|
}
|
|
320
280
|
for (const [id, o] of this.orders) {
|
|
@@ -368,6 +328,7 @@ class OrderManager {
|
|
|
368
328
|
_orderIdAssignedAt;
|
|
369
329
|
_gapSlots;
|
|
370
330
|
_gridDirtyAt;
|
|
331
|
+
_lastStaleTotalsWarnAt;
|
|
371
332
|
_orphanFillsCreditedAt;
|
|
372
333
|
_pendingRecovery;
|
|
373
334
|
_recentFillKeysSnapshot;
|
|
@@ -380,12 +341,12 @@ class OrderManager {
|
|
|
380
341
|
* @param {Object} [config] - Configuration overrides
|
|
381
342
|
*/
|
|
382
343
|
constructor(config = {}) {
|
|
383
|
-
this.config = { ...
|
|
344
|
+
this.config = { ...DEFAULT_CONFIG, ...config };
|
|
384
345
|
this.marketName = this.config.market || (this.config.assetA && this.config.assetB ? `${this.config.assetA}/${this.config.assetB}` : null);
|
|
385
346
|
const logFile = config.logFile || undefined;
|
|
386
347
|
const loggingConfig = this.config.logging;
|
|
387
|
-
this.logger = new
|
|
388
|
-
level: loggingConfig?.level ??
|
|
348
|
+
this.logger = new Logger('DEXBot', {
|
|
349
|
+
level: loggingConfig?.level ?? LOG_LEVEL,
|
|
389
350
|
logFile,
|
|
390
351
|
configOverride: loggingConfig?.config ?? undefined
|
|
391
352
|
});
|
|
@@ -393,10 +354,10 @@ class OrderManager {
|
|
|
393
354
|
this.orders = Object.freeze(new Map());
|
|
394
355
|
this.boundaryIdx = null;
|
|
395
356
|
this.targetGrid = null;
|
|
396
|
-
this.accountant = new
|
|
397
|
-
this.strategy = new
|
|
398
|
-
this.sync = new
|
|
399
|
-
this._rebalanceState =
|
|
357
|
+
this.accountant = new Accountant(this);
|
|
358
|
+
this.strategy = new StrategyEngine(this);
|
|
359
|
+
this.sync = new SyncEngine(this);
|
|
360
|
+
this._rebalanceState = REBALANCE_STATES.NORMAL;
|
|
400
361
|
this._bootstrapping = 0;
|
|
401
362
|
this._broadcastingFlag = 0;
|
|
402
363
|
this._broadcastingStartedAt = 0;
|
|
@@ -437,15 +398,14 @@ class OrderManager {
|
|
|
437
398
|
// Level 2: _syncLock (sync operations — timeout-protected)
|
|
438
399
|
// Level 3: _gridLock (grid mutations)
|
|
439
400
|
// Level 4: _fundLock (fund operations — innermost)
|
|
440
|
-
// Note: AsyncLock IS re-entrant (acquire detects nested calls via
|
|
441
|
-
this._fillProcessingLock = new
|
|
442
|
-
this._divergenceLock = new
|
|
443
|
-
this._syncLock = new
|
|
444
|
-
this._gridLock = new
|
|
445
|
-
level: 3,
|
|
401
|
+
// Note: AsyncLock IS re-entrant (acquire detects nested calls via context).
|
|
402
|
+
this._fillProcessingLock = new AsyncLock({ timeout: TIMING.SYNC_LOCK_TIMEOUT_MS });
|
|
403
|
+
this._divergenceLock = new AsyncLock();
|
|
404
|
+
this._syncLock = new AsyncLock();
|
|
405
|
+
this._gridLock = new AsyncLock({
|
|
446
406
|
onContention: () => { this._metrics.gridLockContention++; }
|
|
447
407
|
});
|
|
448
|
-
this._fundLock = new
|
|
408
|
+
this._fundLock = new AsyncLock({ timeout: 30000 });
|
|
449
409
|
this._recentlyRotatedOrderIds = new Set();
|
|
450
410
|
this._gridSidesUpdated = new Set();
|
|
451
411
|
this._pauseFundRecalc = 0;
|
|
@@ -474,6 +434,7 @@ class OrderManager {
|
|
|
474
434
|
this._orphanFillsCreditedAt = null;
|
|
475
435
|
this._pendingRecovery = null;
|
|
476
436
|
this._recentFillKeysSnapshot = null;
|
|
437
|
+
this._lastStaleTotalsWarnAt = {};
|
|
477
438
|
this._metrics = {
|
|
478
439
|
fundRecalcCount: 0,
|
|
479
440
|
lockAcquisitions: 0,
|
|
@@ -503,8 +464,8 @@ class OrderManager {
|
|
|
503
464
|
_clearWorkingGridRef() {
|
|
504
465
|
this._currentWorkingGridStack.pop();
|
|
505
466
|
this._rebalanceState = this._currentWorkingGridStack.length > 0
|
|
506
|
-
?
|
|
507
|
-
:
|
|
467
|
+
? REBALANCE_STATES.REBALANCING
|
|
468
|
+
: REBALANCE_STATES.NORMAL;
|
|
508
469
|
}
|
|
509
470
|
/**
|
|
510
471
|
* Push a working grid onto the rebalance stack (LIFO), set the state to
|
|
@@ -518,7 +479,7 @@ class OrderManager {
|
|
|
518
479
|
*/
|
|
519
480
|
_pushWorkingGridRef(workingGrid, result = null) {
|
|
520
481
|
this._currentWorkingGridStack.push(workingGrid);
|
|
521
|
-
this._rebalanceState =
|
|
482
|
+
this._rebalanceState = REBALANCE_STATES.REBALANCING;
|
|
522
483
|
if (result) {
|
|
523
484
|
result._workingGridPushed = true;
|
|
524
485
|
}
|
|
@@ -560,20 +521,20 @@ class OrderManager {
|
|
|
560
521
|
}
|
|
561
522
|
_resetRebalanceStateToDepth() {
|
|
562
523
|
this._rebalanceState = this._currentWorkingGridStack.length > 0
|
|
563
|
-
?
|
|
564
|
-
:
|
|
524
|
+
? REBALANCE_STATES.REBALANCING
|
|
525
|
+
: REBALANCE_STATES.NORMAL;
|
|
565
526
|
}
|
|
566
527
|
/**
|
|
567
528
|
* @returns {boolean}
|
|
568
529
|
*/
|
|
569
530
|
isRebalancing() {
|
|
570
|
-
return this._rebalanceState ===
|
|
531
|
+
return this._rebalanceState === REBALANCE_STATES.REBALANCING;
|
|
571
532
|
}
|
|
572
533
|
/**
|
|
573
534
|
* @returns {boolean}
|
|
574
535
|
*/
|
|
575
536
|
isBroadcasting() {
|
|
576
|
-
return this._rebalanceState ===
|
|
537
|
+
return this._rebalanceState === REBALANCE_STATES.BROADCASTING;
|
|
577
538
|
}
|
|
578
539
|
/**
|
|
579
540
|
* @returns {boolean}
|
|
@@ -680,23 +641,6 @@ class OrderManager {
|
|
|
680
641
|
return this.accountant.resetFunds();
|
|
681
642
|
});
|
|
682
643
|
}
|
|
683
|
-
async _deductFromChainFree(orderType, size, operation) {
|
|
684
|
-
if (!this.accountant)
|
|
685
|
-
return;
|
|
686
|
-
return await this._fundLock.acquire(async () => {
|
|
687
|
-
return await this.accountant.tryDeductFromChainFree(orderType, size, operation);
|
|
688
|
-
});
|
|
689
|
-
}
|
|
690
|
-
async _addToChainFree(orderType, size, operation) {
|
|
691
|
-
if (!this.accountant)
|
|
692
|
-
return;
|
|
693
|
-
return await this._fundLock.acquire(async () => {
|
|
694
|
-
return await this.accountant.addToChainFree(orderType, size, operation);
|
|
695
|
-
});
|
|
696
|
-
}
|
|
697
|
-
_getGridTotal(side) {
|
|
698
|
-
return (this.funds?.committed?.grid?.[side] || 0) + (this.funds?.virtual?.[side] || 0);
|
|
699
|
-
}
|
|
700
644
|
/**
|
|
701
645
|
* Computes committed amounts directly from the orders map to ensure the
|
|
702
646
|
* snapshot is internally consistent even when called inside a
|
|
@@ -710,19 +654,19 @@ class OrderManager {
|
|
|
710
654
|
getChainFundsSnapshot() {
|
|
711
655
|
let committedBuy = 0, committedSell = 0;
|
|
712
656
|
for (const order of this.orders.values()) {
|
|
713
|
-
const isActive = (order.state ===
|
|
657
|
+
const isActive = (order.state === ORDER_STATES.ACTIVE || order.state === ORDER_STATES.PARTIAL) && !!order.orderId;
|
|
714
658
|
if (!isActive)
|
|
715
659
|
continue;
|
|
716
660
|
const size = toFiniteNumber(order.size);
|
|
717
661
|
if (size <= 0)
|
|
718
662
|
continue;
|
|
719
|
-
const isBuy = order.type ===
|
|
663
|
+
const isBuy = order.type === ORDER_TYPES.BUY || (order.type === ORDER_TYPES.SPREAD && resolveSpreadOrderSide(order.price, this.config.startPrice) === ORDER_TYPES.BUY);
|
|
720
664
|
if (isBuy)
|
|
721
665
|
committedBuy += size;
|
|
722
666
|
else
|
|
723
667
|
committedSell += size;
|
|
724
668
|
}
|
|
725
|
-
const totals =
|
|
669
|
+
const totals = computeChainFundTotals(this.accountTotals, { buy: committedBuy, sell: committedSell });
|
|
726
670
|
const allocatedBuy = toFiniteNumber(this.funds?.allocated?.buy, totals.chainTotalBuy);
|
|
727
671
|
const allocatedSell = toFiniteNumber(this.funds?.allocated?.sell, totals.chainTotalSell);
|
|
728
672
|
const btsBalance = (this.config.assetA !== 'BTS' && this.config.assetB !== 'BTS')
|
|
@@ -734,12 +678,12 @@ class OrderManager {
|
|
|
734
678
|
* @param {number} [timeoutMs]
|
|
735
679
|
* @returns {Promise<void>}
|
|
736
680
|
*/
|
|
737
|
-
async waitForAccountTotals(timeoutMs =
|
|
738
|
-
if (
|
|
681
|
+
async waitForAccountTotals(timeoutMs = TIMING.ACCOUNT_TOTALS_TIMEOUT_MS) {
|
|
682
|
+
if (hasValidAccountTotals(this.accountTotals, true))
|
|
739
683
|
return;
|
|
740
684
|
let waitPromise = null;
|
|
741
685
|
await this._fundLock.acquire(async () => {
|
|
742
|
-
if (
|
|
686
|
+
if (hasValidAccountTotals(this.accountTotals, true))
|
|
743
687
|
return;
|
|
744
688
|
if (!this._accountTotalsPromise) {
|
|
745
689
|
this._accountTotalsPromise = new Promise((resolve) => {
|
|
@@ -750,7 +694,7 @@ class OrderManager {
|
|
|
750
694
|
});
|
|
751
695
|
if (!waitPromise)
|
|
752
696
|
return;
|
|
753
|
-
await
|
|
697
|
+
await withTimeout(waitPromise, timeoutMs, {
|
|
754
698
|
onTimeout: 'resolve',
|
|
755
699
|
defaultValue: undefined,
|
|
756
700
|
onTimeoutCallback: () => this.logger.log('[FUND] Timeout waiting for account totals', 'warn'),
|
|
@@ -781,7 +725,7 @@ class OrderManager {
|
|
|
781
725
|
async refreshAccountTotalsIfStale(options = {}) {
|
|
782
726
|
const force = options?.force === true;
|
|
783
727
|
const lastFetched = this.accountTotals?._lastFetchedAt || 0;
|
|
784
|
-
if (!force && Date.now() - lastFetched <=
|
|
728
|
+
if (!force && Date.now() - lastFetched <= TIMING.MAX_ACCOUNT_TOTALS_AGE_MS) {
|
|
785
729
|
return { ok: true };
|
|
786
730
|
}
|
|
787
731
|
const fetchedBefore = lastFetched;
|
|
@@ -790,18 +734,35 @@ class OrderManager {
|
|
|
790
734
|
// defer the whole fill batch to the next cycle. Only a genuinely
|
|
791
735
|
// unreachable chain leaves the snapshot stale.
|
|
792
736
|
try {
|
|
793
|
-
await
|
|
737
|
+
await withBlockchainRetry(() => this.fetchAccountTotals(this.accountId), 'refreshAccountTotalsIfStale', { logger: this.logger });
|
|
794
738
|
}
|
|
795
739
|
catch (err) {
|
|
796
|
-
this.logger?.log?.(`[SYNC] refreshAccountTotalsIfStale fetch failed: ${
|
|
740
|
+
this.logger?.log?.(`[SYNC] refreshAccountTotalsIfStale fetch failed: ${getErrorMessage(err)}`, 'warn');
|
|
797
741
|
}
|
|
798
742
|
const fetchedAfter = this.accountTotals?._lastFetchedAt || 0;
|
|
799
743
|
if (fetchedAfter <= fetchedBefore) {
|
|
800
|
-
this.
|
|
744
|
+
if (this._rateLimitStaleTotalsWarn('stale-refresh')) {
|
|
745
|
+
this.logger?.log?.(`[SYNC] refreshAccountTotalsIfStale: accountTotals still stale after refresh (age=${Math.max(0, Date.now() - fetchedAfter)}ms).`, 'warn');
|
|
746
|
+
}
|
|
801
747
|
return { ok: false, reason: 'refresh-failed' };
|
|
802
748
|
}
|
|
803
749
|
return { ok: true };
|
|
804
750
|
}
|
|
751
|
+
/**
|
|
752
|
+
* Rate-limit identical accountTotals-staleness warnings to once per
|
|
753
|
+
* STALE_TOTALS_WARN_RATE_LIMIT_MS per warning kind per manager, so a
|
|
754
|
+
* persistently unreachable chain reports each failure mode once without
|
|
755
|
+
* flooding the log on every fill batch.
|
|
756
|
+
* @param {string} kind - Warning kind key (e.g. 'stale-refresh', 're-anchor')
|
|
757
|
+
* @returns {boolean} true when the warning should be emitted now
|
|
758
|
+
*/
|
|
759
|
+
_rateLimitStaleTotalsWarn(kind) {
|
|
760
|
+
const now = Date.now();
|
|
761
|
+
if (now - (this._lastStaleTotalsWarnAt[kind] || 0) < TIMING.STALE_TOTALS_WARN_RATE_LIMIT_MS)
|
|
762
|
+
return false;
|
|
763
|
+
this._lastStaleTotalsWarnAt[kind] = now;
|
|
764
|
+
return true;
|
|
765
|
+
}
|
|
805
766
|
/**
|
|
806
767
|
* Re-anchor accountTotals to authoritative on-chain values after a fill
|
|
807
768
|
* batch has applied its optimistic accounting and grid mutation.
|
|
@@ -819,7 +780,9 @@ class OrderManager {
|
|
|
819
780
|
async reanchorAccountTotals(label = 'fill-batch') {
|
|
820
781
|
const result = await this.refreshAccountTotalsIfStale({ force: true });
|
|
821
782
|
if (!result.ok) {
|
|
822
|
-
this.
|
|
783
|
+
if (this._rateLimitStaleTotalsWarn('re-anchor')) {
|
|
784
|
+
this.logger?.log?.(`[ACCOUNTING] ${label}: accountTotals re-anchor failed (${result.reason}); marking totals stale.`, 'warn');
|
|
785
|
+
}
|
|
823
786
|
this.accountTotalsStale = true;
|
|
824
787
|
return false;
|
|
825
788
|
}
|
|
@@ -842,12 +805,12 @@ class OrderManager {
|
|
|
842
805
|
if (!this.funds)
|
|
843
806
|
await this.resetFunds();
|
|
844
807
|
await this._recalculateFunds();
|
|
845
|
-
if (
|
|
808
|
+
if (hasValidAccountTotals(this.accountTotals, true) && typeof this._accountTotalsResolve === 'function') {
|
|
846
809
|
try {
|
|
847
810
|
this._accountTotalsResolve();
|
|
848
811
|
}
|
|
849
812
|
catch (e) {
|
|
850
|
-
this.logger?.log?.(`Error resolving account totals promise: ${
|
|
813
|
+
this.logger?.log?.(`Error resolving account totals promise: ${getErrorMessage(e)}`, 'warn');
|
|
851
814
|
}
|
|
852
815
|
this._accountTotalsPromise = null;
|
|
853
816
|
this._accountTotalsResolve = null;
|
|
@@ -867,11 +830,11 @@ class OrderManager {
|
|
|
867
830
|
applyBotFundsAllocation() {
|
|
868
831
|
if (!this.config.botFunds || !this.accountTotals)
|
|
869
832
|
return;
|
|
870
|
-
const { chainTotalBuy, chainTotalSell } =
|
|
833
|
+
const { chainTotalBuy, chainTotalSell } = computeChainFundTotals(this.accountTotals, this.funds?.committed?.chain);
|
|
871
834
|
const account = this.config.preferredAccount;
|
|
872
835
|
const botName = this.config.botKey;
|
|
873
|
-
const allocatedBuy =
|
|
874
|
-
const allocatedSell =
|
|
836
|
+
const allocatedBuy = resolveConfigValueWithRegistry(this.config.botFunds.buy, chainTotalBuy, account, botName, 'buy');
|
|
837
|
+
const allocatedSell = resolveConfigValueWithRegistry(this.config.botFunds.sell, chainTotalSell, account, botName, 'sell');
|
|
875
838
|
if (allocatedBuy === 0 && typeof this.config.botFunds.buy === 'string' && this.config.botFunds.buy.trim().endsWith('%')) {
|
|
876
839
|
if (chainTotalBuy === 0)
|
|
877
840
|
this._triggerAccountTotalsFetchIfNeeded();
|
|
@@ -881,8 +844,8 @@ class OrderManager {
|
|
|
881
844
|
this._triggerAccountTotalsFetchIfNeeded();
|
|
882
845
|
}
|
|
883
846
|
this.funds.allocated = { buy: allocatedBuy, sell: allocatedSell };
|
|
884
|
-
const shouldCapBuy = allocatedBuy > 0 ||
|
|
885
|
-
const shouldCapSell = allocatedSell > 0 ||
|
|
847
|
+
const shouldCapBuy = allocatedBuy > 0 || isExplicitZeroAllocation(this.config.botFunds.buy);
|
|
848
|
+
const shouldCapSell = allocatedSell > 0 || isExplicitZeroAllocation(this.config.botFunds.sell);
|
|
886
849
|
if (shouldCapBuy)
|
|
887
850
|
this.funds.available.buy = Math.min(this.funds.available.buy, Math.max(0, allocatedBuy));
|
|
888
851
|
if (shouldCapSell)
|
|
@@ -937,10 +900,10 @@ class OrderManager {
|
|
|
937
900
|
this._pauseFundRecalc = 0;
|
|
938
901
|
this._pauseFundRecalcWatchdog = null;
|
|
939
902
|
this.recalculateFunds().catch((err) => {
|
|
940
|
-
this.logger?.log?.(`[MANAGER] Watchdog recalc failed: ${
|
|
903
|
+
this.logger?.log?.(`[MANAGER] Watchdog recalc failed: ${getErrorMessage(err)}`, 'error');
|
|
941
904
|
});
|
|
942
905
|
}
|
|
943
|
-
},
|
|
906
|
+
}, TIMING.SAFETY_PAUSE_TIMEOUT_MS);
|
|
944
907
|
}
|
|
945
908
|
}
|
|
946
909
|
/**
|
|
@@ -978,10 +941,10 @@ class OrderManager {
|
|
|
978
941
|
clearTimeout(this._pauseRecalcLoggingWatchdog);
|
|
979
942
|
}
|
|
980
943
|
this._pauseRecalcLoggingWatchdog = setTimeout(() => {
|
|
981
|
-
this.logger?.log?.(`[MANAGER] pauseRecalcLogging safety watchdog: forcing resume after ${
|
|
944
|
+
this.logger?.log?.(`[MANAGER] pauseRecalcLogging safety watchdog: forcing resume after ${TIMING.SAFETY_PAUSE_TIMEOUT_MS}ms`, 'warn');
|
|
982
945
|
this._pauseRecalcLogging = false;
|
|
983
946
|
this._pauseRecalcLoggingWatchdog = null;
|
|
984
|
-
},
|
|
947
|
+
}, TIMING.SAFETY_PAUSE_TIMEOUT_MS);
|
|
985
948
|
}
|
|
986
949
|
/**
|
|
987
950
|
* Resume recalc debug logging.
|
|
@@ -1041,7 +1004,7 @@ class OrderManager {
|
|
|
1041
1004
|
lockOrders(orderIds) {
|
|
1042
1005
|
if (!orderIds)
|
|
1043
1006
|
return;
|
|
1044
|
-
const expiration = Date.now() +
|
|
1007
|
+
const expiration = Date.now() + TIMING.LOCK_TIMEOUT_MS;
|
|
1045
1008
|
for (const id of orderIds)
|
|
1046
1009
|
if (id)
|
|
1047
1010
|
this.shadowOrderIds.set(id, expiration);
|
|
@@ -1106,7 +1069,7 @@ class OrderManager {
|
|
|
1106
1069
|
const updateOptions = this._normalizeOrderUpdateOptions(options);
|
|
1107
1070
|
const { skipAccounting, fee: normalizedFee } = updateOptions;
|
|
1108
1071
|
const oldOrder = this.orders.get(order.id);
|
|
1109
|
-
const validation =
|
|
1072
|
+
const validation = validateOrder(order, oldOrder, context);
|
|
1110
1073
|
for (const warning of validation.warnings) {
|
|
1111
1074
|
this.logger.log(warning.message, 'warn');
|
|
1112
1075
|
}
|
|
@@ -1145,7 +1108,7 @@ class OrderManager {
|
|
|
1145
1108
|
if (this.accountant) {
|
|
1146
1109
|
await this.accountant.updateOptimisticFreeBalance(oldOrder, nextOrder, context, normalizedFee, accountingSkip);
|
|
1147
1110
|
}
|
|
1148
|
-
const updatedOrder =
|
|
1111
|
+
const updatedOrder = deepFreeze({ ...nextOrder });
|
|
1149
1112
|
const id = order.id;
|
|
1150
1113
|
// Track when an orderId is first assigned to a slot. A freshly assigned
|
|
1151
1114
|
// orderId (create/adopt broadcast still in flight, or not yet visible to
|
|
@@ -1159,13 +1122,13 @@ class OrderManager {
|
|
|
1159
1122
|
// younger than SYNC_LOCK_TIMEOUT_MS), so keeping them would leak one
|
|
1160
1123
|
// entry per orderId on a perpetual-turnover bot.
|
|
1161
1124
|
for (const [orderId, assignedAt] of this._orderIdAssignedAt) {
|
|
1162
|
-
if (Date.now() - assignedAt >=
|
|
1125
|
+
if (Date.now() - assignedAt >= TIMING.SYNC_LOCK_TIMEOUT_MS) {
|
|
1163
1126
|
this._orderIdAssignedAt.delete(orderId);
|
|
1164
1127
|
}
|
|
1165
1128
|
}
|
|
1166
1129
|
this._orderIdAssignedAt.set(updatedOrder.orderId, Date.now());
|
|
1167
1130
|
}
|
|
1168
|
-
const newMap =
|
|
1131
|
+
const newMap = cloneMap(this.orders);
|
|
1169
1132
|
newMap.set(id, updatedOrder);
|
|
1170
1133
|
this.orders = Object.freeze(newMap);
|
|
1171
1134
|
this._gridVersion++;
|
|
@@ -1202,8 +1165,8 @@ class OrderManager {
|
|
|
1202
1165
|
wg.syncFromMaster(this.orders, orderId, this._gridVersion);
|
|
1203
1166
|
}
|
|
1204
1167
|
catch (syncErr) {
|
|
1205
|
-
wg.markStale(`working-grid sync failure: ${
|
|
1206
|
-
this.logger.log(`[COW] Failed to sync working grid for order ${orderId}: ${
|
|
1168
|
+
wg.markStale(`working-grid sync failure: ${getErrorMessage(syncErr)}`);
|
|
1169
|
+
this.logger.log(`[COW] Failed to sync working grid for order ${orderId}: ${getErrorMessage(syncErr)}`, 'warn');
|
|
1207
1170
|
}
|
|
1208
1171
|
}
|
|
1209
1172
|
/**
|
|
@@ -1316,7 +1279,7 @@ class OrderManager {
|
|
|
1316
1279
|
rebalanceResult.excludeIds = excl || new Set();
|
|
1317
1280
|
return rebalanceResult;
|
|
1318
1281
|
}
|
|
1319
|
-
const workingGrid = new
|
|
1282
|
+
const workingGrid = new WorkingGrid(this.orders, { baseVersion: this._gridVersion });
|
|
1320
1283
|
return {
|
|
1321
1284
|
actions: [],
|
|
1322
1285
|
stateUpdates: [],
|
|
@@ -1336,28 +1299,28 @@ class OrderManager {
|
|
|
1336
1299
|
const buyCount = Math.max(0, toFiniteNumber(this.config.activeOrders?.buy, 1));
|
|
1337
1300
|
// Get minimum sizes for validation
|
|
1338
1301
|
const minOrderSizeFactor = this.config?.gridLimits?.MIN_ORDER_SIZE_FACTOR;
|
|
1339
|
-
const minSellSize =
|
|
1340
|
-
const minBuySize =
|
|
1302
|
+
const minSellSize = getMinAbsoluteOrderSize(ORDER_TYPES.SELL, this.assets, minOrderSizeFactor);
|
|
1303
|
+
const minBuySize = getMinAbsoluteOrderSize(ORDER_TYPES.BUY, this.assets, minOrderSizeFactor);
|
|
1341
1304
|
// Use integer arithmetic for size comparisons to match blockchain behavior
|
|
1342
1305
|
const sellPrecision = this.assets?.assetA?.precision;
|
|
1343
1306
|
const buyPrecision = this.assets?.assetB?.precision;
|
|
1344
|
-
const minSellSizeInt =
|
|
1345
|
-
const minBuySizeInt =
|
|
1307
|
+
const minSellSizeInt = floatToBlockchainInt(minSellSize, sellPrecision);
|
|
1308
|
+
const minBuySizeInt = floatToBlockchainInt(minBuySize, buyPrecision);
|
|
1346
1309
|
// Get closest virtual sells (lowest prices first = closest to market), limit to sellCount
|
|
1347
|
-
const vSells = this.getOrdersByTypeAndState(
|
|
1310
|
+
const vSells = this.getOrdersByTypeAndState(ORDER_TYPES.SELL, ORDER_STATES.VIRTUAL)
|
|
1348
1311
|
.sort((a, b) => a.price - b.price)
|
|
1349
1312
|
.slice(0, sellCount);
|
|
1350
1313
|
// Filter by minimum size, then reverse for placement order (highest first)
|
|
1351
1314
|
const validSells = vSells
|
|
1352
|
-
.filter((o) =>
|
|
1315
|
+
.filter((o) => floatToBlockchainInt(o.size, sellPrecision) >= minSellSizeInt)
|
|
1353
1316
|
.sort((a, b) => b.price - a.price);
|
|
1354
1317
|
// Get closest virtual buys (highest prices first = closest to market), limit to buyCount
|
|
1355
|
-
const vBuys = this.getOrdersByTypeAndState(
|
|
1318
|
+
const vBuys = this.getOrdersByTypeAndState(ORDER_TYPES.BUY, ORDER_STATES.VIRTUAL)
|
|
1356
1319
|
.sort((a, b) => b.price - a.price)
|
|
1357
1320
|
.slice(0, buyCount);
|
|
1358
1321
|
// Filter by minimum size, then reverse for placement order (lowest first)
|
|
1359
1322
|
const validBuys = vBuys
|
|
1360
|
-
.filter((o) =>
|
|
1323
|
+
.filter((o) => floatToBlockchainInt(o.size, buyPrecision) >= minBuySizeInt)
|
|
1361
1324
|
.sort((a, b) => a.price - b.price);
|
|
1362
1325
|
return [...validSells, ...validBuys];
|
|
1363
1326
|
}
|
|
@@ -1383,13 +1346,6 @@ class OrderManager {
|
|
|
1383
1346
|
}
|
|
1384
1347
|
return result;
|
|
1385
1348
|
}
|
|
1386
|
-
/**
|
|
1387
|
-
* @param {string} type - ORDER_TYPES.BUY or ORDER_TYPES.SELL
|
|
1388
|
-
* @returns {Array<import('./types').Order>}
|
|
1389
|
-
*/
|
|
1390
|
-
getPartialOrdersOnSide(type) {
|
|
1391
|
-
return this.getOrdersByTypeAndState(type, constants_1.ORDER_STATES.PARTIAL);
|
|
1392
|
-
}
|
|
1393
1349
|
/**
|
|
1394
1350
|
* Write boundaryIdx under COW-commit-only discipline.
|
|
1395
1351
|
*
|
|
@@ -1437,29 +1393,29 @@ class OrderManager {
|
|
|
1437
1393
|
* @returns {Promise<Object>}
|
|
1438
1394
|
*/
|
|
1439
1395
|
async checkSpreadCondition(BitShares, batchCb) {
|
|
1440
|
-
return await
|
|
1396
|
+
return await checkSpreadCondition(this, BitShares, batchCb);
|
|
1441
1397
|
}
|
|
1442
1398
|
/**
|
|
1443
1399
|
* @param {Function} batchCb - Batch callback
|
|
1444
1400
|
* @returns {Promise<Object>}
|
|
1445
1401
|
*/
|
|
1446
1402
|
async checkGridHealth(batchCb) {
|
|
1447
|
-
return await
|
|
1403
|
+
return await checkGridHealth(this, batchCb);
|
|
1448
1404
|
}
|
|
1449
1405
|
/**
|
|
1450
1406
|
* @returns {Object} Current spread calculation
|
|
1451
1407
|
*/
|
|
1452
1408
|
calculateCurrentSpread() {
|
|
1453
|
-
return
|
|
1409
|
+
return calculateCurrentSpread(this);
|
|
1454
1410
|
}
|
|
1455
1411
|
/**
|
|
1456
1412
|
* @returns {import('./types').DriftCheckResult}
|
|
1457
1413
|
*/
|
|
1458
1414
|
checkFundDriftAfterFills() {
|
|
1459
|
-
if (!this.assets || !
|
|
1415
|
+
if (!this.assets || !hasValidAccountTotals(this.accountTotals)) {
|
|
1460
1416
|
return { isValid: true, reason: 'Skipped: missing assets or totals' };
|
|
1461
1417
|
}
|
|
1462
|
-
return
|
|
1418
|
+
return checkFundDrift(this.orders, this.accountTotals, this.assets, this.config?.gridLimits ?? null);
|
|
1463
1419
|
}
|
|
1464
1420
|
/**
|
|
1465
1421
|
* @param {Object|number} [pipelineSignals] - Pipeline state signals or queue length
|
|
@@ -1514,37 +1470,20 @@ class OrderManager {
|
|
|
1514
1470
|
if (!this._pipelineBlockedSince)
|
|
1515
1471
|
return false;
|
|
1516
1472
|
const age = Date.now() - this._pipelineBlockedSince;
|
|
1517
|
-
const timeoutMs = this.config?.pipelineTiming?.TIMEOUT_MS ??
|
|
1473
|
+
const timeoutMs = this.config?.pipelineTiming?.TIMEOUT_MS ?? PIPELINE_TIMING.TIMEOUT_MS;
|
|
1518
1474
|
if (age < timeoutMs)
|
|
1519
1475
|
return false;
|
|
1520
1476
|
this.ordersNeedingPriceCorrection = [];
|
|
1521
1477
|
this._pipelineBlockedSince = null;
|
|
1522
1478
|
return true;
|
|
1523
1479
|
}
|
|
1524
|
-
/**
|
|
1525
|
-
* @returns {import('./types').PipelineHealth}
|
|
1526
|
-
*/
|
|
1527
|
-
getPipelineHealth() {
|
|
1528
|
-
const blockedDuration = this._pipelineBlockedSince
|
|
1529
|
-
? Date.now() - this._pipelineBlockedSince
|
|
1530
|
-
: 0;
|
|
1531
|
-
const timeoutMs = this.config?.pipelineTiming?.TIMEOUT_MS ?? constants_1.PIPELINE_TIMING.TIMEOUT_MS;
|
|
1532
|
-
return {
|
|
1533
|
-
isBlocked: this._pipelineBlockedSince !== null,
|
|
1534
|
-
blockedDurationMs: blockedDuration,
|
|
1535
|
-
hasStalled: blockedDuration > timeoutMs,
|
|
1536
|
-
recoveryAttempted: this._recoveryAttempted,
|
|
1537
|
-
correctionsPending: this.ordersNeedingPriceCorrection.length,
|
|
1538
|
-
gridSidesUpdated: this._gridSidesUpdated?.size || 0
|
|
1539
|
-
};
|
|
1540
|
-
}
|
|
1541
1480
|
/**
|
|
1542
1481
|
* @param {Map<string, import('./types').Order>} targetGrid
|
|
1543
1482
|
* @param {number} targetBoundary
|
|
1544
1483
|
* @returns {Object}
|
|
1545
1484
|
*/
|
|
1546
1485
|
reconcileGrid(targetGrid, targetBoundary) {
|
|
1547
|
-
return
|
|
1486
|
+
return reconcileGrid(this.orders, targetGrid, targetBoundary, {
|
|
1548
1487
|
logger: (msg, level) => this.logger.log(msg, level),
|
|
1549
1488
|
dustThresholdPercent: this.config?.gridLimits?.PARTIAL_DUST_THRESHOLD_PERCENTAGE
|
|
1550
1489
|
});
|
|
@@ -1563,9 +1502,9 @@ class OrderManager {
|
|
|
1563
1502
|
async _applySafeRebalanceCOW(fills = [], excludeIds = new Set()) {
|
|
1564
1503
|
const cowEngine = this._getCOWEngine();
|
|
1565
1504
|
if (!cowEngine) {
|
|
1566
|
-
return
|
|
1505
|
+
return buildAbortedResult('COW Engine not initialized (assets not available)');
|
|
1567
1506
|
}
|
|
1568
|
-
this._setRebalanceState(
|
|
1507
|
+
this._setRebalanceState(REBALANCE_STATES.REBALANCING);
|
|
1569
1508
|
const result = await cowEngine.execute({
|
|
1570
1509
|
masterGrid: this.orders,
|
|
1571
1510
|
gridVersion: this._gridVersion,
|
|
@@ -1584,30 +1523,6 @@ class OrderManager {
|
|
|
1584
1523
|
this._pushWorkingGridRef(result.workingGrid, result);
|
|
1585
1524
|
return result;
|
|
1586
1525
|
}
|
|
1587
|
-
_reconcileGridCOW(targetGrid, targetBoundary, workingGrid) {
|
|
1588
|
-
const result = this.reconcileGrid(targetGrid, targetBoundary);
|
|
1589
|
-
if (result.aborted)
|
|
1590
|
-
return result;
|
|
1591
|
-
const actions = (0, validate_1.optimizeRebalanceActions)(result.actions || [], this.orders);
|
|
1592
|
-
(0, validate_1.projectTargetToWorkingGrid)(workingGrid, targetGrid, { actions });
|
|
1593
|
-
return {
|
|
1594
|
-
...result,
|
|
1595
|
-
actions,
|
|
1596
|
-
...(0, validate_1.summarizeActions)(actions)
|
|
1597
|
-
};
|
|
1598
|
-
}
|
|
1599
|
-
_validateWorkingGridFunds(workingGrid, projectedFunds) {
|
|
1600
|
-
return (0, validate_1.validateWorkingGridFunds)(workingGrid, projectedFunds, {
|
|
1601
|
-
buyPrecision: this.assets?.assetB?.precision,
|
|
1602
|
-
sellPrecision: this.assets?.assetA?.precision
|
|
1603
|
-
}, this.assets);
|
|
1604
|
-
}
|
|
1605
|
-
_calculateRequiredFundsFromGrid(workingGrid, precisions = {}) {
|
|
1606
|
-
return (0, validate_1.calculateRequiredFunds)(workingGrid, {
|
|
1607
|
-
buyPrecision: precisions.buyPrecision || this.assets?.assetB?.precision,
|
|
1608
|
-
sellPrecision: precisions.sellPrecision || this.assets?.assetA?.precision
|
|
1609
|
-
});
|
|
1610
|
-
}
|
|
1611
1526
|
_getCowComparePrecisions() {
|
|
1612
1527
|
const buyPrecisionRaw = this.assets?.assetB?.precision;
|
|
1613
1528
|
const sellPrecisionRaw = this.assets?.assetA?.precision;
|
|
@@ -1632,12 +1547,6 @@ class OrderManager {
|
|
|
1632
1547
|
priceRelativeTolerance
|
|
1633
1548
|
};
|
|
1634
1549
|
}
|
|
1635
|
-
_buildStateUpdates(actions, masterGrid) {
|
|
1636
|
-
return (0, validate_1.buildStateUpdates)(actions, masterGrid);
|
|
1637
|
-
}
|
|
1638
|
-
_buildAbortedCOWResult(reason) {
|
|
1639
|
-
return (0, validate_1.buildAbortedResult)(reason);
|
|
1640
|
-
}
|
|
1641
1550
|
async _commitWorkingGrid(workingGrid, _workingIndexes, workingBoundary, options = {}) {
|
|
1642
1551
|
const startTime = Date.now();
|
|
1643
1552
|
let committed = false;
|
|
@@ -1668,11 +1577,11 @@ class OrderManager {
|
|
|
1668
1577
|
comparePrecisions = this._getCowComparePrecisions();
|
|
1669
1578
|
}
|
|
1670
1579
|
catch (precisionErr) {
|
|
1671
|
-
this.logger.log(`[COW] ${
|
|
1580
|
+
this.logger.log(`[COW] ${getErrorMessage(precisionErr)}`, 'error');
|
|
1672
1581
|
releaseStackEntry();
|
|
1673
1582
|
return false;
|
|
1674
1583
|
}
|
|
1675
|
-
const preCommitGuard =
|
|
1584
|
+
const preCommitGuard = evaluateCommit(workingGrid, {
|
|
1676
1585
|
hasLock: false,
|
|
1677
1586
|
currentVersion: this._gridVersion,
|
|
1678
1587
|
masterGrid: this.orders,
|
|
@@ -1684,7 +1593,7 @@ class OrderManager {
|
|
|
1684
1593
|
return false;
|
|
1685
1594
|
}
|
|
1686
1595
|
await this._gridLock.acquire(async () => {
|
|
1687
|
-
const lockCommitGuard =
|
|
1596
|
+
const lockCommitGuard = evaluateCommit(workingGrid, {
|
|
1688
1597
|
hasLock: true,
|
|
1689
1598
|
currentVersion: this._gridVersion,
|
|
1690
1599
|
masterGrid: this.orders,
|
|
@@ -1702,7 +1611,7 @@ class OrderManager {
|
|
|
1702
1611
|
// Ensures COW immutability invariants are maintained for all grid entries.
|
|
1703
1612
|
for (const [, order] of finalMap.entries()) {
|
|
1704
1613
|
if (order && !Object.isFrozen(order)) {
|
|
1705
|
-
|
|
1614
|
+
deepFreeze(order);
|
|
1706
1615
|
}
|
|
1707
1616
|
}
|
|
1708
1617
|
this.orders = Object.freeze(finalMap);
|
|
@@ -1739,12 +1648,12 @@ class OrderManager {
|
|
|
1739
1648
|
}
|
|
1740
1649
|
const duration = Date.now() - startTime;
|
|
1741
1650
|
this.logger.log(`[COW] Grid committed in ${duration}ms`, 'debug');
|
|
1742
|
-
if (stats.size >
|
|
1651
|
+
if (stats.size > COW_PERFORMANCE.GRID_MEMORY_WARNING) {
|
|
1743
1652
|
this.logger.log(`[COW] Warning: Large grid size (${stats.size} orders). Peak memory: ~${Math.round(stats.estimatedBytes / 1024)}KB`, 'warn');
|
|
1744
1653
|
}
|
|
1745
1654
|
}
|
|
1746
1655
|
catch (recalcErr) {
|
|
1747
|
-
this.logger.log(`[COW] Fund recalculation failed post-commit: ${
|
|
1656
|
+
this.logger.log(`[COW] Fund recalculation failed post-commit: ${getErrorMessage(recalcErr)}`, 'error');
|
|
1748
1657
|
this._recoveryState = { ...this._recoveryState, lastFailureAt: Date.now() };
|
|
1749
1658
|
// Re-throw to signal callers that the commit is incomplete
|
|
1750
1659
|
// (grid state committed, but fund state is stale).
|
|
@@ -1768,7 +1677,7 @@ class OrderManager {
|
|
|
1768
1677
|
* @returns {import('./types').PersistenceValidationResult}
|
|
1769
1678
|
*/
|
|
1770
1679
|
validateGridStateForPersistence(options = {}) {
|
|
1771
|
-
const result =
|
|
1680
|
+
const result = validateGridForPersistence(this.orders, this.accountTotals);
|
|
1772
1681
|
const allowBootstrapTransient = options.allowBootstrapTransient !== false;
|
|
1773
1682
|
if (!result.isValid && allowBootstrapTransient && this._bootstrapping) {
|
|
1774
1683
|
this.logger.log(`[BOOTSTRAP] Transient state (expected): ${result.reason}`, 'debug');
|
|
@@ -1824,7 +1733,7 @@ class OrderManager {
|
|
|
1824
1733
|
accountTotals: this.accountTotals,
|
|
1825
1734
|
}));
|
|
1826
1735
|
}
|
|
1827
|
-
const persisted = await
|
|
1736
|
+
const persisted = await persistGridSnapshot(this, this.accountOrders, snapshotOrders, recentFillKeys, effectiveFundSnapshot);
|
|
1828
1737
|
if (persisted === false) {
|
|
1829
1738
|
this.logger.log(`[PERSIST] Grid persistence FAILED (disk full / permissions / write error)`, 'error');
|
|
1830
1739
|
// Keep the dirty flag set so the next flush retries.
|
|
@@ -1859,31 +1768,6 @@ class OrderManager {
|
|
|
1859
1768
|
currentTime: Date.now()
|
|
1860
1769
|
};
|
|
1861
1770
|
}
|
|
1862
|
-
_projectTargetToWorkingGrid(workingGrid, targetGrid) {
|
|
1863
|
-
return (0, validate_1.projectTargetToWorkingGrid)(workingGrid, targetGrid);
|
|
1864
|
-
}
|
|
1865
|
-
_summarizeCowActions(actions) {
|
|
1866
|
-
return (0, validate_1.summarizeActions)(actions);
|
|
1867
|
-
}
|
|
1868
|
-
_evaluateWorkingGridCommit(workingGrid, hasLock = false) {
|
|
1869
|
-
let comparePrecisions;
|
|
1870
|
-
try {
|
|
1871
|
-
comparePrecisions = this._getCowComparePrecisions();
|
|
1872
|
-
}
|
|
1873
|
-
catch (precisionErr) {
|
|
1874
|
-
return {
|
|
1875
|
-
canCommit: false,
|
|
1876
|
-
reason: (0, errors_1.getErrorMessage)(precisionErr),
|
|
1877
|
-
level: 'error'
|
|
1878
|
-
};
|
|
1879
|
-
}
|
|
1880
|
-
return (0, validate_1.evaluateCommit)(workingGrid, {
|
|
1881
|
-
hasLock,
|
|
1882
|
-
currentVersion: this._gridVersion,
|
|
1883
|
-
masterGrid: this.orders,
|
|
1884
|
-
comparePrecisions
|
|
1885
|
-
});
|
|
1886
|
-
}
|
|
1887
1771
|
get _lastIllegalState() {
|
|
1888
1772
|
return this._illegalStateSignal || null;
|
|
1889
1773
|
}
|
|
@@ -1936,5 +1820,5 @@ class OrderManager {
|
|
|
1936
1820
|
};
|
|
1937
1821
|
}
|
|
1938
1822
|
}
|
|
1939
|
-
|
|
1823
|
+
export { OrderManager, COWRebalanceEngine };
|
|
1940
1824
|
//# sourceMappingURL=manager.js.map
|