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.
This commit is contained in:
2026-01-07 22:32:04 +07:00
parent 7f06088161
commit bdc5ba2db7

View File

@@ -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");
}