dexbot 1.6.18 → 1.7.1
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 +68 -0
- package/README.md +24 -18
- package/analysis/README.md +117 -5
- 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/account_resolver.js +5 -6
- package/dist/analysis/analyze_dynamic_weight.js +2 -1
- package/dist/analysis/analyze_kalman.js +3 -2
- package/dist/analysis/analyze_liquidity.d.ts +17 -0
- package/dist/analysis/analyze_liquidity.js +336 -0
- package/dist/analysis/analyze_regime.js +3 -2
- package/dist/analysis/analyze_regime_windows.js +3 -2
- package/dist/analysis/analyze_volatility.js +3 -2
- package/dist/analysis/bot_key_utils.d.ts +44 -1
- package/dist/analysis/bot_key_utils.js +76 -7
- package/dist/analysis/fills_cache.js +2 -1
- package/dist/analysis/grid_correction_check.d.ts +1 -19
- package/dist/analysis/grid_correction_check.js +12 -30
- package/dist/analysis/liquidity/liquidity_chain.d.ts +66 -0
- package/dist/analysis/liquidity/liquidity_chain.js +178 -0
- package/dist/analysis/liquidity/liquidity_chart_generator.d.ts +19 -0
- package/dist/analysis/liquidity/liquidity_chart_generator.js +693 -0
- package/dist/analysis/liquidity/liquidity_depth_model.d.ts +166 -0
- package/dist/analysis/liquidity/liquidity_depth_model.js +258 -0
- package/dist/analysis/liquidity/liquidity_view.d.ts +53 -0
- package/dist/analysis/liquidity/liquidity_view.js +66 -0
- package/dist/analysis/resolve_bot_accounts.js +7 -6
- package/dist/analysis/trade_profitability.js +4 -2
- package/dist/analysis/tradingview/analyze_tradingview.js +2 -1
- package/dist/credential-daemon.js +13 -2
- package/dist/dexbot.js +77 -172
- package/dist/modules/account_bots.d.ts +11 -1
- package/dist/modules/account_orders.d.ts +12 -0
- package/dist/modules/account_orders.js +30 -0
- package/dist/modules/asset_lookup.d.ts +25 -0
- package/dist/modules/asset_lookup.js +75 -0
- package/dist/modules/bitshares-native/transport.js +24 -6
- package/dist/modules/bitshares_client.d.ts +24 -6
- package/dist/modules/bitshares_client.js +44 -7
- package/dist/modules/bot_settings.d.ts +2 -0
- package/dist/modules/chain_scan.d.ts +64 -0
- package/dist/modules/chain_scan.js +80 -0
- package/dist/modules/cli_colors.d.ts +5 -0
- package/dist/modules/cli_colors.js +7 -0
- package/dist/modules/cli_progress.d.ts +51 -0
- package/dist/modules/cli_progress.js +113 -0
- package/dist/modules/constants.d.ts +26 -3
- package/dist/modules/constants.js +63 -8
- package/dist/modules/dexbot_class.d.ts +1 -6
- package/dist/modules/dexbot_class.js +14 -0
- package/dist/modules/dexbot_cow_runtime.d.ts +97 -7
- package/dist/modules/dexbot_cow_runtime.js +634 -136
- package/dist/modules/dexbot_fill_runtime.js +132 -23
- package/dist/modules/dexbot_maintenance_runtime.js +242 -74
- package/dist/modules/dexbot_startup_runtime.js +32 -20
- package/dist/modules/dexbot_state_recovery.d.ts +33 -1
- package/dist/modules/dexbot_state_recovery.js +167 -5
- package/dist/modules/launcher/runtime_entry.d.ts +21 -1
- package/dist/modules/launcher/runtime_entry.js +36 -1
- package/dist/modules/liquidity_pool_ref.d.ts +32 -0
- package/dist/modules/liquidity_pool_ref.js +116 -0
- package/dist/modules/node_connect_policy.d.ts +8 -1
- package/dist/modules/node_connect_policy.js +18 -2
- package/dist/modules/node_flap_score.d.ts +87 -0
- package/dist/modules/node_flap_score.js +124 -0
- package/dist/modules/node_health_cache.d.ts +40 -3
- package/dist/modules/node_health_cache.js +66 -20
- package/dist/modules/node_manager.d.ts +101 -19
- package/dist/modules/node_manager.js +255 -65
- package/dist/modules/order/accounting.js +56 -7
- package/dist/modules/order/format.d.ts +24 -10
- package/dist/modules/order/format.js +30 -10
- package/dist/modules/order/grid.js +91 -13
- package/dist/modules/order/grid_reconcile.d.ts +5 -1
- package/dist/modules/order/grid_reconcile.js +333 -189
- package/dist/modules/order/grid_reconcile_internal.d.ts +38 -2
- package/dist/modules/order/grid_reconcile_internal.js +298 -100
- package/dist/modules/order/index.d.ts +19 -1
- package/dist/modules/order/lifecycle.d.ts +353 -0
- package/dist/modules/order/lifecycle.js +1109 -0
- package/dist/modules/order/manager.d.ts +273 -5
- package/dist/modules/order/manager.js +654 -48
- package/dist/modules/order/placement_lock.d.ts +102 -0
- package/dist/modules/order/placement_lock.js +222 -0
- package/dist/modules/order/strategy.js +13 -1
- package/dist/modules/order/sync_engine.d.ts +33 -1
- package/dist/modules/order/sync_engine.js +241 -73
- package/dist/modules/order/utils/math.d.ts +39 -1
- package/dist/modules/order/utils/math.js +83 -1
- package/dist/modules/order/utils/order.d.ts +181 -5
- package/dist/modules/order/utils/order.js +455 -9
- package/dist/modules/order/utils/system.d.ts +81 -1
- package/dist/modules/order/utils/system.js +331 -44
- package/dist/modules/order/utils/validate.d.ts +7 -1
- package/dist/modules/order/utils/validate.js +80 -3
- package/dist/modules/outcome.d.ts +54 -0
- package/dist/modules/outcome.js +51 -0
- package/dist/modules/settings_merge.d.ts +4 -0
- package/dist/modules/settings_merge.js +9 -0
- package/dist/modules/timing_policy.d.ts +82 -0
- package/dist/modules/timing_policy.js +244 -0
- package/dist/modules/types.d.ts +137 -0
- package/dist/modules/utils/asset_symbols.d.ts +13 -2
- package/dist/modules/utils/asset_symbols.js +21 -2
- package/dist/scripts/analysis_runner.d.ts +10 -0
- package/dist/scripts/analysis_runner.js +33 -0
- package/dist/scripts/analyze-credit.js +54 -17
- package/dist/scripts/analyze-orders.d.ts +4 -2
- package/dist/scripts/analyze-orders.js +115 -35
- package/dist/scripts/asset.d.ts +26 -0
- package/dist/scripts/asset.js +165 -0
- package/dist/scripts/bts_distribution.d.ts +1 -0
- package/dist/scripts/bts_distribution.js +194 -142
- package/dist/scripts/chart_command.d.ts +1 -1
- package/dist/scripts/chart_command.js +60 -86
- package/dist/scripts/chart_targets.d.ts +18 -0
- package/dist/scripts/chart_targets.js +48 -0
- package/dist/scripts/dw.js +1 -1
- package/dist/scripts/liquid.d.ts +24 -0
- package/dist/scripts/liquid.js +134 -0
- package/dist/scripts/rich_list.js +12 -16
- package/dist/scripts/test-credit-renewal.js +12 -5
- package/dist/scripts/token_distribution.d.ts +2 -0
- package/dist/scripts/token_distribution.js +132 -108
- package/dist/scripts/tv.js +1 -1
- package/dist/scripts/verify-browser-bundle.js +18 -0
- package/docs/COW_INVARIANTS.md +69 -10
- package/docs/DEXBOT_COMPARISON.md +6 -6
- package/docs/EVOLUTION.md +10 -8
- package/docs/FUND_MOVEMENT_AND_ACCOUNTING.md +1 -1
- package/docs/GRID_PRICE_INVARIANT.md +5 -2
- package/docs/GRID_RECONCILE.md +36 -11
- package/docs/LOGGING.md +50 -4
- package/docs/ORDER_ENGINE_POST_1.0_RETROSPECTIVE.md +177 -27
- package/docs/README.md +23 -2
- package/docs/WORKFLOW.md +12 -4
- package/docs/architecture.md +42 -5
- package/docs/developer_guide.md +25 -8
- package/modules/README.md +8 -1
- package/package.json +3 -1
- package/scripts/README.md +39 -7
- package/scripts/pdev.sh +14 -1
- package/scripts/pmain.sh +23 -8
- package/scripts/ptest.sh +7 -0
- package/tests/README.md +30 -1
|
@@ -934,11 +934,20 @@ export async function persistGridSnapshot(manager, accountOrders, snapshotOrders
|
|
|
934
934
|
// Bot-scoped market-fill reference: survives grid regeneration, used
|
|
935
935
|
// by spread correction as the price it must not trade through.
|
|
936
936
|
const lastMarketFill = buildLastMarketFillPayload(manager);
|
|
937
|
+
// Restart durability for the pass-1 "size already applied" markers.
|
|
938
|
+
// They live on the manager only, so a crash between pass 1 and the
|
|
939
|
+
// fill-history batch would lose the evidence and replay the fill as a
|
|
940
|
+
// second shrink. Persisted in the existing free-form debugInputs blob
|
|
941
|
+
// (no schema change to the order rows).
|
|
942
|
+
const chainSizeAppliedFills = typeof manager.snapshotChainSizeAppliedFills === 'function'
|
|
943
|
+
? manager.snapshotChainSizeAppliedFills()
|
|
944
|
+
: undefined;
|
|
937
945
|
await accountOrders.storeMasterGrid(orders, btsFeesOwed, manager.boundaryIdx, manager.assets || null, {
|
|
938
946
|
persistedAt: nowIso(),
|
|
939
947
|
config: debugConfig,
|
|
940
948
|
accountTotals,
|
|
941
|
-
btsBalance
|
|
949
|
+
btsBalance,
|
|
950
|
+
...(chainSizeAppliedFills !== undefined ? { chainSizeAppliedFills } : {})
|
|
942
951
|
}, fillKeys, genesis, gapEvacStreaks, pendingFillCrawls, lastFillPivot, lastMarketFill);
|
|
943
952
|
return true;
|
|
944
953
|
}
|
|
@@ -1374,7 +1383,16 @@ export function reanchorBoundaryFromLastFill(manager, boundaryIdx, slots, gapSlo
|
|
|
1374
1383
|
// leaves the remainder at the fill slot, and any boundary that puts the
|
|
1375
1384
|
// fill outside its window necessarily puts that slot inside the implied
|
|
1376
1385
|
// band. Allow exactly that residual (PARTIAL, same side, same ladder
|
|
1377
|
-
// level); every other in-band order still refuses the re-anchor
|
|
1386
|
+
// level); every other in-band order still refuses the re-anchor UNLESS the
|
|
1387
|
+
// move strands strictly fewer placed orders than the committed boundary
|
|
1388
|
+
// (the violation-reducing repair below).
|
|
1389
|
+
//
|
|
1390
|
+
// ANCHOR-AWARE: the residual sits at the slot the re-anchor is derived
|
|
1391
|
+
// from (`anchorIdx`), which is the batch EXTREMUM, not necessarily the
|
|
1392
|
+
// last-processed pivot (`idx`). Comparing against `idx`/`price` made this
|
|
1393
|
+
// allow-list dead code for every chunked sweep whose furthest fill is not
|
|
1394
|
+
// the pivot — i.e. exactly the case the extremum resolution exists for —
|
|
1395
|
+
// so the re-anchor was refused as 'unsafe: placed_order_in_band'.
|
|
1378
1396
|
const allowedInBandSlotIds = [];
|
|
1379
1397
|
const fillSlotOrder = Array.isArray(slots) ? slots[anchorIdx] : null;
|
|
1380
1398
|
if (fillSlotOrder && fillSlotOrder.orderId
|
|
@@ -1388,8 +1406,8 @@ export function reanchorBoundaryFromLastFill(manager, boundaryIdx, slots, gapSlo
|
|
|
1388
1406
|
orderIdx = null;
|
|
1389
1407
|
}
|
|
1390
1408
|
const orderPrice = Number(fillSlotOrder.price);
|
|
1391
|
-
if (orderIdx ===
|
|
1392
|
-
&& Math.abs(orderPrice -
|
|
1409
|
+
if (orderIdx === anchorIdx && Number.isFinite(orderPrice) && orderPrice > 0
|
|
1410
|
+
&& Math.abs(orderPrice - anchorPrice) / anchorPrice <= maxDrift) {
|
|
1393
1411
|
allowedInBandSlotIds.push(String(fillSlotOrder.id));
|
|
1394
1412
|
}
|
|
1395
1413
|
}
|
|
@@ -1401,7 +1419,23 @@ export function reanchorBoundaryFromLastFill(manager, boundaryIdx, slots, gapSlo
|
|
|
1401
1419
|
check = { ok: false, reason: getErrorMessage(err) };
|
|
1402
1420
|
}
|
|
1403
1421
|
if (!check || check.ok !== true) {
|
|
1404
|
-
|
|
1422
|
+
// VIOLATION-REDUCING REPAIR: a fill-derived re-anchor may legitimately
|
|
1423
|
+
// land while older strands are still waiting for GAP-EVAC. Refusing it
|
|
1424
|
+
// keeps the window behind the fill (the counter order stays in-band);
|
|
1425
|
+
// accepting it is safe only when the move strands STRICTLY FEWER placed
|
|
1426
|
+
// orders than the committed boundary already does — the same bound the
|
|
1427
|
+
// strict validator applies, measured instead of assumed. Structural
|
|
1428
|
+
// refusals (crossed book, ceiling, range) are never a repair.
|
|
1429
|
+
const repairsStrands = check?.reason === MathUtils.BOUNDARY_REJECT_PLACED_IN_BAND
|
|
1430
|
+
&& MathUtils.boundaryMoveReducesStrands(boundary, candidate, slots ?? [], gap, allowedInBandSlotIds.length > 0 ? { allowInBandSlotIds: allowedInBandSlotIds } : undefined);
|
|
1431
|
+
if (!repairsStrands) {
|
|
1432
|
+
return { boundaryIdx: boundary, reanchored: false, reason: `unsafe: ${check?.reason ?? 'unknown'}` };
|
|
1433
|
+
}
|
|
1434
|
+
try {
|
|
1435
|
+
manager.logger?.log?.(`[BOUNDARY] Re-anchor accepted as a violation-reducing repair: boundary ${boundary} -> ` +
|
|
1436
|
+
`${candidate} strands fewer placed orders than the committed geometry (${check?.reason})`, 'warn');
|
|
1437
|
+
}
|
|
1438
|
+
catch { /* logging is best-effort */ }
|
|
1405
1439
|
}
|
|
1406
1440
|
try {
|
|
1407
1441
|
manager.logger?.log?.(`[BOUNDARY] Re-anchored from the last fill: ${side} filled at slot ${anchorIdx} ` +
|
|
@@ -1487,23 +1521,123 @@ function resolveFillAnchorIndex(fills, side, genesis, maxDrift) {
|
|
|
1487
1521
|
* live pivot only when `lastFillPivotSource === 'fill'`; otherwise fall back to
|
|
1488
1522
|
* the bot-scoped last-market-fill row, which is written only by real fills and
|
|
1489
1523
|
* survives grid regeneration.
|
|
1524
|
+
*
|
|
1525
|
+
* Read-time TTL applies here too (see resolveFillProvenancedRow): an absolute
|
|
1526
|
+
* geometry move must not be derived from a fill older than the pivot lifetime.
|
|
1527
|
+
*
|
|
1490
1528
|
* @param {Object} manager - OrderManager instance
|
|
1491
1529
|
* @returns {{price: number|null, type: string|null}}
|
|
1492
1530
|
*/
|
|
1493
1531
|
function resolveFillProvenancedReference(manager) {
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1532
|
+
const row = resolveFillProvenancedRow(manager);
|
|
1533
|
+
return { price: row?.price ?? null, type: row?.type ?? null };
|
|
1534
|
+
}
|
|
1535
|
+
/**
|
|
1536
|
+
* The pivot lifetime, in ms. 0 means "no lifetime configured" (never expire).
|
|
1537
|
+
* ONE reader for GRID_LIMITS.LAST_FILL_PIVOT_TTL_MS: the persisted-row
|
|
1538
|
+
* restores, the read-time guards and the re-arm all derive from it.
|
|
1539
|
+
*/
|
|
1540
|
+
function lastFillPivotTtlMs() {
|
|
1541
|
+
const ttl = Number(GRID_LIMITS.LAST_FILL_PIVOT_TTL_MS);
|
|
1542
|
+
return Number.isFinite(ttl) && ttl > 0 ? ttl : 0;
|
|
1543
|
+
}
|
|
1544
|
+
/**
|
|
1545
|
+
* Is a fill reference older than the pivot lifetime?
|
|
1546
|
+
*
|
|
1547
|
+
* The same bound applies at every reading of a fill reference:
|
|
1548
|
+
* `restoreLastFillPivot` / `restoreLastMarketFill` (persisted rows) and
|
|
1549
|
+
* `resolveLastFillGuardReference` / `resolveFillProvenancedRow` (in-memory
|
|
1550
|
+
* rows). Without the read-time half, a long-lived process keeps a fill older
|
|
1551
|
+
* than 24h vetoing one side forever — and the geometry-bind watchdog
|
|
1552
|
+
* re-centers at most ONCE per pivot and then reports that "a fill or the pivot
|
|
1553
|
+
* TTL restores the side" (trackOutOfSpreadStaleness), a promise the code must
|
|
1554
|
+
* keep without a restart.
|
|
1555
|
+
*
|
|
1556
|
+
* An unknown age (missing/0 timestamp) never expires: legacy managers and the
|
|
1557
|
+
* ambiguous two-sided book seed carry no fill clock, and silently unarming
|
|
1558
|
+
* those would be worse than a stale veto.
|
|
1559
|
+
*
|
|
1560
|
+
* @param {unknown} atMs - Row timestamp (ms epoch)
|
|
1561
|
+
* @param {number} [now=Date.now()] - Injectable clock (tests)
|
|
1562
|
+
* @returns {boolean} True when the row is older than the pivot TTL
|
|
1563
|
+
*/
|
|
1564
|
+
function isFillReferenceExpired(atMs, now = Date.now()) {
|
|
1565
|
+
const ttl = lastFillPivotTtlMs();
|
|
1566
|
+
if (ttl <= 0)
|
|
1567
|
+
return false;
|
|
1568
|
+
const at = Number(atMs);
|
|
1569
|
+
if (!Number.isFinite(at) || at <= 0)
|
|
1570
|
+
return false;
|
|
1571
|
+
return now - at > ttl;
|
|
1572
|
+
}
|
|
1573
|
+
/**
|
|
1574
|
+
* Report a pivot expiry once per expired timestamp. The read-time readers run
|
|
1575
|
+
* per probe and per guarded action, so an unguarded line would flood the log;
|
|
1576
|
+
* the marker is the expired row's own clock, which also re-arms the report
|
|
1577
|
+
* when a NEW fill later expires.
|
|
1578
|
+
*/
|
|
1579
|
+
function logExpiredFillReference(manager, atMs, now) {
|
|
1580
|
+
const at = Number(atMs);
|
|
1581
|
+
if (!manager || !Number.isFinite(at) || at <= 0)
|
|
1582
|
+
return;
|
|
1583
|
+
if (Number(manager._lastFillPivotExpiryLoggedAt) === at)
|
|
1584
|
+
return;
|
|
1585
|
+
manager._lastFillPivotExpiryLoggedAt = at;
|
|
1586
|
+
try {
|
|
1587
|
+
manager.logger?.log?.(`[LAST-FILL-GUARD] Pivot expired (age ${Math.round((now - at) / 1000)}s > TTL ` +
|
|
1588
|
+
`${Math.round(lastFillPivotTtlMs() / 1000)}s) — guard re-arms on the next fill`, 'info');
|
|
1500
1589
|
}
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1590
|
+
catch { /* logging is best-effort */ }
|
|
1591
|
+
}
|
|
1592
|
+
/** Normalize one in-memory triple (price, side, clock) into a fill reference row. */
|
|
1593
|
+
function readFillRow(price, type, atMs) {
|
|
1594
|
+
const p = Number(price);
|
|
1595
|
+
if (!Number.isFinite(p) || p <= 0)
|
|
1596
|
+
return null;
|
|
1597
|
+
if (type !== ORDER_TYPES.BUY && type !== ORDER_TYPES.SELL)
|
|
1598
|
+
return null;
|
|
1599
|
+
const at = Number(atMs);
|
|
1600
|
+
return { price: p, type, fillsAt: Number.isFinite(at) && at > 0 ? at : null };
|
|
1601
|
+
}
|
|
1602
|
+
/** Keep a row only while it is inside the pivot lifetime (logging the expiry once). */
|
|
1603
|
+
function acceptFillRow(manager, row, now) {
|
|
1604
|
+
if (!row)
|
|
1605
|
+
return null;
|
|
1606
|
+
if (isFillReferenceExpired(row.fillsAt, now)) {
|
|
1607
|
+
logExpiredFillReference(manager, row.fillsAt, now);
|
|
1608
|
+
return null;
|
|
1505
1609
|
}
|
|
1506
|
-
return
|
|
1610
|
+
return row;
|
|
1611
|
+
}
|
|
1612
|
+
/** Live armed pivot as a row, or null when cold OR past the pivot lifetime. */
|
|
1613
|
+
function liveFillRow(manager, now) {
|
|
1614
|
+
return acceptFillRow(manager, readFillRow(manager?._lastFilledPrice, manager?._lastFilledType, manager?._lastFilledAt), now);
|
|
1615
|
+
}
|
|
1616
|
+
/** Bot-scoped last-market-fill row, or null when absent OR past the pivot lifetime. */
|
|
1617
|
+
function marketFillRow(manager, now) {
|
|
1618
|
+
return acceptFillRow(manager, readFillRow(manager?._lastMarketFillPrice, manager?._lastMarketFillType, manager?._lastMarketFillAt), now);
|
|
1619
|
+
}
|
|
1620
|
+
/**
|
|
1621
|
+
* FILL-PROVENANCED row for absolute geometry repairs (boundary re-anchor,
|
|
1622
|
+
* spread market reference): the live pivot only when its provenance is
|
|
1623
|
+
* `'fill'`, else the bot-scoped market row.
|
|
1624
|
+
*
|
|
1625
|
+
* The row's OWN timestamp travels with the row. Pairing one value's price with
|
|
1626
|
+
* another value's clock is what let `rearmLastFillPivotFromMarketFill` stamp a
|
|
1627
|
+
* market-fill price with a BOOK seed's (fresh) timestamp — silently refreshing
|
|
1628
|
+
* the 24h lifetime on every rebuild.
|
|
1629
|
+
*
|
|
1630
|
+
* @param {Object} manager - OrderManager instance
|
|
1631
|
+
* @param {number} [now=Date.now()] - Injectable clock (tests)
|
|
1632
|
+
* @returns {FillPivotRow|null} Row, or null when both sources are cold/expired
|
|
1633
|
+
*/
|
|
1634
|
+
function resolveFillProvenancedRow(manager, now = Date.now()) {
|
|
1635
|
+
if (manager?.lastFillPivotSource === 'fill') {
|
|
1636
|
+
const live = liveFillRow(manager, now);
|
|
1637
|
+
if (live)
|
|
1638
|
+
return live;
|
|
1639
|
+
}
|
|
1640
|
+
return marketFillRow(manager, now);
|
|
1507
1641
|
}
|
|
1508
1642
|
/**
|
|
1509
1643
|
* Resolve the reference the generic LAST-FILL-GUARD judges against.
|
|
@@ -1525,20 +1659,22 @@ function resolveFillProvenancedReference(manager) {
|
|
|
1525
1659
|
* so this fallback covers only the window before that write, not the whole
|
|
1526
1660
|
* post-rebuild lifetime.
|
|
1527
1661
|
*
|
|
1662
|
+
* LIFETIME: a fill reference older than GRID_LIMITS.LAST_FILL_PIVOT_TTL_MS is
|
|
1663
|
+
* ignored (read-time expiry, same bound the restore path applies), so a stale
|
|
1664
|
+
* pivot cannot veto one side forever in a long-lived process. Unknown ages
|
|
1665
|
+
* (missing timestamp) never expire.
|
|
1666
|
+
*
|
|
1528
1667
|
* @param {Object} manager - OrderManager instance
|
|
1529
1668
|
* @returns {{price: number|null, type: string|null}} Effective pivot (both null when cold)
|
|
1530
1669
|
*/
|
|
1531
1670
|
export function resolveLastFillGuardReference(manager) {
|
|
1532
|
-
const
|
|
1533
|
-
const
|
|
1534
|
-
if (
|
|
1535
|
-
return { price:
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
if (Number.isFinite(marketPrice) && marketPrice > 0 && (marketType === ORDER_TYPES.BUY || marketType === ORDER_TYPES.SELL)) {
|
|
1540
|
-
return { price: marketPrice, type: marketType };
|
|
1541
|
-
}
|
|
1671
|
+
const now = Date.now();
|
|
1672
|
+
const live = liveFillRow(manager, now);
|
|
1673
|
+
if (live)
|
|
1674
|
+
return { price: live.price, type: live.type };
|
|
1675
|
+
const market = marketFillRow(manager, now);
|
|
1676
|
+
if (market)
|
|
1677
|
+
return { price: market.price, type: market.type };
|
|
1542
1678
|
return { price: null, type: null };
|
|
1543
1679
|
}
|
|
1544
1680
|
/**
|
|
@@ -1583,6 +1719,139 @@ export function isLastFillGuardBlocked(price, type, lastPrice, lastType) {
|
|
|
1583
1719
|
return { blocked: true, pivot, threshold: pivot };
|
|
1584
1720
|
return { blocked: false, pivot: null, threshold: null };
|
|
1585
1721
|
}
|
|
1722
|
+
/**
|
|
1723
|
+
* LAST-FILL PLACEMENT GUARD — the direction rule applied to a PLACEMENT.
|
|
1724
|
+
*
|
|
1725
|
+
* Wraps isLastFillGuardBlocked with the same pivot resolution the runtime uses
|
|
1726
|
+
* (live fill pivot, then the bot-scoped market-fill row) and adds the
|
|
1727
|
+
* SAME-LEVEL refusal: a placement within half a grid step of the pivot is a
|
|
1728
|
+
* re-place at the level that just traded, which the raw `>=`/`<=` comparison
|
|
1729
|
+
* lets through because a slot price and its execution price differ by
|
|
1730
|
+
* rounding (live: slot 1310.046339 filled at 1310.060435).
|
|
1731
|
+
*
|
|
1732
|
+
* Unlike the COW-path guard this is meant for the reconcile/startup placement
|
|
1733
|
+
* paths (create + relocation), which previously had no last-fill check at all:
|
|
1734
|
+
* a startup-style top-up could therefore re-create a BUY at the price the bot
|
|
1735
|
+
* had just bought at, or place a BUY above it (the "buying later at higher
|
|
1736
|
+
* prices" incident). Fail-open on a cold pivot (no fill yet) so a fresh grid
|
|
1737
|
+
* still builds.
|
|
1738
|
+
*
|
|
1739
|
+
* @param {Object} manager - OrderManager instance (pivot + config.incrementPercent)
|
|
1740
|
+
* @param {number} price - Target placement price
|
|
1741
|
+
* @param {string} type - ORDER_TYPES.BUY / ORDER_TYPES.SELL
|
|
1742
|
+
* @returns {{blocked: boolean, pivot: number|null, reason?: 'direction'|'same-level'}}
|
|
1743
|
+
*/
|
|
1744
|
+
export function isLastFillPlacementBlocked(manager, price, type) {
|
|
1745
|
+
const numPrice = Number(price);
|
|
1746
|
+
if (!Number.isFinite(numPrice) || numPrice <= 0)
|
|
1747
|
+
return { blocked: false, pivot: null };
|
|
1748
|
+
let reference = { price: null, type: null };
|
|
1749
|
+
try {
|
|
1750
|
+
reference = resolveLastFillGuardReference(manager);
|
|
1751
|
+
}
|
|
1752
|
+
catch {
|
|
1753
|
+
reference = { price: null, type: null };
|
|
1754
|
+
}
|
|
1755
|
+
const pivot = reference.price;
|
|
1756
|
+
const pivotType = reference.type;
|
|
1757
|
+
if (pivot == null || pivotType == null)
|
|
1758
|
+
return { blocked: false, pivot: null };
|
|
1759
|
+
if (type !== ORDER_TYPES.BUY && type !== ORDER_TYPES.SELL)
|
|
1760
|
+
return { blocked: false, pivot };
|
|
1761
|
+
try {
|
|
1762
|
+
if (MathUtils.pricesWithinHalfStep(numPrice, pivot, manager?.config?.incrementPercent)) {
|
|
1763
|
+
return { blocked: true, pivot, reason: 'same-level' };
|
|
1764
|
+
}
|
|
1765
|
+
}
|
|
1766
|
+
catch { /* fall through to the direction rule */ }
|
|
1767
|
+
const guard = isLastFillGuardBlocked(numPrice, type, pivot, pivotType);
|
|
1768
|
+
if (guard.blocked)
|
|
1769
|
+
return { blocked: true, pivot, reason: 'direction' };
|
|
1770
|
+
return { blocked: false, pivot };
|
|
1771
|
+
}
|
|
1772
|
+
/**
|
|
1773
|
+
* Are fills still UNSETTLED (arrived but not yet absorbed by the grid)?
|
|
1774
|
+
*
|
|
1775
|
+
* While this returns true the master grid still holds filled slots as ACTIVE
|
|
1776
|
+
* and the boundary has not crawled for them, so any geometry-derived plan
|
|
1777
|
+
* (window top-up, startup-style reconcile) is built on the PREVIOUS book. The
|
|
1778
|
+
* live incident: a 120s stale broadcast region deferred 12 fills, the
|
|
1779
|
+
* post-fill reconcile saw "buy 12/20" on that stale master and re-created 8
|
|
1780
|
+
* BUYs at the TOP of the un-crawled rail (1282.78 -> 1310.04), which filled
|
|
1781
|
+
* within seconds (9 buy-lower violations, 23.5% of the window's BTS).
|
|
1782
|
+
*
|
|
1783
|
+
* Accepts a bot (its `manager` is inspected) or a bare manager.
|
|
1784
|
+
*
|
|
1785
|
+
* @param {Object} source - DEXBot-like or OrderManager-like instance
|
|
1786
|
+
* @returns {{unsettled: boolean, reason: string|null}}
|
|
1787
|
+
*/
|
|
1788
|
+
export function hasUnsettledFills(source) {
|
|
1789
|
+
if (!source || typeof source !== 'object')
|
|
1790
|
+
return { unsettled: false, reason: null };
|
|
1791
|
+
const outer = source;
|
|
1792
|
+
const mgr = (outer.manager && typeof outer.manager === 'object'
|
|
1793
|
+
? outer.manager
|
|
1794
|
+
: outer);
|
|
1795
|
+
try {
|
|
1796
|
+
const queued = Array.isArray(outer._incomingFillQueue) ? outer._incomingFillQueue.length : 0;
|
|
1797
|
+
if (queued > 0)
|
|
1798
|
+
return { unsettled: true, reason: `${queued} fill(s) queued for processing` };
|
|
1799
|
+
if (outer._deferredFillsPending === true)
|
|
1800
|
+
return { unsettled: true, reason: 'deferred fill drain pending' };
|
|
1801
|
+
const inFlight = Number(mgr._fillBatchInFlight);
|
|
1802
|
+
if (Number.isFinite(inFlight) && inFlight > 0)
|
|
1803
|
+
return { unsettled: true, reason: 'fill batch in flight' };
|
|
1804
|
+
const owed = Array.isArray(mgr._pendingFillCrawls) ? mgr._pendingFillCrawls.length : 0;
|
|
1805
|
+
if (owed > 0)
|
|
1806
|
+
return { unsettled: true, reason: `${owed} owed boundary crawl(s)` };
|
|
1807
|
+
}
|
|
1808
|
+
catch {
|
|
1809
|
+
return { unsettled: false, reason: null };
|
|
1810
|
+
}
|
|
1811
|
+
return { unsettled: false, reason: null };
|
|
1812
|
+
}
|
|
1813
|
+
/**
|
|
1814
|
+
* Age of the most recent bot-level pipeline raise.
|
|
1815
|
+
*
|
|
1816
|
+
* The two counters (`_batchInFlight`, `_recoverySyncInFlight`) each carry a
|
|
1817
|
+
* `*StartedAt` stamp maintained by their raise site; a nested raise (a COW
|
|
1818
|
+
* re-plan, a second recovery frame) refreshes it, so this reports how long ago
|
|
1819
|
+
* the pipeline LAST showed activity, not how long the oldest frame has been
|
|
1820
|
+
* open. That is the question a stale-region watchdog needs: a recent raise is
|
|
1821
|
+
* evidence of progress. Lives here (next to
|
|
1822
|
+
* `hasUnsettledFills`, the same bot/manager field-peeking contract) so the
|
|
1823
|
+
* maintenance tick and the fill consumer cannot disagree about it.
|
|
1824
|
+
*
|
|
1825
|
+
* @param {Object} source - DEXBot-like (or OrderManager-like) instance
|
|
1826
|
+
* @param {number} [nowMs] - Clock override (tests)
|
|
1827
|
+
* @returns {number} Milliseconds since the most recent raise, 0 when nothing
|
|
1828
|
+
* is in flight or the stamp is missing
|
|
1829
|
+
*/
|
|
1830
|
+
export function getPipelineInFlightAgeMs(source, nowMs) {
|
|
1831
|
+
if (!source || typeof source !== 'object')
|
|
1832
|
+
return 0;
|
|
1833
|
+
const outer = source;
|
|
1834
|
+
const bot = (outer.manager && typeof outer.manager === 'object') ? outer : source;
|
|
1835
|
+
const fields = bot;
|
|
1836
|
+
const now = Number.isFinite(Number(nowMs)) ? Number(nowMs) : Date.now();
|
|
1837
|
+
const pairs = [
|
|
1838
|
+
['_batchInFlight', '_batchInFlightStartedAt'],
|
|
1839
|
+
['_recoverySyncInFlight', '_recoverySyncInFlightStartedAt'],
|
|
1840
|
+
];
|
|
1841
|
+
let oldest = -1;
|
|
1842
|
+
for (const [countKey, startedKey] of pairs) {
|
|
1843
|
+
const count = Number(fields[countKey]) || 0;
|
|
1844
|
+
if (count <= 0)
|
|
1845
|
+
continue;
|
|
1846
|
+
const startedAt = Number(fields[startedKey]) || 0;
|
|
1847
|
+
if (!startedAt)
|
|
1848
|
+
continue;
|
|
1849
|
+
const age = Math.max(0, now - startedAt);
|
|
1850
|
+
if (age > oldest)
|
|
1851
|
+
oldest = age;
|
|
1852
|
+
}
|
|
1853
|
+
return oldest > 0 ? oldest : 0;
|
|
1854
|
+
}
|
|
1586
1855
|
/**
|
|
1587
1856
|
* Restore the persisted bot-scoped last-market-fill reference into the
|
|
1588
1857
|
* manager. Deliberately independent of the boundary and the genesis: this is
|
|
@@ -1615,9 +1884,8 @@ export function restoreLastMarketFill(manager, persisted, options = {}) {
|
|
|
1615
1884
|
catch { /* best-effort */ }
|
|
1616
1885
|
};
|
|
1617
1886
|
const now = Number.isFinite(options?.now) ? Number(options.now) : Date.now();
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
manager.logger?.log?.(`[SPREAD-CORRECTION] Persisted last-market-fill expired (age ${Math.round((now - row.fillsAt) / 1000)}s > TTL ${Math.round(ttl / 1000)}s) — reference dropped`, 'info');
|
|
1887
|
+
if (isFillReferenceExpired(row.fillsAt, now)) {
|
|
1888
|
+
manager.logger?.log?.(`[SPREAD-CORRECTION] Persisted last-market-fill expired (age ${Math.round((now - row.fillsAt) / 1000)}s > TTL ${Math.round(lastFillPivotTtlMs() / 1000)}s) — reference dropped`, 'info');
|
|
1621
1889
|
dropPersistedRow();
|
|
1622
1890
|
return false;
|
|
1623
1891
|
}
|
|
@@ -1643,21 +1911,32 @@ export function restoreLastMarketFill(manager, persisted, options = {}) {
|
|
|
1643
1911
|
* rewritten: an off-new-ladder value is handled by the same one-increment
|
|
1644
1912
|
* `resolveOnGridPivot` rule every other probe uses.
|
|
1645
1913
|
*
|
|
1914
|
+
* The timestamp comes from the SAME row as the price (see
|
|
1915
|
+
* resolveFillProvenancedRow). Reading `_lastFilledAt` unconditionally paired a
|
|
1916
|
+
* market-row price with a book-seed clock: a book seed writes `_lastFilledAt`
|
|
1917
|
+
* but never the market row, and `setLastFillPivot(..., 'fill')` then re-stamps
|
|
1918
|
+
* `_lastMarketFillAt` with that newer time — refreshing the 24h lifetime on
|
|
1919
|
+
* every rebuild until the row never expires.
|
|
1920
|
+
*
|
|
1921
|
+
* EXPIRED ROWS DO NOT RE-ARM (resolveFillProvenancedRow drops them), so the
|
|
1922
|
+
* documented pivot lifetime holds with or without a restart.
|
|
1923
|
+
*
|
|
1646
1924
|
* @param {Object} manager - OrderManager instance
|
|
1647
1925
|
* @returns {boolean} True when a pivot was re-armed
|
|
1648
1926
|
*/
|
|
1649
1927
|
export function rearmLastFillPivotFromMarketFill(manager) {
|
|
1650
1928
|
if (!manager)
|
|
1651
1929
|
return false;
|
|
1652
|
-
const
|
|
1653
|
-
|
|
1654
|
-
const type = ref?.type;
|
|
1655
|
-
if (!Number.isFinite(price) || price <= 0 || (type !== ORDER_TYPES.BUY && type !== ORDER_TYPES.SELL))
|
|
1930
|
+
const row = resolveFillProvenancedRow(manager);
|
|
1931
|
+
if (!row)
|
|
1656
1932
|
return false;
|
|
1657
|
-
|
|
1658
|
-
|
|
1933
|
+
// A source with no clock cannot be aged; stamping `now` keeps the re-armed
|
|
1934
|
+
// pivot inside the same lifetime from this rebuild on (production rows
|
|
1935
|
+
// always carry the fill's own `fillsAt`).
|
|
1936
|
+
const at = row.fillsAt != null ? row.fillsAt : Date.now();
|
|
1937
|
+
if (!setLastFillPivot(manager, row.type, row.price, 'fill', at))
|
|
1659
1938
|
return false;
|
|
1660
|
-
manager.logger?.log?.(`[LAST-FILL-GUARD] Re-armed on the new generation: ${price}(${type}) from the last real fill ` +
|
|
1939
|
+
manager.logger?.log?.(`[LAST-FILL-GUARD] Re-armed on the new generation: ${row.price}(${row.type}) from the last real fill ` +
|
|
1661
1940
|
`(age ${Math.max(0, Math.round((Date.now() - at) / 1000))}s) — the guard survives a rebuild by design`, 'info');
|
|
1662
1941
|
return true;
|
|
1663
1942
|
}
|
|
@@ -1856,10 +2135,11 @@ export function restoreLastFillPivot(manager, persisted, options = {}) {
|
|
|
1856
2135
|
}
|
|
1857
2136
|
catch { /* best-effort */ }
|
|
1858
2137
|
};
|
|
1859
|
-
// TTL first: an expired pivot is dropped, not re-armed
|
|
2138
|
+
// TTL first: an expired pivot is dropped, not re-armed (one shared bound
|
|
2139
|
+
// with the read-time readers and the market-row restore).
|
|
1860
2140
|
const now = Number.isFinite(options?.now) ? Number(options.now) : Date.now();
|
|
1861
|
-
if (
|
|
1862
|
-
manager.logger?.log?.(`[LAST-FILL-GUARD] Persisted pivot expired (age ${Math.round((now - row.fillsAt) / 1000)}s > TTL ${Math.round(
|
|
2141
|
+
if (isFillReferenceExpired(row.fillsAt, now)) {
|
|
2142
|
+
manager.logger?.log?.(`[LAST-FILL-GUARD] Persisted pivot expired (age ${Math.round((now - row.fillsAt) / 1000)}s > TTL ${Math.round(lastFillPivotTtlMs() / 1000)}s) — guard re-arms on the next fill`, 'info');
|
|
1863
2143
|
dropPersistedRow();
|
|
1864
2144
|
return false;
|
|
1865
2145
|
}
|
|
@@ -2515,11 +2795,16 @@ export async function withBlockchainRetry(fn, label, options) {
|
|
|
2515
2795
|
}
|
|
2516
2796
|
catch (err) {
|
|
2517
2797
|
lastError = err;
|
|
2518
|
-
// Report node failure so NodeManager can blacklist and trigger failover
|
|
2798
|
+
// Report node failure so NodeManager can blacklist and trigger failover.
|
|
2799
|
+
// Attribute it to the node that actually served the attempt —
|
|
2800
|
+
// getBestNode() is the top *candidate*, which is frequently a
|
|
2801
|
+
// different node than the connected one, so using it punished an
|
|
2802
|
+
// innocent node and left the failing one in place.
|
|
2519
2803
|
try {
|
|
2520
|
-
const { getNodeManager } = require('../../bitshares_client');
|
|
2804
|
+
const { getNodeManager, getActiveNodeUrl } = require('../../bitshares_client');
|
|
2521
2805
|
const nodeManager = getNodeManager?.();
|
|
2522
|
-
const nodeUrl =
|
|
2806
|
+
const nodeUrl = (typeof getActiveNodeUrl === 'function' ? getActiveNodeUrl() : null)
|
|
2807
|
+
|| nodeManager?.getBestNode?.();
|
|
2523
2808
|
if (nodeUrl && typeof nodeManager.reportNodeFailure === 'function') {
|
|
2524
2809
|
nodeManager.reportNodeFailure(nodeUrl, getErrorMessage(err), 'blockchain-op');
|
|
2525
2810
|
}
|
|
@@ -2533,9 +2818,11 @@ export async function withBlockchainRetry(fn, label, options) {
|
|
|
2533
2818
|
}
|
|
2534
2819
|
// All retries exhausted — force-switch to a different node and retry once more
|
|
2535
2820
|
try {
|
|
2536
|
-
const { getNodeManager, reconnectForCycle } = require('../../bitshares_client');
|
|
2821
|
+
const { getNodeManager, reconnectForCycle, getActiveNodeUrl } = require('../../bitshares_client');
|
|
2537
2822
|
const nodeManager = getNodeManager?.();
|
|
2538
|
-
|
|
2823
|
+
// Blacklist the node the operation ran on, not the best candidate.
|
|
2824
|
+
const failedNode = (typeof getActiveNodeUrl === 'function' ? getActiveNodeUrl() : null)
|
|
2825
|
+
|| nodeManager?.getBestNode?.();
|
|
2539
2826
|
if (failedNode && typeof nodeManager.blacklistNode === 'function') {
|
|
2540
2827
|
nodeManager.blacklistNode(failedNode);
|
|
2541
2828
|
logger?.log?.(`${label}: blacklisted node ${failedNode.substring(0, 40)}... after ${maxRetries} failed attempts. Switching nodes...`, 'warn');
|
|
@@ -224,7 +224,13 @@ declare function hasExecutableActions(rebalanceResult: unknown): boolean;
|
|
|
224
224
|
* with {chainOrderId, price, size, type} to check beyond the grid
|
|
225
225
|
* @returns {{isValid: boolean, violations: Array<Object>, violatingTargetIds: Set<string>}} Validation result
|
|
226
226
|
*/
|
|
227
|
-
declare function validateCreateTargetSlots(actions: CowAction[], orders: Map<string, ManagedOrder>, _assets?: AssetPair | null, chainOrderCandidates?: UnmatchedChainOrder[], incrementPercent?: number | null
|
|
227
|
+
declare function validateCreateTargetSlots(actions: CowAction[], orders: Map<string, ManagedOrder>, _assets?: AssetPair | null, chainOrderCandidates?: UnmatchedChainOrder[], incrementPercent?: number | null, pendingBroadcasts?: Array<{
|
|
228
|
+
slotId?: unknown;
|
|
229
|
+
price?: unknown;
|
|
230
|
+
size?: unknown;
|
|
231
|
+
type?: unknown;
|
|
232
|
+
fingerprint?: unknown;
|
|
233
|
+
}>): {
|
|
228
234
|
isValid: boolean;
|
|
229
235
|
violations: CreateTargetViolation[];
|
|
230
236
|
violatingTargetIds: Set<string>;
|
|
@@ -40,6 +40,7 @@ import * as Format from '../format.js';
|
|
|
40
40
|
import { ORDER_STATES, ORDER_TYPES, COW_ACTIONS, COW_ORIGINS, GRID_LIMITS } from '../../constants.js';
|
|
41
41
|
import { floatToBlockchainInt, blockchainToFloat, getPrecisionSlack, getPrecisionByOrderType, getDoubleDustThreshold, getSellStartIdx, isSlotIndexInGapBand, isEvacuationRotationAllowed, clamp, pricesOnSameLevel, sameGridPrice } from './math.js';
|
|
42
42
|
import { parseSlotIndex } from './slot.js';
|
|
43
|
+
import { isSlotCreateUncertain } from '../lifecycle.js';
|
|
43
44
|
import { isOrderOnChain, isPhantomOrder, convertToSpreadPlaceholder, detectGapEvacuationCandidates, updateGapEvacuationStreaks, isNonBlockingUnmatchedOrder } from './order.js';
|
|
44
45
|
const { isValidNumber, toFiniteNumber } = Format;
|
|
45
46
|
// Pre-computed valid sets
|
|
@@ -454,6 +455,15 @@ function reconcileGrid(masterGrid, targetGrid, targetBoundary, options = {}) {
|
|
|
454
455
|
for (const [id, targetOrder] of targetGrid) {
|
|
455
456
|
const masterOrder = masterGrid.get(id);
|
|
456
457
|
if (!masterOrder || masterOrder.state === ORDER_STATES.VIRTUAL) {
|
|
458
|
+
// A VIRTUAL slot that still OWNS a chain order id is not a hole: the
|
|
459
|
+
// level already holds a live order (the dark-level state owned by the
|
|
460
|
+
// sync/startup revive path). Planning a CREATE here would place a
|
|
461
|
+
// second order at the same price. A genuinely stale id is cleared by
|
|
462
|
+
// sync pass 1 (`sync-clear-stale-id`), which makes the slot a plain
|
|
463
|
+
// hole again on the next cycle.
|
|
464
|
+
if (masterOrder && masterOrder.state === ORDER_STATES.VIRTUAL && masterOrder.orderId) {
|
|
465
|
+
continue;
|
|
466
|
+
}
|
|
457
467
|
// Slot is empty or virtual - check if we need to fill it
|
|
458
468
|
if (targetOrder.size > 0 && targetOrder.state === ORDER_STATES.ACTIVE) {
|
|
459
469
|
// This is a hole that needs filling
|
|
@@ -742,7 +752,7 @@ function hasExecutableActions(rebalanceResult) {
|
|
|
742
752
|
* with {chainOrderId, price, size, type} to check beyond the grid
|
|
743
753
|
* @returns {{isValid: boolean, violations: Array<Object>, violatingTargetIds: Set<string>}} Validation result
|
|
744
754
|
*/
|
|
745
|
-
function validateCreateTargetSlots(actions, orders, _assets = null, chainOrderCandidates = [], incrementPercent = null) {
|
|
755
|
+
function validateCreateTargetSlots(actions, orders, _assets = null, chainOrderCandidates = [], incrementPercent = null, pendingBroadcasts = []) {
|
|
746
756
|
const safeActions = Array.isArray(actions) ? actions : [];
|
|
747
757
|
const orderMap = orders instanceof Map ? orders : new Map();
|
|
748
758
|
const releasedSlotIds = new Set();
|
|
@@ -767,8 +777,19 @@ function validateCreateTargetSlots(actions, orders, _assets = null, chainOrderCa
|
|
|
767
777
|
if (!targetId || releasedSlotIds.has(targetId))
|
|
768
778
|
continue;
|
|
769
779
|
const current = orderMap.get(targetId);
|
|
770
|
-
//
|
|
771
|
-
|
|
780
|
+
// OCCUPANCY = the slot still owns a chain order ID, in ANY state.
|
|
781
|
+
// isOrderOnChain() (ACTIVE/PARTIAL only) was too narrow: a slot
|
|
782
|
+
// virtualised while its order was still live (phantom cleanup on a
|
|
783
|
+
// truncated read, refused-commit adoption) kept its orderId but read as
|
|
784
|
+
// "empty", so a CREATE could be planned onto a level that already held a
|
|
785
|
+
// live order (duplicate price level). A genuinely STALE id is cleared by
|
|
786
|
+
// sync pass 1 (`sync-clear-stale-id`), which turns the slot back into a
|
|
787
|
+
// plain hole on the next cycle — so this gate cannot strand a level.
|
|
788
|
+
const currentOwnsChainId = Boolean(current?.orderId);
|
|
789
|
+
// Single definition of the durable "CREATE result lost" marker
|
|
790
|
+
// (modules/order/lifecycle.ts) — this site used to read the raw flag.
|
|
791
|
+
const currentCreateUncertain = isSlotCreateUncertain(current);
|
|
792
|
+
if (current && (isOrderOnChain(current) || currentOwnsChainId)) {
|
|
772
793
|
violations.push({
|
|
773
794
|
targetId,
|
|
774
795
|
currentOrderId: current.orderId,
|
|
@@ -777,6 +798,19 @@ function validateCreateTargetSlots(actions, orders, _assets = null, chainOrderCa
|
|
|
777
798
|
reason: 'slot_occupied'
|
|
778
799
|
});
|
|
779
800
|
}
|
|
801
|
+
else if (current && currentCreateUncertain) {
|
|
802
|
+
// A CREATE whose broadcast outcome is unknown. Unlike slot_occupied
|
|
803
|
+
// this is NOT proof of a live order, so it must not abort the whole
|
|
804
|
+
// batch (runPreBroadcastGuards only hard-aborts on slot_occupied) —
|
|
805
|
+
// it only withholds this CREATE until the uncertainty resolves.
|
|
806
|
+
violations.push({
|
|
807
|
+
targetId,
|
|
808
|
+
currentOrderId: current.orderId,
|
|
809
|
+
currentType: current.type,
|
|
810
|
+
currentState: current.state,
|
|
811
|
+
reason: 'slot_create_uncertain'
|
|
812
|
+
});
|
|
813
|
+
}
|
|
780
814
|
if (action.order?.price != null && action.order?.type != null) {
|
|
781
815
|
// Key on the BROADCAST price (action.order.price — the price
|
|
782
816
|
// actually placed on chain), never the live slot price: layer-5
|
|
@@ -835,6 +869,49 @@ function validateCreateTargetSlots(actions, orders, _assets = null, chainOrderCa
|
|
|
835
869
|
}
|
|
836
870
|
}
|
|
837
871
|
}
|
|
872
|
+
// Pending-broadcast collisions: an uncertain CREATE from an earlier
|
|
873
|
+
// batch already targets this slot/price. Its orderId is not visible yet
|
|
874
|
+
// (the broadcast outcome is unknown), so the chain-orphan layers above
|
|
875
|
+
// cannot see it. Landing a second CREATE at the same level duplicates
|
|
876
|
+
// the order. Check BOTH the exact slot id and the price level, because
|
|
877
|
+
// a rebuilt grid can re-map a price onto a different slot id.
|
|
878
|
+
if (pendingBroadcasts.length > 0) {
|
|
879
|
+
const pendingSlotIds = new Set();
|
|
880
|
+
for (const p of pendingBroadcasts) {
|
|
881
|
+
if (p?.slotId != null && String(p.slotId).length > 0)
|
|
882
|
+
pendingSlotIds.add(String(p.slotId));
|
|
883
|
+
}
|
|
884
|
+
for (const entry of createEntries) {
|
|
885
|
+
if (pendingSlotIds.has(entry.targetId)) {
|
|
886
|
+
violations.push({
|
|
887
|
+
targetId: entry.targetId,
|
|
888
|
+
currentOrderId: `pending:${entry.targetId}`,
|
|
889
|
+
currentType: entry.type,
|
|
890
|
+
currentState: 'PENDING_BROADCAST',
|
|
891
|
+
reason: 'pending_broadcast_collision'
|
|
892
|
+
});
|
|
893
|
+
continue;
|
|
894
|
+
}
|
|
895
|
+
if (!_assets)
|
|
896
|
+
continue;
|
|
897
|
+
for (const p of pendingBroadcasts) {
|
|
898
|
+
if (p?.price == null)
|
|
899
|
+
continue;
|
|
900
|
+
if (p.type && entry.type && p.type !== entry.type)
|
|
901
|
+
continue;
|
|
902
|
+
if (pricesOnSameLevel(Number(p.price), Number(p.size) || 0, p.type, Number(entry.price), Number(entry.size) || 0, entry.type, _assets, incrementPercent)) {
|
|
903
|
+
violations.push({
|
|
904
|
+
targetId: entry.targetId,
|
|
905
|
+
currentOrderId: `pending:${String(p.fingerprint ?? p.slotId ?? 'unknown')}`,
|
|
906
|
+
currentType: entry.type,
|
|
907
|
+
currentState: 'PENDING_BROADCAST',
|
|
908
|
+
reason: 'pending_broadcast_collision'
|
|
909
|
+
});
|
|
910
|
+
break;
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
}
|
|
838
915
|
// Same-batch duplicate slot
|
|
839
916
|
const seen = new Set();
|
|
840
917
|
for (const entry of createEntries) {
|