示例#1
0
        public void Initialize_InvalidPair_ThrowsException()
        {
            var socketMock  = new Mock <IWebSocket>();
            var binanceMock = InitBinanceClientMock();

            var subject = new Binance(socketMock.Object, binanceMock.Object, Mock.Of <ITime>());

            var e = Expect.ThrowAsync <ArgumentException>(async() => {
                await subject.Initialize(Assets.BTC, Assets.DOGE);
            });

            Assert.AreEqual("Trading pair BTCDOGE is not available on Binance", e.Message);
        }
示例#2
0
        public void Initialize_ValidPair_OpensSocketAndSubscribesAndReturnsBalances()
        {
            var socketMock  = new Mock <IWebSocket>();
            var binanceMock = InitBinanceClientMock();

            var subject = new Binance(socketMock.Object, binanceMock.Object, Mock.Of <ITime>());

            var(asset1, asset2) = subject.Initialize(Assets.BTC, Assets.USDT).Result;

            socketMock.Verify(m => m.Connect("wss://stream.binance.com:9443/ws/btcusdt@ticker"));
            Assert.AreEqual(1M, asset1);
            Assert.AreEqual(10000M, asset2);
        }
示例#3
0
        public void Sell_TransactionLessThanMinQuantity_ReturnsNull()
        {
            var binanceClient = InitBinanceClientMock();
            var subject       = new Binance(Mock.Of <IWebSocket>(), binanceClient.Object, Mock.Of <ITime>());

            subject.Initialize(Assets.BTC, Assets.USDT).Wait();

            var result = subject.Sell(new Sample()
            {
                Value = 0.0001M
            }, 0.000099M).Result;

            Assert.IsNull(result);
            binanceClient.Verify(m => m.CreateOrder(It.IsAny <CreateOrderRequest>()), Times.Never());
        }
示例#4
0
        public void Dispose_Initialized_CantDoStuff()
        {
            var socketMock  = new Mock <IWebSocket>();
            var binanceMock = InitBinanceClientMock();

            var subject = new Binance(socketMock.Object, binanceMock.Object, Mock.Of <ITime>());

            subject.Initialize(Assets.BTC, Assets.USDT).Wait();

            subject.Dispose();

            var e = Expect.ThrowAsync <InvalidOperationException>(async() => {
                await subject.GetCurrentPrice();
            });

            Assert.AreEqual("Binance cannot GetCurrentPrice until Initialized!", e.Message);
            socketMock.Verify(m => m.Dispose());
        }
示例#5
0
        public void CheckOrder_OrderFilled_ReturnsOrderWithSameIDMarkedFilled()
        {
            var binanceClient = InitBinanceClientMock();

            binanceClient.Setup(m => m.QueryOrder(It.Is <QueryOrderRequest>(q =>
                                                                            q.OriginalClientOrderId == "123" &&
                                                                            q.Symbol == "BTCUSDT"), 5000))
            .ReturnsAsync(new OrderResponse()
            {
                Status = OrderStatus.Filled
            });
            var subject = new Binance(Mock.Of <IWebSocket>(), binanceClient.Object, Mock.Of <ITime>());

            subject.Initialize(Assets.BTC, Assets.USDT).Wait();

            var result = subject.CheckOrder(new Order()
            {
                Id = "123"
            }).Result;

            Assert.AreEqual("123", result.Id);
            Assert.IsTrue(result.Fulfilled);
        }
示例#6
0
        public void GetCurrentPrice_NonTickerMessage_Retries()
        {
            var time = DateTime.Now;
            var message = new { e = "24hrTicker", b = 1.25 }.Json();
            var dummyMessage = new { type = "cat facts" }.Json();
            var socketMock  = new Mock <IWebSocket>();
            var timeMock    = new Mock <ITime>();
            var binanceMock = InitBinanceClientMock();

            var subject = new Binance(socketMock.Object, binanceMock.Object, timeMock.Object);

            subject.Initialize(Assets.BTC, Assets.USDT).Wait();

            socketMock.SetupSequence(m => m.ReceiveMessage())
            .ReturnsAsync(dummyMessage)
            .ReturnsAsync(message);
            timeMock.Setup(m => m.Now).Returns(time);

            var result = subject.GetCurrentPrice().Result;

            Assert.AreEqual(time, result.DateTime);
            Assert.AreEqual(1.25M, result.Value);
        }
示例#7
0
        public void GetCurrentPrice_SocketDisconnct_ReconnectsAndRetries()
        {
            var time = DateTime.Now;
            var message = new { e = "24hrTicker", b = 1.25 }.Json();
            var socketMock  = new Mock <IWebSocket>();
            var timeMock    = new Mock <ITime>();
            var binanceMock = InitBinanceClientMock();

            var subject = new Binance(socketMock.Object, binanceMock.Object, timeMock.Object);

            subject.Initialize(Assets.BTC, Assets.USDT).Wait();

            socketMock.SetupSequence(m => m.ReceiveMessage())
            .ThrowsAsync(new WebSocketException("Shit's broken yo"))
            .ReturnsAsync(message);
            timeMock.Setup(m => m.Now).Returns(time);

            var result = subject.GetCurrentPrice().Result;

            Assert.AreEqual(time, result.DateTime);
            Assert.AreEqual(1.25M, result.Value);
            socketMock.Verify(m => m.Connect("wss://stream.binance.com:9443/ws/btcusdt@ticker"));
        }
示例#8
0
        public void Sell_TransactionMoreThanMinQuantity_SubmitsAndReturnsOrder()
        {
            var binanceClient = InitBinanceClientMock();

            binanceClient.Setup(m => m.CreateOrder(It.Is <CreateOrderRequest>(r =>
                                                                              r.Symbol == "BTCUSDT" &&
                                                                              r.Side == OrderSide.Sell &&
                                                                              r.Type == OrderType.Market &&
                                                                              r.Quantity == 0.0005M
                                                                              ))).ReturnsAsync(new ResultCreateOrderResponse()
            {
                ClientOrderId = "123"
            });
            var subject = new Binance(Mock.Of <IWebSocket>(), binanceClient.Object, Mock.Of <ITime>());

            subject.Initialize(Assets.BTC, Assets.USDT).Wait();

            var result = subject.Sell(new Sample()
            {
                Value = 0.0001M
            }, 0.0005M).Result;

            Assert.AreEqual("123", result.Id);
        }