Ejemplo n.º 1
0
        public IEnumerable <Order> Convert(ExecutionProcessedEvent source, IEnumerable <Order> destination,
                                           ResolutionContext context)
        {
            foreach (var item in source.Orders)
            {
                var order = Mapper.Map <Order>(item);

                order.SequenceNumber = source.SequenceNumber;

                foreach (var trade in order.Trades)
                {
                    trade.OrderId = order.Id;
                }

                yield return(order);
            }
        }
        private Task ProcessMessageAsync(ExecutionEvent message)
        {
            var orders = message.Orders.Select(x => new OrderModel
            {
                Id              = Guid.Parse(x.ExternalId),
                WalletId        = Guid.Parse(x.WalletId),
                Volume          = decimal.Parse(x.Volume),
                AssetPairId     = x.AssetPairId,
                CreateDt        = x.CreatedAt,
                LowerLimitPrice = ParseNullabe(x.LowerLimitPrice),
                LowerPrice      = ParseNullabe(x.LowerPrice),
                MatchDt         = x.LastMatchTime,
                MatchingId      = Guid.Parse(x.Id),
                Price           = ParseNullabe(x.Price),
                RegisterDt      = x.Registered,
                RejectReason    = x.RejectReason,
                RemainingVolume = ParseNullabe(x.RemainingVolume),
                Side            = (Contracts.Cqrs.Models.Enums.OrderSide)(int) x.Side,
                Status          = (Contracts.Cqrs.Models.Enums.OrderStatus)(int) x.Status,
                StatusDt        = x.StatusDate,
                Straight        = x.OrderType == OrderType.Limit || x.OrderType == OrderType.StopLimit || x.Straight,
                Type            = (Contracts.Cqrs.Models.Enums.OrderType)(int) x.OrderType,
                UpperLimitPrice = ParseNullabe(x.UpperLimitPrice),
                UpperPrice      = ParseNullabe(x.UpperPrice),
                Trades          = x.Trades?.Select(t => new TradeModel
                {
                    Id               = Guid.Parse(t.TradeId),
                    WalletId         = Guid.Parse(x.WalletId),
                    AssetPairId      = x.AssetPairId,
                    BaseAssetId      = t.BaseAssetId,
                    BaseVolume       = decimal.Parse(t.BaseVolume),
                    Price            = decimal.Parse(t.Price),
                    Timestamp        = t.Timestamp,
                    QuotingAssetId   = t.QuotingAssetId,
                    QuotingVolume    = decimal.Parse(t.QuotingVolume),
                    Index            = t.Index,
                    Role             = (Contracts.Cqrs.Models.Enums.TradeRole)(int) t.Role,
                    FeeSize          = ParseNullabe(t.Fees?.FirstOrDefault()?.Volume),
                    FeeAssetId       = t.Fees?.FirstOrDefault()?.AssetId,
                    OppositeWalletId = Guid.Parse(t.OppositeWalletId),
                }).ToList()
            }).ToList();

            foreach (var order in orders.Where(x => _walletIds.Contains(x.WalletId.ToString())))
            {
                _log.Info("Order from ME", $"order: {new {order.Id, order.Status, message.Header.SequenceNumber}.ToJson()}");
            }

            var @event = new ExecutionProcessedEvent
            {
                SequenceNumber = message.Header.SequenceNumber,
                Orders         = orders
            };

            _cqrsEngine.PublishEvent(@event, BoundedContext.Name);

            foreach (var order in orders.Where(x => x.Trades != null && x.Trades.Any()))
            {
                var tradeProcessedEvent = new ManualOrderTradeProcessedEvent
                {
                    Order = order
                };
                _cqrsEngine.PublishEvent(tradeProcessedEvent, BoundedContext.Name);
            }

            foreach (var order in message.Orders.Where(x => x.Trades != null && x.Trades.Count > 0))
            {
                var orderType = order.OrderType == OrderType.Market ? FeeOperationType.Trade : FeeOperationType.LimitTrade;
                var orderId   = order.Id;
                foreach (var trade in order.Trades)
                {
                    if (trade.Fees != null)
                    {
                        var feeEvent = new FeeChargedEvent
                        {
                            OperationId   = orderId,
                            OperationType = orderType,
                            Fee           = trade.Fees.ToJson()
                        };
                        _cqrsEngine.PublishEvent(feeEvent, BoundedContext.Name);
                    }
                }
            }

            var limitOrders = orders.Where(x => x.Type == Contracts.Cqrs.Models.Enums.OrderType.Limit ||
                                           x.Type == Contracts.Cqrs.Models.Enums.OrderType.StopLimit).ToList();

            foreach (var order in limitOrders.Where(x => x.Status == OrderStatus.Cancelled))
            {
                var orderCancelledEvent = new OrderCancelledEvent
                {
                    OrderId     = order.Id,
                    Status      = order.Status,
                    AssetPairId = order.AssetPairId,
                    Price       = order.Price,
                    Timestamp   = order.StatusDt,
                    Volume      = order.Volume,
                    WalletId    = order.WalletId
                };
                _cqrsEngine.PublishEvent(orderCancelledEvent, BoundedContext.Name);
            }

            foreach (var order in limitOrders.Where(x => x.Status == OrderStatus.Placed))
            {
                var orderPlacedEvent = new OrderPlacedEvent
                {
                    OrderId     = order.Id,
                    Status      = order.Status,
                    AssetPairId = order.AssetPairId,
                    Price       = order.Price,
                    Timestamp   = order.StatusDt,
                    Volume      = order.Volume,
                    WalletId    = order.WalletId,
                    CreateDt    = order.CreateDt
                };
                _cqrsEngine.PublishEvent(orderPlacedEvent, BoundedContext.Name);
            }

            foreach (var order in limitOrders.Where(x =>
                                                    (x.Status == OrderStatus.Matched || x.Status == OrderStatus.PartiallyMatched) &&
                                                    x.Trades.Any(t => t.Role == TradeRole.Taker)))
            {
                var orderPlacedEvent = new OrderPlacedEvent
                {
                    OrderId     = order.Id,
                    Status      = order.Status,
                    AssetPairId = order.AssetPairId,
                    Price       = order.Price,
                    Timestamp   = order.StatusDt,
                    Volume      = order.Volume,
                    WalletId    = order.WalletId,
                    CreateDt    = order.CreateDt
                };
                _cqrsEngine.PublishEvent(orderPlacedEvent, BoundedContext.Name);
            }

            return(Task.CompletedTask);
        }