Example #1
0
 public double AddOrUpdateUsdExchangeRates()
 {
     if (_usdBtcExchangeRate == null)
     {
         var cryptoCompareResult = CryptoCompareService.GetUsdBtcExchangeRate();
         _usdBtcExchangeRate = cryptoCompareResult.ExchangeRates.Values.FirstOrDefault();
     }
     return(_usdBtcExchangeRate.Value);
 }
Example #2
0
        public void UpdateUsdExchangeRates()
        {
            using (GmiUnitOfWork gmiUnitOfWork = new GmiUnitOfWork())
            {
                //Get Coins from DB,
                int allCoinsInDBCount = gmiUnitOfWork.GenericRepository.Count(new SearchCriteria <Coin>(int.MaxValue, 1)
                {
                    FilterExpression = c => c.Difficulty > 0 && c.BlockReward > 0 && c.LastBlock > 0
                });


                int pageNumber = 1;
                int pageSize   = 30;
                int lastPage   = (int)Math.Ceiling((double)allCoinsInDBCount / pageSize);

                string usdCode = "USD";

                while (pageNumber <= lastPage)
                {
                    var searchCriteria = new SearchCriteria <Coin>(pageSize, pageNumber)
                    {
                        FilterExpression = c => c.Difficulty > 0 && c.BlockReward > 0 && c.LastBlock > 0
                    };
                    var partialResult = gmiUnitOfWork.GenericRepository.Search(searchCriteria).Result;
                    var coinNames     = partialResult.Select(s => s.Tag.ToUpper()).ToList();
                    List <CryptoComparePriceResult> exchangeRatesPartial = CryptoCompareService.GetCryptoComparePriceExchangeRateMultiSource(coinNames, usdCode);
                    foreach (var cryptoComparePriceResult in exchangeRatesPartial)
                    {
                        var coin = partialResult.FirstOrDefault(c =>
                                                                c.Tag.ToUpper() == cryptoComparePriceResult.SourceCurrenceCode.ToUpper());
                        if (coin == null)
                        {
                            continue;
                        }
                        coin.ExchangeRateUsd = cryptoComparePriceResult.ExchangeRates[usdCode];
                    }
                    pageNumber++;
                }

                gmiUnitOfWork.Commit();
            }
        }
Example #3
0
 public double GetLiveUsdExchangeRate(string cryptoCurrencyCode)
 {
     return(CryptoCompareService.GetCryptoComparePriceExchangeRate(cryptoCurrencyCode, "USD")?.ExchangeRates.Values.FirstOrDefault() ?? 0);
 }