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); }
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); }
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()); }
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()); }
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); }
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); }
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")); }
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); }