public void CreateFromString_TestOnlyBids()
        {
            var rawBookupdateMessage = TestSocketMessages.BookUpdateMessageOnlyBids;
            var bookUpdateMessage    = BookUpdateMessage.CreateFromString(rawBookupdateMessage);

            Assert.NotNull(bookUpdateMessage);
            Assert.Equal(1234, bookUpdateMessage.ChannelId);
            Assert.Null(bookUpdateMessage.Asks);
            Assert.NotNull(bookUpdateMessage.Bids);
            Assert.Single(bookUpdateMessage.Bids);
        }
        public void CreateFromString_TestOnlyBids()
        {
            var rawBookupdateMessage = @"[1234,{""b"": [[""5541.30000"",""0.00000000"",""1534614335.345903""]]}]";
            var bookUpdateMessage    = BookUpdateMessage.CreateFromString(rawBookupdateMessage);

            Assert.NotNull(bookUpdateMessage);
            Assert.Equal(1234, bookUpdateMessage.ChannelId);
            Assert.Null(bookUpdateMessage.Asks);
            Assert.NotNull(bookUpdateMessage.Bids);
            Assert.Single(bookUpdateMessage.Bids);
        }
        public void CreateFromString_TestOnlyAsks()
        {
            var rawBookupdateMessage = @"[1234,{""a"": [[""5541.30000"",""2.50700000"",""1534614248.456738""],[""5542.50000"",""0.40100000"",""1534614248.456738""]]}]";
            var bookUpdateMessage    = BookUpdateMessage.CreateFromString(rawBookupdateMessage);

            Assert.NotNull(bookUpdateMessage);
            Assert.Equal(1234, bookUpdateMessage.ChannelId);
            Assert.NotNull(bookUpdateMessage.Asks);
            Assert.Equal(2, bookUpdateMessage.Asks.Length);
            Assert.Null(bookUpdateMessage.Bids);
        }
        public void CreateFromString_TestComplete()
        {
            var rawBookupdateMessage = TestSocketMessages.BookUpdateCompleteMessage;
            var bookUpdateMessage    = BookUpdateMessage.CreateFromString(rawBookupdateMessage);

            Assert.NotNull(bookUpdateMessage);
            Assert.Equal(1234, bookUpdateMessage.ChannelId);
            Assert.NotNull(bookUpdateMessage.Asks);
            Assert.Equal(2, bookUpdateMessage.Asks.Length);
            Assert.NotNull(bookUpdateMessage.Bids);
            Assert.Single(bookUpdateMessage.Bids);
        }
        public void CreateFromString_TestAsksRepublished()
        {
            var rawBookupdateMessage = TestSocketMessages.BookUpdateAsksRepublished;
            var bookUpdateMessage    = BookUpdateMessage.CreateFromString(rawBookupdateMessage);

            Assert.NotNull(bookUpdateMessage);
            Assert.Equal(1234, bookUpdateMessage.ChannelId);
            Assert.NotNull(bookUpdateMessage.Asks);
            Assert.Equal(2, bookUpdateMessage.Asks.Length);
            Assert.Null(bookUpdateMessage.Bids);

            #endregion
        }
