public override async Task GetPriceUpdates(PriceUpdatesRequest request, IServerStreamWriter <PriceUpdate> responseStream, ServerCallContext context)
        {
            Console.WriteLine($"New price stream connect. peer:{context.Peer}");

            var entities = _pricesReader.Get(PriceEntity.GetPk());

            var prices = _mapper.Map <List <PriceUpdate> >(entities);

            if (request.AssetPairIds.Any())
            {
                prices = prices.Where(x => request.AssetPairIds.Contains(x.AssetPairId)).ToList();
            }

            var streamInfo = new StreamInfo <PriceUpdate>
            {
                Stream           = responseStream,
                CancelationToken = context.CancellationToken,
                Peer             = context.Peer,
                Keys             = request.AssetPairIds.ToArray()
            };

            var task = await _priceStreamService.RegisterStreamAsync(streamInfo, prices);

            await task;
        }
        public override async Task <PricesResponse> GetPrices(PricesRequest request, ServerCallContext context)
        {
            var entities = _pricesReader.Get(PriceEntity.GetPk());

            List <PriceUpdate> result;

            if (entities.Any())
            {
                result = _mapper.Map <List <PriceUpdate> >(entities);
            }
            else
            {
                var marketData = await _marketDataClient.GetMarketDataAsync(new Empty());

                result = _mapper.Map <List <PriceUpdate> >(marketData.Items.ToList());
            }

            if (request.AssetPairIds.Any())
            {
                result = result.Where(x =>
                                      request.AssetPairIds.Contains(x.AssetPairId, StringComparer.InvariantCultureIgnoreCase))
                         .ToList();
            }

            var response = new PricesResponse();

            response.Payload.AddRange(result);

            return(response);
        }
示例#3
0
        public async Task <IActionResult> GetPrices([FromQuery] string[] assetPairIds)
        {
            var entities = _pricesReader.Get(PriceEntity.GetPk());

            List <PriceModel> result;

            if (entities.Any())
            {
                result = _mapper.Map <List <PriceModel> >(entities);
            }
            else
            {
                var marketData = await _marketDataClient.GetMarketDataAsync(new Empty());

                result = _mapper.Map <List <PriceModel> >(marketData.Items.ToList());
            }

            if (assetPairIds.Any())
            {
                result = result.Where(x =>
                                      assetPairIds.Contains(x.AssetPairId, StringComparer.InvariantCultureIgnoreCase))
                         .ToList();
            }

            return(Ok(ResponseModel <IReadOnlyCollection <PriceModel> > .Ok(result)));
        }
示例#4
0
        public override Task <PricesResponse> GetPrices(PricesRequest request, ServerCallContext context)
        {
            var entities = _pricesReader.Get(PriceEntity.GetPk());

            var result = new List <PriceUpdate>();

            if (entities.Any())
            {
                result = _mapper.Map <List <PriceUpdate> >(entities);
            }

            if (request.AssetPairIds.Any())
            {
                result = result.Where(x =>
                                      request.AssetPairIds.Contains(x.AssetPairId, StringComparer.InvariantCultureIgnoreCase))
                         .ToList();
            }

            var response = new PricesResponse {
                Body = new PricesResponse.Types.Body()
            };

            response.Body.Prices.AddRange(result);

            return(Task.FromResult(response));
        }
示例#5
0
        public void Start()
        {
            _pricesReader.SubscribeToChanges(prices =>
            {
                var tasks = new List <Task>();

                foreach (var price in prices)
                {
                    tasks.Add(_priceStream.WriteToStreamAsync(_mapper.Map <PriceUpdate>(price), price.AssetPairId));
                }

                Task.WhenAll(tasks).GetAwaiter().GetResult();
            });

            _candlesReader.SubscribeToChanges(candles =>
            {
                var tasks = new List <Task>();

                foreach (var candle in candles)
                {
                    var key = $"{candle.AssetPairId}_{candle.PriceType}_{candle.TimeInterval}";
                    tasks.Add(_candlesStream.WriteToStreamAsync(_mapper.Map <CandleUpdate>(candle), key));
                }

                Task.WhenAll(tasks).GetAwaiter().GetResult();
            });

            _tickersReader.SubscribeToChanges(tickers =>
            {
                var tasks = new List <Task>();

                foreach (var ticker in tickers)
                {
                    var priceEntity = _pricesReader.Get(PriceEntity.GetPk(), ticker.AssetPairId);

                    var priceUpdate = new PriceUpdate
                    {
                        AssetPairId    = ticker.AssetPairId,
                        VolumeBase24H  = ticker.VolumeBase.ToString(CultureInfo.InvariantCulture),
                        VolumeQuote24H = ticker.VolumeQuote.ToString(CultureInfo.InvariantCulture),
                        PriceChange24H = ticker.PriceChange.ToString(CultureInfo.InvariantCulture),
                        Timestamp      = Timestamp.FromDateTime(ticker.UpdatedDt.ToUniversalTime())
                    };

                    if (priceEntity != null)
                    {
                        priceUpdate.Ask = priceEntity.Ask.ToString(CultureInfo.InvariantCulture);
                        priceUpdate.Bid = priceEntity.Bid.ToString(CultureInfo.InvariantCulture);
                    }

                    tasks.Add(_priceStream.WriteToStreamAsync(priceUpdate, priceUpdate.AssetPairId));
                }

                Task.WhenAll(tasks).GetAwaiter().GetResult();
            });

            _orderbooksReader.SubscribeToChanges(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();
            });

            _sessionsReader.SubscribeToChanges(sessions => { });

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