Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query the initial book instead of waiting for the snapshot to set the… #68

Merged
merged 2 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading