Add ETH and USDC balance check before start/restart bot and autoswap

This commit is contained in:
2025-09-23 14:03:46 +07:00
parent d13ac9fd21
commit 40f3c66694
23 changed files with 847 additions and 284 deletions

View File

@@ -1,4 +1,5 @@
using Managing.Application.Abstractions.Grains;
using Managing.Application.Abstractions;
using Managing.Application.Abstractions.Grains;
using Managing.Application.Abstractions.Services;
using Managing.Application.ManageBot.Commands;
using Managing.Common;
@@ -11,12 +12,14 @@ namespace Managing.Application.ManageBot
{
private readonly IAccountService _accountService;
private readonly IGrainFactory _grainFactory;
private readonly IBotService _botService;
public StartBotCommandHandler(
IAccountService accountService, IGrainFactory grainFactory)
IAccountService accountService, IGrainFactory grainFactory, IBotService botService)
{
_accountService = accountService;
_grainFactory = grainFactory;
_botService = botService;
}
public async Task<BotStatus> Handle(StartBotCommand request, CancellationToken cancellationToken)
@@ -39,20 +42,34 @@ namespace Managing.Application.ManageBot
$"Bot trading balance must be greater than {Constants.GMX.Config.MinimumPositionAmount}");
}
var account = await _accountService.GetAccount(request.Config.AccountName, true, true);
var account = await _accountService.GetAccount(request.Config.AccountName, true, false);
if (account == null)
{
throw new Exception($"Account {request.Config.AccountName} not found");
}
var usdcBalance = account.Balances.FirstOrDefault(b => b.TokenName == Ticker.USDC.ToString());
if (usdcBalance == null ||
usdcBalance.Value < Constants.GMX.Config.MinimumPositionAmount ||
usdcBalance.Value < request.Config.BotTradingBalance)
// Check balances for EVM/GMX V2 accounts before starting
if (account.Exchange == TradingExchanges.Evm || account.Exchange == TradingExchanges.GmxV2)
{
throw new Exception($"Account {request.Config.AccountName} has no USDC balance or not enough balance");
var balanceCheckResult = await _botService.CheckAccountBalancesAsync(account);
if (!balanceCheckResult.IsSuccessful)
{
throw new InvalidOperationException(balanceCheckResult.Message);
}
}
// For other exchanges, keep the original USDC balance check
if (account.Exchange != TradingExchanges.Evm && account.Exchange != TradingExchanges.GmxV2)
{
var usdcBalance = account.Balances.FirstOrDefault(b => b.TokenName == Ticker.USDC.ToString());
if (usdcBalance == null ||
usdcBalance.Value < Constants.GMX.Config.MinimumPositionAmount ||
usdcBalance.Value < request.Config.BotTradingBalance)
{
throw new Exception($"Account {request.Config.AccountName} has no USDC balance or not enough balance");
}
}
try