From e0a064456a60214a50efeca23bb2b37692d26871 Mon Sep 17 00:00:00 2001 From: cryptooda Date: Tue, 6 Jan 2026 17:39:01 +0700 Subject: [PATCH] Refactor bots allocation USD value calculation in AgentService and AgentGrain - Updated the calculation of bots allocation USD value to directly sum BotTradingBalance from Bot entities, eliminating the need for additional service calls to fetch bot configurations. - This change aims to prevent potential deadlocks and improve performance by reducing unnecessary asynchronous calls. --- src/Managing.Application/Agents/AgentService.cs | 8 +++----- src/Managing.Application/Bots/Grains/AgentGrain.cs | 10 +++------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/Managing.Application/Agents/AgentService.cs b/src/Managing.Application/Agents/AgentService.cs index b3b1355a..a2dee3c3 100644 --- a/src/Managing.Application/Agents/AgentService.cs +++ b/src/Managing.Application/Agents/AgentService.cs @@ -321,11 +321,9 @@ public class AgentService : IAgentService var botsAllocationUsdValue = 0m; if (activeStrategies.Any()) { - var botIds = activeStrategies.Select(b => b.Identifier); - var botConfigs = await ServiceScopeHelpers.WithScopedService>( - _serviceScopeFactory, - async botService => await botService.GetBotConfigsByIdsAsync(botIds)); - botsAllocationUsdValue = botConfigs.Sum(config => config.BotTradingBalance); + // Calculate bots allocation USD value directly from Bot entities (avoid calling grains to prevent deadlocks) + // Bot entities already contain BotTradingBalance from the database + botsAllocationUsdValue = activeStrategies.Sum(bot => bot.BotTradingBalance); } var freshBalance = new AgentBalance diff --git a/src/Managing.Application/Bots/Grains/AgentGrain.cs b/src/Managing.Application/Bots/Grains/AgentGrain.cs index 6296d76d..60b74b9a 100644 --- a/src/Managing.Application/Bots/Grains/AgentGrain.cs +++ b/src/Managing.Application/Bots/Grains/AgentGrain.cs @@ -261,13 +261,9 @@ public class AgentGrain : Grain, IAgentGrain { runtime = activeStrategies.Min(p => p.StartupTime); - // Calculate bots allocation USD value from bot configurations - var botIds = activeStrategies.Select(b => b.Identifier); - var botConfigs = - await ServiceScopeHelpers.WithScopedService>( - _scopeFactory, - async (botService) => { return await botService.GetBotConfigsByIdsAsync(botIds); }); - botsAllocationUsdValue = botConfigs.Sum(config => config.BotTradingBalance); + // Calculate bots allocation USD value directly from Bot entities (avoid calling grains to prevent deadlocks) + // Bot entities already contain BotTradingBalance from the database + botsAllocationUsdValue = activeStrategies.Sum(bot => bot.BotTradingBalance); } var summary = new AgentSummary