Ejemplo n.º 6
0
        private void HandleData(KrakenMessageEventArgs eventArgs, SubscriptionStatus subscription)
        {
            var dataType = subscription.Subscription.Name;

            if (dataType == SubscribeOptionNames.Ticker)
            {
                var tickerMessage = TickerMessage.CreateFromString(eventArgs.RawContent, subscription);
                TickerReceived.InvokeAll(this, new TickerEventArgs(subscription.ChannelId.Value, subscription.Pair, tickerMessage));
            }
            if (dataType == SubscribeOptionNames.OHLC)
            {
                var ohlcMessage = OhlcMessage.CreateFromString(eventArgs.RawContent);
                OhlcReceived.InvokeAll(this, new OhlcEventArgs(subscription.ChannelId.Value, subscription.Pair, ohlcMessage));
            }
            if (dataType == SubscribeOptionNames.Trade)
            {
                var tradeMessage = TradeMessage.CreateFromString(eventArgs.RawContent);
                TradeReceived.InvokeAll(this, new TradeEventArgs(subscription.ChannelId.Value, subscription.Pair, tradeMessage));
            }
            if (dataType == SubscribeOptionNames.Spread)
            {
                var spreadMessage = SpreadMessage.CreateFromString(eventArgs.RawContent);
                SpreadReceived.InvokeAll(this, new SpreadEventArgs(subscription.ChannelId.Value, subscription.Pair, spreadMessage));
            }
            if (dataType == SubscribeOptionNames.Book)
            {
                if (eventArgs.RawContent.Contains(@"""as"":") && eventArgs.RawContent.Contains(@"""bs"":"))
                {
                    var bookSnapshotMessage = BookSnapshotMessage.CreateFromString(eventArgs.RawContent);
                    BookSnapshotReceived.InvokeAll(this, new KrakenDataEventArgs <BookSnapshotMessage>(eventArgs.ChannelId.Value, subscription.Pair, bookSnapshotMessage));
                }
                if (eventArgs.RawContent.Contains(@"""a"":") || eventArgs.RawContent.Contains(@"""b"":"))
                {
                    var bookUpdateMessage = BookUpdateMessage.CreateFromString(eventArgs.RawContent);
                    BookUpdateReceived.InvokeAll(this, new KrakenDataEventArgs <BookUpdateMessage>(eventArgs.ChannelId.Value, subscription.Pair, bookUpdateMessage));
                }
            }
        }
Ejemplo n.º 7
0
        private void HandleIncomingMessage(object sender, KrakenMessageEventArgs eventArgs)
        {
            logger.LogDebug("Handling incoming '{event}' message", eventArgs.Event);

            switch (eventArgs.Event)
            {
            case Heartbeat.EventName:
                var heartbeat = serializer.Deserialize <Heartbeat>(eventArgs.RawContent);
                logger.LogTrace("Heartbeat received: {heartbeat}", heartbeat);
                HeartbeatReceived.InvokeAll(this, heartbeat);
                break;

            case SystemStatus.EventName:
                var systemStatus = serializer.Deserialize <SystemStatus>(eventArgs.RawContent);
                logger.LogTrace("System status changed: {systemStatus}", systemStatus);
                SystemStatus = systemStatus;
                SystemStatusChanged.InvokeAll(this, systemStatus);
                break;

            case SubscriptionStatus.EventName:
                var subscriptionStatus = serializer.Deserialize <SubscriptionStatus>(eventArgs.RawContent);
                logger.LogTrace("Subscription status changed: {subscriptionStatus}", subscriptionStatus);

                SynchronizeSubscriptions(subscriptionStatus);
                SubscriptionStatusChanged.InvokeAll(this, subscriptionStatus);

                break;

            case "data":

                var subscription = Subscriptions.ContainsKey(eventArgs.ChannelId.Value) ? Subscriptions[eventArgs.ChannelId.Value] : null;
                if (subscription == null)
                {
                    logger.LogWarning("Didn't find a subscription for channelId {channelId}", eventArgs.ChannelId);
                    break;
                }

                var dataType = subscription.Subscription.Name;

                if (dataType == SubscribeOptionNames.Ticker)
                {
                    var tickerMessage = TickerMessage.CreateFromString(eventArgs.RawContent, subscription);
                    TickerReceived.InvokeAll(this, new TickerEventArgs(subscription.ChannelId.Value, subscription.Pair, tickerMessage));
                }
                if (dataType == SubscribeOptionNames.OHLC)
                {
                    var ohlcMessage = OhlcMessage.CreateFromString(eventArgs.RawContent);
                    OhlcReceived.InvokeAll(this, new OhlcEventArgs(subscription.ChannelId.Value, subscription.Pair, ohlcMessage));
                }
                if (dataType == SubscribeOptionNames.Trade)
                {
                    var tradeMessage = TradeMessage.CreateFromString(eventArgs.RawContent);
                    TradeReceived.InvokeAll(this, new TradeEventArgs(subscription.ChannelId.Value, subscription.Pair, tradeMessage));
                }
                if (dataType == SubscribeOptionNames.Spread)
                {
                    var spreadMessage = SpreadMessage.CreateFromString(eventArgs.RawContent);
                    SpreadReceived.InvokeAll(this, new SpreadEventArgs(subscription.ChannelId.Value, subscription.Pair, spreadMessage));
                }
                if (dataType == SubscribeOptionNames.Book)
                {
                    if (eventArgs.RawContent.Contains(@"""as"":") && eventArgs.RawContent.Contains(@"""bs"":"))
                    {
                        var bookSnapshotMessage = BookSnapshotMessage.CreateFromString(eventArgs.RawContent);
                        BookSnapshotReceived.InvokeAll(this, new KrakenDataEventArgs <BookSnapshotMessage>(eventArgs.ChannelId.Value, subscription.Pair, bookSnapshotMessage));
                    }
                    if (eventArgs.RawContent.Contains(@"""a"":") || eventArgs.RawContent.Contains(@"""b"":"))
                    {
                        var bookUpdateMessage = BookUpdateMessage.CreateFromString(eventArgs.RawContent);
                        BookUpdateReceived.InvokeAll(this, new KrakenDataEventArgs <BookUpdateMessage>(eventArgs.ChannelId.Value, subscription.Pair, bookUpdateMessage));
                    }
                }

                break;

            default:
                logger.LogWarning("Could not handle incoming message: {message}", eventArgs.RawContent);
                break;
            }
        }
Ejemplo n.º 8
0
        public async Task SaveAsync(BookMessage model)
        {
            bool inserted = false;
            var  book     = await _db.Books.FilterTitle(model.Title).FirstOrDefaultAsync();

            if (book == null)
            {
                inserted = true;
                book     = new Entities.Book();
            }

            book.Title    = model.Title;
            book.Language = model.Language;

            if (model.Category != null)
            {
                var category = await _db.Categories.FirstOrDefaultAsync(x => x.Name == model.Category);

                if (category == null)
                {
                    category      = new Entities.BookCategory();
                    category.Name = model.Category;
                }

                if (!book.Categories.Any(x => x.Category.Name == category.Name))
                {
                    book.Categories.Add(new Entities.BookCategoryBook()
                    {
                        Category = category
                    });
                }
            }

            var authorName = model.Author;

            if (FeatureAddTableAtuthor.Get())
            {
                book.Author = await _db.Authors.FirstOrDefaultAsync(x => x.Name == authorName);

                if (book.Author == null)
                {
                    book.Author      = new Entities.BookAuthor();
                    book.Author.Name = authorName;
                }
            }
            else
            {
                book.AuthorName = authorName;
            }

            if (inserted)
            {
                await _db.AddAsync(book);
            }
            else
            {
                _db.Update(book);
            }

            await _db.SaveChangesAsync();

            var message = new BookUpdateMessage
            {
                Id       = book.Id,
                Title    = book.Title,
                Author   = authorName,
                Language = book.Language
            };

            await _bus.PublishAsync(ExchangeNames.Book, message);
        }