Add MasterBotUserId and MasterAgentName for copy trading support

- Introduced MasterBotUserId and MasterAgentName properties to facilitate copy trading functionality.
- Updated relevant models, controllers, and database entities to accommodate these new properties.
- Enhanced validation logic in StartCopyTradingCommandHandler to ensure proper ownership checks for master strategies.
This commit is contained in:
2025-11-20 00:33:31 +07:00
parent 97103fbfe8
commit ff2df2d9ac
13 changed files with 1852 additions and 20 deletions

View File

@@ -8,6 +8,7 @@ using Managing.Domain.Accounts;
using Managing.Domain.Bots;
using Managing.Domain.Users;
using MediatR;
using System;
using static Managing.Common.Enums;
namespace Managing.Application.ManageBot
@@ -47,24 +48,31 @@ namespace Managing.Application.ManageBot
throw new ArgumentException($"Master bot with identifier {request.MasterBotIdentifier} not found");
}
// Special validation for Kudai strategy - check staking requirements
if (string.Equals(request.MasterBotIdentifier.ToString(), "Kudai", StringComparison.OrdinalIgnoreCase))
{
await ValidateKudaiStakingRequirements(request.User);
}
else
{
// Verify the user owns the keys of the master strategy
var ownedKeys = await _kaigenService.GetOwnedKeysAsync(request.User);
var hasMasterStrategyKey = ownedKeys.Items.Any(key =>
string.Equals(key.AgentName, masterBot.User.AgentName, StringComparison.OrdinalIgnoreCase) &&
key.Owned >= 1);
// Check if copy trading validation should be bypassed (for testing)
var enableValidation = Environment.GetEnvironmentVariable("ENABLE_COPY_TRADING_VALIDATION")?
.Equals("true", StringComparison.OrdinalIgnoreCase) == true;
if (!hasMasterStrategyKey)
if (enableValidation)
{
// Special validation for Kudai strategy - check staking requirements
if (string.Equals(request.MasterBotIdentifier.ToString(), "Kudai", StringComparison.OrdinalIgnoreCase))
{
throw new UnauthorizedAccessException(
$"You don't own the keys for the master strategy '{request.MasterBotIdentifier}'. " +
"You must own at least 1 key for this strategy to copy trade from it.");
await ValidateKudaiStakingRequirements(request.User);
}
else
{
// Verify the user owns the keys of the master strategy
var ownedKeys = await _kaigenService.GetOwnedKeysAsync(request.User);
var hasMasterStrategyKey = ownedKeys.Items.Any(key =>
string.Equals(key.AgentName, masterBot.User.AgentName, StringComparison.OrdinalIgnoreCase) &&
key.Owned >= 1);
if (!hasMasterStrategyKey)
{
throw new UnauthorizedAccessException(
$"You don't own the keys for the master strategy '{request.MasterBotIdentifier}'. " +
"You must own at least 1 key for this strategy to copy trade from it.");
}
}
}
@@ -156,6 +164,7 @@ namespace Managing.Application.ManageBot
// Set copy trading specific properties
IsForCopyTrading = true,
MasterBotIdentifier = request.MasterBotIdentifier,
MasterBotUserId = masterBot.User.Id,
// Set computed/default properties
IsForBacktest = false,