/// <summary> /// Iterate through all payments and lookup cryptocompare data to calculate fiat amounts /// </summary> private void PopulateFiatCurrenyAmounts() { try { // Exit if no fiat currency is selected if (Application.Current.Properties["Currency"] == null) { return; } foreach (MinerPaymentSummary MinerPaymentSummary in MinerPaymentsData.MinerPaymentSummaryList) { // Get CryptoCompare historical rates for the past 30 days for this particular coin HistoDayResponse histoDayResponse = GetCryptoCompare(MinerPaymentSummary.CoinType); // Iterate through each day and calculate based on the rates foreach (MinerPaymentsGroupedByDay minerPaymentsGroupedByDay in MinerPaymentSummary.MinerPaymentsGroupedByDayList.Where(x => x.PaymentDate >= DateTime.Now.AddDays(-30)).OrderByDescending(y => y.PaymentDate)) { CalculateDailyPaymentUsingHistoricalRates(MinerPaymentSummary, histoDayResponse, minerPaymentsGroupedByDay); } } OnPropertyChanged("MinerPaymentsData"); } catch (Exception e) { ShowError(String.Format("{0} {1}", "Error populating fiat currency amounts", e.Message)); } }
/// <summary> /// Lookup CryptoCompare data using miner's preferred fiat currency /// </summary> private HistoDayResponse GetCryptoCompare(CoinType coinType) { try { HistoDayResponse histoDayResponse = new HistoDayResponse(); // Exit if no fiat currency is selected if (Application.Current.Properties["Currency"] == null) { return(new HistoDayResponse()); } string fiatCurrencyISOSymbol = Application.Current.Properties["Currency"].ToString(); // Load CryptoCompare data CryptoCompareAPI cryptoCompareAPI = new CryptoCompareAPI(); histoDayResponse = cryptoCompareAPI.GetCryptoCompareResponse(coinType.ToString(), fiatCurrencyISOSymbol); return(histoDayResponse); } catch (Exception e) { ShowError(string.Format("Error loading coin market cap data: {0}", e.Message)); return(new HistoDayResponse()); } }
/// <summary> /// Call API and GET coin data from www.cryptocompare.com /// </summary> /// <param name="cryptoCurrenctName"></param> /// <param name="fiatCurrencySymbol"></param> /// <returns></returns> public HistoDayResponse GetCryptoCompareResponse(string cryptoCurrencyName, string fiatCurrencySymbol) { try { // Instatiate response object HistoDayResponse histoDayResponse = new HistoDayResponse(); // Call CryptoCompare and get daily historical rates string apiURL = string.Format(APIConstants.CryptoCompareHistoDayAPIURL, cryptoCurrencyName, fiatCurrencySymbol); histoDayResponse = DownloadSerializedJSONData <HistoDayResponse>(apiURL); return(histoDayResponse); } catch (Exception e) { logger.Error(e, "Could not download crypto compare data."); return(new HistoDayResponse()); } }
/// <summary> /// Lookup daily historical rate for each day and calculate payment in users fiat currency /// </summary> /// <param name="MinerPaymentSummary"></param> /// <param name="histoDayResponse"></param> /// <param name="minerPaymentsGroupedByDay"></param> /// <returns></returns> private void CalculateDailyPaymentUsingHistoricalRates(MinerPaymentSummary MinerPaymentSummary, HistoDayResponse histoDayResponse, MinerPaymentsGroupedByDay minerPaymentsGroupedByDay) { // Lookup CryptoCompare rate for the specific date HistoDateResponseData histoDayResponseDay = histoDayResponse.data.Where(x => x.dateTime <= minerPaymentsGroupedByDay.PaymentDate.ToUniversalTime().AddHours(9) && x.dateTime >= minerPaymentsGroupedByDay.PaymentDate.ToUniversalTime().AddHours(-9)).FirstOrDefault(); if (histoDayResponseDay == null) { minerPaymentsGroupedByDay.PaymentAmountFiat = 0; minerPaymentsGroupedByDay.FiatExchangeRate = 0; ShowError(String.Format("{0} {1} Missing", minerPaymentsGroupedByDay.PaymentDate.ToUniversalTime(), MinerPaymentSummary.CoinType)); } else { minerPaymentsGroupedByDay.FiatExchangeRate = histoDayResponseDay.high; minerPaymentsGroupedByDay.PaymentAmountFiat = (minerPaymentsGroupedByDay.PaymentAmount * histoDayResponseDay.high); } }