diff --git a/src/Managing.Api/Controllers/AccountController.cs b/src/Managing.Api/Controllers/AccountController.cs index 9338d700..d5985b0d 100644 --- a/src/Managing.Api/Controllers/AccountController.cs +++ b/src/Managing.Api/Controllers/AccountController.cs @@ -14,18 +14,22 @@ namespace Managing.Api.Controllers public class AccountController : BaseController { private readonly IAccountService _AccountService; + private readonly ITradingService _TradingService; /// /// Initializes a new instance of the class. /// /// Service for account-related operations. /// Service for user-related operations. + /// Service for trading-related operations. public AccountController( IAccountService AccountService, - IUserService userService) + IUserService userService, + ITradingService TradingService) : base(userService) { _AccountService = AccountService; + _TradingService = TradingService; } /// @@ -101,7 +105,7 @@ namespace Managing.Api.Controllers public async Task> SwapGmxTokens(string name, [FromBody] SwapTokensRequest request) { var user = await GetUser(); - var result = await _AccountService.SwapGmxTokensAsync( + var result = await _TradingService.SwapGmxTokensAsync( user, name, request.FromTicker, diff --git a/src/Managing.Application.Abstractions/Services/IAccountService.cs b/src/Managing.Application.Abstractions/Services/IAccountService.cs index 88bc9722..3d1f68dd 100644 --- a/src/Managing.Application.Abstractions/Services/IAccountService.cs +++ b/src/Managing.Application.Abstractions/Services/IAccountService.cs @@ -37,9 +37,6 @@ public interface IAccountService Task> GetAccountsBalancesByUserAsync(User user, bool hideSecrets = true); Task GetGmxClaimableSummaryAsync(User user, string accountName); - Task SwapGmxTokensAsync(User user, string accountName, Ticker fromTicker, Ticker toTicker, - double amount, string orderType = "market", double? triggerRatio = null, double allowedSlippage = 0.5); - Task SendTokenAsync(User user, string accountName, string recipientAddress, Ticker ticker, decimal amount, int? chainId = null); diff --git a/src/Managing.Application.Abstractions/Services/ITradingService.cs b/src/Managing.Application.Abstractions/Services/ITradingService.cs index c7758dd9..17fd79e9 100644 --- a/src/Managing.Application.Abstractions/Services/ITradingService.cs +++ b/src/Managing.Application.Abstractions/Services/ITradingService.cs @@ -55,4 +55,6 @@ public interface ITradingService Task GetIndicatorByNameUserAsync(string name, User user); Task GetScenarioByNameUserAsync(string scenarioName, User user); Task> GetPositionByUserIdAsync(int userId); + Task SwapGmxTokensAsync(User user, string accountName, Ticker fromTicker, Ticker toTicker, + double amount, string orderType = "market", double? triggerRatio = null, double allowedSlippage = 0.5); } \ No newline at end of file diff --git a/src/Managing.Application/Accounts/AccountService.cs b/src/Managing.Application/Accounts/AccountService.cs index 05beadb8..d21fa052 100644 --- a/src/Managing.Application/Accounts/AccountService.cs +++ b/src/Managing.Application/Accounts/AccountService.cs @@ -258,46 +258,6 @@ public class AccountService : IAccountService } } - public async Task SwapGmxTokensAsync(User user, string accountName, Ticker fromTicker, Ticker toTicker, - double amount, string orderType = "market", double? triggerRatio = null, double allowedSlippage = 0.5) - { - // Get the account for the user - var account = await GetAccountByUser(user, accountName, true, false); - - if (account == null) - { - throw new ArgumentException($"Account '{accountName}' not found for user '{user.Name}'"); - } - - // Ensure the account has a valid address/key - if (string.IsNullOrEmpty(account.Key)) - { - throw new ArgumentException($"Account '{accountName}' does not have a valid address"); - } - - try - { - // Call the Web3ProxyService to swap GMX tokens - var swapInfos = await _web3ProxyService.SwapGmxTokensAsync( - account.Key, - fromTicker, - toTicker, - amount, - orderType, - triggerRatio, - allowedSlippage - ); - - return swapInfos; - } - catch (Exception ex) when (!(ex is ArgumentException || ex is InvalidOperationException)) - { - _logger.LogError(ex, "Error swapping GMX tokens for account {AccountName} and user {UserName}", - accountName, user.Name); - throw new InvalidOperationException($"Failed to swap GMX tokens: {ex.Message}", ex); - } - } - public async Task SendTokenAsync(User user, string accountName, string recipientAddress, Ticker ticker, decimal amount, int? chainId = null) { diff --git a/src/Managing.Application/Bots/BacktestSpotBot.cs b/src/Managing.Application/Bots/BacktestSpotBot.cs new file mode 100644 index 00000000..e69de29b diff --git a/src/Managing.Application/Bots/Grains/AgentGrain.cs b/src/Managing.Application/Bots/Grains/AgentGrain.cs index ed8529a7..9d522699 100644 --- a/src/Managing.Application/Bots/Grains/AgentGrain.cs +++ b/src/Managing.Application/Bots/Grains/AgentGrain.cs @@ -448,7 +448,7 @@ public class AgentGrain : Grain, IAgentGrain var user = await _userService.GetUserByIdAsync(userId); // Perform the swap - var swapInfo = await _accountService.SwapGmxTokensAsync(user, accountName, + var swapInfo = await _tradingService.SwapGmxTokensAsync(user, accountName, Ticker.USDC, Ticker.ETH, Constants.GMX.Config.AutoSwapAmount); if (swapInfo.Success) diff --git a/src/Managing.Application/Trading/TradingService.cs b/src/Managing.Application/Trading/TradingService.cs index 3c2e4888..8364b7e7 100644 --- a/src/Managing.Application/Trading/TradingService.cs +++ b/src/Managing.Application/Trading/TradingService.cs @@ -28,6 +28,7 @@ public class TradingService : ITradingService private readonly IEvmManager _evmManager; private readonly ILogger _logger; private readonly ISynthPredictionService _synthPredictionService; + private readonly IWeb3ProxyService _web3ProxyService; public TradingService( ITradingRepository tradingRepository, @@ -39,7 +40,8 @@ public class TradingService : ITradingService IMessengerService messengerService, IStatisticRepository statisticRepository, IEvmManager evmManager, - ISynthPredictionService synthPredictionService) + ISynthPredictionService synthPredictionService, + IWeb3ProxyService web3ProxyService) { _tradingRepository = tradingRepository; _exchangeService = exchangeService; @@ -51,6 +53,7 @@ public class TradingService : ITradingService _statisticRepository = statisticRepository; _evmManager = evmManager; _synthPredictionService = synthPredictionService; + _web3ProxyService = web3ProxyService; } public async Task DeleteScenarioAsync(string name) @@ -447,4 +450,44 @@ public class TradingService : ITradingService { return await _tradingRepository.GetPositionByUserIdAsync(userId); } + + public async Task SwapGmxTokensAsync(User user, string accountName, Ticker fromTicker, Ticker toTicker, + double amount, string orderType = "market", double? triggerRatio = null, double allowedSlippage = 0.5) + { + // Get the account for the user + var account = await _accountService.GetAccountByUser(user, accountName, true, false); + + if (account == null) + { + throw new ArgumentException($"Account '{accountName}' not found for user '{user.Name}'"); + } + + // Ensure the account has a valid address/key + if (string.IsNullOrEmpty(account.Key)) + { + throw new ArgumentException($"Account '{accountName}' does not have a valid address"); + } + + try + { + // Call the Web3ProxyService to swap GMX tokens + var swapInfos = await _web3ProxyService.SwapGmxTokensAsync( + account.Key, + fromTicker, + toTicker, + amount, + orderType, + triggerRatio, + allowedSlippage + ); + + return swapInfos; + } + catch (Exception ex) when (!(ex is ArgumentException || ex is InvalidOperationException)) + { + _logger.LogError(ex, "Error swapping GMX tokens for account {AccountName} and user {UserName}", + accountName, user.Name); + throw new InvalidOperationException($"Failed to swap GMX tokens: {ex.Message}", ex); + } + } } \ No newline at end of file