Ejemplo n.º 1
0
        public decimal GetRate()
        {
            if (Rates == null || !Rates.Any())
            {
                throw new Exception("Cannot fetch exchange rate.");
            }

            var ratesOnDate = Rates.First().Value;

            if (!ratesOnDate.HasValue)
            {
                throw new Exception("Cannot fetch exchange rate.");
            }

            var rateToTargetCurrency = ratesOnDate.Value;

            return(rateToTargetCurrency);
        }
Ejemplo n.º 2
0
        public async Task <List <RateRecord> > GetRates(bool fromMemory = false)
        {
            List <RateRecord> records = new List <RateRecord>();

            if (_proxy.IsConnected && !fromMemory || _proxy.IsConnected && !Rates.Any())
            {
                //Try catch in case that proxy disconnect before next keepAlive check to return stored data
                try
                {
                    var rates = await _proxy.GetRates();

                    if (rates != null)
                    {
                        Rates.Clear();
                    }
                    foreach (var rate in rates)
                    {
                        records.Add(rate);
                        Rates.Add(rate);
                    }
                }
                catch (Exception ex)
                {
                    foreach (var rate in Rates)
                    {
                        records.Add(rate);
                    }
                }
            }
            else
            {
                foreach (var rate in Rates)
                {
                    records.Add(rate);
                }
            }
            return(Rates);
        }
Ejemplo n.º 3
0
        private async Task Rate_Coinbase(string base_currency)
        {
            try
            {
                var maxAge = AGE_FIAT2FIAT_RATE > AGE_MIXEDRATE_RATE ? AGE_MIXEDRATE_RATE : AGE_FIAT2FIAT_RATE;
                if (!Rates.Any(x => x.Base == base_currency) || !Rates.Any(x => x.Quote == base_currency) || Rates.Any(x => (x.Base == base_currency || x.Quote == base_currency) && (DateTime.Now - x.Date > maxAge)))
                {
                    try
                    {
                        Dispatcher.Invoke(() => { ExchangeRateInfo.Text = $"Checking [{base_currency}]..."; });

                        using (WebClient webClient = new())
                        {
                            string json = default;

                            try
                            {
                                json = webClient.DownloadString($"https://api.coinbase.com/v2/exchange-rates?currency={base_currency}");
                            }
                            catch
                            {
                                if (Currencies_Bitfinex.Contains(base_currency))
                                {
                                    await Rate_Bitfinex(base_currency);

                                    return;
                                }
                            }

                            var deserialized = JsonConvert.DeserializeObject <JSON_Coinbase>(json);
                            var cur          = Currencies.ToArray();

                            foreach (var quote_currency in cur)
                            {
                                if (quote_currency != base_currency)
                                {
                                    if (deserialized.data.Rates.ContainsKey(quote_currency))
                                    {
                                        if (Requests.FirstOrDefault(x => x.Item1 == base_currency && x.Item2 == quote_currency && x.Item3 != Services.Coinbase) != default)
                                        {
                                            continue;
                                        }

                                        if (Rates.ToList().Exists(x => x.Base == base_currency && x.Quote == quote_currency)) // Update
                                        {
                                            var exrEntry = Rates.FirstOrDefault(x => x.Base == base_currency && x.Quote == quote_currency && x.Exchange == Services.Coinbase);

                                            if (exrEntry != default)
                                            {
                                                exrEntry.Date     = DateTime.Now;
                                                exrEntry.Exchange = Services.Coinbase;

                                                exrEntry.Rate = Res.FIAT.Contains(quote_currency) && !Res.FIAT.Contains(base_currency)
                                                    ? Ext.TruncateDecimal(decimal.Parse(deserialized.data.Rates[quote_currency], NumberStyles.Float, new NumberFormatInfo()
                                                {
                                                    NumberDecimalSeparator = "."
                                                }), 2)
                                                    : Ext.TruncateDecimal(decimal.Parse(deserialized.data.Rates[quote_currency], NumberStyles.Float, new NumberFormatInfo()
                                                {
                                                    NumberDecimalSeparator = "."
                                                }), 8);

                                                Dispatcher.Invoke(() => { ExchangeRateInfo.Text = $"Checking [{base_currency}]... updated."; });

                                                if (DateTime.Now - new TimeSpan(1, 0, 0) > History_Long[(base_currency, quote_currency)].Last().Time)
                                                {
                                                    lock (lock_history)
                                                    {
                                                        History_Long[(base_currency, quote_currency)].Add(new TimeData()
                                                        {
                                                            Time = DateTime.Now, Rate = (double)exrEntry.Rate
                                                        });

                                                        if (DateTime.Now - maxAgeLongHistory > History_Long[(base_currency, quote_currency)][0].Time)
                                                        {
                                                            History_Long[(base_currency, quote_currency)].RemoveAt(0);
                                                        }