Enhance user settings management by adding new properties and updating related functionality
This commit introduces additional user settings properties, including TrendStrongAgreementThreshold, SignalAgreementThreshold, AllowSignalTrendOverride, and DefaultExchange, to the User entity and associated DTOs. The UserController and UserService are updated to handle these new settings, allowing users to customize their trading configurations more effectively. Database migrations are also included to ensure proper schema updates for the new fields.
This commit is contained in:
@@ -313,6 +313,10 @@ public class AgentGrain : Grain, IAgentGrain
|
||||
|
||||
public async Task<BalanceCheckResult> CheckAndEnsureEthBalanceAsync(Guid requestingBotId, string accountName)
|
||||
{
|
||||
// Get user settings
|
||||
var userId = (int)this.GetPrimaryKeyLong();
|
||||
var user = await _userService.GetUserByIdAsync(userId);
|
||||
|
||||
// Check if a swap is already in progress
|
||||
if (_state.State.IsSwapInProgress)
|
||||
{
|
||||
@@ -358,6 +362,15 @@ public class AgentGrain : Grain, IAgentGrain
|
||||
};
|
||||
}
|
||||
|
||||
// Check low ETH amount alert threshold
|
||||
var lowEthAlertThreshold = user.LowEthAmountAlert ?? Constants.GMX.Config.MinimumTradeEthBalanceUsd;
|
||||
if (balanceData.EthValueInUsd < lowEthAlertThreshold)
|
||||
{
|
||||
_logger.LogWarning(
|
||||
"ETH balance below alert threshold for user {UserId} - ETH: {EthValue:F2} USD (threshold: {Threshold:F2} USD)",
|
||||
userId, balanceData.EthValueInUsd, lowEthAlertThreshold);
|
||||
}
|
||||
|
||||
_logger.LogInformation(
|
||||
"Agent {UserId} balance check - ETH: {EthValue:F2} USD, USDC: {UsdcValue:F2} USD (cached: {IsCached})",
|
||||
this.GetPrimaryKeyLong(), balanceData.EthValueInUsd, balanceData.UsdcValue,
|
||||
@@ -402,18 +415,34 @@ public class AgentGrain : Grain, IAgentGrain
|
||||
};
|
||||
}
|
||||
|
||||
// Check if we have enough USDC for swap (need at least 5 USD for swap)
|
||||
if (balanceData.UsdcValue <
|
||||
(Constants.GMX.Config.MinimumPositionAmount + (decimal)Constants.GMX.Config.AutoSwapAmount))
|
||||
// Check if autoswap is enabled for this user
|
||||
if (!user.EnableAutoswap)
|
||||
{
|
||||
_logger.LogInformation("Autoswap is disabled for user {UserId}, skipping swap",
|
||||
userId);
|
||||
return new BalanceCheckResult
|
||||
{
|
||||
IsSuccessful = false,
|
||||
FailureReason = BalanceCheckFailureReason.None,
|
||||
Message = "Autoswap is disabled for this user",
|
||||
ShouldStopBot = false
|
||||
};
|
||||
}
|
||||
|
||||
// Get autoswap amount from user settings or use default
|
||||
var autoswapAmount = user.AutoswapAmount ?? (decimal)Constants.GMX.Config.AutoSwapAmount;
|
||||
|
||||
// Check if we have enough USDC for swap
|
||||
if (balanceData.UsdcValue < (Constants.GMX.Config.MinimumPositionAmount + autoswapAmount))
|
||||
{
|
||||
_logger.LogWarning(
|
||||
"Insufficient USDC balance for swap - ETH: {EthValue:F2} USD, USDC: {UsdcValue:F2} USD (need {AutoSwapAmount} USD for swap)",
|
||||
balanceData.EthValueInUsd, balanceData.UsdcValue, Constants.GMX.Config.AutoSwapAmount);
|
||||
balanceData.EthValueInUsd, balanceData.UsdcValue, autoswapAmount);
|
||||
return new BalanceCheckResult
|
||||
{
|
||||
IsSuccessful = false,
|
||||
FailureReason = BalanceCheckFailureReason.InsufficientUsdcForSwap,
|
||||
Message = $"Insufficient USDC balance for swap (need {Constants.GMX.Config.AutoSwapAmount} USD)",
|
||||
Message = $"Insufficient USDC balance for swap (need {autoswapAmount} USD)",
|
||||
ShouldStopBot = true
|
||||
};
|
||||
}
|
||||
@@ -440,22 +469,18 @@ public class AgentGrain : Grain, IAgentGrain
|
||||
|
||||
try
|
||||
{
|
||||
_logger.LogInformation("Initiating USDC to ETH swap for agent {UserId} - swapping 5 USDC",
|
||||
this.GetPrimaryKeyLong());
|
||||
_logger.LogInformation("Initiating USDC to ETH swap for agent {UserId} - swapping {Amount} USDC",
|
||||
this.GetPrimaryKeyLong(), autoswapAmount);
|
||||
|
||||
// Get user for the swap
|
||||
var userId = (int)this.GetPrimaryKeyLong();
|
||||
var user = await _userService.GetUserByIdAsync(userId);
|
||||
|
||||
// Perform the swap
|
||||
// Perform the swap using user's autoswap amount
|
||||
var swapInfo = await _tradingService.SwapGmxTokensAsync(user, accountName,
|
||||
Ticker.USDC, Ticker.ETH, Constants.GMX.Config.AutoSwapAmount);
|
||||
Ticker.USDC, Ticker.ETH, (double)autoswapAmount);
|
||||
|
||||
if (swapInfo.Success)
|
||||
{
|
||||
_logger.LogInformation(
|
||||
"Successfully swapped 5 USDC to ETH for agent {UserId}, transaction hash: {Hash}",
|
||||
userId, swapInfo.Hash);
|
||||
"Successfully swapped {Amount} USDC to ETH for agent {UserId}, transaction hash: {Hash}",
|
||||
autoswapAmount, userId, swapInfo.Hash);
|
||||
|
||||
// Update last swap time and invalidate cache
|
||||
_state.State.LastSwapTime = DateTime.UtcNow;
|
||||
|
||||
Reference in New Issue
Block a user