dexbot 1.6.15 → 1.6.17
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/CHANGELOG.md +52 -0
- package/README.md +4 -4
- package/analysis/tradingview/README.md +2 -2
- package/analysis/trend_detection/DYNAMIC_WEIGHT_RESEARCH.md +1 -1
- package/claw/docs/DEXBOT2_TUNING_CHEAT_SHEET.md +3 -2
- package/claw/modules/chain_queries.ts +3 -3
- package/claw/package.json +1 -1
- package/claw/runtimes/openclaw-plugin/openclaw.plugin.json +1 -1
- package/claw/runtimes/openclaw-plugin/package.json +1 -1
- package/dist/analysis/ama_fitting/analyze_lambda_vs_slow.js +5 -3
- package/dist/analysis/ama_fitting/fetch_lp_candles.js +8 -1
- package/dist/analysis/ama_fitting/optimizer_high_resolution.d.ts +50 -1
- package/dist/analysis/ama_fitting/optimizer_high_resolution.js +33 -7
- package/dist/analysis/bot_fitting/backtest_bot_fitting.d.ts +8 -6
- package/dist/analysis/bot_fitting/backtest_bot_fitting.js +8 -10
- package/dist/analysis/bot_key_utils.js +9 -7
- package/dist/analysis/chart_utils.d.ts +9 -2
- package/dist/analysis/chart_utils.js +51 -1
- package/dist/analysis/tradingview/grid_reset_sim.js +1 -1
- package/dist/analysis/tradingview/tradingview_uplot_chart_generator.d.ts +2 -1
- package/dist/analysis/tradingview/tradingview_uplot_chart_generator.js +23 -9
- package/dist/analysis/trend_detection/backtest_ama_slope_huber.js +1 -1
- package/dist/analysis/trend_detection/dynamic_weight_chart_generator.js +21 -2
- package/dist/analysis/trend_detection/kalman_chart_generator.js +1 -0
- package/dist/analysis/trend_detection/regime_chart_generator.js +1 -1
- package/dist/analysis/trend_detection/volatility_chart_generator.js +1 -0
- package/dist/dexbot.js +89 -138
- package/dist/market_adapter/inputs/fetch_book_data.js +36 -12
- package/dist/market_adapter/inputs/fetch_lp_data.d.ts +6 -0
- package/dist/market_adapter/inputs/fetch_lp_data.js +65 -19
- package/dist/market_adapter/inputs/kibana_feed_source.js +45 -12
- package/dist/market_adapter/inputs/window_cache.d.ts +14 -1
- package/dist/market_adapter/inputs/window_cache.js +53 -17
- package/dist/market_adapter/lp_chart_core.js +1 -0
- package/dist/market_adapter/utils/chain.js +4 -3
- package/dist/market_adapter/utils/pair_orientation.d.ts +60 -0
- package/dist/market_adapter/utils/pair_orientation.js +142 -0
- package/dist/modules/account_bots.d.ts +40 -3
- package/dist/modules/account_bots.js +77 -37
- package/dist/modules/account_orders.d.ts +4 -1
- package/dist/modules/account_orders.js +4 -1
- package/dist/modules/bitshares-native/resolvers.js +3 -3
- package/dist/modules/bitshares_client.d.ts +12 -10
- package/dist/modules/bitshares_client.js +12 -10
- package/dist/modules/bot_settings.js +3 -3
- package/dist/modules/chain_keys.d.ts +6 -2
- package/dist/modules/chain_keys.js +5 -1
- package/dist/modules/chain_orders.d.ts +6 -2
- package/dist/modules/chain_orders.js +6 -2
- package/dist/modules/constants.d.ts +11 -0
- package/dist/modules/constants.js +41 -7
- package/dist/modules/credit_pricing.d.ts +20 -1
- package/dist/modules/credit_pricing.js +23 -1
- package/dist/modules/credit_runtime.js +33 -16
- package/dist/modules/dexbot_class.d.ts +6 -2
- package/dist/modules/dexbot_class.js +8 -3
- package/dist/modules/dexbot_cow_runtime.d.ts +28 -37
- package/dist/modules/dexbot_cow_runtime.js +47 -66
- package/dist/modules/dexbot_maintenance_runtime.js +68 -10
- package/dist/modules/graceful_shutdown.d.ts +6 -2
- package/dist/modules/graceful_shutdown.js +6 -2
- package/dist/modules/launcher/bot_supervisor.js +4 -0
- package/dist/modules/launcher/credential_daemon.d.ts +14 -2
- package/dist/modules/launcher/credential_daemon.js +60 -26
- package/dist/modules/launcher/launch_modes.js +6 -3
- package/dist/modules/launcher/monolithic_runtime.d.ts +39 -1
- package/dist/modules/launcher/monolithic_runtime.js +60 -1
- package/dist/modules/launcher/runtime_mode.d.ts +79 -0
- package/dist/modules/launcher/runtime_mode.js +223 -0
- package/dist/modules/market_adapter_whitelist.d.ts +8 -0
- package/dist/modules/market_adapter_whitelist.js +8 -0
- package/dist/modules/order/accounting.d.ts +15 -1
- package/dist/modules/order/accounting.js +15 -1
- package/dist/modules/order/async_lock.d.ts +9 -3
- package/dist/modules/order/async_lock.js +9 -3
- package/dist/modules/order/export.d.ts +0 -5
- package/dist/modules/order/export.js +0 -5
- package/dist/modules/order/format.d.ts +6 -4
- package/dist/modules/order/format.js +6 -4
- package/dist/modules/order/grid.d.ts +36 -18
- package/dist/modules/order/grid.js +186 -33
- package/dist/modules/order/index.d.ts +16 -1
- package/dist/modules/order/index.js +1 -1
- package/dist/modules/order/logger_state.d.ts +1 -1
- package/dist/modules/order/logger_state.js +1 -1
- package/dist/modules/order/manager.d.ts +30 -7
- package/dist/modules/order/manager.js +108 -12
- package/dist/modules/order/strategy.d.ts +5 -0
- package/dist/modules/order/strategy.js +23 -2
- package/dist/modules/order/sync_engine.d.ts +26 -11
- package/dist/modules/order/sync_engine.js +26 -11
- package/dist/modules/order/utils/math.d.ts +100 -22
- package/dist/modules/order/utils/math.js +106 -22
- package/dist/modules/order/utils/order.d.ts +77 -20
- package/dist/modules/order/utils/order.js +125 -27
- package/dist/modules/order/utils/system.d.ts +107 -11
- package/dist/modules/order/utils/system.js +176 -11
- package/dist/modules/order/utils/validate.d.ts +8 -3
- package/dist/modules/order/utils/validate.js +10 -3
- package/dist/modules/types.d.ts +35 -2
- package/dist/modules/utils/asset_symbols.d.ts +5 -5
- package/dist/modules/utils/asset_symbols.js +5 -5
- package/dist/modules/utils/errors.d.ts +2 -1
- package/dist/modules/utils/errors.js +2 -1
- package/dist/scripts/analyze-credit.js +64 -23
- package/dist/scripts/bts_distribution.d.ts +66 -0
- package/dist/scripts/bts_distribution.js +2102 -0
- package/dist/scripts/rich_list.d.ts +32 -0
- package/dist/scripts/rich_list.js +16 -8
- package/dist/scripts/token_distribution.d.ts +45 -0
- package/dist/scripts/token_distribution.js +1870 -0
- package/dist/unlock.js +91 -42
- package/docs/BITSHARES_ONBOARDING.md +7 -0
- package/docs/COW_INVARIANTS.md +7 -1
- package/docs/DEXBOT_COMPARISON.md +5 -5
- package/docs/EVOLUTION.md +10 -8
- package/docs/FUND_MOVEMENT_AND_ACCOUNTING.md +5 -3
- package/docs/GRID_PRICE_INVARIANT.md +6 -0
- package/docs/GRID_RECALCULATION.md +18 -6
- package/docs/LIFECYCLE.md +8 -8
- package/docs/LOGGING.md +9 -0
- package/docs/MPA_CREDIT_USAGE.md +3 -2
- package/docs/ORDER_ENGINE_POST_1.0_RETROSPECTIVE.md +121 -4
- package/docs/README.md +1 -1
- package/docs/WORKFLOW.md +5 -5
- package/docs/architecture.md +98 -1
- package/market_adapter/README.md +10 -3
- package/package.json +5 -1
- package/scripts/README.md +85 -148
- package/tests/README.md +29 -0
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
* converted to blockchain integers internally using asset precision.
|
|
18
18
|
*
|
|
19
19
|
* ===============================================================================
|
|
20
|
-
* EXPORTS (
|
|
20
|
+
* EXPORTS (25 functions + 1 error class)
|
|
21
21
|
* ===============================================================================
|
|
22
22
|
*
|
|
23
23
|
* ACCOUNT MANAGEMENT (4 functions - async)
|
|
@@ -74,8 +74,12 @@
|
|
|
74
74
|
* Returns 'history' (use fill event data) or 'open' (fetch open orders)
|
|
75
75
|
* 24. broadcastTxWithClassification(tx, accountName, operations) - Broadcast with outcome classification
|
|
76
76
|
*
|
|
77
|
+
* BROADCAST FAILURE DETECTION (1 function)
|
|
78
|
+
* 25. findOverReducingUpdateOpError(result, operations) - Detect limit_order_update
|
|
79
|
+
* ops whose negative delta would over-reduce the order
|
|
80
|
+
*
|
|
77
81
|
* ERROR HANDLING:
|
|
78
|
-
*
|
|
82
|
+
* 26. BroadcastUncertainError - Error class for uncertain broadcast results
|
|
79
83
|
*
|
|
80
84
|
* ===============================================================================
|
|
81
85
|
*
|
|
@@ -105,6 +105,7 @@
|
|
|
105
105
|
* 17. COW_PERFORMANCE - Copy-on-write grid performance settings
|
|
106
106
|
* 18. REBALANCE_STATES - Rebalance lifecycle state enum
|
|
107
107
|
* 19. COW_ACTIONS - Copy-on-write action type enum
|
|
108
|
+
* COW_ORIGINS - Copy-on-write action origin/source enum
|
|
108
109
|
* 20. CR_ZONES - Credit ratio zone constants
|
|
109
110
|
* 21. DEFAULT_TARGET_CR - Default target collateral ratio
|
|
110
111
|
* 22. NATIVE_CLIENT - Native blockchain client configuration
|
|
@@ -114,6 +115,10 @@
|
|
|
114
115
|
* 26. DAEMON_ERRORS - Credential daemon error codes
|
|
115
116
|
* 27. DAEMON_CODES - Credential daemon codes
|
|
116
117
|
* 28. CREDENTIAL_PROMPTS - Credential prompt configuration
|
|
118
|
+
* 29. RANGE_QUALITY - Bot-editor pre-entry price-range legend thresholds
|
|
119
|
+
* 30. buildDefaultGeneralSettings() - Build the canonical default
|
|
120
|
+
* general.settings.json document (first-run seed, editor fallback,
|
|
121
|
+
* and local-overrides merge all share it)
|
|
117
122
|
*
|
|
118
123
|
* ===============================================================================
|
|
119
124
|
*
|
|
@@ -225,6 +230,9 @@ declare let TIMING: {
|
|
|
225
230
|
FILL_TOTALS_RETRY_MAX_MS: number;
|
|
226
231
|
SPREAD_STALE_WARN_MS: number;
|
|
227
232
|
SPREAD_STALE_ESCALATE_MS: number;
|
|
233
|
+
SPREAD_STALE_IN_TOLERANCE_RESET_MS: number;
|
|
234
|
+
SPREAD_STALE_FUND_ESCALATE_MS: number;
|
|
235
|
+
SPREAD_STALE_GEOMETRY_ESCALATE_MS: number;
|
|
228
236
|
SPREAD_CORRECTION_MAX_ATTEMPTS: number;
|
|
229
237
|
SPREAD_STALE_TRANSIENT_GRACE: number;
|
|
230
238
|
SPREAD_CORRECTION_SYNC_FRESH_MS: number;
|
|
@@ -768,6 +776,9 @@ declare function buildDefaultGeneralSettings(): {
|
|
|
768
776
|
FILL_TOTALS_RETRY_MAX_MS: number;
|
|
769
777
|
SPREAD_STALE_WARN_MS: number;
|
|
770
778
|
SPREAD_STALE_ESCALATE_MS: number;
|
|
779
|
+
SPREAD_STALE_IN_TOLERANCE_RESET_MS: number;
|
|
780
|
+
SPREAD_STALE_FUND_ESCALATE_MS: number;
|
|
781
|
+
SPREAD_STALE_GEOMETRY_ESCALATE_MS: number;
|
|
771
782
|
SPREAD_CORRECTION_MAX_ATTEMPTS: number;
|
|
772
783
|
SPREAD_STALE_TRANSIENT_GRACE: number;
|
|
773
784
|
SPREAD_CORRECTION_SYNC_FRESH_MS: number;
|
|
@@ -106,6 +106,7 @@
|
|
|
106
106
|
* 17. COW_PERFORMANCE - Copy-on-write grid performance settings
|
|
107
107
|
* 18. REBALANCE_STATES - Rebalance lifecycle state enum
|
|
108
108
|
* 19. COW_ACTIONS - Copy-on-write action type enum
|
|
109
|
+
* COW_ORIGINS - Copy-on-write action origin/source enum
|
|
109
110
|
* 20. CR_ZONES - Credit ratio zone constants
|
|
110
111
|
* 21. DEFAULT_TARGET_CR - Default target collateral ratio
|
|
111
112
|
* 22. NATIVE_CLIENT - Native blockchain client configuration
|
|
@@ -115,6 +116,10 @@
|
|
|
115
116
|
* 26. DAEMON_ERRORS - Credential daemon error codes
|
|
116
117
|
* 27. DAEMON_CODES - Credential daemon codes
|
|
117
118
|
* 28. CREDENTIAL_PROMPTS - Credential prompt configuration
|
|
119
|
+
* 29. RANGE_QUALITY - Bot-editor pre-entry price-range legend thresholds
|
|
120
|
+
* 30. buildDefaultGeneralSettings() - Build the canonical default
|
|
121
|
+
* general.settings.json document (first-run seed, editor fallback,
|
|
122
|
+
* and local-overrides merge all share it)
|
|
118
123
|
*
|
|
119
124
|
* ===============================================================================
|
|
120
125
|
*
|
|
@@ -330,6 +335,28 @@ let TIMING = {
|
|
|
330
335
|
// by moving the boundary). Time-based so it holds for any tick cadence.
|
|
331
336
|
SPREAD_STALE_WARN_MS: 10 * 60 * 1000, // 10 minutes
|
|
332
337
|
SPREAD_STALE_ESCALATE_MS: 30 * 60 * 1000, // 30 minutes
|
|
338
|
+
// SPREAD_STALE_IN_TOLERANCE_RESET_MS: how long the spread must stay
|
|
339
|
+
// continuously within tolerance before the persistence clock is cleared.
|
|
340
|
+
// A single in-tolerance reading between refusals must NOT clear it: the
|
|
341
|
+
// measured spread oscillates around the threshold, and resetting on each
|
|
342
|
+
// good glance let a live bot sit out of tolerance for 84 minutes while
|
|
343
|
+
// every individual cycle looked recoverable. A placed correction always
|
|
344
|
+
// clears the clock immediately.
|
|
345
|
+
SPREAD_STALE_IN_TOLERANCE_RESET_MS: 10 * 60 * 1000, // 10 minutes
|
|
346
|
+
// SPREAD_STALE_FUND_ESCALATE_MS: escalated window used when the correction
|
|
347
|
+
// reports a RAIL-OVERSUBSCRIBED fund stall (virtual reservation > chain
|
|
348
|
+
// free on the corrected side, e.g. free 454560 vs virtual 456046). That is
|
|
349
|
+
// not "wait for a fill" — the rail itself must be re-sized from chain
|
|
350
|
+
// truth, which the structural re-center does. A shorter window keeps the
|
|
351
|
+
// side dead for minutes instead of the 84 minutes observed live.
|
|
352
|
+
SPREAD_STALE_FUND_ESCALATE_MS: 5 * 60 * 1000, // 5 minutes
|
|
353
|
+
// SPREAD_STALE_GEOMETRY_ESCALATE_MS: escalated window used when the
|
|
354
|
+
// correction reports the geometry bind 'last-fill-blocked' — every
|
|
355
|
+
// tightening slot on the corrected side sits at/through the last fill, i.e.
|
|
356
|
+
// the sell window itself is behind the last sale (boundary/crawl lag). No
|
|
357
|
+
// amount of waiting fixes that; a re-center does. Waiting the structural
|
|
358
|
+
// 30 minutes just leaves the side unquoted.
|
|
359
|
+
SPREAD_STALE_GEOMETRY_ESCALATE_MS: 10 * 60 * 1000, // 10 minutes
|
|
333
360
|
// SPREAD_CORRECTION_MAX_ATTEMPTS: bounded attempts for one spread-correction
|
|
334
361
|
// cycle (initial broadcast + fresh-master re-plan retries). Kept next to the
|
|
335
362
|
// other SPREAD_* tunables so the retry budget is centralized; a value < 1
|
|
@@ -1157,7 +1184,7 @@ let MARKET_ADAPTER = {
|
|
|
1157
1184
|
// Lower values make the AMA channel reach maximum influence more easily.
|
|
1158
1185
|
// Higher values require a stronger price move before AMA reaches full effect.
|
|
1159
1186
|
// nob: amaS% (AMA Max Slope %)
|
|
1160
|
-
DYNAMIC_WEIGHT_AMA_MAX_SLOPE_PCT: 0.
|
|
1187
|
+
DYNAMIC_WEIGHT_AMA_MAX_SLOPE_PCT: 0.1,
|
|
1161
1188
|
// DYNAMIC_WEIGHT_KALMAN_MAX_SLOPE_PCT: Trend size that counts as "full strength" for
|
|
1162
1189
|
// the Kalman composite branch. Kept separate from the AMA slope knob so the two
|
|
1163
1190
|
// channels can saturate independently.
|
|
@@ -1171,7 +1198,8 @@ let MARKET_ADAPTER = {
|
|
|
1171
1198
|
// nob: nz% (Neutral Zone %)
|
|
1172
1199
|
DYNAMIC_WEIGHT_AMA_NEUTRAL_ZONE_PCT: 0,
|
|
1173
1200
|
// DYNAMIC_WEIGHT_AMA_HUBER: Single source of truth for the canonical AMA-slope
|
|
1174
|
-
// estimator (computeHuberWindowSlopePct in
|
|
1201
|
+
// estimator (computeHuberWindowSlopePct in
|
|
1202
|
+
// market_adapter/core/strategies/dynamic_weight_series.ts).
|
|
1175
1203
|
// Every consumer — live market adapter, grid-reset replay, bot-fitting
|
|
1176
1204
|
// backtests, both browser-embedded research charts — reads these values:
|
|
1177
1205
|
// Node callers via the module import, generated HTML via an injected
|
|
@@ -1377,9 +1405,15 @@ let MARKET_ADAPTER = {
|
|
|
1377
1405
|
// DEFAULT_AMA_KEY: Built-in default profile for `gridPrice: "ama"` when no
|
|
1378
1406
|
// pair-specific market_profiles entry exists.
|
|
1379
1407
|
DEFAULT_AMA_KEY: 'AMA3',
|
|
1380
|
-
// AMAS: Built-in AMA presets
|
|
1381
|
-
// Fitted with per-AMA λ weights (0.0031 / 0.0025 / 0.00185 / 0.0013) on 1h candles
|
|
1408
|
+
// AMAS: Built-in AMA presets. AMA1 is the fitted winner (λ=0.0031) on 1h candles
|
|
1382
1409
|
// (2023-07 → 2026-07, SMA-warmup-aligned optimizer, ER=781 fixed, Fast=5.2 fixed).
|
|
1410
|
+
// AMA2–AMA4 are spaced from AMA1 on an exact −9%-per-step effective-smoothing
|
|
1411
|
+
// ladder evaluated at AMA_CONVERGENCE_ER_AVG (0.151):
|
|
1412
|
+
// smooth = (ER·(fastSC−slowSC) + slowSC)², fastSC = 2/(fastPeriod+1),
|
|
1413
|
+
// slowSC = 2/(slowPeriod+1).
|
|
1414
|
+
// Unrounded targets are 62.10 / 71.48 / 83.46 / 99.27; the values below are
|
|
1415
|
+
// rounded to one decimal (the optimizer's 0.1 quantum). Re-derive if
|
|
1416
|
+
// fastPeriod or AMA_CONVERGENCE_ER_AVG changes.
|
|
1383
1417
|
// These serve as stable defaults for the market adapter and can be overridden by
|
|
1384
1418
|
// pair-specific profiles in profiles/market_profiles.json.
|
|
1385
1419
|
AMAS: {
|
|
@@ -1393,19 +1427,19 @@ let MARKET_ADAPTER = {
|
|
|
1393
1427
|
name: 'AMA2',
|
|
1394
1428
|
erPeriod: 781,
|
|
1395
1429
|
fastPeriod: 5.2,
|
|
1396
|
-
slowPeriod:
|
|
1430
|
+
slowPeriod: 71.5,
|
|
1397
1431
|
},
|
|
1398
1432
|
AMA3: {
|
|
1399
1433
|
name: 'AMA3',
|
|
1400
1434
|
erPeriod: 781,
|
|
1401
1435
|
fastPeriod: 5.2,
|
|
1402
|
-
slowPeriod: 83.
|
|
1436
|
+
slowPeriod: 83.5,
|
|
1403
1437
|
},
|
|
1404
1438
|
AMA4: {
|
|
1405
1439
|
name: 'AMA4',
|
|
1406
1440
|
erPeriod: 781,
|
|
1407
1441
|
fastPeriod: 5.2,
|
|
1408
|
-
slowPeriod:
|
|
1442
|
+
slowPeriod: 99.3,
|
|
1409
1443
|
},
|
|
1410
1444
|
},
|
|
1411
1445
|
// AMA_CONVERGENCE_ER_AVG: Typical market Efficiency Ratio used to estimate
|
|
@@ -65,6 +65,25 @@ declare function borrowAmountForCollateral(collateralAmountInt: unknown, collate
|
|
|
65
65
|
* accept op).
|
|
66
66
|
*/
|
|
67
67
|
declare function capBorrowToCollateral(requestedBorrowInt: unknown, availableCollateralInt: unknown, collateralPrice: OfferPrice | null | undefined, debtAssetId?: string | null, collateralAssetId?: string | null): number | null;
|
|
68
|
+
/**
|
|
69
|
+
* Pick the conversion rate used to value collateral.
|
|
70
|
+
*
|
|
71
|
+
* LP-share collateral is a claim on pool reserves, so its market value comes
|
|
72
|
+
* from the live pool and must win over the credit offer's fixed contract price
|
|
73
|
+
* (which is a haircut that can go stale). Other collateral uses the offer
|
|
74
|
+
* price; the pool rate is only a fallback.
|
|
75
|
+
*
|
|
76
|
+
* Returns the selected rate plus its source, or null/ null when neither is
|
|
77
|
+
* usable.
|
|
78
|
+
*/
|
|
79
|
+
declare function selectCollateralConversionRate({ offerRate, poolRate, isLiquidityPoolShare, }: {
|
|
80
|
+
offerRate?: unknown;
|
|
81
|
+
poolRate?: unknown;
|
|
82
|
+
isLiquidityPoolShare?: boolean;
|
|
83
|
+
}): {
|
|
84
|
+
rate: number | null;
|
|
85
|
+
source: 'offer' | 'pool' | null;
|
|
86
|
+
};
|
|
68
87
|
/**
|
|
69
88
|
* Per-deal CR from floats plus a conversion rate:
|
|
70
89
|
* (collateralFloat * rate) / debtFloat.
|
|
@@ -94,5 +113,5 @@ declare function dailyOfferFeeRate(offer: {
|
|
|
94
113
|
* (repay * feeRate + denom - 1) / denom. Zero when nothing is owed.
|
|
95
114
|
*/
|
|
96
115
|
declare function creditDealFee(repayAmountInt: unknown, feeRate: unknown, feeDenom: unknown): number;
|
|
97
|
-
export { normalizeCollateralMap, creditPriceOrientation, extractOfferConversionRate, collateralValueFromOfferPrice, requiredCollateralForBorrow, borrowAmountForCollateral, capBorrowToCollateral, creditDealCollateralRatio, averageCollateralRatio, dailyOfferFeeRate, creditDealFee, };
|
|
116
|
+
export { normalizeCollateralMap, creditPriceOrientation, extractOfferConversionRate, collateralValueFromOfferPrice, requiredCollateralForBorrow, borrowAmountForCollateral, capBorrowToCollateral, selectCollateralConversionRate, creditDealCollateralRatio, averageCollateralRatio, dailyOfferFeeRate, creditDealFee, };
|
|
98
117
|
//# sourceMappingURL=credit_pricing.d.ts.map
|
|
@@ -179,6 +179,28 @@ function capBorrowToCollateral(requestedBorrowInt, availableCollateralInt, colla
|
|
|
179
179
|
return null;
|
|
180
180
|
return Math.min(requested, budgetBorrow);
|
|
181
181
|
}
|
|
182
|
+
/**
|
|
183
|
+
* Pick the conversion rate used to value collateral.
|
|
184
|
+
*
|
|
185
|
+
* LP-share collateral is a claim on pool reserves, so its market value comes
|
|
186
|
+
* from the live pool and must win over the credit offer's fixed contract price
|
|
187
|
+
* (which is a haircut that can go stale). Other collateral uses the offer
|
|
188
|
+
* price; the pool rate is only a fallback.
|
|
189
|
+
*
|
|
190
|
+
* Returns the selected rate plus its source, or null/ null when neither is
|
|
191
|
+
* usable.
|
|
192
|
+
*/
|
|
193
|
+
function selectCollateralConversionRate({ offerRate, poolRate, isLiquidityPoolShare, }) {
|
|
194
|
+
const offer = positiveOrNull(offerRate);
|
|
195
|
+
const pool = positiveOrNull(poolRate);
|
|
196
|
+
if (isLiquidityPoolShare && pool !== null)
|
|
197
|
+
return { rate: pool, source: 'pool' };
|
|
198
|
+
if (offer !== null)
|
|
199
|
+
return { rate: offer, source: 'offer' };
|
|
200
|
+
if (pool !== null)
|
|
201
|
+
return { rate: pool, source: 'pool' };
|
|
202
|
+
return { rate: null, source: null };
|
|
203
|
+
}
|
|
182
204
|
/**
|
|
183
205
|
* Per-deal CR from floats plus a conversion rate:
|
|
184
206
|
* (collateralFloat * rate) / debtFloat.
|
|
@@ -243,5 +265,5 @@ function creditDealFee(repayAmountInt, feeRate, feeDenom) {
|
|
|
243
265
|
return 0;
|
|
244
266
|
return Number(((repay * rate) + denom - 1n) / denom);
|
|
245
267
|
}
|
|
246
|
-
export { normalizeCollateralMap, creditPriceOrientation, extractOfferConversionRate, collateralValueFromOfferPrice, requiredCollateralForBorrow, borrowAmountForCollateral, capBorrowToCollateral, creditDealCollateralRatio, averageCollateralRatio, dailyOfferFeeRate, creditDealFee, };
|
|
268
|
+
export { normalizeCollateralMap, creditPriceOrientation, extractOfferConversionRate, collateralValueFromOfferPrice, requiredCollateralForBorrow, borrowAmountForCollateral, capBorrowToCollateral, selectCollateralConversionRate, creditDealCollateralRatio, averageCollateralRatio, dailyOfferFeeRate, creditDealFee, };
|
|
247
269
|
//# sourceMappingURL=credit_pricing.js.map
|
|
@@ -672,14 +672,32 @@ class CreditRuntime {
|
|
|
672
672
|
const cached = positiveOrNull(this.state.positions[posKey]?.creditConversionRate);
|
|
673
673
|
const cachedAt = this.state.positions[posKey]?.creditConversionRateAt || 0;
|
|
674
674
|
const cachedIsFresh = cached !== null && (Date.now() - cachedAt) < CREDIT_RATE_MAX_AGE_MS;
|
|
675
|
-
//
|
|
676
|
-
//
|
|
677
|
-
//
|
|
678
|
-
//
|
|
679
|
-
//
|
|
680
|
-
//
|
|
681
|
-
|
|
682
|
-
|
|
675
|
+
// LP-share collateral is a claim on pool reserves, so its market value
|
|
676
|
+
// comes from the live pool and must win over the offer's fixed
|
|
677
|
+
// acceptable_collateral price (a contract haircut). This matches the
|
|
678
|
+
// borrow gate in _calculateCollateralValueInDebtAsset(). Other
|
|
679
|
+
// collateral uses the offer map; pool/market pricing is only a
|
|
680
|
+
// fallback for those pairs.
|
|
681
|
+
{
|
|
682
|
+
const debtAsset = await this._resolveAsset(debtAssetId);
|
|
683
|
+
const collateralAsset = await this._resolveAsset(collateralAssetId);
|
|
684
|
+
if (collateralAsset?.for_liquidity_pool && debtAsset?.id) {
|
|
685
|
+
const poolRate = await deriveLiquidityPoolTokenValue(BitShares, String(collateralAsset.id), String(debtAsset.id), 'auto', true).catch((e) => {
|
|
686
|
+
this.log(`credit runtime: pool token rate derivation failed for ${collateralAsset.id}/${debtAsset.id}: ${getErrorMessage(e)}`);
|
|
687
|
+
return null;
|
|
688
|
+
});
|
|
689
|
+
if (poolRate != null && Number.isFinite(poolRate) && poolRate > 0) {
|
|
690
|
+
if (!this.state.positions[posKey])
|
|
691
|
+
this.state.positions[posKey] = {};
|
|
692
|
+
this.state.positions[posKey].creditConversionRate = poolRate;
|
|
693
|
+
this.state.positions[posKey].creditConversionRateAt = Date.now();
|
|
694
|
+
return options.includeSource ? { price: poolRate, source: 'pool-derived' } : poolRate;
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
// Non-LP collateral uses the offer map (live-offer / owned-offer) —
|
|
699
|
+
// the offer's acceptable_collateral price is the contract's
|
|
700
|
+
// debt-per-collateral conversion rate.
|
|
683
701
|
const offerIds = new Set();
|
|
684
702
|
const deals = Array.isArray(this.state.positions[posKey]?.creditDeals)
|
|
685
703
|
? this.state.positions[posKey].creditDeals
|
|
@@ -747,14 +765,13 @@ class CreditRuntime {
|
|
|
747
765
|
return options.includeSource ? { price: rate, source: 'live-offer' } : rate;
|
|
748
766
|
}
|
|
749
767
|
}
|
|
750
|
-
// Offer map had no usable entry
|
|
751
|
-
//
|
|
752
|
-
//
|
|
753
|
-
//
|
|
754
|
-
// Only attempt when cached is
|
|
755
|
-
//
|
|
756
|
-
//
|
|
757
|
-
// resolved above.
|
|
768
|
+
// Offer map had no usable entry (or the LP pool valuation above
|
|
769
|
+
// failed) — fall back to pool derivation for LP shares, then to
|
|
770
|
+
// universal DEX pricing (direct market, else bridge hops via BTS) so
|
|
771
|
+
// every collateral/debt pair resolves a rate even when the collateral
|
|
772
|
+
// or pool is not part of the credit offer. Only attempt when cached is
|
|
773
|
+
// stale to avoid hourly deriveLiquidityPoolTokenValue failures.
|
|
774
|
+
// Reuses debtAsset/collateralAsset resolved above.
|
|
758
775
|
if (!cachedIsFresh) {
|
|
759
776
|
if (collateralAsset?.for_liquidity_pool && debtAsset?.id) {
|
|
760
777
|
const poolRate = await deriveLiquidityPoolTokenValue(BitShares, String(collateralAsset.id), String(debtAsset.id), 'auto', true).catch((e) => {
|
|
@@ -45,7 +45,9 @@
|
|
|
45
45
|
* ===============================================================================
|
|
46
46
|
*
|
|
47
47
|
* HELPER FUNCTIONS (module-level):
|
|
48
|
-
* -
|
|
48
|
+
* - shouldCoalesceBacklogReplay(config, managerConfig) - Decide whether a
|
|
49
|
+
* startup backlog should be drained as one coalesced batch
|
|
50
|
+
* (bot-entry normalization lives in bot_settings.ts: normalizeBotEntry)
|
|
49
51
|
*
|
|
50
52
|
* ===============================================================================
|
|
51
53
|
*
|
|
@@ -187,9 +189,11 @@ declare class DEXBot implements BotLike {
|
|
|
187
189
|
_outOfSpreadSince: number;
|
|
188
190
|
_outOfSpreadStaleWarned: boolean;
|
|
189
191
|
_outOfSpreadTransientCount: number;
|
|
192
|
+
/** When the spread first read within tolerance; cleared on any out-of-tolerance cycle. */
|
|
193
|
+
_outOfSpreadToleranceSince: number;
|
|
190
194
|
_postRecoveryRebalanceDefers: number;
|
|
191
195
|
_skipEmptyReadConfirmDelay: boolean;
|
|
192
|
-
|
|
196
|
+
_spreadStallArmed: boolean;
|
|
193
197
|
_spreadCorrectionStall: SpreadCorrectionStall | null;
|
|
194
198
|
_strandedHoldSince: Map<string, number>;
|
|
195
199
|
_structuralGridResyncDeferCount: number;
|
|
@@ -45,7 +45,9 @@
|
|
|
45
45
|
* ===============================================================================
|
|
46
46
|
*
|
|
47
47
|
* HELPER FUNCTIONS (module-level):
|
|
48
|
-
* -
|
|
48
|
+
* - shouldCoalesceBacklogReplay(config, managerConfig) - Decide whether a
|
|
49
|
+
* startup backlog should be drained as one coalesced batch
|
|
50
|
+
* (bot-entry normalization lives in bot_settings.ts: normalizeBotEntry)
|
|
49
51
|
*
|
|
50
52
|
* ===============================================================================
|
|
51
53
|
*
|
|
@@ -215,9 +217,11 @@ class DEXBot {
|
|
|
215
217
|
_outOfSpreadSince;
|
|
216
218
|
_outOfSpreadStaleWarned;
|
|
217
219
|
_outOfSpreadTransientCount;
|
|
220
|
+
/** When the spread first read within tolerance; cleared on any out-of-tolerance cycle. */
|
|
221
|
+
_outOfSpreadToleranceSince;
|
|
218
222
|
_postRecoveryRebalanceDefers;
|
|
219
223
|
_skipEmptyReadConfirmDelay;
|
|
220
|
-
|
|
224
|
+
_spreadStallArmed;
|
|
221
225
|
_spreadCorrectionStall;
|
|
222
226
|
_strandedHoldSince;
|
|
223
227
|
_structuralGridResyncDeferCount;
|
|
@@ -277,6 +281,7 @@ class DEXBot {
|
|
|
277
281
|
this._outOfSpreadSince = 0;
|
|
278
282
|
this._outOfSpreadStaleWarned = false;
|
|
279
283
|
this._outOfSpreadTransientCount = 0;
|
|
284
|
+
this._outOfSpreadToleranceSince = 0;
|
|
280
285
|
this.logPrefix = options.logPrefix || '';
|
|
281
286
|
this._credentialDaemonWatchdogInterval = null;
|
|
282
287
|
this._credentialDaemonDown = false;
|
|
@@ -336,7 +341,7 @@ class DEXBot {
|
|
|
336
341
|
// Spread-correction stall state: the boolean gate and its companion cause
|
|
337
342
|
// are read by getTargetedSyncReason before the first spread check, so
|
|
338
343
|
// initialize both rather than leaving them undefined.
|
|
339
|
-
this.
|
|
344
|
+
this._spreadStallArmed = false;
|
|
340
345
|
this._spreadCorrectionStall = null;
|
|
341
346
|
this._lightweightSyncCheckAt = 0;
|
|
342
347
|
this._targetedDriftSyncCooldownMs = this.config.timing?.TARGETED_DRIFT_SYNC_COOLDOWN_MS;
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
* the same pattern as dexbot_fill_runtime.ts.
|
|
9
9
|
*/
|
|
10
10
|
import * as chainOrdersModule from './chain_orders.js';
|
|
11
|
+
import { isLastFillGuardBlocked } from './order/utils/system.js';
|
|
11
12
|
/**
|
|
12
13
|
* Re-verify a B-stamped gap-evacuation rotation against the LIVE committed
|
|
13
14
|
* geometry (boundaryIdx/_gapSlots at execution time). The stamp freezes
|
|
@@ -56,6 +57,19 @@ export interface CowResult extends JsonObj {
|
|
|
56
57
|
workingIndexes: unknown;
|
|
57
58
|
origin?: string;
|
|
58
59
|
refillSlotIds?: string[];
|
|
60
|
+
/**
|
|
61
|
+
* Owed fill-crawl records this plan's derivation actually folded. The
|
|
62
|
+
* commit clears ONLY these; a record the derivation could not apply (a
|
|
63
|
+
* truncated cross-chunk shift, an edge-classified slot, a newer record for
|
|
64
|
+
* the same slot) stays owed. Without this the commit cleared the whole
|
|
65
|
+
* ledger and silently lost the difference (2026-10-25: 7 fills swept
|
|
66
|
+
* slots 147-153, boundary ended 7 levels behind).
|
|
67
|
+
*/
|
|
68
|
+
consumedCrawlIds?: Array<{
|
|
69
|
+
slotId: string;
|
|
70
|
+
side: string;
|
|
71
|
+
ts?: number;
|
|
72
|
+
}>;
|
|
59
73
|
fills?: unknown[];
|
|
60
74
|
}
|
|
61
75
|
/** A chain order enriched with the loose fields COW recovery reads. */
|
|
@@ -304,47 +318,24 @@ declare function recordRestampBuyAboveAnchor(bot: BotLike, price: unknown, type:
|
|
|
304
318
|
* @returns {string[]} destinations inside the gap band
|
|
305
319
|
*/
|
|
306
320
|
declare function collectGapBandPlacementShadowSlots(actions: CowAction[] | null | undefined, boundary: unknown, gapSlots: unknown): string[];
|
|
307
|
-
/**
|
|
308
|
-
* LAST-FILL-GUARD helper — pivot ± halfIncrement (replaces price-tolerance).
|
|
309
|
-
* last fill @x with increment i (blocked shapes): BUY > x*(1 - i/2/100),
|
|
310
|
-
* SELL < x*(1 + i/2/100)
|
|
311
|
-
* e.g. x=1000, i=0.5% => BUY > 997.5 and SELL < 1002.5 are blocked
|
|
312
|
-
* Cold (pivot null or lastType null) => disabled. Spread-correction CREATES
|
|
313
|
-
* bypass per-action (see broadcast sites); gap-evacuation rotation UPDATEs
|
|
314
|
-
* bypass only via the violation-reducing allowance (origin='gap-evacuation',
|
|
315
|
-
* UPDATE-only, frozen B-stamp or live-proven isEvacuationRotationAllowed) —
|
|
316
|
-
* see the UPDATE rotation guard block.
|
|
317
|
-
* The pivot is the latest fill of either side and is never expired: it stays
|
|
318
|
-
* the durable mark (last sold level floors new sells; buy fills pull it down
|
|
319
|
-
* and re-open the sell side, buy-below-sell is never gated).
|
|
320
|
-
* @param {number} price - Target order price
|
|
321
|
-
* @param {number} size - Order size (unused, kept for compat)
|
|
322
|
-
* @param {string} type - ORDER_TYPES.BUY/SELL
|
|
323
|
-
* @param {number|null} lastPrice - Most recent fill price
|
|
324
|
-
* @param {string|null} lastType - Most recent fill side (BUY/SELL)
|
|
325
|
-
* @param {number|any} incrementPercent - Grid increment percent (e.g. 0.5). If assets object passed, falls back to default.
|
|
326
|
-
* @returns {{blocked: boolean, pivot: number|null, halfInc: number, threshold: number|null}}
|
|
327
|
-
*/
|
|
328
|
-
declare function isLastFillGuardBlocked(price: number, _size: number, type: unknown, lastPrice: number | null, lastType: string | null, incrementPercent: number): {
|
|
329
|
-
blocked: boolean;
|
|
330
|
-
pivot: number | null;
|
|
331
|
-
halfInc?: number;
|
|
332
|
-
threshold?: number | null;
|
|
333
|
-
};
|
|
334
321
|
/**
|
|
335
322
|
* Whether a spread-correction CREATE may bypass the LAST-FILL-GUARD.
|
|
336
323
|
*
|
|
337
|
-
*
|
|
338
|
-
*
|
|
339
|
-
*
|
|
324
|
+
* Remaining purpose: the STARTUP BOOK SEED proxy. Before the first real fill a
|
|
325
|
+
* single-sided book arms the guard on our own resting quote (max bid / min
|
|
326
|
+
* ask) — a proxy that `b14a1fc8` deliberately keeps non-persistent ("a book
|
|
327
|
+
* seed never fossilizes into the snapshot as market truth") because it goes
|
|
328
|
+
* wrong whenever the book is not adjacent to the last fill. Blocking a
|
|
329
|
+
* tightening placement against that proxy would be arbitrary, so the correction
|
|
330
|
+
* is released from it while the local snapshot can be trusted.
|
|
331
|
+
*
|
|
332
|
+
* A real FILL reference always denies the bypass. Since the planner now filters
|
|
333
|
+
* candidates with the SAME predicate the guard applies, planner-built
|
|
334
|
+
* corrections never even propose an op that would need it — the bypass is a
|
|
335
|
+
* proxy-case valve, not a licence to trade through the last real fill.
|
|
340
336
|
*
|
|
341
|
-
*
|
|
342
|
-
*
|
|
343
|
-
* issue-#31 adverse fill.
|
|
344
|
-
* 2. A cold/book pivot is a heuristic (book seed) or absent, so the historical
|
|
345
|
-
* fail-open bypass is kept — but only while the LOCAL snapshot is fresh.
|
|
346
|
-
* A stale snapshot (known-unmatched chain orders, or an old sync) can hide
|
|
347
|
-
* the real window edge, so the bypass is withheld and the guard applies.
|
|
337
|
+
* Denied too when the snapshot is stale or has known-unmatched chain orders
|
|
338
|
+
* (a stale edge can hide the real window edge), fail-closed on a gate throw.
|
|
348
339
|
*
|
|
349
340
|
* Exported for tests.
|
|
350
341
|
* @param {Object} bot
|