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 1 commit
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);
}
}
}
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