Use usdc balance instead usdc value for ensuring balance check

This commit is contained in:
2025-10-05 01:36:25 +07:00
parent 63683d6bdf
commit de0d042254
7 changed files with 72 additions and 96 deletions

View File

@@ -590,7 +590,7 @@ public class AgentGrain : Grain, IAgentGrain
return null;
}
var userAccounts = await _accountService.GetAccountsByUserAsync(user, hideSecrets: true, true);
var userAccounts = await _accountService.GetAccountsByUserAsync(user, hideSecrets: true, false);
var account = userAccounts.FirstOrDefault(a => a.Name == accountName);
if (account == null)
{
@@ -604,7 +604,7 @@ public class AgentGrain : Grain, IAgentGrain
var usdcBalance = balances.FirstOrDefault(b => b.TokenName?.ToUpper() == "USDC");
var ethValueInUsd = ethBalance?.Amount * ethBalance?.Price ?? 0;
var usdcValue = usdcBalance?.Value ?? 0;
var usdc = usdcBalance?.Amount ?? 0;
// Cache the balance data
var balanceData = new CachedBalanceData
@@ -612,7 +612,7 @@ public class AgentGrain : Grain, IAgentGrain
LastFetched = DateTime.UtcNow,
AccountName = accountName,
EthValueInUsd = ethValueInUsd,
UsdcValue = usdcValue
UsdcValue = usdc
};
_state.State.CachedBalanceData = balanceData;

View File

@@ -67,7 +67,7 @@ namespace Managing.Application.Bots.Models
/// <summary>
/// Whether the cached data is still valid (less than 1 minute old)
/// </summary>
public bool IsValid => DateTime.UtcNow - LastFetched < TimeSpan.FromMinutes(1.5);
public bool IsValid => DateTime.UtcNow - LastFetched < TimeSpan.FromMinutes(1);
}
/// <summary>

View File

@@ -394,35 +394,28 @@ namespace Managing.Application.ManageBot
var totalAllocatedForAccount = 0m;
var usdcBalance = account.Balances.FirstOrDefault(b => b.TokenName == Ticker.USDC.ToString());
if (botsForUser.Any())
foreach (var bot in botsForUser)
{
foreach (var bot in botsForUser)
if (excludeIdentifier != default && bot.Identifier == excludeIdentifier)
{
if (excludeIdentifier != default && bot.Identifier == excludeIdentifier)
{
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;
}
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;
}
}
else
{
totalAllocatedForAccount = usdcBalance?.Value ?? 0m;
}
var usdcValue = usdcBalance?.Value ?? 0m;