예제 #1
0
        public async Task <IActionResult> SeedBinance()
        {
            var coinMarketCapData = _httpClient.GetStringAsync("https://api.coinmarketcap.com/v2/listings/");
            var binanceInfo       = _httpClient.GetStringAsync("https://www.binance.com/api/v1/exchangeInfo");

            await Task.WhenAll(binanceInfo, coinMarketCapData);

            var cmcTemplate         = new { data = new[] { new { name = "", symbol = "" } } };
            var cmcData             = JsonConvert.DeserializeAnonymousType(coinMarketCapData.Result, cmcTemplate).data;
            var binanceInfoTemplate = new { symbols = new[] { new { symbol = "", baseAsset = "", quoteAsset = "" } } };

            var symbols = JsonConvert.DeserializeAnonymousType(binanceInfo.Result, binanceInfoTemplate).symbols;

            var binance = new Exchange
            {
                Id   = "binance",
                Web  = "www.binance.com",
                Name = "Binance",
                ProvidesFullHistoryData = true,
            };

            _repo.Add(binance);

            var errors = new List <string>();

            var currencies = symbols.Aggregate(new HashSet <string>(), (res, val) => //Get unique currencies
            {
                res.Add(val.baseAsset);
                res.Add(val.quoteAsset);
                return(res);
            });

            foreach (var c in currencies) // Set currencies
            {
                var cmcMatches = cmcData.Where(o => o.symbol == c);
                if (cmcMatches.Count() != 1)
                {
                    errors.Add(c);
                }


                _currencyRepository.AddNotSave(new Currency
                {
                    Id   = c,
                    Name = cmcMatches.FirstOrDefault()?.name ?? c
                });
            }
            _currencyRepository.Save();

            foreach (var c in currencies) // Set exchangeCurrencies
            {
                _exchangeCurrencyRepository.AddNotSave(new ExchangeCurrency
                {
                    Id                 = "binance_" + c,
                    CurrencyId         = c,
                    ExchangeId         = "binance",
                    CurrencyExchangeId = c
                });
            }
            _exchangeCurrencyRepository.Save();

            foreach (var m in symbols) // Set markets
            {
                _marketRepository.AddNotSave(new Market
                {
                    MarketCurrencyId = m.quoteAsset,
                    CurrencyId       = m.baseAsset,
                    Id = m.quoteAsset + "_" + m.baseAsset,
                });
            }
            _marketRepository.Save();

            foreach (var m in symbols) // Set exchange markets
            {
                _exchangeMarketRepository.AddNotSave(new ExchangeMarket
                {
                    ExchangeId       = "binance",
                    Id               = "binance_" + m.quoteAsset + "_" + m.baseAsset,
                    MarketExchangeId = m.symbol,
                    MarketId         = m.quoteAsset + "_" + m.baseAsset
                });
            }
            _exchangeMarketRepository.Save();

            return(Ok(errors));
        }