Fix worker cancelled on worker + Cache tickers

This commit is contained in:
2025-04-30 13:19:03 +07:00
parent df4392b17e
commit bc1ef78747
11 changed files with 116 additions and 55 deletions

View File

@@ -336,20 +336,43 @@ public class EvmManager : IEvmManager
return chainBalances;
}
public async Task<List<Candle>> GetCandles(Ticker ticker, DateTime startDate,
Timeframe timeframe)
public async Task<List<Candle>> GetCandles(Ticker ticker, DateTime startDate, Timeframe timeframe,
bool isFirstCall = false)
{
string gmxTimeframe = GmxHelpers.GeTimeframe(timeframe);
var gmxPrices = await _httpClient.GetFromJsonAsync<GmxV2Prices>(
$"https://arbitrum-api.gmxinfra.io/prices/candles?tokenSymbol={ticker}&period={gmxTimeframe}&limit=10000");
int limit = isFirstCall ? 10000 : CalculateCandleLimit(startDate, timeframe);
GmxV2Prices? gmxPrices = null;
int maxRetries = 3;
int delayMs = 1000;
for (int attempt = 1; attempt <= maxRetries; attempt++)
{
try
{
gmxPrices = await _httpClient.GetFromJsonAsync<GmxV2Prices>(
$"https://arbitrum-api.gmxinfra.io/prices/candles?tokenSymbol={ticker}&period={gmxTimeframe}&limit={limit}");
break;
}
catch (HttpRequestException ex) when (ex.InnerException is IOException)
{
Console.Error.WriteLine($"Attempt {attempt}: Network error while fetching candles: {ex.Message}");
if (attempt == maxRetries)
throw;
await Task.Delay(delayMs * attempt);
}
catch (Exception ex)
{
Console.Error.WriteLine($"Unexpected error: {ex.Message}");
throw;
}
}
if (gmxPrices == null)
return null;
var filteredCandles = gmxPrices.Candles.Where(p => p[0] >= startDate.ToUnixTimestamp()).ToList();
var candles = new List<Candle>();
var timeBetweenCandles =
gmxPrices.Candles.Count > 2 ? gmxPrices.Candles[0][0] - gmxPrices.Candles[1][0] : 900; // Default 15 minutes
@@ -362,6 +385,25 @@ public class EvmManager : IEvmManager
return candles.OrderBy(c => c.Date).ToList();
}
private int CalculateCandleLimit(DateTime startDate, Timeframe timeframe)
{
var now = DateTime.UtcNow;
var minutesPerCandle = timeframe switch
{
Timeframe.OneMinute => 1,
Timeframe.FiveMinutes => 5,
Timeframe.FifteenMinutes => 15,
Timeframe.ThirtyMinutes => 30,
Timeframe.OneHour => 60,
Timeframe.FourHour => 240,
Timeframe.OneDay => 1440,
_ => 15
};
var totalMinutes = (now - startDate).TotalMinutes;
var candlesNeeded = (int)Math.Ceiling(totalMinutes / minutesPerCandle);
return Math.Min(candlesNeeded + 5, 10000);
}
public decimal GetVolume(SubgraphProvider subgraphProvider, Ticker ticker)
{
var subgraph = GetSubgraph(subgraphProvider);
@@ -850,4 +892,10 @@ public class EvmManager : IEvmManager
await _web3ProxyService.CallPrivyServiceAsync<PrivySigningResponse>("sign-message", requestBody);
return response.Signature;
}
// Overload to match IEvmManager interface
public async Task<List<Candle>> GetCandles(Ticker ticker, DateTime startDate, Timeframe timeframe)
{
return await GetCandles(ticker, startDate, timeframe, false);
}
}