コード例 #1
0
        private void Check_Currencies_Bitfinex()
        {
            _ = Task.Run(async() =>
            {
                var failCounter = 0;
                while (failCounter < 10)
                {
                    if (!AwaitOnline(Services.Bitfinex, true))
                    {
                        return;
                    }

                    try
                    {
                        using (WebClient webclient = new())
                        {
                            var json = webclient.DownloadString("https://api-pub.bitfinex.com/v2/conf/pub:list:currency");

                            json = json.Remove(0, 1);
                            json = json.Remove(json.Length - 1);

                            var currencies = JsonConvert.DeserializeObject <string[]>(json);

                            foreach (var item in currencies)
                            {
                                Currencies_Bitfinex.Add(item);
                            }

                            log.Information($"Found {currencies.Length} Currencies at Bitfinex.");
                            break;
                        }
                    }
                    catch (Exception ex) when(ex.Message.Contains("Service Temporarily Unavailable") || ex.Message.Contains("503"))
                    {
                        Dispatcher.Invoke(() => { ExchangeRateInfo.Text = $"BFX Currencies temporarily unavailable..."; });
                    }
                    catch (Exception ex)
                    {
                        failCounter++;
                        log.Error($"Querying Bitfinex currencies: {ex.Short()}");
                        await Task.Delay(5000 * failCounter);
                    }
                }
            });
        }
コード例 #2
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);
                                                        }