Add agent summary update functionality and improve user controller

- Introduced a new endpoint in UserController to update the agent summary, ensuring balance data is refreshed after transactions.
- Implemented ForceUpdateSummaryImmediate method in IAgentGrain to allow immediate updates without cooldown checks.
- Enhanced StartBotCommandHandler to force update the agent summary before starting the bot, ensuring accurate balance data.
- Updated TypeScript API client to include the new update-agent-summary method for frontend integration.
This commit is contained in:
2026-01-03 03:09:44 +07:00
parent 78c2788ba7
commit fb49190346
7 changed files with 163 additions and 3 deletions

View File

@@ -68,6 +68,13 @@ namespace Managing.Application.Abstractions.Grains
[OneWay]
Task ForceUpdateSummary();
/// <summary>
/// Forces an immediate update of the agent summary without cooldown check (for critical updates like after topup)
/// Invalidates cached balance data to ensure fresh balance fetch
/// </summary>
[OneWay]
Task ForceUpdateSummaryImmediate();
/// <summary>
/// Updates the agent summary by recalculating from position data (used for initialization or manual refresh)
/// </summary>

View File

@@ -176,6 +176,23 @@ public class AgentGrain : Grain, IAgentGrain
await UpdateSummary();
}
/// <summary>
/// Forces an immediate update of the agent summary without cooldown check (for critical updates like after topup)
/// Invalidates cached balance data to ensure fresh balance fetch
/// </summary>
public async Task ForceUpdateSummaryImmediate()
{
// Invalidate cached balance data to force fresh fetch
_state.State.CachedBalanceData = null;
await _state.WriteStateAsync();
_logger.LogInformation("Force updating agent summary immediately for user {UserId} (cache invalidated)",
this.GetPrimaryKeyLong());
// Update summary immediately without cooldown check
await UpdateSummary();
}
/// <summary>
/// Updates the agent summary by recalculating from position data (used for initialization or manual refresh)
/// </summary>

View File

@@ -0,0 +1,16 @@
using Managing.Domain.Users;
using MediatR;
namespace Managing.Application.ManageBot.Commands
{
public class UpdateAgentSummaryCommand : IRequest<Unit>
{
public User User { get; }
public UpdateAgentSummaryCommand(User user)
{
User = user;
}
}
}

View File

@@ -5,6 +5,7 @@ using Managing.Application.ManageBot.Commands;
using Managing.Common;
using Managing.Domain.Accounts;
using MediatR;
using Microsoft.Extensions.Logging;
using static Managing.Common.Enums;
namespace Managing.Application.ManageBot
@@ -16,15 +17,22 @@ namespace Managing.Application.ManageBot
private readonly IBotService _botService;
private readonly ITradingService _tradingService;
private readonly IFlagsmithService _flagsmithService;
private readonly ILogger<StartBotCommandHandler> _logger;
public StartBotCommandHandler(
IAccountService accountService, IGrainFactory grainFactory, IBotService botService, ITradingService tradingService, IFlagsmithService flagsmithService)
IAccountService accountService,
IGrainFactory grainFactory,
IBotService botService,
ITradingService tradingService,
IFlagsmithService flagsmithService,
ILogger<StartBotCommandHandler> logger)
{
_accountService = accountService;
_grainFactory = grainFactory;
_botService = botService;
_tradingService = tradingService;
_flagsmithService = flagsmithService;
_logger = logger;
}
public async Task<BotStatus> Handle(StartBotCommand request, CancellationToken cancellationToken)
@@ -137,6 +145,19 @@ namespace Managing.Application.ManageBot
$"Balance : {usdcBalance?.Value:F2 ?? 0} Available: {availableAllocation:F2} USDC.");
}
// Force update agent summary to ensure we have the latest balance before starting bot
try
{
var agentGrain = _grainFactory.GetGrain<IAgentGrain>(request.User.Id);
await agentGrain.ForceUpdateSummaryImmediate();
}
catch (Exception ex)
{
// Log but don't fail - balance check already happened
// This is just to ensure summary is up to date before starting
_logger.LogWarning(ex, "Failed to update agent summary before starting bot for user {UserId}", request.User.Id);
}
try
{
var botGrain = _grainFactory.GetGrain<ILiveTradingBotGrain>(Guid.NewGuid());

View File

@@ -0,0 +1,40 @@
using Managing.Application.Abstractions.Grains;
using Managing.Application.ManageBot.Commands;
using MediatR;
using Microsoft.Extensions.Logging;
namespace Managing.Application.ManageBot
{
public class UpdateAgentSummaryCommandHandler : IRequestHandler<UpdateAgentSummaryCommand, Unit>
{
private readonly IGrainFactory _grainFactory;
private readonly ILogger<UpdateAgentSummaryCommandHandler> _logger;
public UpdateAgentSummaryCommandHandler(
IGrainFactory grainFactory,
ILogger<UpdateAgentSummaryCommandHandler> logger)
{
_grainFactory = grainFactory;
_logger = logger;
}
public async Task<Unit> Handle(UpdateAgentSummaryCommand request, CancellationToken cancellationToken)
{
try
{
var agentGrain = _grainFactory.GetGrain<IAgentGrain>(request.User.Id);
await agentGrain.ForceUpdateSummaryImmediate();
_logger.LogInformation("Agent summary updated for user {UserId} after topup", request.User.Id);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error updating agent summary for user {UserId}", request.User.Id);
throw;
}
return Unit.Value;
}
}
}