Fix allocated amount when no bot
This commit is contained in:
@@ -111,7 +111,7 @@ namespace Managing.Application.ManageBot
|
||||
var botConfig = await botGrain.GetConfiguration();
|
||||
var account = await ServiceScopeHelpers.WithScopedService<IAccountService, Account>(
|
||||
_scopeFactory,
|
||||
async accountService => await accountService.GetAccount(botConfig.AccountName, true, false));
|
||||
async accountService => await accountService.GetAccount(botConfig.AccountName, true, true));
|
||||
|
||||
if (account.Exchange == TradingExchanges.Evm || account.Exchange == TradingExchanges.GmxV2)
|
||||
{
|
||||
@@ -219,7 +219,7 @@ namespace Managing.Application.ManageBot
|
||||
public async Task<IEnumerable<TradingBotConfig>> GetBotConfigsByIdsAsync(IEnumerable<Guid> botIds)
|
||||
{
|
||||
var configs = new List<TradingBotConfig>();
|
||||
|
||||
|
||||
foreach (var botId in botIds)
|
||||
{
|
||||
try
|
||||
@@ -234,7 +234,7 @@ namespace Managing.Application.ManageBot
|
||||
// Continue with other bots even if one fails
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return configs;
|
||||
}
|
||||
|
||||
@@ -378,7 +378,8 @@ namespace Managing.Application.ManageBot
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<decimal> GetAvailableAllocationUsdAsync(Account account, Guid excludeIdentifier = default)
|
||||
public async Task<decimal> GetAvailableAllocationUsdAsync(Account account,
|
||||
Guid excludeIdentifier = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -387,40 +388,44 @@ namespace Managing.Application.ManageBot
|
||||
async repo =>
|
||||
{
|
||||
// Get all bots for the account's user
|
||||
var botsForUser = await repo.GetBotsByUserIdAsync(account.User.Id);
|
||||
var botsForUser = (await repo.GetBotsByUserIdAsync(account.User.Id)).ToList();
|
||||
|
||||
// Sum allocations for bots using this account name, excluding the requested identifier
|
||||
decimal totalAllocatedForAccount = 0m;
|
||||
foreach (var bot in botsForUser)
|
||||
var totalAllocatedForAccount = 0m;
|
||||
var usdcBalance = account.Balances.FirstOrDefault(b => b.TokenName == Ticker.USDC.ToString());
|
||||
|
||||
if (botsForUser.Any())
|
||||
{
|
||||
if (excludeIdentifier != default && bot.Identifier == excludeIdentifier)
|
||||
foreach (var bot in botsForUser)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (excludeIdentifier != default && bot.Identifier == excludeIdentifier)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var grain = _grainFactory.GetGrain<ILiveTradingBotGrain>(bot.Identifier);
|
||||
TradingBotConfig config;
|
||||
try
|
||||
{
|
||||
config = await grain.GetConfiguration();
|
||||
}
|
||||
catch
|
||||
{
|
||||
continue;
|
||||
}
|
||||
var grain = _grainFactory.GetGrain<ILiveTradingBotGrain>(bot.Identifier);
|
||||
TradingBotConfig config;
|
||||
try
|
||||
{
|
||||
config = await grain.GetConfiguration();
|
||||
}
|
||||
catch
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (string.Equals(config.AccountName, account.Name, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
totalAllocatedForAccount += config.BotTradingBalance;
|
||||
if (string.Equals(config.AccountName, account.Name, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
totalAllocatedForAccount += config.BotTradingBalance;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
totalAllocatedForAccount = usdcBalance?.Value ?? 0m;
|
||||
}
|
||||
|
||||
// Current USDC balance for the account
|
||||
var balances = await ServiceScopeHelpers.WithScopedService<IExchangeService, IEnumerable<Balance>>(
|
||||
_scopeFactory, async exchangeService => await exchangeService.GetBalances(account));
|
||||
var usdc = balances.FirstOrDefault(b => b.TokenName?.ToUpper() == "USDC");
|
||||
var usdcValue = usdc?.Value ?? 0m;
|
||||
|
||||
var usdcValue = usdcBalance?.Value ?? 0m;
|
||||
var available = usdcValue - totalAllocatedForAccount;
|
||||
return available < 0m ? 0m : available;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user