dexbot 1.7.0 → 1.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/CHANGELOG.md +70 -0
  2. package/README.md +3 -3
  3. package/analysis/README.md +33 -13
  4. package/claw/package.json +1 -1
  5. package/claw/runtimes/openclaw-plugin/openclaw.plugin.json +1 -1
  6. package/claw/runtimes/openclaw-plugin/package.json +1 -1
  7. package/dist/analysis/account_resolver.js +5 -6
  8. package/dist/analysis/analyze_liquidity.js +15 -4
  9. package/dist/analysis/bot_key_utils.d.ts +44 -1
  10. package/dist/analysis/bot_key_utils.js +76 -7
  11. package/dist/analysis/fills_cache.js +2 -1
  12. package/dist/analysis/grid_correction_check.d.ts +1 -19
  13. package/dist/analysis/grid_correction_check.js +12 -30
  14. package/dist/analysis/liquidity/liquidity_chart_generator.js +190 -20
  15. package/dist/analysis/liquidity/liquidity_depth_model.d.ts +41 -16
  16. package/dist/analysis/liquidity/liquidity_depth_model.js +49 -21
  17. package/dist/analysis/liquidity/liquidity_view.d.ts +15 -0
  18. package/dist/analysis/liquidity/liquidity_view.js +11 -0
  19. package/dist/analysis/resolve_bot_accounts.js +7 -6
  20. package/dist/analysis/trade_profitability.js +2 -1
  21. package/dist/credential-daemon.js +13 -2
  22. package/dist/dexbot.js +4 -4
  23. package/dist/modules/account_bots.d.ts +12 -1
  24. package/dist/modules/account_bots.js +7 -5
  25. package/dist/modules/account_orders.d.ts +12 -0
  26. package/dist/modules/account_orders.js +30 -0
  27. package/dist/modules/bitshares-native/transport.js +24 -6
  28. package/dist/modules/bitshares_client.d.ts +24 -6
  29. package/dist/modules/bitshares_client.js +44 -7
  30. package/dist/modules/bot_settings.d.ts +2 -0
  31. package/dist/modules/constants.d.ts +28 -3
  32. package/dist/modules/constants.js +79 -10
  33. package/dist/modules/dexbot_class.d.ts +23 -6
  34. package/dist/modules/dexbot_class.js +44 -0
  35. package/dist/modules/dexbot_cow_runtime.d.ts +128 -8
  36. package/dist/modules/dexbot_cow_runtime.js +688 -188
  37. package/dist/modules/dexbot_fill_runtime.js +124 -35
  38. package/dist/modules/dexbot_maintenance_runtime.d.ts +1 -1
  39. package/dist/modules/dexbot_maintenance_runtime.js +259 -79
  40. package/dist/modules/dexbot_startup_runtime.js +32 -20
  41. package/dist/modules/dexbot_state_recovery.d.ts +33 -1
  42. package/dist/modules/dexbot_state_recovery.js +167 -5
  43. package/dist/modules/node_connect_policy.d.ts +8 -1
  44. package/dist/modules/node_connect_policy.js +18 -2
  45. package/dist/modules/node_flap_score.d.ts +87 -0
  46. package/dist/modules/node_flap_score.js +124 -0
  47. package/dist/modules/node_health_cache.d.ts +40 -3
  48. package/dist/modules/node_health_cache.js +66 -20
  49. package/dist/modules/node_manager.d.ts +101 -19
  50. package/dist/modules/node_manager.js +255 -65
  51. package/dist/modules/order/accounting.d.ts +9 -4
  52. package/dist/modules/order/accounting.js +131 -19
  53. package/dist/modules/order/async_lock.d.ts +7 -1
  54. package/dist/modules/order/async_lock.js +7 -1
  55. package/dist/modules/order/export.js +25 -18
  56. package/dist/modules/order/fill_ledger.d.ts +99 -0
  57. package/dist/modules/order/fill_ledger.js +103 -0
  58. package/dist/modules/order/format.d.ts +24 -10
  59. package/dist/modules/order/format.js +30 -10
  60. package/dist/modules/order/grid.d.ts +75 -18
  61. package/dist/modules/order/grid.js +289 -72
  62. package/dist/modules/order/grid_reconcile.d.ts +5 -1
  63. package/dist/modules/order/grid_reconcile.js +333 -210
  64. package/dist/modules/order/grid_reconcile_internal.d.ts +38 -2
  65. package/dist/modules/order/grid_reconcile_internal.js +223 -91
  66. package/dist/modules/order/index.d.ts +18 -5
  67. package/dist/modules/order/index.js +5 -5
  68. package/dist/modules/order/lifecycle.d.ts +353 -0
  69. package/dist/modules/order/lifecycle.js +1109 -0
  70. package/dist/modules/order/manager.d.ts +277 -20
  71. package/dist/modules/order/manager.js +680 -64
  72. package/dist/modules/order/placement_lock.d.ts +102 -0
  73. package/dist/modules/order/placement_lock.js +222 -0
  74. package/dist/modules/order/strategy.js +13 -1
  75. package/dist/modules/order/sync_engine.d.ts +74 -114
  76. package/dist/modules/order/sync_engine.js +254 -198
  77. package/dist/modules/order/utils/math.d.ts +29 -4
  78. package/dist/modules/order/utils/math.js +61 -4
  79. package/dist/modules/order/utils/order.d.ts +106 -37
  80. package/dist/modules/order/utils/order.js +256 -81
  81. package/dist/modules/order/utils/system.d.ts +99 -6
  82. package/dist/modules/order/utils/system.js +249 -35
  83. package/dist/modules/order/utils/validate.d.ts +7 -1
  84. package/dist/modules/order/utils/validate.js +48 -2
  85. package/dist/modules/outcome.d.ts +54 -0
  86. package/dist/modules/outcome.js +51 -0
  87. package/dist/modules/settings_merge.d.ts +4 -0
  88. package/dist/modules/settings_merge.js +9 -0
  89. package/dist/modules/timing_policy.d.ts +82 -0
  90. package/dist/modules/timing_policy.js +244 -0
  91. package/dist/modules/types.d.ts +149 -22
  92. package/dist/scripts/analyze-credit.js +54 -17
  93. package/dist/scripts/analyze-orders.d.ts +4 -2
  94. package/dist/scripts/analyze-orders.js +113 -34
  95. package/dist/scripts/chart_targets.js +11 -15
  96. package/dist/scripts/test-credit-renewal.js +12 -5
  97. package/docs/COW_INVARIANTS.md +71 -8
  98. package/docs/DEXBOT_COMPARISON.md +6 -6
  99. package/docs/EVOLUTION.md +10 -8
  100. package/docs/FUND_MOVEMENT_AND_ACCOUNTING.md +1 -1
  101. package/docs/GRID_PRICE_INVARIANT.md +1 -1
  102. package/docs/GRID_RECONCILE.md +18 -13
  103. package/docs/LOGGING.md +25 -1
  104. package/docs/ORDER_ENGINE_POST_1.0_RETROSPECTIVE.md +183 -26
  105. package/docs/README.md +23 -2
  106. package/docs/WORKFLOW.md +9 -3
  107. package/docs/architecture.md +68 -13
  108. package/docs/developer_guide.md +25 -8
  109. package/modules/README.md +6 -0
  110. package/package.json +3 -3
  111. package/scripts/README.md +16 -0
  112. package/scripts/pdev.sh +14 -1
  113. package/scripts/pmain.sh +23 -8
  114. package/scripts/ptest.sh +7 -0
  115. package/tests/README.md +30 -1
