Пример #1
0
        public void SubscribingToTradeStream_Should_TriggerWhenTradeStreamMessageIsReceived()
        {
            // arrange
            var socket = new TestSocket();
            var client = TestHelpers.CreateSocketClient(socket);

            BinanceStreamTrade result = null;

            client.Spot.SubscribeToTradeUpdates("ETHBTC", (test) => result = test);

            var data = new BinanceCombinedStream <BinanceStreamTrade>()
            {
                Stream = "test",
                Data   = new BinanceStreamTrade()
                {
                    Event         = "TestTradeStream",
                    EventTime     = new DateTime(2017, 1, 1),
                    Symbol        = "ETHBTC",
                    BuyerIsMaker  = true,
                    BuyerOrderId  = 10000000000000,
                    SellerOrderId = 2000000000000,
                    Price         = 1.1m,
                    Quantity      = 2.2m,
                    TradeTime     = new DateTime(2017, 1, 1)
                }
            };

            // act
            socket.InvokeMessage(data);

            // assert
            Assert.IsNotNull(result);
            Assert.IsTrue(TestHelpers.AreEqual(data.Data, result));
        }
        public async Task SubscribingToUserStream_Should_TriggerWhenAccountUpdateStreamMessageIsReceived()
        {
            // arrange
            var socket = new TestSocket();
            var client = TestHelpers.CreateSocketClient(socket);

            BinanceStreamBalanceUpdate result = null;
            await client.Spot.SubscribeToUserDataUpdatesAsync("test", null, null, null, (test) => result = test.Data);

            var data = new BinanceCombinedStream <BinanceStreamBalanceUpdate>
            {
                Stream = "test",
                Data   = new BinanceStreamBalanceUpdate()
                {
                    Event        = "balanceUpdate",
                    EventTime    = new DateTime(2017, 1, 1),
                    Asset        = "BTC",
                    BalanceDelta = 1,
                    ClearTime    = new DateTime(2018, 1, 1),
                }
            };

            // act
            socket.InvokeMessage(data);

            // assert
            Assert.IsNotNull(result);
            Assert.IsTrue(TestHelpers.AreEqual(data.Data, result));
        }
Пример #3
0
        //
        public async Task <string> OpenCombinedStream(params BinanceStreamNameInfo[] infoItems)
        {
            IBinanceStream stream;

            lock (this.streamsCacheLocker)
            {
                string streamName = BinanceCombinedStream.CombineStreamName(infoItems);
                if (this.streamsCache.ContainsKey(streamName))
                {
                    return(streamName);
                }

                var combinedStream = new BinanceCombinedStream(this.socketBaseEndpoint, streamName, infoItems);

                combinedStream.Opened        += this.Stream_Opened;
                combinedStream.Closed        += this.Stream_Closed;
                combinedStream.Error         += this.Stream_Error;
                combinedStream.EventReceived += this.Stream_EventReceived;
                combinedStream.Initialize();

                stream = combinedStream;

                this.streamsCache.Add(streamName, combinedStream);
            }

            await stream.OpenAsync();

            return(stream.Name);
        }
