diff --git a/src/Managing.Application/Bots/SpotBot.cs b/src/Managing.Application/Bots/SpotBot.cs index 01c8827e..f0708946 100644 --- a/src/Managing.Application/Bots/SpotBot.cs +++ b/src/Managing.Application/Bots/SpotBot.cs @@ -1105,6 +1105,31 @@ public class SpotBot : TradingBotBase await VerifyTokenBalanceCleared(closedPosition); } + // Wait for swap to settle and refresh USDC balance + // This prevents the bot from stopping due to "low USDC" before the ETH→USDC swap completes + if (!Config.IsForWatchingOnly && Config.TradingType != TradingType.BacktestSpot) + { + await LogDebugAsync( + $"⏳ Waiting for swap to settle and refreshing USDC balance...\n" + + $"Position: `{closedPosition.Identifier}`"); + + // Wait 3 seconds for the swap to settle on-chain + await Task.Delay(3000); + + // Refresh USDC balance to reflect the swapped tokens + await VerifyAndUpdateBalanceAsync(); + + // Invalidate AgentGrain balance cache to force fresh fetch on next balance check + // This ensures the bot won't stop due to "low USDC" using stale cached data + await ServiceScopeHelpers.WithScopedService(_scopeFactory, async grainFactory => + { + var agentGrain = grainFactory.GetGrain(Account.User.Id); + await agentGrain.ForceUpdateSummaryImmediate(); + }); + + await LogDebugAsync($"✅ Balance refreshed and cache invalidated after position close"); + } + if (tradeClosingPosition) { await SetPositionStatus(signal.Identifier, PositionStatus.Finished);