/// <summary> /// <inheritdoc/> /// </summary> /// <param name="source"><inheritdoc/></param> /// <param name="dest"><inheritdoc/></param> /// <param name="amount"><inheritdoc/></param> /// <returns><inheritdoc/></returns> protected override async Task <TimedQuote> getQuoteAsync(Currencies source, Currencies dest, ConversionBag convStatus) { // conditions of convertibility have been done already, but convStatus can be null if (convStatus == null) { convStatus = new ConversionBag(); } string idKey = $"{source}{dest}{DefaultCurrency}{this.GetHashCode()}"; convStatus.Bag.Add(idKey, "yes"); var qs = await ConversionProviderFactory.GetConversionProvider().GetQuoteAsync(source, DefaultCurrency, convStatus); if (qs == null) { return(null); } var qd = await ConversionProviderFactory.GetConversionProvider().GetQuoteAsync(dest, DefaultCurrency, convStatus); if (qd == null) { return(null); } return(new TimedQuote() { BaseCurrency = source, QuoteCurrency = dest, Midpoint = qs.Midpoint / qd.Midpoint, UpdatedAtUTC = qs.UpdatedAtUTC < qd.UpdatedAtUTC ? qs.UpdatedAtUTC : qd.UpdatedAtUTC }); }
/// <summary> /// <inheritdoc/> /// </summary> /// <param name="source"><inheritdoc/></param> /// <param name="dest"><inheritdoc/></param> /// <param name="convStatus"><inheritdoc/></param> /// <returns><inheritdoc/></returns> public virtual TimedQuote GetQuote(Currencies source, Currencies dest, ConversionBag convStatus) { if (source == dest) { return new TimedQuote() { BaseCurrency = source, Midpoint = 1, QuoteCurrency = dest, UpdatedAtUTC = DateTime.UtcNow.AddYears(1) } } ; if (CanConvert(source, dest, convStatus) == true) { TimedQuote quote = getQuote(source, dest, convStatus); if (quote != null && quote.UpdatedAtUTC.AddMilliseconds(ConversionProviderFactory.QuotesValidForMillis) >= DateTime.UtcNow) { if (this is CacheConversionProvider == false) // if the provider who got us the quote is not the cache provider { ConversionProviderFactory.AddToCache(quote, quote.UpdatedAtUTC); // add the quote to the cache } return(quote); } } if (TryNext == false) { return(null); } return(ConversionProviderFactory.GetNext(this)?.GetQuote(source, dest, convStatus)); }
/// <summary> /// <inheritdoc/> /// </summary> /// <param name="source"><inheritdoc/></param> /// <param name="dest"><inheritdoc/></param> /// <param name="amount"><inheritdoc/></param> /// <returns><inheritdoc/></returns> protected override TimedQuote getQuote(Currencies source, Currencies dest, ConversionBag convStatus) { var quote = FixedConversions.ContainsKey(source) ? FixedConversions[source][dest] : FixedConversions[dest][source]; // conditions of convertibility have been done already return(new TimedQuote() { BaseCurrency = source, QuoteCurrency = dest, Midpoint = source == quote.BaseCurrency ? quote.Midpoint : 1 / quote.Midpoint, UpdatedAtUTC = DateTime.UtcNow }); }
public override bool CanConvert(Currencies source, Currencies dest, ConversionBag convStatus) { if (base.CanConvert(source, dest, convStatus) == false) { return(false); } lock (sync) { bool containsQuote = quotes.ContainsKey(source) && quotes[source].ContainsKey(dest); bool isValid = containsQuote && quotes[source][dest].UpdatedAtUTC.AddMilliseconds(CacheExpirationMillis) >= DateTime.UtcNow; return(containsQuote && isValid); } }
/// <summary> /// Will be able to convert only if both currencies are different from the BaseCurrency /// <inheritdoc/> /// </summary> /// <param name="source"><inheritdoc/></param> /// <param name="dest"><inheritdoc/></param> /// <returns></returns> public override bool CanConvert(Currencies source, Currencies dest, ConversionBag convStatus) { if (base.CanConvert(source, dest, convStatus) == false) { return(false); } string idKey = $"{source}{dest}{DefaultCurrency}{this.GetHashCode()}"; if (convStatus?.Bag.ContainsKey(idKey) ?? false) { return(false); } return(source != DefaultCurrency && dest != DefaultCurrency); }
/// <summary> /// <inheritdoc/> /// </summary> /// <param name="source"><inheritdoc/></param> /// <param name="dest"><inheritdoc/></param> /// <param name="amount"><inheritdoc/></param> /// <returns> <inheritdoc/></returns> public decimal Convert(Currencies source, Currencies dest, decimal amount, ConversionBag convStatus) { if (source.Equals(dest)) { return(amount); } if (amount == decimal.Zero || amount == decimal.MinValue || amount == decimal.MaxValue) { return(amount); } var quote = GetQuote(source, dest, convStatus); if (quote == null) { return(decimal.MinValue); } return(quote.BaseCurrency.Equals(source) ? quote.Midpoint * amount : amount / quote.Midpoint); }
/// <summary> /// <inheritdoc/> /// </summary> /// <param name="source"> <inheritdoc/></param> /// <param name="dest"> <inheritdoc/></param> /// <returns> <inheritdoc/></returns> public virtual bool CanConvert(Currencies source, Currencies dest, ConversionBag convStatus) { return(Currency.GetBySymbol(source) != null && Currency.GetBySymbol(dest) != null); }
/// <summary> /// When implemented in derived class, asynchronously gets a quote for the given pair or null if not found, /// conditions of convertibility have already been checked /// The returned Quote will have BaseCurrency=source and QuoteCurrency=dest /// </summary> /// <param name="source">The currency to convert from</param> /// <param name="dest">The currency to convert to</param> protected virtual async Task <TimedQuote> getQuoteAsync(Currencies source, Currencies dest, ConversionBag convStatus) { return(getQuote(source, dest, convStatus)); }
/// <summary> /// When implemented in derived class gets a quote for the given pair or null if not found, /// conditions of convertibility have already been checked /// The returned Quote will have BaseCurrency=source and QuoteCurrency=dest /// </summary> /// <param name="source">The currency to convert from</param> /// <param name="dest">The currency to convert to</param> protected abstract TimedQuote getQuote(Currencies source, Currencies dest, ConversionBag convStatus);
/// <summary> /// <inheritdoc/> /// </summary> /// <param name="source"><inheritdoc/></param> /// <param name="dest"><inheritdoc/></param> /// <returns></returns> public override bool CanConvert(Currencies source, Currencies dest, ConversionBag convStatus) { return(base.CanConvert(source, dest, convStatus) && (FixedConversions.ContainsKey(source) && FixedConversions[source].ContainsKey(dest) || FixedConversions.ContainsKey(dest) && FixedConversions[dest].ContainsKey(source))); }
protected override TimedQuote getQuote(Currencies source, Currencies dest, ConversionBag convStatus) { return(quotes[source][dest]); }