From 061499e27c628924c544b00609682079ec80e329 Mon Sep 17 00:00:00 2001 From: Jonnern <10881387+Jonnern@users.noreply.github.com> Date: Tue, 2 Apr 2024 15:52:00 +0200 Subject: [PATCH 1/2] Query the initial book instead of waiting for the snapshot to set the order book --- CoinEx.Net.UnitTests/OrderBookTests.cs | 24 +++++++++++++++++++ .../SpotApi/CoinExSocketClientSpotApi.cs | 2 +- .../CoinExSpotSymbolOrderBook.cs | 8 +++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 CoinEx.Net.UnitTests/OrderBookTests.cs diff --git a/CoinEx.Net.UnitTests/OrderBookTests.cs b/CoinEx.Net.UnitTests/OrderBookTests.cs new file mode 100644 index 0000000..7f75557 --- /dev/null +++ b/CoinEx.Net.UnitTests/OrderBookTests.cs @@ -0,0 +1,24 @@ +using CoinEx.Net.SymbolOrderBooks; +using CryptoExchange.Net.Objects; +using NUnit.Framework; +using System.Threading.Tasks; + +namespace CoinEx.Net.UnitTests +{ + [TestFixture] + public class OrderBookTests + { + [TestCase] + public async Task StartOrderBook_Should_BeSynced() + { + // arrange + using var book = new CoinExSpotSymbolOrderBook("BTCUSDT"); + + // act + await book.StartAsync(); + + // assert + Assert.That(book.Status == OrderBookStatus.Synced); + } + } +} diff --git a/CoinEx.Net/Clients/SpotApi/CoinExSocketClientSpotApi.cs b/CoinEx.Net/Clients/SpotApi/CoinExSocketClientSpotApi.cs index 5512830..2811530 100644 --- a/CoinEx.Net/Clients/SpotApi/CoinExSocketClientSpotApi.cs +++ b/CoinEx.Net/Clients/SpotApi/CoinExSocketClientSpotApi.cs @@ -187,7 +187,7 @@ public async Task> SubscribeToOrderBookUpdatesAsy mergeDepth.ValidateIntBetween(nameof(mergeDepth), 0, 8); limit.ValidateIntValues(nameof(limit), 5, 10, 20); - var subscription = new CoinExDepthSubscription(_logger, symbol, new object[] { symbol, limit, CoinExHelpers.MergeDepthIntToString(mergeDepth), false }, onMessage); + var subscription = new CoinExDepthSubscription(_logger, symbol, new object[] { symbol, limit, CoinExHelpers.MergeDepthIntToString(mergeDepth), true }, onMessage); return await SubscribeAsync(subscription, ct).ConfigureAwait(false); } diff --git a/CoinEx.Net/SymbolOrderBooks/CoinExSpotSymbolOrderBook.cs b/CoinEx.Net/SymbolOrderBooks/CoinExSpotSymbolOrderBook.cs index cc6bcac..b98a58e 100644 --- a/CoinEx.Net/SymbolOrderBooks/CoinExSpotSymbolOrderBook.cs +++ b/CoinEx.Net/SymbolOrderBooks/CoinExSpotSymbolOrderBook.cs @@ -76,6 +76,10 @@ protected override async Task> DoStartAsync(Cance Status = OrderBookStatus.Syncing; + // Query the initial order book + var initialBook = await _socketClient.SpotApi.GetOrderBookAsync(Symbol, Levels!.Value, 0).ConfigureAwait(false); + SetInitialOrderBook(DateTime.UtcNow.Ticks, initialBook.Data.Bids, initialBook.Data.Asks); + var setResult = await WaitForSetOrderBookAsync(_initialDataTimeout, ct).ConfigureAwait(false); return setResult ? result : new CallResult(setResult.Error!); } @@ -83,6 +87,10 @@ protected override async Task> DoStartAsync(Cance /// protected override async Task> DoResyncAsync(CancellationToken ct) { + // Query the initial order book + var initialBook = await _socketClient.SpotApi.GetOrderBookAsync(Symbol, Levels!.Value, 0).ConfigureAwait(false); + SetInitialOrderBook(DateTime.UtcNow.Ticks, initialBook.Data.Bids, initialBook.Data.Asks); + return await WaitForSetOrderBookAsync(_initialDataTimeout, ct).ConfigureAwait(false); } From 928a1bf1003383d495a24e5368a711c5d266a5b7 Mon Sep 17 00:00:00 2001 From: Jonnern <10881387+Jonnern@users.noreply.github.com> Date: Wed, 3 Apr 2024 10:39:00 +0200 Subject: [PATCH 2/2] Add diffUpdates as optional argument to book subscription --- CoinEx.Net/Clients/SpotApi/CoinExSocketClientSpotApi.cs | 4 ++-- .../Interfaces/Clients/SpotApi/ICoinExSocketClientSpotApi.cs | 3 ++- CoinEx.Net/SymbolOrderBooks/CoinExSpotSymbolOrderBook.cs | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/CoinEx.Net/Clients/SpotApi/CoinExSocketClientSpotApi.cs b/CoinEx.Net/Clients/SpotApi/CoinExSocketClientSpotApi.cs index 2811530..4ae8d20 100644 --- a/CoinEx.Net/Clients/SpotApi/CoinExSocketClientSpotApi.cs +++ b/CoinEx.Net/Clients/SpotApi/CoinExSocketClientSpotApi.cs @@ -181,13 +181,13 @@ public async Task> SubscribeToAllTickerUpdatesAsy } /// - public async Task> SubscribeToOrderBookUpdatesAsync(string symbol, int limit, int mergeDepth, Action> onMessage, CancellationToken ct = default) + public async Task> SubscribeToOrderBookUpdatesAsync(string symbol, int limit, int mergeDepth, Action> onMessage, bool diffUpdates, CancellationToken ct = default) { symbol.ValidateCoinExSymbol(); mergeDepth.ValidateIntBetween(nameof(mergeDepth), 0, 8); limit.ValidateIntValues(nameof(limit), 5, 10, 20); - var subscription = new CoinExDepthSubscription(_logger, symbol, new object[] { symbol, limit, CoinExHelpers.MergeDepthIntToString(mergeDepth), true }, onMessage); + var subscription = new CoinExDepthSubscription(_logger, symbol, new object[] { symbol, limit, CoinExHelpers.MergeDepthIntToString(mergeDepth), diffUpdates }, onMessage); return await SubscribeAsync(subscription, ct).ConfigureAwait(false); } diff --git a/CoinEx.Net/Interfaces/Clients/SpotApi/ICoinExSocketClientSpotApi.cs b/CoinEx.Net/Interfaces/Clients/SpotApi/ICoinExSocketClientSpotApi.cs index 63c6e18..60db0a3 100644 --- a/CoinEx.Net/Interfaces/Clients/SpotApi/ICoinExSocketClientSpotApi.cs +++ b/CoinEx.Net/Interfaces/Clients/SpotApi/ICoinExSocketClientSpotApi.cs @@ -76,9 +76,10 @@ public interface ICoinExSocketClientSpotApi : ISocketApiClient, IDisposable /// The limit of results to receive in a update /// The depth of merging, based on 8 decimals. 1 mergeDepth will merge the last decimals of all order in the book, 7 will merge the last 7 decimals of all orders together /// Data handler + /// Set to true to get snapshot first, then diff updates /// Cancellation token for closing this subscription /// A stream subscription. This stream subscription can be used to be notified when the socket is disconnected/reconnected - Task> SubscribeToOrderBookUpdatesAsync(string symbol, int limit, int mergeDepth, Action> onMessage, CancellationToken ct = default); + Task> SubscribeToOrderBookUpdatesAsync(string symbol, int limit, int mergeDepth, Action> onMessage, bool diffUpdates = false, CancellationToken ct = default); /// /// Gets the latest trades on a symbol diff --git a/CoinEx.Net/SymbolOrderBooks/CoinExSpotSymbolOrderBook.cs b/CoinEx.Net/SymbolOrderBooks/CoinExSpotSymbolOrderBook.cs index b98a58e..7337222 100644 --- a/CoinEx.Net/SymbolOrderBooks/CoinExSpotSymbolOrderBook.cs +++ b/CoinEx.Net/SymbolOrderBooks/CoinExSpotSymbolOrderBook.cs @@ -64,7 +64,7 @@ public CoinExSpotSymbolOrderBook(string symbol, /// protected override async Task> DoStartAsync(CancellationToken ct) { - var result = await _socketClient.SpotApi.SubscribeToOrderBookUpdatesAsync(Symbol, Levels!.Value, 0, HandleUpdate).ConfigureAwait(false); + var result = await _socketClient.SpotApi.SubscribeToOrderBookUpdatesAsync(Symbol, Levels!.Value, 0, HandleUpdate, diffUpdates: true).ConfigureAwait(false); if (!result) return result;