Пример #4
0
        public void SubscribingToKlineStream_Should_TriggerWhenKlineStreamMessageIsReceived()
        {
            // arrange
            var socket = new Mock <IWebsocket>();

            socket.Setup(s => s.Close());
            socket.Setup(s => s.Connect()).Returns(Task.FromResult(true));
            socket.Setup(s => s.SetEnabledSslProtocols(It.IsAny <System.Security.Authentication.SslProtocols>()));

            var factory = new Mock <IWebsocketFactory>();

            factory.Setup(s => s.CreateWebsocket(It.IsAny <Log>(), It.IsAny <string>())).Returns(socket.Object);

            BinanceStreamKlineData result = null;
            var client = new BinanceSocketClient {
                SocketFactory = factory.Object
            };

            client.SubscribeToKlineStream("test", KlineInterval.OneMinute, (test) => result = test);

            var data = new BinanceCombinedStream <BinanceStreamKlineData>()
            {
                Stream = "test",
                Data   = new BinanceStreamKlineData()
                {
                    Event     = "TestKlineStream",
                    EventTime = new DateTime(2017, 1, 1),
                    Symbol    = "test",
                    Data      = new BinanceStreamKline()
                    {
                        TakerBuyBaseAssetVolume = 0.1m,
                        Close      = 0.2m,
                        CloseTime  = new DateTime(2017, 1, 2),
                        Final      = true,
                        FirstTrade = 10000000000,
                        High       = 0.3m,
                        Interval   = KlineInterval.OneMinute,
                        LastTrade  = 2000000000000,
                        Low        = 0.4m,
                        Open       = 0.5m,
                        TakerBuyQuoteAssetVolume = 0.6m,
                        QuoteAssetVolume         = 0.7m,
                        OpenTime   = new DateTime(2017, 1, 1),
                        Symbol     = "test",
                        TradeCount = 10,
                        Volume     = 0.8m
                    }
                }
            };

            // act
            socket.Raise(r => r.OnMessage += null, JsonConvert.SerializeObject(data));

            // assert
            Assert.IsNotNull(result);
            Assert.IsTrue(TestHelpers.PublicInstancePropertiesEqual(data.Data, result, "Data"));
            Assert.IsTrue(TestHelpers.PublicInstancePropertiesEqual(data.Data.Data, result.Data));
        }
        public void SubscribingToUserStream_Should_TriggerWhenOcoOrderUpdateStreamMessageIsReceived()
        {
            // arrange
            var socket = new TestSocket();
            var client = TestHelpers.CreateSocketClient(socket, new BinanceSocketClientOptions()
            {
                LogLevel = LogLevel.Debug
            });

            BinanceStreamOrderList result = null;

            client.Spot.SubscribeToUserDataUpdatesAsync("test", null, (test) => result = test.Data, null, null);

            var data = new BinanceCombinedStream <BinanceStreamOrderList>
            {
                Stream = "test",
                Data   = new BinanceStreamOrderList()
                {
                    Event             = "listStatus",
                    EventTime         = new DateTime(2017, 1, 1),
                    Symbol            = "BNBUSDT",
                    ContingencyType   = "OCO",
                    ListStatusType    = ListStatusType.Done,
                    ListOrderStatus   = ListOrderStatus.Done,
                    OrderListId       = 1,
                    ListClientOrderId = "2",
                    TransactionTime   = new DateTime(2018, 1, 1),
                    Orders            = new[]
                    {
                        new BinanceStreamOrderId()
                        {
                            Symbol        = "BNBUSDT",
                            OrderId       = 2,
                            ClientOrderId = "3"
                        },
                        new BinanceStreamOrderId()
                        {
                            Symbol        = "BNBUSDT",
                            OrderId       = 3,
                            ClientOrderId = "4"
                        }
                    }
                }
            };

            // act
            socket.InvokeMessage(data);

            // assert
            Assert.IsNotNull(result);
            Assert.IsTrue(TestHelpers.AreEqual(data.Data, result, "Orders"));
            Assert.IsTrue(TestHelpers.AreEqual(data.Data.Orders.ToList()[0], result.Orders.ToList()[0]));
            Assert.IsTrue(TestHelpers.AreEqual(data.Data.Orders.ToList()[1], result.Orders.ToList()[1]));
        }
Пример #6
0
        public void SubscribingToKlineStream_Should_TriggerWhenKlineStreamMessageIsReceived()
        {
            // arrange
            var socket = new TestSocket();
            var client = TestHelpers.CreateSocketClient(socket);

            IBinanceStreamKlineData result = null;

            client.Spot.SubscribeToKlineUpdatesAsync("ETHBTC", KlineInterval.OneMinute, (test) => result = test);

            var data = new BinanceCombinedStream <BinanceStreamKlineData>()
            {
                Stream = "test",
                Data   = new BinanceStreamKlineData()
                {
                    Event     = "TestKlineStream",
                    EventTime = new DateTime(2017, 1, 1),
                    Symbol    = "ETHBTC",
                    Data      = new BinanceStreamKline()
                    {
                        TakerBuyBaseVolume = 0.1m,
                        Close               = 0.2m,
                        CloseTime           = new DateTime(2017, 1, 2),
                        Final               = true,
                        FirstTrade          = 10000000000,
                        High                = 0.3m,
                        Interval            = KlineInterval.OneMinute,
                        LastTrade           = 2000000000000,
                        Low                 = 0.4m,
                        Open                = 0.5m,
                        TakerBuyQuoteVolume = 0.6m,
                        QuoteVolume         = 0.7m,
                        OpenTime            = new DateTime(2017, 1, 1),
                        Symbol              = "test",
                        TradeCount          = 10,
                        BaseVolume          = 0.8m
                    }
                }
            };

            // act
            socket.InvokeMessage(data);

            // assert
            Assert.IsNotNull(result);
            Assert.IsTrue(TestHelpers.AreEqual(data.Data, result, "Data"));
            Assert.IsTrue(TestHelpers.AreEqual(data.Data.Data, result.Data));
        }
        public void SubscribingToUserStream_Should_TriggerWhenOrderUpdateStreamMessageIsReceived()
        {
            // arrange
            var socket = new TestSocket();
            var client = TestHelpers.CreateSocketClient(socket);

            BinanceStreamOrderUpdate result = null;

            client.Spot.SubscribeToUserDataUpdatesAsync("test", (test) => result = test.Data, null, null, null);

            var data = new BinanceCombinedStream <BinanceStreamOrderUpdate>
            {
                Stream = "test",
                Data   = new BinanceStreamOrderUpdate()
                {
                    Event           = "executionReport",
                    EventTime       = new DateTime(2017, 1, 1),
                    BuyerIsMaker    = true,
                    Commission      = 2.2m,
                    CommissionAsset = "test",
                    ExecutionType   = ExecutionType.Trade,
                    I                     = 100000000000,
                    OrderId               = 100000000000,
                    Price                 = 6.6m,
                    Quantity              = 8.8m,
                    RejectReason          = OrderRejectReason.AccountCannotSettle,
                    Side                  = OrderSide.Buy,
                    Status                = OrderStatus.Filled,
                    Symbol                = "test",
                    TimeInForce           = TimeInForce.GoodTillCancel,
                    TradeId               = 10000000000000,
                    Type                  = OrderType.Limit,
                    ClientOrderId         = "123",
                    IcebergQuantity       = 9.9m,
                    IsWorking             = true,
                    OriginalClientOrderId = "456",
                    StopPrice             = 10.10m
                }
            };

            // act
            socket.InvokeMessage(data);

            // assert
            Assert.IsNotNull(result);
            Assert.IsTrue(TestHelpers.AreEqual(data.Data, result, "Balances"));
        }
