Phase 3c: Unkritische Call-Sites auf Repositories umgestellt
- MarketSyncService: markets-Zugriffe -> IMarketRepository (kein _db mehr). - PolymarketWssClient: demo_positions -> IPositionRepository, accounts -> IAccountRepository (closed_trades bleibt vorerst auf _db, ClosedTrade-Repo folgt in Phase 5). - Build 0 Fehler. Hot-Path (CopyTradingEngine, TraderMonitorService) und frm_main-UI bewusst noch NICHT migriert (Schritt B / Phase 5). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,7 @@ using System.Threading.Tasks;
|
|||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
|
using PolyTrader.Core.Persistence;
|
||||||
using PolyTraderSharp.Extensions;
|
using PolyTraderSharp.Extensions;
|
||||||
using PolyTraderSharp.Models;
|
using PolyTraderSharp.Models;
|
||||||
using PolyTraderSharp.Services;
|
using PolyTraderSharp.Services;
|
||||||
@@ -12,15 +13,15 @@ namespace PolyTraderSharp.Services
|
|||||||
public class MarketSyncService : BackgroundService
|
public class MarketSyncService : BackgroundService
|
||||||
{
|
{
|
||||||
private readonly PolymarketApiService _apiService;
|
private readonly PolymarketApiService _apiService;
|
||||||
private readonly IMongoDatabase _db;
|
private readonly IMarketRepository _marketRepo;
|
||||||
private readonly TerminalLogger _logger;
|
private readonly TerminalLogger _logger;
|
||||||
private readonly JobStatusRow _jobStatus;
|
private readonly JobStatusRow _jobStatus;
|
||||||
private readonly TradingState _state;
|
private readonly TradingState _state;
|
||||||
|
|
||||||
public MarketSyncService(PolymarketApiService apiService, IMongoDatabase db, TerminalLogger logger, JobManager jobManager, TradingState state)
|
public MarketSyncService(PolymarketApiService apiService, IMarketRepository marketRepo, TerminalLogger logger, JobManager jobManager, TradingState state)
|
||||||
{
|
{
|
||||||
_apiService = apiService;
|
_apiService = apiService;
|
||||||
_db = db;
|
_marketRepo = marketRepo;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_state = state;
|
_state = state;
|
||||||
|
|
||||||
@@ -93,18 +94,17 @@ namespace PolyTraderSharp.Services
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var col = _db.GetCollection<MarketData>("markets");
|
_marketRepo.EnsureIndexes();
|
||||||
col.EnsureIndex(x => x.Id);
|
|
||||||
|
|
||||||
int inserted = 0;
|
int inserted = 0;
|
||||||
int updated = 0;
|
int updated = 0;
|
||||||
|
|
||||||
foreach (var market in newMarkets)
|
foreach (var market in newMarkets)
|
||||||
{
|
{
|
||||||
var existing = col.LiteFindOne(x => x.Id == market.Id);
|
var existing = _marketRepo.GetById(market.Id);
|
||||||
if (existing == null)
|
if (existing == null)
|
||||||
{
|
{
|
||||||
col.Insert(market);
|
_marketRepo.Insert(market);
|
||||||
inserted++;
|
inserted++;
|
||||||
|
|
||||||
// NEW: Hot-Load active markets directly into RAM Cache
|
// NEW: Hot-Load active markets directly into RAM Cache
|
||||||
@@ -137,7 +137,7 @@ namespace PolyTraderSharp.Services
|
|||||||
existing.ClobTokenIds = market.ClobTokenIds;
|
existing.ClobTokenIds = market.ClobTokenIds;
|
||||||
}
|
}
|
||||||
|
|
||||||
col.Update(existing);
|
_marketRepo.Update(existing);
|
||||||
updated++;
|
updated++;
|
||||||
|
|
||||||
// Keep RAM cache synchronized to prevent using stale active/closed flags
|
// Keep RAM cache synchronized to prevent using stale active/closed flags
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
|
using PolyTrader.Core.Persistence;
|
||||||
using PolyTraderSharp.Extensions;
|
using PolyTraderSharp.Extensions;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@@ -23,7 +24,9 @@ namespace PolyTraderSharp.Services
|
|||||||
private readonly PolymarketClobClient _clob;
|
private readonly PolymarketClobClient _clob;
|
||||||
private readonly TerminalLogger _logger;
|
private readonly TerminalLogger _logger;
|
||||||
private readonly IMongoDatabase _db;
|
private readonly IMongoDatabase _db;
|
||||||
|
private readonly IPositionRepository _positionRepo;
|
||||||
|
private readonly IAccountRepository _accountRepo;
|
||||||
|
|
||||||
// Tracking rate limits for auto redeem: max 2 attempts per position, 5 min apart
|
// Tracking rate limits for auto redeem: max 2 attempts per position, 5 min apart
|
||||||
private readonly ConcurrentDictionary<string, (int Count, DateTime LastAttempt)> _redeemAttempts = new();
|
private readonly ConcurrentDictionary<string, (int Count, DateTime LastAttempt)> _redeemAttempts = new();
|
||||||
|
|
||||||
@@ -32,13 +35,17 @@ namespace PolyTraderSharp.Services
|
|||||||
ServerSettings settings,
|
ServerSettings settings,
|
||||||
PolymarketClobClient clob,
|
PolymarketClobClient clob,
|
||||||
TerminalLogger logger,
|
TerminalLogger logger,
|
||||||
IMongoDatabase db)
|
IMongoDatabase db,
|
||||||
|
IPositionRepository positionRepo,
|
||||||
|
IAccountRepository accountRepo)
|
||||||
{
|
{
|
||||||
_state = state;
|
_state = state;
|
||||||
_settings = settings;
|
_settings = settings;
|
||||||
_clob = clob;
|
_clob = clob;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_db = db;
|
_db = db;
|
||||||
|
_positionRepo = positionRepo;
|
||||||
|
_accountRepo = accountRepo;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
@@ -248,7 +255,7 @@ namespace PolyTraderSharp.Services
|
|||||||
{
|
{
|
||||||
if (acc.OpenPositions.TryRemove(pos.TokenId, out _))
|
if (acc.OpenPositions.TryRemove(pos.TokenId, out _))
|
||||||
{
|
{
|
||||||
_db.GetCollection<Position>($"demo_positions_{acc.AccountId}").Delete(pos.TokenId);
|
_positionRepo.DeleteDemo(acc.AccountId, pos.TokenId);
|
||||||
|
|
||||||
decimal exactLimitPrice = acc.PreRedeemLimit;
|
decimal exactLimitPrice = acc.PreRedeemLimit;
|
||||||
decimal exitUsd = pos.Size * exactLimitPrice;
|
decimal exitUsd = pos.Size * exactLimitPrice;
|
||||||
@@ -278,7 +285,7 @@ namespace PolyTraderSharp.Services
|
|||||||
};
|
};
|
||||||
|
|
||||||
_db.GetCollection<ClosedTrade>("closed_trades").Insert(ct);
|
_db.GetCollection<ClosedTrade>("closed_trades").Insert(ct);
|
||||||
_db.GetCollection<AccountState>("accounts").Upsert(acc);
|
_accountRepo.Upsert(acc);
|
||||||
|
|
||||||
_logger.Trade($"✅ [AUTO REDEEM DEMO ERFOLGREICH] {pos.MarketQuestion} | Exit: {pos.Size:F2} @ {exactLimitPrice:F3} | PnL: ${realizedPnl:F2}");
|
_logger.Trade($"✅ [AUTO REDEEM DEMO ERFOLGREICH] {pos.MarketQuestion} | Exit: {pos.Size:F2} @ {exactLimitPrice:F3} | PnL: ${realizedPnl:F2}");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user