Enhance SpotBot balance management after position closure
- Added logic to wait for ETH→USDC swap to settle before refreshing the USDC balance, preventing premature bot stoppage due to low balance. - Implemented balance verification and cache invalidation for accurate balance checks post-swap. - Improved logging to indicate the status of balance refresh and cache invalidation after closing a position.
This commit is contained in:
@@ -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<IGrainFactory>(_scopeFactory, async grainFactory =>
|
||||
{
|
||||
var agentGrain = grainFactory.GetGrain<IAgentGrain>(Account.User.Id);
|
||||
await agentGrain.ForceUpdateSummaryImmediate();
|
||||
});
|
||||
|
||||
await LogDebugAsync($"✅ Balance refreshed and cache invalidated after position close");
|
||||
}
|
||||
|
||||
if (tradeClosingPosition)
|
||||
{
|
||||
await SetPositionStatus(signal.Identifier, PositionStatus.Finished);
|
||||
|
||||
Reference in New Issue
Block a user