From bdc5ba2db723072787b902e2f05bda83c9f97dd9 Mon Sep 17 00:00:00 2001 From: cryptooda Date: Wed, 7 Jan 2026 22:32:04 +0700 Subject: [PATCH] Enhance SpotBot to handle small leftover token balances after closing positions - Added logic to check if remaining token balances are below $2 USD and verified in exchange history before logging warnings or accepting them as successfully closed. - Improved logging messages for better clarity on the status of token balances after closing positions and force close attempts, ensuring accurate tracking of transactions. --- src/Managing.Application/Bots/SpotBot.cs | 40 ++++++++++++++++++++++++ 1 file changed, 40 insertions(+) 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"); }