Fix 10 critical bugs in Engine, Retention, Reconciliation and API

This commit is contained in:
Richard
2026-07-09 19:37:02 +02:00
parent 21da1e867f
commit 44f48284a2
7 changed files with 121 additions and 31 deletions
@@ -177,6 +177,18 @@ public class TradeRetentionWorker : BackgroundService
var totalAmount = list.Sum(t => t.Amount);
if (totalSize <= 0) continue;
if (positions.TryGetValue(outcomeId, out var pos))
{
// Bug 2: Check for unapplied trades before compacting
var hasUnappliedTrades = await db.Trades.AnyAsync(
t => t.TraderId == traderId && t.MarketOutcomeId == outcomeId && t.Id > pos.LastAppliedTradeId, ct);
if (hasUnappliedTrades)
{
continue;
}
}
var weightedAvgPrice = totalAmount / totalSize;
// Grab a representative trade to copy fields
@@ -210,12 +222,12 @@ public class TradeRetentionWorker : BackgroundService
await db.SaveChangesAsync(ct);
// Bump the position checkpoint so it doesn't get double counted
if (positions.TryGetValue(outcomeId, out var pos))
if (positions.TryGetValue(outcomeId, out var updatePos))
{
pos.LastAppliedTradeId = Math.Max(pos.LastAppliedTradeId, compactedTrade.Id);
updatePos.LastAppliedTradeId = Math.Max(updatePos.LastAppliedTradeId, compactedTrade.Id);
// Mark as pruned so we don't accidentally reset and replay (which would lose the exact intraday timestamps)
pos.IsHistoryPruned = true;
db.TraderPositions.Update(pos);
updatePos.IsHistoryPruned = true;
db.TraderPositions.Update(updatePos);
}
compactedTradeCount += list.Count - 1;