Gmx V2 Prices (#8)
* Update prices api * Update worker services in Program.cs
This commit is contained in:
@@ -11,6 +11,7 @@ using Managing.Domain.Trades;
|
||||
using Managing.Infrastructure.Evm.Abstractions;
|
||||
using Managing.Infrastructure.Evm.Models;
|
||||
using Managing.Infrastructure.Evm.Models.Gmx.v1;
|
||||
using Managing.Infrastructure.Evm.Models.Gmx.v2;
|
||||
using Managing.Infrastructure.Evm.Referentials;
|
||||
using Managing.Infrastructure.Evm.Services;
|
||||
using Managing.Infrastructure.Evm.Services.Gmx;
|
||||
@@ -38,6 +39,12 @@ public class EvmManager : IEvmManager
|
||||
private Dictionary<string, Dictionary<string, decimal>> _geckoPrices;
|
||||
private readonly GmxV2Service _gmxV2Service;
|
||||
|
||||
private readonly List<Ticker> _eligibleTickers = new List<Ticker>()
|
||||
{
|
||||
Ticker.BTC, Ticker.ETH, Ticker.LINK, Ticker.GMX, Ticker.SOL, Ticker.XRP, Ticker.ARB, Ticker.BNB, Ticker.AAVE,
|
||||
Ticker.PEPE, Ticker.DOGE, Ticker.UNI
|
||||
};
|
||||
|
||||
public EvmManager(IEnumerable<ISubgraphPrices> subgraphs)
|
||||
{
|
||||
var defaultChain = ChainService.GetEthereum();
|
||||
@@ -366,27 +373,24 @@ public class EvmManager : IEvmManager
|
||||
Timeframe timeframe)
|
||||
{
|
||||
string gmxTimeframe = GmxHelpers.GeTimeframe(timeframe);
|
||||
var gmxPrices = await _httpClient.GetFromJsonAsync<GmxPrices>(
|
||||
$"https://stats.gmx.io/api/candles/{ticker}?preferableChainId=42161&period={gmxTimeframe}&from={startDate.ToUnixTimestamp()}&preferableSource=fast");
|
||||
//var subgraph = _subgraphs.First(s => s.GetProvider() == subgraphProvider);
|
||||
//var prices = await subgraph.GetPrices(ticker, startDate, timeframe);
|
||||
|
||||
//if (prices == null)
|
||||
//{
|
||||
// foreach (var subgraphFallback in _subgraphs.Where(s => s.GetProvider() != subgraphProvider))
|
||||
// {
|
||||
// prices = await subgraphFallback.GetPrices(ticker, startDate, timeframe);
|
||||
|
||||
// if (prices != null)
|
||||
// break;
|
||||
// }
|
||||
//}
|
||||
var gmxPrices = await _httpClient.GetFromJsonAsync<GmxV2Prices>(
|
||||
$"https://arbitrum-api.gmxinfra.io/prices/candles?tokenSymbol={ticker}&period={gmxTimeframe}&limit=10000");
|
||||
|
||||
if (gmxPrices == null)
|
||||
return null;
|
||||
|
||||
gmxPrices.prices.RemoveAt(gmxPrices.prices.Count - 1);
|
||||
return gmxPrices.prices.Select(p => GmxMappers.Map(p, ticker, timeframe)).ToList();
|
||||
gmxPrices.Candles = gmxPrices.Candles.Where(p => p[0] >= startDate.ToUnixTimestamp()).ToList();
|
||||
gmxPrices.Candles.RemoveAt(gmxPrices.Candles.Count - 1);
|
||||
|
||||
var candles = new List<Candle>();
|
||||
var timeBetweenCandles = gmxPrices.Candles[0][0] - gmxPrices.Candles[1][0];
|
||||
for (int i = 0; i < gmxPrices.Candles.Count; i++)
|
||||
{
|
||||
var c = GmxV2Mappers.Map(gmxPrices.Candles[i], ticker, timeframe, (int)timeBetweenCandles);
|
||||
candles.Add(c);
|
||||
}
|
||||
|
||||
return candles.OrderBy(c => c.Date).ToList();
|
||||
}
|
||||
|
||||
public decimal GetVolume(SubgraphProvider subgraphProvider, Ticker ticker)
|
||||
@@ -403,9 +407,13 @@ public class EvmManager : IEvmManager
|
||||
|
||||
public async Task<List<Ticker>> GetAvailableTicker()
|
||||
{
|
||||
var subgraph = GetSubgraph(SubgraphProvider.Gbc);
|
||||
var pairs = await subgraph.GetTickers();
|
||||
return pairs.ToList();
|
||||
var tokenList = await _httpClient.GetFromJsonAsync<GmxV2TokenList>(
|
||||
"https://arbitrum-api.gmxinfra.io/tokens");
|
||||
|
||||
if (tokenList == null)
|
||||
return null;
|
||||
|
||||
return GmxV2Mappers.Map(tokenList).Where(t => _eligibleTickers.Contains(t)).ToList();
|
||||
}
|
||||
|
||||
public async Task<Candle> GetCandle(SubgraphProvider subgraphProvider, Ticker ticker)
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Managing.Infrastructure.Evm.Models.Gmx.v2;
|
||||
|
||||
public class GmxV2Prices
|
||||
{
|
||||
[JsonProperty("period")]
|
||||
[JsonPropertyName("period")]
|
||||
public string Period { get; set; }
|
||||
|
||||
[JsonProperty("candles")]
|
||||
[JsonPropertyName("candles")]
|
||||
public List<List<double>> Candles { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Managing.Infrastructure.Evm.Models.Gmx.v2;
|
||||
|
||||
public class GmxV2TokenList
|
||||
{
|
||||
[JsonProperty("tokens")]
|
||||
[JsonPropertyName("tokens")]
|
||||
public List<Token> Tokens { get; set; }
|
||||
}
|
||||
|
||||
public class Token
|
||||
{
|
||||
[JsonProperty("symbol")]
|
||||
[JsonPropertyName("symbol")]
|
||||
public string Symbol { get; set; }
|
||||
|
||||
[JsonProperty("address")]
|
||||
[JsonPropertyName("address")]
|
||||
public string Address { get; set; }
|
||||
|
||||
[JsonProperty("decimals")]
|
||||
[JsonPropertyName("decimals")]
|
||||
public int Decimals { get; set; }
|
||||
|
||||
[JsonProperty("synthetic")]
|
||||
[JsonPropertyName("synthetic")]
|
||||
public bool? Synthetic { get; set; }
|
||||
}
|
||||
@@ -2,6 +2,7 @@ using System.Numerics;
|
||||
using Managing.ABI.GmxV2.SyntheticsReader.ContractDefinition;
|
||||
using Managing.Common;
|
||||
using Managing.Core;
|
||||
using Managing.Domain.Candles;
|
||||
using Managing.Domain.Trades;
|
||||
using Managing.Infrastructure.Evm.Models.Gmx.v2;
|
||||
using Nethereum.Web3;
|
||||
@@ -104,4 +105,39 @@ internal static class GmxV2Mappers
|
||||
ShortTokenPrice = mp.ShortTokenPrice
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
public static Candle Map(List<double> marketPrices, Enums.Ticker ticker, Enums.Timeframe timeframe, int timeBetween)
|
||||
{
|
||||
return new Candle()
|
||||
{
|
||||
Date = DateHelpers.GetFromUnixTimestamp((int)marketPrices[0]).AddSeconds(timeBetween).AddSeconds(-1),
|
||||
OpenTime = DateHelpers.GetFromUnixTimestamp((int)marketPrices[0]),
|
||||
Open = Convert.ToDecimal(marketPrices[1]),
|
||||
High = Convert.ToDecimal(marketPrices[2]),
|
||||
Low = Convert.ToDecimal(marketPrices[3]),
|
||||
Close = Convert.ToDecimal(marketPrices[4]),
|
||||
Exchange = Enums.TradingExchanges.Evm,
|
||||
Ticker = ticker.ToString(),
|
||||
Timeframe = timeframe
|
||||
};
|
||||
}
|
||||
|
||||
public static List<Enums.Ticker> Map(GmxV2TokenList tokenList)
|
||||
{
|
||||
var tokens = new List<Enums.Ticker>();
|
||||
foreach (var t in tokenList.Tokens)
|
||||
{
|
||||
try
|
||||
{
|
||||
var ticker = MiscExtensions.ParseEnum<Enums.Ticker>(t.Symbol);
|
||||
tokens.Add(ticker);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
}
|
||||
|
||||
return tokens;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user