private void FillMissingRates(List <CurrencyConvertion> rates) { List <string> froms = rates.Select(o => o.From).Distinct().ToList(); List <string> tos = rates.Select(o => o.To).Distinct().ToList(); foreach (string from in froms) { foreach (string to in tos) { if (from == to) { continue; } CurrencyConvertion rate = rates.SingleOrDefault(o => o.From == from && o.To == to); if (rate == null) { FillMissingRate(rates, from, to); } } } }
private void FillMissingRate(List <CurrencyConvertion> rates, string from, string to) { List <CurrencyConvertion> partielFroms = rates.Where(o => o.From == from).ToList(); List <string> tos = partielFroms.Select(o => o.To).Distinct().ToList(); List <CurrencyConvertion> nexusFroms = rates.Where(o => tos.Contains(o.From)).ToList(); CurrencyConvertion nexusTo = nexusFroms.Where(o => o.To == to).FirstOrDefault(); if (nexusTo != null) { AddMissingRate(rates, from, to, partielFroms, nexusTo); } }
private void AddMissingRate( List <CurrencyConvertion> rates, string from, string to, List <CurrencyConvertion> partielFroms, CurrencyConvertion nexusTo) { CurrencyConvertion nexusFrom = partielFroms.Where(o => o.To == nexusTo.From).Single(); CurrencyConvertion rate = new CurrencyConvertion { From = from, To = to, Rate = nexusTo.Rate * nexusFrom.Rate }; rates.Add(rate); }
protected async Task ConvertToNativeTarget(string nativeCurrency, decimal nativeAmount) { if (CryptoCurrencyAmount != 0) { CurrencyConvertion currencyConvertion = MainComponent.Instance.CurrencyConvertionsContainer[nativeCurrency + "->" + MainComponent.Instance.TargetCurrency, TimeStamp]; if (currencyConvertion != null) { NativeCurrency = MainComponent.Instance.TargetCurrency; NativeAmount = nativeAmount * currencyConvertion.ConvertionRate; ValueForOneCryptoTokenInNative = NativeAmount / CryptoCurrencyAmount; } else { string date = TimeStamp.ToString("yyyy-MM-dd"); using (CallRateLimiter httpClient = new CallRateLimiter("Grandtrunk-Currencies", 5, TimeSpan.FromSeconds(1))) { HttpResponseMessage httpResponse = await httpClient.GetDataAsync($"http://currencies.apps.grandtrunk.net/getrate/{date}/{nativeCurrency}/{MainComponent.Instance.TargetCurrency}"); if (httpResponse.IsSuccessStatusCode) { if (decimal.TryParse(await httpResponse.Content.ReadAsStringAsync(), out decimal exchangeRate)) { lock (MainComponent.Instance) { if (!MainComponent.Instance.CurrencyConvertionsContainer.ContainsKey(nativeCurrency + "->" + MainComponent.Instance.TargetCurrency, TimeStamp)) { MainComponent.Instance.CurrencyConvertionsContainer.Add(new CurrencyConvertion(nativeCurrency, MainComponent.Instance.TargetCurrency, TimeStamp, exchangeRate)); } } NativeCurrency = MainComponent.Instance.TargetCurrency; NativeAmount = nativeAmount * exchangeRate; ValueForOneCryptoTokenInNative = NativeAmount / CryptoCurrencyAmount; } } else { } } } } }
private void PassTransactionsToDefualtCurrency(List <CurrencyConvertion> Rates, List <Transaction> transactions) { transactions.ForEach(t => { if (t.Currency != defualtCurrency) { CurrencyConvertion Rate = Rates.SingleOrDefault(c => c.From == t.Currency && c.To == defualtCurrency); if (Rate != null) { t.Currency = defualtCurrency; t.Amount *= Rate.Rate; t.Amount = bankCalculus.RoundBank(t.Amount); } } }); }
public void Add(CurrencyConvertion currencyConvertion) { Init(); if (_fromToCurrencies.ContainsKey(currencyConvertion.FromToCurrencies)) { if (!_fromToCurrencies[currencyConvertion.FromToCurrencies].Any(c => c.DateOfConvertionRate == currencyConvertion.DateOfConvertionRate)) { _fromToCurrencies[currencyConvertion.FromToCurrencies].Add(currencyConvertion); _currencyConvertions.Add(currencyConvertion); } } else { _fromToCurrencies.Add(currencyConvertion.FromToCurrencies, new List <CurrencyConvertion> { currencyConvertion }); _currencyConvertions.Add(currencyConvertion); } }
public void InsertOrUpdateRates(List <CurrencyConvertion> rates) { MongoDal mongoDal = new MongoDal(ratesCollectionName, configuration); foreach (CurrencyConvertion rate in rates) { CurrencyConvertion existentRate = mongoDal.GetWhere <CurrencyConvertion>(o => o.From == rate.From && o.To == rate.To).FirstOrDefault(); if (existentRate == null) { mongoDal.AddObject <CurrencyConvertion>(rate); } else { existentRate.Rate = rate.Rate; mongoDal.UpadeteObject <CurrencyConvertion>(existentRate); } } }