diff --git a/src/Managing.Application/Bots/SpotBot.cs b/src/Managing.Application/Bots/SpotBot.cs index db8ee70d..d68b7ad5 100644 --- a/src/Managing.Application/Bots/SpotBot.cs +++ b/src/Managing.Application/Bots/SpotBot.cs @@ -1530,11 +1530,32 @@ public class SpotBot : TradingBotBase if (tokenBalance is { Amount: > 0 } && tokenBalance.Amount > maxDustAmount) { + // Check if remaining balance is small enough (< $2 USD) and verified in history + if (tokenBalance.Value < 2m) + { + // Check if the closing swap exists in history + var (sellFoundInHistory, _) = await CheckSpotPositionInExchangeHistory(closedPosition); + + if (sellFoundInHistory) + { + await LogDebugAsync( + $"✅ Small Leftover Accepted - Position Verified Closed\n" + + $"Position: `{closedPosition.Identifier}`\n" + + $"Ticker: {Config.Ticker}\n" + + $"Remaining Token Balance: `{tokenBalance.Amount:F5}`\n" + + $"USD Value: `${tokenBalance.Value:F2}` (below $2 threshold)\n" + + $"Sell transaction confirmed in exchange history\n" + + $"Accepting as successfully closed - leftover is likely slippage/rounding"); + return; // Position is verified closed, no force close needed + } + } + await LogWarningAsync( $"⚠️ Token Balance Not Fully Cleared After Closing\n" + $"Position: `{closedPosition.Identifier}`\n" + $"Ticker: {Config.Ticker}\n" + $"Remaining Token Balance: `{tokenBalance.Amount:F5}`\n" + + $"USD Value: `${tokenBalance.Value:F2}`\n" + $"Expected: `0` or less than `{maxDustAmount:F5}` (dust)\n" + $"Attempting to force close remaining balance..."); @@ -1611,10 +1632,29 @@ public class SpotBot : TradingBotBase if (finalBalance is { Amount: > 0.0001m }) { + // Check if remaining balance is small enough (< $2 USD) and verified in history + if (finalBalance.Value < 2m) + { + var (sellFoundInHistory, _) = await CheckSpotPositionInExchangeHistory(position); + + if (sellFoundInHistory) + { + await LogInformationAsync( + $"✅ Small Leftover Accepted After Force Close\n" + + $"Position: `{position.Identifier}`\n" + + $"Remaining: `{finalBalance.Amount:F5}`\n" + + $"USD Value: `${finalBalance.Value:F2}` (below $2 threshold)\n" + + $"Sell transaction confirmed in exchange history\n" + + $"Accepting as successfully closed - leftover is likely slippage/rounding"); + return; + } + } + await LogWarningAsync( $"⚠️ Balance Still Remaining After Force Close Attempt\n" + $"Position: `{position.Identifier}`\n" + $"Remaining: `{finalBalance.Amount:F5}`\n" + + $"USD Value: `${finalBalance.Value:F2}`\n" + $"This will be handled on the next bot cycle\n" + $"Manual intervention may be required if issue persists"); }