Refactor ETH balance and gas fee checks in SpotBot

- Updated balance checks to utilize user-defined thresholds for minimum trading and swap balances, enhancing flexibility.
- Improved gas fee validation by incorporating user settings, allowing for more personalized transaction management.
- Enhanced logging to provide clearer messages regarding balance sufficiency and gas fee limits, improving user feedback during operations.
This commit is contained in:
2026-01-06 00:43:51 +07:00
parent efbb116ed2
commit 5e7b2b34d4
10 changed files with 1264 additions and 72 deletions

View File

@@ -410,7 +410,9 @@ public class AgentGrain : Grain, IAgentGrain
}
// If ETH balance is sufficient, return success
if (balanceData.EthValueInUsd >= Constants.GMX.Config.MinimumTradeEthBalanceUsd)
// Use user's low ETH alert threshold as the minimum trading balance
var minTradeEthBalance = user.LowEthAmountAlert ?? Constants.GMX.Config.MinimumTradeEthBalanceUsd;
if (balanceData.EthValueInUsd >= minTradeEthBalance)
{
return new BalanceCheckResult
{
@@ -421,13 +423,15 @@ public class AgentGrain : Grain, IAgentGrain
};
}
if (balanceData.EthValueInUsd < Constants.GMX.Config.MinimumSwapEthBalanceUsd)
// Check if ETH is below absolute minimum (half of the alert threshold)
var minSwapEthBalance = minTradeEthBalance * 0.67m; // 67% of alert threshold
if (balanceData.EthValueInUsd < minSwapEthBalance)
{
return new BalanceCheckResult
{
IsSuccessful = false,
FailureReason = BalanceCheckFailureReason.InsufficientEthBelowMinimum,
Message = "ETH balance below minimum required amount",
Message = $"ETH balance below minimum required amount ({minSwapEthBalance:F2} USD)",
ShouldStopBot = true
};
}

View File

@@ -552,14 +552,16 @@ namespace Managing.Application.ManageBot
}
// Check ETH minimum balance for trading
if (ethValueInUsd < Constants.GMX.Config.MinimumTradeEthBalanceUsd)
// Use user's low ETH alert threshold, fallback to default constant
var minEthBalance = account.User.LowEthAmountAlert ?? Constants.GMX.Config.MinimumTradeEthBalanceUsd;
if (ethValueInUsd < minEthBalance)
{
return new BalanceCheckResult
{
IsSuccessful = false,
FailureReason = BalanceCheckFailureReason.InsufficientEthBelowMinimum,
Message =
$"ETH balance ({ethValueInUsd:F2} USD) is below minimum required amount ({Constants.GMX.Config.MinimumTradeEthBalanceUsd} USD) for trading. Please add more ETH to restart the bot.",
$"ETH balance ({ethValueInUsd:F2} USD) is below minimum required amount ({minEthBalance:F2} USD) for trading. Please add more ETH to restart the bot.",
ShouldStopBot = true
};
}

View File

@@ -51,10 +51,13 @@ namespace Managing.Application.Trading.Handlers
if (account.Exchange == TradingExchanges.Evm || account.Exchange == TradingExchanges.GmxV2)
{
var currentGasFees = await exchangeService.GetFee(account);
if (currentGasFees > Constants.GMX.Config.MaximumGasFeeUsd)
// Use user's max gas fee setting, fallback to default constant
var maxGasFeeThreshold = request.User.MaxTxnGasFeePerPosition ?? Constants.GMX.Config.MaximumGasFeeUsd;
if (currentGasFees > maxGasFeeThreshold)
{
throw new InsufficientFundsException(
$"Gas fee too high for position opening: {currentGasFees:F2} USD (threshold: {Constants.GMX.Config.MaximumGasFeeUsd} USD). Position opening rejected.",
$"Gas fee too high for position opening: {currentGasFees:F2} USD (threshold: {maxGasFeeThreshold:F2} USD). Position opening rejected.",
InsufficientFundsType.HighNetworkFee);
}
}