Skip to content

Commit

Permalink
Query the initial book instead of waiting for the snapshot to set the… (
Browse files Browse the repository at this point in the history
#68)

* Query the initial book instead of waiting for the snapshot to set the order book

* Add diffUpdates as optional argument to book subscription
  • Loading branch information
Jonnern authored Apr 3, 2024
1 parent 8ffd830 commit e2d72fd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
24 changes: 24 additions & 0 deletions CoinEx.Net.UnitTests/OrderBookTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
4 changes: 2 additions & 2 deletions CoinEx.Net/Clients/SpotApi/CoinExSocketClientSpotApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,13 @@ public async Task<CallResult<UpdateSubscription>> SubscribeToAllTickerUpdatesAsy
}

/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToOrderBookUpdatesAsync(string symbol, int limit, int mergeDepth, Action<DataEvent<CoinExSocketOrderBook>> onMessage, CancellationToken ct = default)
public async Task<CallResult<UpdateSubscription>> SubscribeToOrderBookUpdatesAsync(string symbol, int limit, int mergeDepth, Action<DataEvent<CoinExSocketOrderBook>> 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), false }, onMessage);
var subscription = new CoinExDepthSubscription(_logger, symbol, new object[] { symbol, limit, CoinExHelpers.MergeDepthIntToString(mergeDepth), diffUpdates }, onMessage);
return await SubscribeAsync(subscription, ct).ConfigureAwait(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ public interface ICoinExSocketClientSpotApi : ISocketApiClient, IDisposable
/// <param name="limit">The limit of results to receive in a update</param>
/// <param name="mergeDepth">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</param>
/// <param name="onMessage">Data handler</param>
/// <param name="diffUpdates">Set to true to get snapshot first, then diff updates</param>
/// <param name="ct">Cancellation token for closing this subscription</param>
/// <returns>A stream subscription. This stream subscription can be used to be notified when the socket is disconnected/reconnected</returns>
Task<CallResult<UpdateSubscription>> SubscribeToOrderBookUpdatesAsync(string symbol, int limit, int mergeDepth, Action<DataEvent<CoinExSocketOrderBook>> onMessage, CancellationToken ct = default);
Task<CallResult<UpdateSubscription>> SubscribeToOrderBookUpdatesAsync(string symbol, int limit, int mergeDepth, Action<DataEvent<CoinExSocketOrderBook>> onMessage, bool diffUpdates = false, CancellationToken ct = default);

/// <summary>
/// Gets the latest trades on a symbol
Expand Down
10 changes: 9 additions & 1 deletion CoinEx.Net/SymbolOrderBooks/CoinExSpotSymbolOrderBook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public CoinExSpotSymbolOrderBook(string symbol,
/// <inheritdoc />
protected override async Task<CallResult<UpdateSubscription>> 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;

Expand All @@ -76,13 +76,21 @@ protected override async Task<CallResult<UpdateSubscription>> 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<UpdateSubscription>(setResult.Error!);
}

/// <inheritdoc />
protected override async Task<CallResult<bool>> 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);
}

Expand Down

0 comments on commit e2d72fd

Please sign in to comment.