using CryptoExchange.Net.SharedApis;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
namespace CryptoExchange.Net
{
///
/// Cache for symbol parsing
///
public static class ExchangeSymbolCache
{
private static ConcurrentDictionary _symbolInfos = new ConcurrentDictionary();
///
/// Update the cached symbol data for an exchange
///
/// Id for the provided data
/// Symbol data
public static void UpdateSymbolInfo(string topicId, SharedSpotSymbol[] updateData)
{
if(!_symbolInfos.TryGetValue(topicId, out var exchangeInfo))
{
exchangeInfo = new ExchangeInfo(DateTime.UtcNow, updateData.ToDictionary(x => x.Name, x => x.SharedSymbol));
_symbolInfos.TryAdd(topicId, exchangeInfo);
}
if (DateTime.UtcNow - exchangeInfo.UpdateTime < TimeSpan.FromMinutes(60))
return;
_symbolInfos[topicId] = new ExchangeInfo(DateTime.UtcNow, updateData.ToDictionary(x => x.Name, x => x.SharedSymbol));
}
///
/// Parse a symbol name to a SharedSymbol
///
/// Id for the provided data
/// Symbol name
public static SharedSymbol? ParseSymbol(string topicId, string? symbolName)
{
if (symbolName == null)
return null;
if (!_symbolInfos.TryGetValue(topicId, out var exchangeInfo))
return null;
if (!exchangeInfo.Symbols.TryGetValue(symbolName, out var symbolInfo))
return null;
return new SharedSymbol(symbolInfo.TradingMode, symbolInfo.BaseAsset, symbolInfo.QuoteAsset, symbolName)
{
DeliverTime = symbolInfo.DeliverTime
};
}
class ExchangeInfo
{
public DateTime UpdateTime { get; set; }
public Dictionary Symbols { get; set; }
public ExchangeInfo(DateTime updateTime, Dictionary symbols)
{
UpdateTime = updateTime;
Symbols = symbols;
}
}
}
}