Ejemplo n.º 1
0
        public void Start()
        {
            _pricesReader.SubscribeToUpdateEvents(prices =>
            {
                var tasks = prices.Select(price => _priceStraem.WriteToStreamAsync(_mapper.Map <PriceUpdate>(price), price.AssetPairId)).ToList();
                Task.WhenAll(tasks).GetAwaiter().GetResult();
            }, deleted => { });

            _tickerReader.SubscribeToUpdateEvents(tickers =>
            {
                var tasks = tickers.Select(ticker => _tickerStream.WriteToStreamAsync(_mapper.Map <TickerUpdate>(ticker))).ToList();
                Task.WhenAll(tasks).GetAwaiter().GetResult();
            }, deleted => { });

            _orderbookReader.SubscribeToUpdateEvents(orderbooks =>
            {
                var tasks = new List <Task>();

                foreach (var orderbook in orderbooks)
                {
                    var item = _mapper.Map <Orderbook>(orderbook);
                    item.Asks.AddRange(_mapper.Map <List <Orderbook.Types.PriceVolume> >(orderbook.Asks));
                    item.Bids.AddRange(_mapper.Map <List <Orderbook.Types.PriceVolume> >(orderbook.Bids));
                    tasks.Add(_orderbookStream.WriteToStreamAsync(item, orderbook.AssetPairId));
                }

                Task.WhenAll(tasks).GetAwaiter().GetResult();
            }, deleted => { });

            _balanceReader.SubscribeToUpdateEvents(balances =>
            {
                var balancesByWallet = balances.GroupBy(x => x.WalletId);
                var tasks            = new List <Task>();

                foreach (var walletBalanes in balancesByWallet)
                {
                    var balanceUpdate = new BalanceUpdate();
                    balanceUpdate.Balances.AddRange(_mapper.Map <List <Balance> >(walletBalanes.ToList()));
                    tasks.Add(_balanceStream.WriteToStreamAsync(balanceUpdate, walletBalanes.Key));
                }

                Task.WhenAll(tasks).GetAwaiter().GetResult();
            }, deleted => { });

            Console.WriteLine("Stream services started.");
        }