Files
managing-apps/src/Managing.Api/Controllers/DataController.cs
Oda 65bdb8e34f GMX v2 - Trading (#7)
* Move PrivateKeys.cs

* Update gitignore

* Update gitignore

* updt

* Extract GmxServiceTests.cs

* Refact

* update todo

* Update code

* Fix hashdata

* Replace static token hashed datas

* Set allowance

* Add get orders

* Add get orders tests

* Add ignore

* add close orders

* revert

* Add get gas limit

* Start increasePosition. Todo: Finish GetExecutionFee and estimateGas

* little refact

* Update gitignore

* Fix namespaces and clean repo

* Add tests samples

* Add execution fee

* Add increase position

* Handle backtest on the frontend

* Add tests

* Update increase

* Test increase

* fix increase

* Fix size

* Start get position

* Update get positions

* Fix get position

* Update rpc and trade mappers

* Finish close position

* Fix leverage
2025-01-30 23:06:22 +07:00

100 lines
4.2 KiB
C#

using Managing.Application.Abstractions;
using Managing.Application.Abstractions.Services;
using Managing.Application.Hubs;
using Managing.Application.Workers.Abstractions;
using Managing.Domain.Candles;
using Managing.Domain.Statistics;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using static Managing.Common.Enums;
namespace Managing.Api.Controllers;
/// <summary>
/// Controller for handling data-related operations such as retrieving tickers, spotlight data, and candles.
/// Requires authorization for access.
/// </summary>
[ApiController]
[Authorize]
[Route("[controller]")]
public class DataController : ControllerBase
{
private readonly IExchangeService _exchangeService;
private readonly IAccountService _accountService;
private readonly ICacheService _cacheService;
private readonly IStatisticService _statisticService;
private readonly IHubContext<CandleHub> _hubContext;
/// <summary>
/// Initializes a new instance of the <see cref="DataController"/> class.
/// </summary>
/// <param name="exchangeService">Service for interacting with exchanges.</param>
/// <param name="accountService">Service for account management.</param>
/// <param name="cacheService">Service for caching data.</param>
/// <param name="statisticService">Service for statistical analysis.</param>
/// <param name="hubContext">SignalR hub context for real-time communication.</param>
public DataController(
IExchangeService exchangeService,
IAccountService accountService,
ICacheService cacheService,
IStatisticService statisticService,
IHubContext<CandleHub> hubContext)
{
_exchangeService = exchangeService;
_accountService = accountService;
_cacheService = cacheService;
_statisticService = statisticService;
_hubContext = hubContext;
}
/// <summary>
/// Retrieves tickers for a given account and timeframe, utilizing caching to improve performance.
/// </summary>
/// <param name="accountName">The name of the account to retrieve tickers for.</param>
/// <param name="timeframe">The timeframe for which to retrieve tickers.</param>
/// <returns>An array of tickers.</returns>
[HttpPost("GetTickers")]
public async Task<ActionResult<Ticker[]>> GetTickers(string accountName, Timeframe timeframe)
{
var account = await _accountService.GetAccount(accountName, true, false);
var cacheKey = string.Concat(accountName, timeframe.ToString());
var tickers = _cacheService.GetOrSave(cacheKey,
() => { return _exchangeService.GetTickers(account, timeframe).Result; }, TimeSpan.FromHours(2));
return Ok(tickers);
}
/// <summary>
/// Retrieves the latest spotlight overview, using caching to enhance response times.
/// </summary>
/// <returns>A <see cref="SpotlightOverview"/> object containing spotlight data.</returns>
[HttpGet("Spotlight")]
public ActionResult<SpotlightOverview> GetSpotlight()
{
var overview = _cacheService.GetOrSave(nameof(SpotlightOverview),
() => { return _statisticService.GetLastSpotlight(DateTime.Now.AddHours(-3)); }, TimeSpan.FromMinutes(2));
if (overview?.Spotlights.Count < overview?.ScenarioCount)
{
overview = _statisticService.GetLastSpotlight(DateTime.Now.AddHours(-3));
}
return Ok(overview);
}
/// <summary>
/// Retrieves candle data for a given exchange, ticker, start date, and timeframe.
/// </summary>
/// <param name="exchange">The exchange to retrieve candles from.</param>
/// <param name="ticker">The ticker symbol to retrieve candles for.</param>
/// <param name="startDate">The start date for the candle data.</param>
/// <param name="timeframe">The timeframe for the candle data.</param>
/// <returns>A list of <see cref="Candle"/> objects.</returns>
[HttpGet("GetCandles")]
public async Task<ActionResult<List<Candle>>> GetCandles(TradingExchanges exchange, Ticker ticker,
DateTime startDate, Timeframe timeframe)
{
return Ok(await _exchangeService.GetCandlesInflux(exchange, ticker, startDate, timeframe));
}
}