@@ -0,0 +1,102 @@
1
+ /**
2
+ * modules/order/placement_lock.ts - Exclusive placement region
3
+ *
4
+ * One mutex shared by EVERY chain-mutating placement path (COW batches,
5
+ * startup reconcile Phase 2, correction drains). The broadcast refcount in
6
+ * OrderManager (startBroadcasting/stopBroadcasting) only tells fills to stand
7
+ * aside; it does NOT stop two placement paths from broadcasting at the same
8
+ * time. When two paths plan from the same chain snapshot but from different
9
+ * masters (one update-first, one create-first) both generations can land at
10
+ * identical prices — the duplicate-order incident this lock closes.
11
+ *
12
+ * Design notes:
13
+ * - Token based (acquire returns a release lease) so callers can hold the
14
+ * region across a multi-chunk broadcast without wrapping the whole method
15
+ * in a callback. The two token-level holders (COW broadcast body, startup
16
+ * reconcile Phase 1+2) own their lease explicitly; every other path goes
17
+ * through {@link withPlacement}, which is the one acquire/release contract.
18
+ * - NOT reentrant. Nested work must NOT acquire again: a caller that already
19
+ * holds the region hands its lease to the specific inner operation (the COW
20
+ * pre-batch drain does this via `correctAllPriceMismatches({ placementLease })`),
21
+ * and that inner operation verifies it with
22
+ * {@link PlacementCapableManager.isPlacementLeaseCurrent} before mutating —
23
+ * `isPlacementActive()` is NOT a substitute, it is true for any holder.
24
+ * - FIFO queue with a bounded wait. A waiter that times out or observes
25
+ * shutdown gets `null` and MUST abort its batch instead of broadcasting.
26
+ */
27
+ export interface PlacementLease {
28
+ kind: string;
29
+ release(): void;
30
+ }
31
+ export interface PlacementAcquireOptions {
32
+ timeoutMs?: number;
33
+ isShuttingDown?: () => boolean;
34
+ }
35
+ /**
36
+ * Minimal manager surface needed by {@link withPlacement}. Kept structural so
37
+ * callers in utils/ can use it without importing OrderManager (cycle safety).
38
+ */
39
+ export interface PlacementCapableManager {
40
+ acquirePlacement?(kind: string, options?: PlacementAcquireOptions): Promise<PlacementLease | null>;
41
+ tryAcquirePlacement?(kind: string): PlacementLease | null;
42
+ isPlacementActive?(): boolean;
43
+ /** Whether this lease still owns the region (see leaseTokens). */
44
+ isPlacementLeaseCurrent?(lease: PlacementLease | null | undefined): boolean;
45
+ placementKind?(): string | null;
46
+ }
47
+ /**
48
+ * Run `fn` inside the exclusive placement region.
49
+ *
50
+ * This is the single entry point every chain-mutating placement path should
51
+ * use (COW broadcast, startup reconcile Phase 2, correction drains, dust /
52
+ * residual cancels, pool swaps). It centralizes the acquire/release contract
53
+ * so a path cannot forget to release.
54
+ *
55
+ * Nesting is NOT expressed here: a caller that already holds the region passes
56
+ * its lease to the specific inner operation, which then checks
57
+ * `isPlacementLeaseCurrent()` before mutating (see the placement_lock header).
58
+ *
59
+ * @returns `null` when the region could not be acquired (timeout/shutdown);
60
+ * callers must abort/defer their broadcast in that case.
61
+ */
62
+ export declare function withPlacement<T>(manager: PlacementCapableManager | null | undefined, kind: string, fn: (lease: PlacementLease) => Promise<T>, options?: PlacementAcquireOptions): Promise<T | null>;
63
+ interface PlacementWaiter {
64
+ kind: string;
65
+ resolve: (lease: PlacementLease | null) => void;
66
+ timer: ReturnType<typeof setTimeout> | null;
67
+ }
68
+ declare class PlacementLock {
69
+ _holder: string | null;
70
+ _since: number;
71
+ _waiters: PlacementWaiter[];
72
+ _token: number;
73
+ _onRegionEnd: (() => void) | null;
74
+ constructor(onRegionEnd?: () => void);
75
+ _notifyRegionEnd(): void;
76
+ isHeld(): boolean;
77
+ kind(): string | null;
78
+ heldForMs(): number;
79
+ queueLength(): number;
80
+ /**
81
+ * Acquire the exclusive placement region.
82
+ * Resolves with a lease, or null when the wait timed out / shutdown began.
83
+ */
84
+ acquire(kind: string, options?: PlacementAcquireOptions): Promise<PlacementLease | null>;
85
+ /**
86
+ * Non-blocking acquire used by paths that must defer instead of queue.
87
+ * @returns a lease when the region was free, otherwise null.
88
+ */
89
+ tryAcquire(kind: string): PlacementLease | null;
90
+ /**
91
+ * Force-release the region on shutdown: every waiter resolves null and the
92
+ * current holder's lease becomes a no-op. A hung broadcast can never block
93
+ * process exit on this lock.
94
+ */
95
+ forceRelease(): void;
96
+ /** Whether the given lease is still the one that owns the region. */
97
+ isLeaseCurrent(lease: PlacementLease | null | undefined): boolean;
98
+ _makeLease(kind: string, token: number): PlacementLease;
99
+ _release(token: number): void;
100
+ }
101
+ export default PlacementLock;
102
+ //# sourceMappingURL=placement_lock.d.ts.map
@@ -0,0 +1,222 @@
1
+ /**
2
+ * modules/order/placement_lock.ts - Exclusive placement region
3
+ *
4
+ * One mutex shared by EVERY chain-mutating placement path (COW batches,
5
+ * startup reconcile Phase 2, correction drains). The broadcast refcount in
6
+ * OrderManager (startBroadcasting/stopBroadcasting) only tells fills to stand
7
+ * aside; it does NOT stop two placement paths from broadcasting at the same
8
+ * time. When two paths plan from the same chain snapshot but from different
9
+ * masters (one update-first, one create-first) both generations can land at
10
+ * identical prices — the duplicate-order incident this lock closes.
11
+ *
12
+ * Design notes:
13
+ * - Token based (acquire returns a release lease) so callers can hold the
14
+ * region across a multi-chunk broadcast without wrapping the whole method
15
+ * in a callback. The two token-level holders (COW broadcast body, startup
16
+ * reconcile Phase 1+2) own their lease explicitly; every other path goes
17
+ * through {@link withPlacement}, which is the one acquire/release contract.
18
+ * - NOT reentrant. Nested work must NOT acquire again: a caller that already
19
+ * holds the region hands its lease to the specific inner operation (the COW
20
+ * pre-batch drain does this via `correctAllPriceMismatches({ placementLease })`),
21
+ * and that inner operation verifies it with
22
+ * {@link PlacementCapableManager.isPlacementLeaseCurrent} before mutating —
23
+ * `isPlacementActive()` is NOT a substitute, it is true for any holder.
24
+ * - FIFO queue with a bounded wait. A waiter that times out or observes
25
+ * shutdown gets `null` and MUST abort its batch instead of broadcasting.
26
+ */
27
+ /**
28
+ * Lease → lock token registry.
29
+ *
30
+ * A lease object is all a nested caller holds, so it cannot prove ownership by
31
+ * `isPlacementActive()` (any holder satisfies that). The token the lock stamped
32
+ * into the lease at hand-out time is kept here out-of-band (WeakMap), which
33
+ * keeps the public lease shape unchanged while letting the lock answer "is this
34
+ * still the lease that owns the region?".
35
+ */
36
+ const leaseTokens = new WeakMap();
37
+ /** Used when the caller has no lock (dry-run mocks / browser shims). */
38
+ const NOOP_PLACEMENT_LEASE = { kind: 'noop', release: () => { } };
39
+ /**
40
+ * Run `fn` inside the exclusive placement region.
41
+ *
42
+ * This is the single entry point every chain-mutating placement path should
43
+ * use (COW broadcast, startup reconcile Phase 2, correction drains, dust /
44
+ * residual cancels, pool swaps). It centralizes the acquire/release contract
45
+ * so a path cannot forget to release.
46
+ *
47
+ * Nesting is NOT expressed here: a caller that already holds the region passes
48
+ * its lease to the specific inner operation, which then checks
49
+ * `isPlacementLeaseCurrent()` before mutating (see the placement_lock header).
50
+ *
51
+ * @returns `null` when the region could not be acquired (timeout/shutdown);
52
+ * callers must abort/defer their broadcast in that case.
53
+ */
54
+ export async function withPlacement(manager, kind, fn, options = {}) {
55
+ if (!manager || typeof manager.acquirePlacement !== 'function') {
56
+ return await fn(NOOP_PLACEMENT_LEASE);
57
+ }
58
+ const acquireOptions = {
59
+ ...(options.timeoutMs != null ? { timeoutMs: options.timeoutMs } : {}),
60
+ ...(options.isShuttingDown ? { isShuttingDown: options.isShuttingDown } : {}),
61
+ };
62
+ const lease = await manager.acquirePlacement(kind, acquireOptions);
63
+ if (!lease)
64
+ return null;
65
+ try {
66
+ return await fn(lease);
67
+ }
68
+ finally {
69
+ lease.release();
70
+ }
71
+ }
72
+ class PlacementLock {
73
+ _holder;
74
+ _since;
75
+ _waiters;
76
+ _token;
77
+ _onRegionEnd;
78
+ constructor(onRegionEnd) {
79
+ this._holder = null;
80
+ this._since = 0;
81
+ this._waiters = [];
82
+ this._token = 0;
83
+ this._onRegionEnd = typeof onRegionEnd === 'function' ? onRegionEnd : null;
84
+ }
85
+ _notifyRegionEnd() {
86
+ if (!this._onRegionEnd)
87
+ return;
88
+ try {
89
+ this._onRegionEnd();
90
+ }
91
+ catch { /* listener errors must never escape a release */ }
92
+ }
93
+ isHeld() {
94
+ return this._holder !== null;
95
+ }
96
+ kind() {
97
+ return this._holder;
98
+ }
99
+ heldForMs() {
100
+ return this._holder !== null && this._since > 0 ? Date.now() - this._since : 0;
101
+ }
102
+ queueLength() {
103
+ return this._waiters.length;
104
+ }
105
+ /**
106
+ * Acquire the exclusive placement region.
107
+ * Resolves with a lease, or null when the wait timed out / shutdown began.
108
+ */
109
+ async acquire(kind, options = {}) {
110
+ const timeoutMs = Number.isFinite(Number(options.timeoutMs)) && Number(options.timeoutMs) > 0
111
+ ? Number(options.timeoutMs)
112
+ : 30000;
113
+ const isShuttingDown = typeof options.isShuttingDown === 'function'
114
+ ? options.isShuttingDown
115
+ : () => false;
116
+ if (isShuttingDown())
117
+ return null;
118
+ if (this._holder === null) {
119
+ this._holder = kind;
120
+ this._since = Date.now();
121
+ this._token += 1;
122
+ return this._makeLease(kind, this._token);
123
+ }
124
+ return await new Promise((resolve) => {
125
+ const waiter = { kind, resolve, timer: null };
126
+ waiter.timer = setTimeout(() => {
127
+ const idx = this._waiters.indexOf(waiter);
128
+ if (idx >= 0)
129
+ this._waiters.splice(idx, 1);
130
+ resolve(null);
131
+ }, timeoutMs);
132
+ this._waiters.push(waiter);
133
+ if (isShuttingDown()) {
134
+ const idx = this._waiters.indexOf(waiter);
135
+ if (idx >= 0)
136
+ this._waiters.splice(idx, 1);
137
+ if (waiter.timer)
138
+ clearTimeout(waiter.timer);
139
+ resolve(null);
140
+ }
141
+ });
142
+ }
143
+ /**
144
+ * Non-blocking acquire used by paths that must defer instead of queue.
145
+ * @returns a lease when the region was free, otherwise null.
146
+ */
147
+ tryAcquire(kind) {
148
+ // Fairness: never barge ahead of waiters queued behind the holder.
149
+ // Otherwise a rapid try-acquire loop can starve a bounded waiter until
150
+ // its timeout even though the region is momentarily free.
151
+ if (this._holder !== null || this._waiters.length > 0)
152
+ return null;
153
+ this._holder = kind;
154
+ this._since = Date.now();
155
+ this._token += 1;
156
+ return this._makeLease(kind, this._token);
157
+ }
158
+ /**
159
+ * Force-release the region on shutdown: every waiter resolves null and the
160
+ * current holder's lease becomes a no-op. A hung broadcast can never block
161
+ * process exit on this lock.
162
+ */
163
+ forceRelease() {
164
+ const waiters = this._waiters.splice(0, this._waiters.length);
165
+ for (const waiter of waiters) {
166
+ if (waiter.timer)
167
+ clearTimeout(waiter.timer);
168
+ waiter.resolve(null);
169
+ }
170
+ this._holder = null;
171
+ this._since = 0;
172
+ this._token += 1;
173
+ this._notifyRegionEnd();
174
+ }
175
+ /** Whether the given lease is still the one that owns the region. */
176
+ isLeaseCurrent(lease) {
177
+ if (!lease || this._holder === null)
178
+ return false;
179
+ const token = leaseTokens.get(lease);
180
+ return token !== undefined && token === this._token;
181
+ }
182
+ _makeLease(kind, token) {
183
+ let released = false;
184
+ const lease = {
185
+ kind,
186
+ release: () => {
187
+ if (released)
188
+ return;
189
+ released = true;
190
+ this._release(token);
191
+ },
192
+ };
193
+ leaseTokens.set(lease, token);
194
+ return lease;
195
+ }
196
+ _release(token) {
197
+ // Stale-lease guard: a lease released after forceRelease (or after the
198
+ // lock was re-acquired by someone else) must NOT release the current
199
+ // holder. Only the token that still owns the region may release it.
200
+ if (token !== this._token || this._holder === null)
201
+ return;
202
+ const next = this._waiters.shift();
203
+ if (!next) {
204
+ this._holder = null;
205
+ this._since = 0;
206
+ this._token += 1;
207
+ // The region is fully free: wake any fill/rebalance that deferred
208
+ // on isPlacementActive(). A handoff keeps the region contiguous and
209
+ // must NOT wake intermediate work.
210
+ this._notifyRegionEnd();
211
+ return;
212
+ }
213
+ if (next.timer)
214
+ clearTimeout(next.timer);
215
+ this._holder = next.kind;
216
+ this._since = Date.now();
217
+ this._token += 1;
218
+ next.resolve(this._makeLease(next.kind, this._token));
219
+ }
220
+ }
221
+ export default PlacementLock;
222
+ //# sourceMappingURL=placement_lock.js.map
@@ -272,8 +272,20 @@ class StrategyEngine {
272
272
  // inventory. The fill is the anchor: place it outside its own window.
273
273
  // Validated like a boundary commit (strands no live in-band order), and
274
274
  // only for an on-ladder fill price.
275
- const reanchor = reanchorBoundaryFromLastFill(this.manager, newBoundaryIdx, allSlots, gapSlots, fills);
275
+ const allowReanchor = (() => {
276
+ const budget = this.manager._boundaryReanchorBudget;
277
+ // undefined = legacy/no fill cycle: allowed. 0 = this fill cycle
278
+ // already consumed its one absolute anchor. >0 = budget remaining.
279
+ return budget === undefined || Number(budget) !== 0;
280
+ })();
281
+ const reanchor = allowReanchor
282
+ ? reanchorBoundaryFromLastFill(this.manager, newBoundaryIdx, allSlots, gapSlots, fills)
283
+ : { boundaryIdx: newBoundaryIdx, reanchored: false };
276
284
  if (reanchor.reanchored) {
285
+ const budget = Number(this.manager._boundaryReanchorBudget);
286
+ this.manager._boundaryReanchorBudget = Number.isFinite(budget) && budget > 0
287
+ ? budget - 1
288
+ : 0;
277
289
  newBoundaryIdx = reanchor.boundaryIdx;
278
290
  // An absolute fill anchor already reflects every older owed crawl.
279
291
  // The derivation may have left records owed (cap exhausted, mixed
@@ -15,7 +15,7 @@
15
15
  * Uses AsyncLock to prevent concurrent sync operations (defense-in-depth locking).
16
16
  *
17
17
  * ===============================================================================
18
- * TABLE OF CONTENTS - SyncEngine Class (12 methods) + module-level helpers
18
+ * TABLE OF CONTENTS - SyncEngine Class (14 methods) + module-level helpers
19
19
  * ===============================================================================
20
20
  *
21
21
  * MODULE-LEVEL HELPERS (internal, not exported):
@@ -42,42 +42,46 @@
42
42
  * 4. _performSyncFromOpenOrders(mgr, precA, precB, parsedChain, rawChain, options) - Core sync logic (internal)
43
43
  * Performs actual two-pass reconciliation without locking
44
44
  * Pass 1: Match grid orders to chain (known grid → chain, includes orphan cleanup)
45
- * Pass 2: Add missing chain orders (unknown chain → grid)
45
+ * Pass 2: Bind unmatched chain orders to their genesis-ladder slot (chain → grid)
46
46
  *
47
- * FILL PROCESSING (5 methods)
47
+ * FILL PROCESSING (6 methods)
48
48
  * 5. _findMatchingGridOrder(mgr, orderId) - Look up the grid slot owning a chain order (internal)
49
49
  *
50
- * 6. _computeFillContext(mgr, matchedGridOrder, paysAssetId, paysAmountRaw) -
51
- * Resolve the asset/side context for a fill (internal)
50
+ * 6. _computeFillContext(mgr, matchedGridOrder, paysAssetId, paysAmountRaw,
51
+ * receivesAssetId, receivesAmountRaw) - Resolve the asset/side context for
52
+ * a fill (internal)
52
53
  *
53
54
  * 7. _computeFillTransitionResult(mgr, params) - Compute the post-fill slot
54
55
  * transition and fund deltas without mutating state (internal, async)
55
56
  *
56
- * 8. syncFromFillHistory(fill) - Process single fill event synchronously
57
+ * 8. _absorbUnmatchedFill(mgr, params) - Attribute a fill whose slot was
58
+ * already virtualised (thin wrapper over utils/order.ts) (internal, async)
59
+ *
60
+ * 9. syncFromFillHistory(fill) - Process single fill event synchronously
57
61
  * Updates grid order state based on fill data
58
62
  * Updates fund state and accounting
59
63
  * Handles both maker and taker fills
60
64
  *
61
- * 9. syncFromFillHistoryBatch(fills) - Process multiple fill events in batch
65
+ * 10. syncFromFillHistoryBatch(fills) - Process multiple fill events in batch
62
66
  * Acquires _gridLock once for all fills in block group
63
67
  * Batches drift refetch into one get_objects RPC call
64
68
  * Applies all grid mutations in a single applyGridUpdateBatch
65
69
  *
66
70
  * FULL SYNCHRONIZATION (2 methods - async)
67
- * 10. _assertGenesisInvariant(mgr) - Fail-closed check that a snapshot has a genesis ladder (internal)
71
+ * 11. _assertGenesisInvariant(mgr) - Fail-closed check that a snapshot has a genesis ladder (internal)
68
72
  *
69
- * 11. synchronizeWithChain(chainData, source) - Full sync (fetch + sync) (async)
73
+ * 12. synchronizeWithChain(chainData, source) - Full sync (fetch + sync) (async)
70
74
  * Fetches fresh account balances
71
75
  * Calls syncFromOpenOrders() with chain data
72
76
  * Source: event type that triggered sync (fill, poll, broadcast, etc.)
73
77
  *
74
78
  * ACCOUNT STATE (2 methods - async)
75
- * 12. fetchAccountBalancesAndSetTotals() - Fetch account totals (async)
79
+ * 13. fetchAccountBalancesAndSetTotals() - Fetch account totals (async)
76
80
  * Retrieves BUY/SELL totals and free balances from blockchain
77
81
  * Sets manager.accountTotals
78
82
  * Triggers fund recalculation
79
83
  *
80
- * 13. initializeAssets() - Initialize asset metadata (async)
84
+ * 14. initializeAssets() - Initialize asset metadata (async)
81
85
  * Fetches asset precision and other metadata
82
86
  * Sets manager.assets
83
87
  * Called once at bot startup
@@ -101,10 +105,16 @@
101
105
  * - Update sizes and mark as filled if needed
102
106
  * - Downgrade to VIRTUAL if not found on-chain
103
107
  *
104
- * PASS 2: Chain → Grid
105
- * - For each chain order not matched to grid order
106
- * - Create new grid order for unexpected chain order
107
- * - Mark as ACTIVE with blockchain orderId
108
+ * PASS 2: Chain → Grid (genesis-frozen nearest-slot adoption)
109
+ * - For each chain order not matched in pass 1, resolve its slot from the
110
+ * frozen price ladder (`slotIndexForPrice`) and adopt it there
111
+ * - No tolerance matcher remains: without a ladder the E2 gate refuses the
112
+ * sync, and a ladder-less grid defers every orphan instead of matching it
113
+ * - A price outside the grid range is HELD/deferred (nearest slot is only
114
+ * reported as a hint) — it is never adopted into a rail slot
115
+ * - An order whose price cannot be resolved to a slot is queued for a
116
+ * cancel-only correction; the deferred (no-ladder / out-of-grid) cases are
117
+ * logged and held, never dropped silently
108
118
  *
109
119
  * ===============================================================================
110
120
  */
@@ -151,6 +161,12 @@ interface FillTransitionParams {
151
161
  * (GRID-PRICE-INVARIANT).
152
162
  */
153
163
  fillPrice?: number | null;
164
+ /**
165
+ * Base-amount units of this fill already reflected in the chain snapshot
166
+ * by sync pass 1. The transition subtracts only `filledAmountInt -
167
+ * alreadyAppliedInt`; a partial value handles split/aggregated fills.
168
+ */
169
+ alreadyAppliedInt?: number;
154
170
  }
155
171
  interface SyncChainData {
156
172
  gridOrderId?: string;
@@ -176,72 +192,15 @@ declare class SyncEngine {
176
192
  */
177
193
  constructor(manager: OrderManagerLike);
178
194
  /**
179
- * Reconcile grid orders against fresh blockchain open orders snapshot.
180
- * This is the MAIN SYNCHRONIZATION MECHANISM that corrects the grid state when
181
- * the blockchain state diverges from our local expectations.
195
+ * Reconcile grid orders against a fresh blockchain open-orders snapshot.
196
+ * This is the MAIN SYNCHRONIZATION MECHANISM: it corrects the grid when the
197
+ * chain state diverges from local expectations. The two-pass structure, the
198
+ * lock hierarchy and the per-pass rules live in the module header; the
199
+ * fail-closed genesis gate (E2) runs before anything is touched.
182
200
  *
183
- * CRITICAL: This method uses AsyncLock (defense-in-depth) to ensure only one
184
- * full-sync operation runs at a time. WITHIN that lock, per-order locks prevent
185
- * concurrent createOrder/cancelOrder races.
186
- *
187
- * LOCK HIERARCHY:
188
- * 1. _syncLock (AsyncLock): Ensures only one full-sync at a time
189
- * 2. Per-order locks (shadowOrderIds): Protect specific orders during sync
190
- * 3. Lock refresh mechanism: Prevents timeout during long reconciliation
191
- *
192
- * RECONCILIATION FLOW:
193
- * ========================================================================
194
- * This method performs a two-pass reconciliation:
195
- *
196
- * PASS 1: Match grid orders to chain orders (known grid → chain lookup)
197
- * - For each grid order with an orderId, find the matching chain order
198
- * - Detect partial fills: if chain size < grid size, downgrade to PARTIAL state
199
- * - Detect full fills: if order no longer exists on chain, convert to SPREAD
200
- * - Detect price slippage: flag orders for price correction if needed
201
- * - Update grid order sizes to match chain reality
202
- *
203
- * PASS 2: Orphan chain orders (chain → grid lookup)
204
- * - For chain orders not matched in Pass 1, find best grid slot match
205
- * - This handles cases where an order was placed but grid lost track (race condition)
206
- * - Uses price tolerance and geometric proximity to find the best match
207
- * - Once matched, retroactively assign orderId and synchronize state
208
- *
209
- * CRITICAL RULES:
210
- * 1. ACTIVE orders can only stay ACTIVE if size matches chain exactly
211
- * If chain size < grid size → must transition to PARTIAL
212
- * 2. If an ACTIVE order is not found on chain → it filled → convert to SPREAD
213
- * 3. Precision matters: Use blockchain integer arithmetic to compare sizes
214
- * Floating point comparisons can give false positives for partial fills
215
- * 4. Size updates are applied via applyChainSizeToGridOrder() which handles
216
- * precision conversion and may adjust sizes slightly for blockchain granularity
217
- *
218
- * PRICE TOLERANCE CALCULATION:
219
- * calculatePriceTolerance() can return null in these cases:
220
- * 1. assets parameter is null/missing
221
- * 2. gridPrice is 0 or null (invalid price)
222
- * 3. orderSize is 0 or null (invalid size - orders should not sync with 0 size)
223
- * 4. assetA or assetB precision is undefined (asset metadata not loaded)
224
- * When tolerance is null, we treat it as 0 (strict: any price difference flagged).
225
- * This is safe because null signals a configuration/data issue, not a real order.
226
- *
227
- * RETURNS: { filledOrders, updatedOrders, ordersNeedingCorrection }
228
- * - filledOrders: Orders that completed (now SPREAD placeholders)
229
- * - updatedOrders: All orders modified during sync (state changes, size updates)
230
- * - ordersNeedingCorrection: Orders with price slippage requiring correction
231
- *
232
- * EDGE CASES HANDLED:
233
- * - Orphan chain orders (placed but grid lost track due to race condition)
234
- * - Partial fills (size reduced on chain)
235
- * - Full fills (order removed from chain completely)
236
- * - Price tolerance (small slippage acceptable, large slippage flagged)
237
- * - Precision mismatches (blockchain integer precision vs float grid)
238
- * - Double spending prevention (each chain order matched to at most one grid order)
239
- */
240
- /**
241
- * Synchronize grid orders with blockchain open orders snapshot.
242
201
  * @param {Array|null} chainOrders - Array of blockchain order objects
243
202
  * @param {Object} [options={}] - Sync options (e.g., { skipAccounting: true })
244
- * @returns {Promise<Object>} Result with filledOrders, updatedOrders, ordersNeedingCorrection
203
+ * @returns {Promise<SyncResult>} filledOrders, updatedOrders, ordersNeedingCorrection
245
204
  */
246
205
  syncFromOpenOrders(chainOrders: unknown[] | null, options?: Record<string, unknown>): Promise<SyncResult>;
247
206
  /**
@@ -284,6 +243,7 @@ declare class SyncEngine {
284
243
  * @param {number} assetBPrecision - Asset B precision
285
244
  * @param {Map<string, any>} parsedChainOrders - Parsed chain orders by ID
286
245
  * @param {Map<string, Object>} rawChainOrders - Raw blockchain order objects by ID
246
+ * @param {Set<string>} malformedChainOrderIds - Chain order IDs that were returned but could not be parsed (UNKNOWN, not absent)
287
247
  * @param {Set<string>} chainOrderIdsOnGrid - Output set: chain order IDs matched to grid
288
248
  * @param {Set<string>} matchedGridOrderIds - Output set: grid order IDs matched to chain
289
249
  * @param {Array<Object>} filledOrders - Output array: orders detected as filled
@@ -293,7 +253,7 @@ declare class SyncEngine {
293
253
  * @param {Object} options - Sync options (skipAccounting, etc.)
294
254
  * @returns {Promise<any>}
295
255
  */
296
- _performSyncFromOpenOrders(mgr: OrderManagerLike, assetAPrecision: number, assetBPrecision: number, parsedChainOrders: Map<string, ParsedChainOrder>, rawChainOrders: Map<string, Record<string, unknown>>, chainOrderIdsOnGrid: Set<string>, matchedGridOrderIds: Set<string>, filledOrders: ManagedOrder[], updatedOrders: ManagedOrder[], ordersNeedingCorrection: PendingPriceCorrection[], unmatchedChainOrders: UnmatchedChainOrder[], options: Record<string, unknown>): Promise<{
256
+ _performSyncFromOpenOrders(mgr: OrderManagerLike, assetAPrecision: number, assetBPrecision: number, parsedChainOrders: Map<string, ParsedChainOrder>, rawChainOrders: Map<string, Record<string, unknown>>, malformedChainOrderIds: Set<string>, chainOrderIdsOnGrid: Set<string>, matchedGridOrderIds: Set<string>, filledOrders: ManagedOrder[], updatedOrders: ManagedOrder[], ordersNeedingCorrection: PendingPriceCorrection[], unmatchedChainOrders: UnmatchedChainOrder[], options: Record<string, unknown>): Promise<{
297
257
  filledOrders: ManagedOrder[];
298
258
  updatedOrders: ManagedOrder[];
299
259
  ordersNeedingCorrection: PendingPriceCorrection[];
@@ -425,21 +385,42 @@ declare class SyncEngine {
425
385
  deferred?: undefined;
426
386
  }>;
427
387
  /**
428
- * High-level dispatcher for different blockchain synchronization sources.
429
- * Routes to the appropriate sync strategy based on the data source.
388
+ * E2 invariant assert: a manager holding grid slots but no price ladder is
389
+ * in the undefined state the grid-price invariant forbids (INV-GRID-004).
390
+ *
391
+ * Reports once per grid generation (latched on the manager, counted in
392
+ * `_genesisInvariantViolations`) and, when the bot wired the structural
393
+ * resync, requests one so the ladder is re-derived. Returns FALSE so the
394
+ * caller ABORTS the sync untouched: with no ladder there is no authority for
395
+ * a slot's price, and the legacy tolerance matcher that used to cover this
396
+ * state is gone — continuing would let an off-grid chain price be adopted
397
+ * into a slot.
398
+ *
399
+ * Non-throwing on purpose: the load-side gates (E1/E3) own the fail-closed
400
+ * decision, this one makes a bypass loud without stranding a live manager.
401
+ * @param {unknown} mgr - OrderManager instance
402
+ * @returns {boolean} true when the grid may be synced (no slots, or a usable
403
+ * ladder is present); false when the sync must not run.
404
+ */
405
+ _assertGenesisInvariant(mgr: OrderManagerLike): boolean;
406
+ /**
407
+ * High-level dispatcher for the different blockchain synchronization
408
+ * sources. Routes each source to its strategy and returns a uniform result
409
+ * shape.
430
410
  *
431
411
  * SOURCES AND STRATEGIES:
432
412
  * ========================================================================
433
413
  * source: 'createOrder'
434
414
  * Purpose: Grid order was successfully placed on-chain
435
- * Data: { gridOrderId, chainOrderId, isPartialPlacement, fee }
415
+ * Data: { gridOrderId, chainOrderId, isPartialPlacement, expectedType, fee, order? }
436
416
  * Action:
437
417
  * 1. Look up grid order by gridOrderId
438
418
  * 2. Assign the returned chainOrderId (so we can find it later)
439
419
  * 3. Transition state based on isPartialPlacement:
440
420
  * - false → ACTIVE (full order placed)
441
421
  * - true → PARTIAL (placed as partial, likely due to insufficient funds)
442
- * 4. Update optimistic chainFree balance (deduct fees if BTS pair)
422
+ * 4. Apply the transition through _applyOrderUpdate (skipAccounting:
423
+ * false), which owns the optimistic chainFree update and the BTS fee
443
424
  * Fund Impact: Funds transition from free → locked/committed
444
425
  *
445
426
  * source: 'cancelOrder'
@@ -449,9 +430,11 @@ declare class SyncEngine {
449
430
  * 1. Find grid order by orderId (reverse lookup)
450
431
  * 2. Transition to VIRTUAL (order no longer on-chain)
451
432
  * 3. Clear orderId so it can be re-used
452
- * 4. Update optimistic chainFree balance (add funds back as free)
433
+ * 4. Apply the transition through _applyOrderUpdate
453
434
  * Fund Impact: Funds transition from locked → free
454
- * Note: This is used for direct cancellations (not rotation/consolidation)
435
+ * Note: This is used for direct cancellations (not rotation/consolidation).
436
+ * A cancel whose slot vanished still deducts its fee from the totals
437
+ * via adjustTotalBalance, so the books cannot drift.
455
438
  *
456
439
  * source: 'readOpenOrders' or 'periodicBlockchainFetch'
457
440
  * Purpose: Full snapshot sync of all open orders from blockchain
@@ -460,38 +443,15 @@ declare class SyncEngine {
460
443
  * Use Case: Periodic health check or startup initialization
461
444
  *
462
445
  * FUND TRACKING:
463
- * Both 'createOrder' and 'cancelOrder' call updateOptimisticFreeBalance() to
464
- * keep the optimistic chainFree balance in sync with actual on-chain state.
465
- * This prevents fund leaks where placed orders weren't deducted or cancelled
466
- * orders weren't released.
467
- *
468
- * RETURNS: { newOrders, ordersNeedingCorrection }
469
- * Most callers use ordersNeedingCorrection to flag price corrections needed.
470
- * Only syncFromOpenOrders() populates ordersNeedingCorrection.
446
+ * The two event sources move funds through _applyOrderUpdate (and, for an
447
+ * unmatched cancel, adjustTotalBalance), so the optimistic chainFree balance
448
+ * cannot be bypassed by an event path. This prevents fund leaks where placed
449
+ * orders weren't deducted or cancelled orders weren't released.
471
450
  *
472
451
  * @param {Object} chainData - Chain event data
473
- * @param {string} source - Source identifier ('createOrder', 'cancelOrder', 'readOpenOrders', etc.)
474
- * @returns {Promise<Object>} { newOrders, ordersNeedingCorrection }
452
+ * @param {string} source - Source identifier ('createOrder', 'cancelOrder', 'readOpenOrders', 'periodicBlockchainFetch')
453
+ * @returns {Promise<Object>} { newOrders, filledOrders, updatedOrders, ordersNeedingCorrection }
475
454
  */
476
- /**
477
- * E2 invariant assert: a manager holding grid slots but no price ladder is
478
- * in the undefined state the grid-price invariant forbids (INV-GRID-004).
479
- *
480
- * Reports once per grid generation (latched on the manager, counted in
481
- * `_genesisInvariantViolations`) and, when the bot wired the structural
482
- * resync, requests one so the ladder is re-derived. Returns FALSE so the
483
- * caller ABORTS the sync untouched: with no ladder there is no authority for
484
- * a slot's price, and the legacy tolerance matcher that used to cover this
485
- * state is gone — continuing would let an off-grid chain price be adopted
486
- * into a slot.
487
- *
488
- * Non-throwing on purpose: the load-side gates (E1/E3) own the fail-closed
489
- * decision, this one makes a bypass loud without stranding a live manager.
490
- * @param {unknown} mgr - OrderManager instance
491
- * @returns {boolean} true when the grid may be synced (no slots, or a usable
492
- * ladder is present); false when the sync must not run.
493
- */
494
- _assertGenesisInvariant(mgr: OrderManagerLike): boolean;
495
455
  synchronizeWithChain(chainData: SyncChainData, source: string): Promise<SyncResult>;
496
456
  /**
497
457
  * Fetch account balances from blockchain and update optimistic fund totals.