Пример #8
0
        public void SubscribingToTradeStream_Should_TriggerWhenTradeStreamMessageIsReceived()
        {
            // arrange
            var socket = new Mock <IWebsocket>();

            socket.Setup(s => s.Close());
            socket.Setup(s => s.Connect()).Returns(Task.FromResult(true));
            socket.Setup(s => s.SetEnabledSslProtocols(It.IsAny <System.Security.Authentication.SslProtocols>()));

            var factory = new Mock <IWebsocketFactory>();

            factory.Setup(s => s.CreateWebsocket(It.IsAny <Log>(), It.IsAny <string>())).Returns(socket.Object);

            BinanceStreamTrade result = null;
            var client = new BinanceSocketClient {
                SocketFactory = factory.Object
            };

            client.SubscribeToTradesStream("test", (test) => result = test);

            var data = new BinanceCombinedStream <BinanceStreamTrade>()
            {
                Stream = "test",
                Data   = new BinanceStreamTrade()
                {
                    Event         = "TestTradeStream",
                    EventTime     = new DateTime(2017, 1, 1),
                    Symbol        = "test",
                    TradeId       = 1000000000000,
                    BuyerIsMaker  = true,
                    BuyerOrderId  = 10000000000000,
                    SellerOrderId = 2000000000000,
                    Price         = 1.1m,
                    Quantity      = 2.2m,
                    TradeTime     = new DateTime(2017, 1, 1)
                }
            };

            // act
            socket.Raise(r => r.OnMessage += null, JsonConvert.SerializeObject(data));

            // assert
            Assert.IsNotNull(result);
            Assert.IsTrue(TestHelpers.PublicInstancePropertiesEqual(data.Data, result));
        }
Пример #9
0
        public void SubscribingToSymbolTicker_Should_TriggerWhenSymbolTickerStreamMessageIsReceived()
        {
            // arrange
            var socket = new TestSocket();
            var client = TestHelpers.CreateSocketClient(socket, new BinanceSocketClientOptions()
            {
                LogVerbosity = LogVerbosity.Debug
            });

            IBinanceTick result = null;

            client.Spot.SubscribeToSymbolTickerUpdates("ETHBTC", (test) => result = test);

            var data = new BinanceCombinedStream <BinanceStreamTick>()
            {
                Stream = "test",
                Data   = new BinanceStreamTick()
                {
                    FirstTradeId      = 1,
                    HighPrice         = 0.7m,
                    LastTradeId       = 2,
                    LowPrice          = 0.8m,
                    OpenPrice         = 0.9m,
                    PrevDayClosePrice = 1.0m,
                    PriceChange       = 1.1m,
                    Symbol            = "test",
                    BaseVolume        = 1.3m,
                    QuoteVolume       = 1.4m,
                    TotalTrades       = 3
                }
            };

            // act
            socket.InvokeMessage(data);

            // assert
            Assert.IsNotNull(result);
            Assert.IsTrue(TestHelpers.AreEqual(data.Data, result));
        }
        public async Task SubscribingToAllSymbolTicker_Should_TriggerWhenAllSymbolTickerStreamMessageIsReceived()
        {
            // arrange
            var socket = new TestSocket();
            var client = TestHelpers.CreateSocketClient(socket);

            IBinanceTick[] result = null;
            await client.Spot.SubscribeToAllSymbolTickerUpdatesAsync((test) => result = test.Data.ToArray());

            var data = new BinanceCombinedStream <BinanceStreamTick[]>
            {
                Data = new[]
                {
                    new BinanceStreamTick()
                    {
                        FirstTradeId      = 1,
                        HighPrice         = 0.7m,
                        LastTradeId       = 2,
                        LowPrice          = 0.8m,
                        OpenPrice         = 0.9m,
                        PrevDayClosePrice = 1.0m,
                        PriceChange       = 1.1m,
                        Symbol            = "test",
                        BaseVolume        = 1.3m,
                        QuoteVolume       = 1.4m,
                        TotalTrades       = 3
                    }
                },
                Stream = "!ticker@arr"
            };

            // act
            socket.InvokeMessage(data);

            // assert
            Assert.IsNotNull(result);
            Assert.IsTrue(TestHelpers.AreEqual(data.Data[0], result[0]));
        }