Skip to content
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
7 changes: 7 additions & 0 deletions Crypto.Compare.sln
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Crypto.Compare.GateIo", "so
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Crypto.Compare.BitMart", "sources\integrations\Crypto.Compare.BitMart\Crypto.Compare.BitMart.csproj", "{B6196161-CEF3-48D7-8F56-692472A5E4FB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Crypto.Comapre.Binance", "sources\integrations\Crypto.Comapre.Binance\Crypto.Comapre.Binance.csproj", "{A62F8D6B-37A8-4D7A-A759-03E3C883239B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -111,6 +113,10 @@ Global
{B6196161-CEF3-48D7-8F56-692472A5E4FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B6196161-CEF3-48D7-8F56-692472A5E4FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B6196161-CEF3-48D7-8F56-692472A5E4FB}.Release|Any CPU.Build.0 = Release|Any CPU
{A62F8D6B-37A8-4D7A-A759-03E3C883239B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A62F8D6B-37A8-4D7A-A759-03E3C883239B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A62F8D6B-37A8-4D7A-A759-03E3C883239B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A62F8D6B-37A8-4D7A-A759-03E3C883239B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -135,6 +141,7 @@ Global
{410272B7-D024-49E8-9435-DDF0F309A134} = {A9E9246F-B758-4D14-ADAD-1EF937DCC5E8}
{A2DB814C-895A-4D09-AFC1-FDD0C988F2FD} = {A9E9246F-B758-4D14-ADAD-1EF937DCC5E8}
{B6196161-CEF3-48D7-8F56-692472A5E4FB} = {A9E9246F-B758-4D14-ADAD-1EF937DCC5E8}
{A62F8D6B-37A8-4D7A-A759-03E3C883239B} = {A9E9246F-B758-4D14-ADAD-1EF937DCC5E8}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D179164C-BFE5-45FD-BD60-476E93A84589}
Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,17 @@ INSERT INTO public."Providers"(
Need to replace in the request:
AccessKey and SecretKey - for keys from the exchange's personal account

### BINANCE

```sql
INSERT INTO public."Providers"(
"Name", "WebSite", "BaseUrl", "AccessKey", "SecretKey", "Status", "CreatedAt", "UpdatedAt")
VALUES ('binance', 'https://binance.com', 'https://api.binance.com', 'AccessKey', 'SecretKey', 1,NOW(), NOW());
```

Need to replace in the request:
AccessKey and SecretKey - for keys from the exchange's personal account

## Getting API keys for exchanges

- **ByBit:** https://www.bybit.com/en/help-center/article/How-to-create-your-API-key?category=ae1012f19fad1c184e
Expand Down Expand Up @@ -502,6 +513,18 @@ INSERT INTO public."Providers"(
"Name", "WebSite", "BaseUrl", "AccessKey", "SecretKey", "Status", "CreatedAt", "UpdatedAt", "PasswordKey")
VALUES ( 'bitmart', 'https://bitmart.com', 'https://api-cloud.bitmart.com', 'AccessKey', 'SecretKey', 1,NOW(), NOW(), '');
```

Нужно заменить в запросе:
AccessKey и SecretKey - на ключи с личного кабинета биржи

### BINANCE

```sql
INSERT INTO public."Providers"(
"Name", "WebSite", "BaseUrl", "AccessKey", "SecretKey", "Status", "CreatedAt", "UpdatedAt")
VALUES ('binance', 'https://binance.com', 'https://api.binance.com', 'AccessKey', 'SecretKey', 1,NOW(), NOW());
```

Нужно заменить в запросе:
AccessKey и SecretKey - на ключи с личного кабинета биржи

Expand Down
4 changes: 2 additions & 2 deletions sources/core/Crypto.Compare.Data/SymbolProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ public class SymbolProvider
/// Last date get quotes
/// </summary>
public DateTime UpdatedAt { get; set; }

/// <summary>
/// id symbol
/// </summary>
public long SymbolId { get; set; }

public virtual Symbol? Symbol { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class SymbolProviderUpdateDatabaseQueueService : BackgroundService
/// </summary>
private const int WaitTimeout = 3000;

private static List<string> propertyExclude = new() { "Symbol" };
//private static List<string> propertyExclude = new() { "Symbol" };


private readonly ISymbolProviderUpdateDatabaseQueue _symbolProviderUpdateDatabaseQueue;
Expand Down Expand Up @@ -51,7 +51,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)

await dbContext.BulkUpdateAsync(models, option =>
{
option.PropertiesToExclude = propertyExclude;
// option.PropertiesToExclude = propertyExclude;
}, cancellationToken: stoppingToken);

_logger.LogDebug("Update prices database for: {Count} symbols", models.Count);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
using Binance.Net;
using Binance.Net.Clients;
using Binance.Net.Interfaces;
using Binance.Net.Interfaces.Clients;
using Binance.Net.Objects.Models.Spot.Socket;
using Binance.Net.Objects.Options;
using Crypto.Compare.Adapter;
using Crypto.Compare.Adapter.AdaptersObservable;
using Crypto.Compare.Adapter.Impl;
using Crypto.Compare.Data;
using CryptoExchange.Net.Objects.Errors;
using CryptoExchange.Net.Objects.Sockets;
using CryptoExchange.Net.SharedApis;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;


namespace Crypto.Compare.BingX;

public class BinanceOrderBookListener : IListenerAdapter
{
private const string LogPrefix = "Binance:";
private Provider _provider;
private bool _isStarted;
private readonly IAdaptersObservable _adaptersObservable;
private readonly IBinanceSocketClient _socketClient;
private readonly IBinanceRestClient _restClient;
private readonly ILogger<BinanceOrderBookListener> _logger;
private readonly SortedDictionary<string, SharedSpotSymbol> _symbols = new();
private readonly List<BinanceSocketClient> _sockets = new();
private readonly ILoggerFactory _loggerFactory;
private readonly IOptions<BinanceSocketOptions> _options;

public BinanceOrderBookListener(IBinanceSocketClient socketClient, IBinanceRestClient restClient, ILogger<BinanceOrderBookListener> logger,
IAdaptersObservable adaptersObservable, ILoggerFactory loggerFactory, IOptions<BinanceSocketOptions> options)
{
_socketClient = socketClient;
_restClient = restClient;
_logger = logger;
_adaptersObservable = adaptersObservable;
_loggerFactory = loggerFactory;
_options = options;
BinanceSocketClient.SetDefaultOptions(x =>
{
x.ApiCredentials = new BinanceCredentials(_provider.AccessKey, _provider.SecretKey);
});
}

public async Task StartAsync(Provider provider, CancellationToken cancellationToken)
{
_provider = provider;

// get all symbols
if (!await LoadSymbolsAsync(cancellationToken))
{
return;
}

// subscribe on events
if (!await SubscribeSymbolsAsync(cancellationToken))
{
return;
}

// started
_isStarted = true;
}

private async Task<bool> SubscribeSymbolsAsync(CancellationToken cancellationToken)
{
try
{
var socketConnection = GetConnection();
var keys = _symbols.Values.Select(x => x.Name).ToList();

foreach (var symbols in SplitIntoBatches(keys, 15))
{
(socketConnection, var result) = await TrySubscribeBatchesAsync(symbols, socketConnection, cancellationToken);
if (!result)
{
return _sockets.Count >= 1;
}
}


return true;
}
catch (Exception e)
{
_logger.LogError(e, LogPrefix + " Subscribe on symbols failed");
return false;
}
}

private static IEnumerable<List<string>> SplitIntoBatches(List<string> source, int batchSize)
{
for (var i = 0; i < source.Count; i += batchSize)
{
yield return source.GetRange(i, Math.Min(batchSize, source.Count - i));
}
}

private async Task<(BinanceSocketClient connection, bool subscribeSymbolsAsync)>
TrySubscribeBatchesAsync(List<string> symbols, BinanceSocketClient socketConnection, CancellationToken cancellationToken)
{
var result = await socketConnection.SpotApi.ExchangeData.SubscribeToOrderBookUpdatesAsync(symbols, 1, HandlerAction, cancellationToken).ConfigureAwait(false);

if (!result.Success)
{
if (result.Error.ErrorType == ErrorType.Unknown)
{
// try create new connection and subscribing
socketConnection = GetConnection();
result = await socketConnection.SpotApi.ExchangeData.SubscribeToOrderBookUpdatesAsync(symbols, 1, HandlerAction, cancellationToken);
if (result.Success)
{
return (socketConnection, true);
}

_logger.LogWarning(LogPrefix + " Not subscribe on ticker: {Symbols}", string.Join(',', symbols));
return (socketConnection, false);
}

_logger.LogWarning(LogPrefix + " Not subscribe on tickers: {Symbols}", string.Join(',', symbols));
return (socketConnection, false);
}

_logger.LogInformation(LogPrefix + " subscribed success, connection: {Number}, {Symbols}", _sockets.Count, string.Join(',', symbols));
return (socketConnection, true);
}

private void HandlerAction(DataEvent<IBinanceEventOrderBook> @event)
{
throw new NotImplementedException();
}

private BinanceSocketClient GetConnection()
{
var connection = new BinanceSocketClient(_options, _loggerFactory);
connection.SetApiCredentials(new BinanceCredentials(_provider.AccessKey, _provider.SecretKey));

_sockets.Add(connection);
return connection;
}


private async Task<bool> LoadSymbolsAsync(CancellationToken cancellationToken)
{
try
{

var exchangeInfo = await _restClient.SpotApi.SharedClient.GetSpotSymbolsAsync(
new GetSymbolsRequest(tradingMode:TradingMode.Spot), ct: cancellationToken);

if (!exchangeInfo.Success)
{
_logger.LogError(LogPrefix + " Get symbols failed");
return false;
}

foreach (var symbol in exchangeInfo.Data)
{

if (!symbol.Trading)
{
continue;
}

_symbols.Add(symbol.Name.ToLowerInvariant(), symbol);
}
}
catch (Exception e)
{
_logger.LogError(e, LogPrefix + " Load symbols failed");
return false;
}

return true;
}

/// <summary>
/// Handler event change book of currency
/// </summary>
/// <param name="dataEvent">Data of event</param>
private void HandlerAction(DataEvent<BinanceStreamBookPrice> dataEvent)
{
var askPrice = 0m;
var bidPrice = 0m;
var askQuantity = 0m;
var bidQuantity = 0m;
if (string.IsNullOrEmpty(dataEvent.Symbol))
{
return;
}

if (!_symbols.TryGetValue(dataEvent.Symbol.ToLowerInvariant(), out var symbol))
{
_logger.LogWarning("Symbol not found: {Symbol}", dataEvent.Symbol);
return;
}
var parts = symbol.Name.Split('-', 2);
var (baseSymbol, quoteSymbol) = (parts[0].ToLowerInvariant(), parts.Length > 1 ? parts[1].ToLowerInvariant() : string.Empty);
if (dataEvent.Data?.BestAskPrice > 0)
{
askPrice = dataEvent.Data.BestAskPrice;
askQuantity = dataEvent.Data.BestAskQuantity;
}

if (dataEvent.Data?.BestBidPrice > 0)
{
bidPrice = dataEvent.Data.BestBidPrice;
bidQuantity = dataEvent.Data.BestBidQuantity;
}

_adaptersObservable.OnNext(new SymbolData(ticker: dataEvent.Symbol,
providerId: _provider.Id,
askPrice: askPrice,
bidPrice: bidPrice,
askQuantity: askQuantity,
bidQuantity: bidQuantity,
baseSymbol: baseSymbol,
quoteSymbol: quoteSymbol));
}

public Task StopAsync(CancellationToken cancellationToken)
{
_socketClient?.Dispose();
foreach (var socketClient in _sockets)
{
socketClient?.Dispose();
}

return Task.CompletedTask;
}

public bool IsSupported(string providerName)
=> string.Equals(providerName, Consts.ProviderName, StringComparison.OrdinalIgnoreCase);

public bool IsStarted()
=> _isStarted;
}
6 changes: 6 additions & 0 deletions sources/integrations/Crypto.Comapre.Binance/Consts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Crypto.Compare.BingX;

public class Consts
{
public const string ProviderName = "Binance";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Binance.Net" Version="12.11.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\core\Crypto.Compare.Adapter\Crypto.Compare.Adapter.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BingX.Net.Clients;
using BingX.Net;
using BingX.Net.Clients;
using BingX.Net.Enums;
using BingX.Net.Interfaces.Clients;
using BingX.Net.Objects.Models;
Expand Down Expand Up @@ -26,8 +27,8 @@ public class BingXOrderBookListener : IListenerAdapter
private readonly IBingXSocketClient _socketClient;
private readonly IBingXRestClient _restClient;
private readonly ILogger<BingXOrderBookListener> _logger;
private readonly SortedDictionary<string, BingXSymbol> _symbols = new ();
private readonly List<IBingXSocketClient> _sockets = new ();
private readonly SortedDictionary<string, BingXSymbol> _symbols = new();
private readonly List<IBingXSocketClient> _sockets = new();
private readonly ILoggerFactory _loggerFactory;
private readonly IOptions<BingXSocketOptions> _options;

Expand Down Expand Up @@ -120,10 +121,8 @@ private async Task<bool> SubscribeSymbolsAsync(CancellationToken cancellationTok
private BingXSocketClient GetConnection()
{
var connection = new BingXSocketClient(_options, _loggerFactory);
connection.SetOptions(new UpdateOptions
{
ApiCredentials = new ApiCredentials(_provider.AccessKey, _provider.SecretKey)
});
connection.SetApiCredentials(new BingXCredentials(_provider.AccessKey, _provider.SecretKey));

_sockets.Add(connection);
return connection;
}
Expand Down
Loading
Loading