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
… order book
  • Loading branch information
Jonnern committed Apr 2, 2024
1 parent 8ffd830 commit 061499e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
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);
}
}
}
2 changes: 1 addition & 1 deletion CoinEx.Net/Clients/SpotApi/CoinExSocketClientSpotApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public async Task<CallResult<UpdateSubscription>> 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);
}

Expand Down
8 changes: 8 additions & 0 deletions CoinEx.Net/SymbolOrderBooks/CoinExSpotSymbolOrderBook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 061499e

Please sign in to comment.