/// <summary> /// Lookup CoinMarketCap data using miner's preferred fiat currency /// </summary> public void InitCoinMarketCap() { try { // Exit if no fiat currency is selected if (Application.Current.Properties["Currency"] == null) { return; } // Exit if select worker is not currently mining if (MinerMonitorStat == null || MinerMonitorStat.CoinType == CoinType.UNDEFINED) { return; } string fiatCurrencyISOSymbol = Application.Current.Properties["Currency"].ToString(); // Attempt to get crypto coin name CoinNames.CoinNameDictionary.TryGetValue(MinerMonitorStat.CoinType, out string cryptoCurrencyName); // Load CoinMarketCap data CoinMarketCapAPI coinMarketCapAPI = new CoinMarketCapAPI(); CoinMarketCapResponse = coinMarketCapAPI.GetCoinMarketCapResponse(cryptoCurrencyName, fiatCurrencyISOSymbol); OnPropertyChanged("CoinMarketCapResponse"); } catch (Exception e) { ShowError(string.Format("Error loading coin market cap data: {0}", e.Message)); } }
public void InitWhatToMine() { // Instantiate new W2M objects whatToMineData = new WhatToMineData(); whatToMineData.WhatToMineResponseList = new List <WhatToMineResponse>(); // Make sure that Monitor stat list is not null if (MinerMonitorStatListGrouped == null) { return; } // Iterate through each miner that has monitor stats foreach (MinerMonitorStat minerMonitorStat in MinerMonitorStatListGrouped) { // Retrieve forecasts from W2M WhatToMineResponse whatToMineResponse = GetWhatToMineEstimates(minerMonitorStat); whatToMineResponse = WhatToMineDataFormatter.FormatWhatToMineData(whatToMineResponse, minerMonitorStat.CoinType); // Retrieve the current rates from CoinMarketCap for this particular coin and user's selected fiat CoinMarketCapResponse coinMarketCapResponse = GetCoinMarketCapData(minerMonitorStat.CoinType); whatToMineResponse.Revenue = Decimal.Round(Convert.ToDecimal(whatToMineResponse.Estimated_rewards) * Convert.ToDecimal(coinMarketCapResponse.price_fiat), 2).ToString(); whatToMineData.WhatToMineResponseList.Add(whatToMineResponse); } whatToMineData.WhatToMineResponseList.OrderBy(x => x.name).ToList(); CalculateForecastLast24Hour(); CalculateForecastNext24Hour(); OnPropertyChanged("WhatToMineData"); }
/// <summary> /// Get CoinMarketCap info for conversion using specified fiat currency for worker /// </summary> /// <param name="coinType"></param> private CoinMarketCapResponse GetCoinMarketCapData(CoinType coinType) { try { // Exit if no fiat currency is selected if (Application.Current.Properties["Currency"] == null) { return(new CoinMarketCapResponse()); } string fiatCurrencyISOSymbol = Application.Current.Properties["Currency"].ToString(); // Attempt to get crypto coin name CoinNames.CoinNameDictionary.TryGetValue(coinType, out string cryptoCurrencyName); // Load CoinMarketCap data CoinMarketCapAPI coinMarketCapAPI = new CoinMarketCapAPI(); CoinMarketCapResponse coinMarketCapResponse = coinMarketCapAPI.GetCoinMarketCapResponse(cryptoCurrencyName, fiatCurrencyISOSymbol); return(coinMarketCapResponse); } catch (Exception e) { ShowError(string.Format("Error retrieving coin market cap data: {0}", e.Message)); return(new CoinMarketCapResponse()); } }
/// <summary> /// Read Crypto currency data and price from th API /// </summary> /// <param name="symbols"></param> /// <returns></returns> private async Task <IEnumerable <CryptoCurrencyWithPrice> > RequestLatestQuotes(params string[] symbols) { try { var jsonStream = await _httpClient.GetStreamAsync( $"/v1/cryptocurrency/quotes/latest?aux=is_active&symbol={string.Join(",", symbols)}") .ConfigureAwait(false); var coinInfo = await CoinMarketCapResponse <CryptoCurrencyWithPrice> .FromJson(jsonStream) .ConfigureAwait(false); if (coinInfo == null) { _logger.LogError("Empty response on getting quotes from MarketCoinCap"); return(null); } if (coinInfo.Status.ErrorCode != 0) { _logger.LogError(coinInfo.Status.ErrorMessage); return(null); } return(coinInfo.Data); } catch (Exception e) { _logger.LogError(e, "Error on getting quotes from MarketCoinCap"); } return(null); }
/// <summary> /// Populate the payment summarized revenue for the next 24 hours for all coins and also for each individual coin /// </summary> private void CalculateRevenueLast24Hour() { try { // Iterate through each payment summary in the list for each coin foreach (MinerPaymentSummary minerPaymentSummary in minerPaymentsData.MinerPaymentSummaryList) { // Update coin logo for each miner CoinLogos.CoinLogoDictionary.TryGetValue(minerPaymentSummary.CoinType, out string logoSourceLocation); if (minerPaymentSummary.CoinType != CoinType.UNDEFINED) { minerPaymentSummary.CoinLogo = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, logoSourceLocation); } // Find W2M record to get exchange rate data WhatToMineResponse whatToMineResponse = WhatToMineData.WhatToMineResponseList.Where(x => x.tag == minerPaymentSummary.CoinType.ToString()).FirstOrDefault(); // If there is no W2M data then do not try to convert rates if (whatToMineResponse == null) { minerPaymentSummary.RevenueLast24HourCoin = Decimal.Round(minerPaymentSummary.MinerPaymentDetails24HoursList.Sum(x => x.PaymentAmount), 6); minerPaymentSummary.RevenueLast24HourBTC = 0; minerPaymentSummary.RevenueLast24HourUSD = 0; } else { // Retrieve the current rates from CoinMarketCap for this particular coin and user's selected fiat CoinMarketCapResponse coinMarketCapResponse = GetCoinMarketCapData(minerPaymentSummary.CoinType); minerPaymentSummary.RevenueLast24HourCoin = Decimal.Round(minerPaymentSummary.MinerPaymentDetails24HoursList.Sum(x => x.PaymentAmount), 6); minerPaymentSummary.RevenueLast24HourBTC = Decimal.Round(minerPaymentSummary.RevenueLast24HourCoin * Convert.ToDecimal(whatToMineResponse.exchange_rate), 6); minerPaymentSummary.RevenueLast24HourUSD = Decimal.Round(minerPaymentSummary.RevenueLast24HourCoin * Convert.ToDecimal(coinMarketCapResponse.price_fiat), 2);; } } // Calculate sum revenue across all coins minerPaymentsData.RevenueLast24HourUSD = minerPaymentsData.MinerPaymentSummaryList.Sum(x => x.RevenueLast24HourUSD); minerPaymentsData.RevenueLast24HourBTC = minerPaymentsData.MinerPaymentSummaryList.Sum(x => x.RevenueLast24HourBTC); // Order actual payment history minerPaymentsData.MinerPaymentSummaryList = minerPaymentsData.MinerPaymentSummaryList.OrderBy(x => x.CoinType.ToString()).ToList(); OnPropertyChanged("MinerPaymentsData"); } catch (Exception e) { ShowError(string.Format("Error calculating last 24 hour payment data {0}", e.Message)); } }
/// <summary> /// Call API and GET coin data from www.coinmarketcap.com /// </summary> /// <param name="cryptoCurrenctName"></param> /// <param name="fiatCurrencySymbol"></param> /// <returns></returns> public CoinMarketCapResponse GetCoinMarketCapResponse(string cryptoCurrencyName, string fiatCurrencySymbol) { try { // Instatiate response object CoinMarketCapResponse coinMarketCapResponse = new CoinMarketCapResponse(); coinMarketCapResponse.fiat_currency_iso_symbol = fiatCurrencySymbol; // Replace spaces with dashes cryptoCurrencyName = cryptoCurrencyName.Replace(" ", "-"); // Call CoinMarketCap and get rates for BTC and Fiat currency specified string apiURL = string.Format(APIConstants.CoinMarketCapAPIURL, cryptoCurrencyName, fiatCurrencySymbol); JArray response = DownloadSerializedJSONData <JArray>(apiURL); // Manually map CoinMarketCap data since we don't know the exact property name foreach (JObject parsedObject in response.Children <JObject>()) { // Manually map response to object since the property names vary depending on fiat used foreach (JProperty parsedProperty in parsedObject.Properties()) { string propertyName = parsedProperty.Name; if (propertyName.Equals("price_btc")) { coinMarketCapResponse.price_btc = (decimal)parsedProperty.Value; } if (propertyName.Equals(String.Format("price_{0}", fiatCurrencySymbol.ToLower()))) { coinMarketCapResponse.price_fiat = (decimal)parsedProperty.Value; } if (propertyName.Equals(String.Format("volume_{0}_24h", fiatCurrencySymbol.ToLower()))) { coinMarketCapResponse.volume_fiat_24h = (decimal)parsedProperty.Value; } if (propertyName.Equals(String.Format("market_cap_{0}", fiatCurrencySymbol.ToLower()))) { coinMarketCapResponse.market_cap_fiat = (decimal)parsedProperty.Value; } if (propertyName.Equals("percent_change_24h")) { coinMarketCapResponse.percent_change_24h = (decimal)parsedProperty.Value; } if (propertyName.Equals("percent_change_7d")) { coinMarketCapResponse.percent_change_7d = (decimal)parsedProperty.Value; } } } return(coinMarketCapResponse); } catch (Exception e) { logger.Error(e, "Could not download coin market cap data."); return(new CoinMarketCapResponse()); } }