Пример #1
0
        private async Task <List <ITradeDataUpdate> > ProcessDataUpdates(SubmitTradeItemResponse response)
        {
            var cacheUsers      = new HashSet <Guid>();
            var cacheTradePairs = new HashSet <int>();
            var results         = new List <ITradeDataUpdate>();

            if (!response.FilledOrders.IsNullOrEmpty())
            {
                foreach (var order in response.FilledOrders)
                {
                    results.Add(new TradeOrderBookUpdate
                    {
                        Amount      = order.Amount,
                        Rate        = order.Rate,
                        Total       = order.Amount * order.Rate,
                        Type        = order.Type,
                        TradePairId = order.TradePairId,
                        Action      = TradeUpdateAction.Remove
                    });

                    results.Add(new TradeHistoryUpdate
                    {
                        Amount      = order.Amount,
                        Rate        = order.Rate,
                        Total       = order.Amount * order.Rate,
                        Timestamp   = order.Timestamp,
                        Type        = order.Type,
                        TradePairId = order.TradePairId
                    });

                    results.Add(new TradeUserHistoryUpdate
                    {
                        UserId      = order.UserId,
                        Amount      = order.Amount,
                        Rate        = order.Rate,
                        Total       = order.Amount * order.Rate,
                        Timestamp   = order.Timestamp,
                        Type        = TradeHistoryType.Buy,
                        TradePairId = order.TradePairId
                    });

                    results.Add(new TradeUserHistoryUpdate
                    {
                        UserId      = order.ToUserId,
                        Amount      = order.Amount,
                        Rate        = order.Rate,
                        Total       = order.Amount * order.Rate,
                        Timestamp   = order.Timestamp,
                        Type        = TradeHistoryType.Sell,
                        TradePairId = order.TradePairId
                    });

                    cacheUsers.Add(order.UserId);
                    cacheUsers.Add(order.ToUserId);
                    cacheTradePairs.Add(order.TradePairId);
                }
            }

            if (response.Order != null)
            {
                results.Add(new TradeOpenOrderUpdate
                {
                    OrderId     = response.Order.Id,
                    UserId      = response.Order.UserId,
                    Amount      = response.Order.Amount,
                    Rate        = response.Order.Rate,
                    Total       = response.Order.Amount * response.Order.Rate,
                    Type        = response.Order.Type,
                    TradePairId = response.Order.TradePairId,
                    Remaining   = response.Order.Remaining,
                    Status      = response.Order.Status,
                    Action      = TradeUpdateAction.Add,
                    Market      = $"{response.Order.Symbol}/{response.Order.BaseSymbol}"
                });

                results.Add(new TradeOrderBookUpdate
                {
                    Amount      = response.Order.Amount,
                    Rate        = response.Order.Rate,
                    Total       = response.Order.Amount * response.Order.Rate,
                    Type        = response.Order.Type,
                    TradePairId = response.Order.TradePairId,
                    Action      = TradeUpdateAction.Add
                });

                cacheUsers.Add(response.Order.UserId);
                cacheTradePairs.Add(response.Order.TradePairId);
            }

            if (!response.OrdersUpdated.IsNullOrEmpty())
            {
                foreach (var order in response.OrdersUpdated)
                {
                    results.Add(new TradeOpenOrderUpdate
                    {
                        OrderId     = order.OrderId,
                        UserId      = order.UserId,
                        Amount      = order.Amount,
                        Rate        = order.Rate,
                        Total       = order.Amount * order.Rate,
                        Type        = order.Type,
                        TradePairId = order.TradePairId,
                        Remaining   = order.Remaining,
                        Status      = order.Remaining != order.Amount
                                                        ? TradeStatus.Partial
                                                        : TradeStatus.Pending,
                        Action = order.Remaining <= 0
                                                        ? TradeUpdateAction.Remove
                                                        : TradeUpdateAction.Update
                    });

                    cacheUsers.Add(order.UserId);
                    cacheTradePairs.Add(order.TradePairId);
                }
            }

            if (response.Ticker != null)
            {
                var priceUpdate = new TradePriceUpdate
                {
                    TradePairId = response.Ticker.TradePairId,
                    BaseVolume  = response.Ticker.BaseVolume,
                    Change      = response.Ticker.Change,
                    High        = response.Ticker.High,
                    Last        = response.Ticker.Last,
                    Low         = response.Ticker.Low,
                    Market      = response.Ticker.Market,
                    Volume      = response.Ticker.Volume,
                };
                results.Add(priceUpdate);
                //await TradePairReader.UpdatePriceCache(priceUpdate).ConfigureAwait(false);
                //await ExchangeReader.UpdatePriceCache(priceUpdate).ConfigureAwait(false);
            }

            // Clear caches
            if (cacheTradePairs.Any())
            {
                foreach (var tradepair in cacheTradePairs)
                {
                    await ClearCache(tradepair);

                    if (cacheUsers.Any())
                    {
                        foreach (var user in cacheUsers)
                        {
                            await ClearCache(tradepair, user);

                            results.Add(new TradeBalanceUpdate
                            {
                                TradePairId = tradepair,
                                UserId      = user
                            });
                        }
                    }
                }
            }

            return(results);
        }
Пример #2
0
        private async Task <List <INotification> > ProcessNotifications(SubmitTradeItemResponse response)
        {
            var results = new List <INotification>();

            if (!response.FilledOrders.IsNullOrEmpty())
            {
                foreach (var filledOrder in response.FilledOrders)
                {
                    results.Add(new NotificationModel
                    {
                        Type         = NotificationLevelType.Info,
                        Header       = $"Order Filled",
                        Notification = $"You sold {filledOrder.Amount:F8} {filledOrder.Symbol} for {filledOrder.Amount * filledOrder.Rate:F8} {filledOrder.BaseSymbol}",
                        UserId       = filledOrder.ToUserId
                    });

                    results.Add(new NotificationModel
                    {
                        Type         = NotificationLevelType.Info,
                        Header       = $"Order Filled",
                        Notification = $"You bought {filledOrder.Amount:F8} {filledOrder.Symbol} for {filledOrder.Amount * filledOrder.Rate:F8} {filledOrder.BaseSymbol}",
                        UserId       = filledOrder.UserId
                    });
                }
            }

            if (response.Order != null)
            {
                results.Add(new NotificationModel
                {
                    Type         = NotificationLevelType.Info,
                    Header       = $"Order Placed",
                    Notification = $"Placed {response.Order.Type} order.{Environment.NewLine}{response.Order.Amount:F8} {response.Order.Symbol} @ {response.Order.Rate:F8} {response.Order.BaseSymbol}",
                    UserId       = response.Order.UserId
                });
            }

            if (response.OrderRefund != null)
            {
                results.Add(new NotificationModel
                {
                    Type         = NotificationLevelType.Info,
                    Header       = $"Order Refund",
                    Notification = $"Refunded {response.OrderRefund.Amount:F8} {response.OrderRefund.Symbol} from buy order.",
                    UserId       = response.OrderRefund.UserId
                });
            }

            if (!results.IsNullOrEmpty())
            {
                using (var context = DataContextFactory.CreateContext())
                {
                    foreach (var notification in results)
                    {
                        context.Notifications.Add(new Entity.UserNotification
                        {
                            Title        = notification.Header,
                            Notification = notification.Notification,
                            Type         = notification.Type.ToString(),
                            UserId       = notification.UserId.ToString(),
                            Timestamp    = DateTime.UtcNow
                        });
                    }
                    await context.SaveChangesAsync().ConfigureAwait(false);
                }
            }
            return(results);
        }