Skip to content

Commit

Permalink
Updated CryptoExchange.Net version
Browse files Browse the repository at this point in the history
  • Loading branch information
JKorf committed Jun 11, 2024
1 parent fe8679d commit 1400eb5
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 27 deletions.
4 changes: 2 additions & 2 deletions BingX.Net/BingX.Net.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<LangVersion>10.0</LangVersion>
Expand Down Expand Up @@ -48,7 +48,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="CryptoExchange.Net" Version="7.5.2" />
<PackageReference Include="CryptoExchange.Net" Version="7.6.0" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
14 changes: 12 additions & 2 deletions BingX.Net/BingXAuthenticationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,24 @@ public BingXAuthenticationProvider(ApiCredentials credentials) : base(credential
{
}

public override void AuthenticateRequest(RestApiClient apiClient, Uri uri, HttpMethod method, IDictionary<string, object> uriParams, IDictionary<string, object> bodyParams, Dictionary<string, string> headers, bool auth, ArrayParametersSerialization arraySerialization, HttpMethodParameterPosition parameterPosition, RequestBodyFormat bodyFormat)
public override void AuthenticateRequest(
RestApiClient apiClient,
Uri uri,
HttpMethod method,
IDictionary<string, object> uriParameters,
IDictionary<string, object> bodyParameters,
Dictionary<string, string> headers,
bool auth,
ArrayParametersSerialization arraySerialization,
HttpMethodParameterPosition parameterPosition,
RequestBodyFormat requestBodyFormat)
{
headers.Add("X-BX-APIKEY", GetApiKey());

if (!auth)
return;

var parameters = parameterPosition == HttpMethodParameterPosition.InUri ? uriParams : bodyParams;
var parameters = parameterPosition == HttpMethodParameterPosition.InUri ? uriParameters : bodyParameters;
var timestamp = DateTimeConverter.ConvertToMilliseconds(GetTimestamp(apiClient)).Value;
parameters.Add("timestamp", timestamp);
if (!parameters.ContainsKey("recvWindow"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
using CryptoExchange.Net.Converters.SystemTextJson;
using System.Net.WebSockets;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices.ComTypes;
using System.Linq;

namespace BingX.Net.Clients.PerpetualFuturesApi
{
Expand Down Expand Up @@ -59,7 +62,8 @@ protected override AuthenticationProvider CreateAuthenticationProvider(ApiCreden
/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToTradeUpdatesAsync(string symbol, Action<DataEvent<IEnumerable<BingXFuturesTradeUpdate>>> onMessage, CancellationToken ct = default)
{
var subscription = new BingXSubscription<IEnumerable<BingXFuturesTradeUpdate>>(_logger, symbol + "@trade", symbol + "@trade", onMessage, false);
var stream = symbol + "@trade";
var subscription = new BingXSubscription<IEnumerable<BingXFuturesTradeUpdate>>(_logger, stream, stream, x => onMessage(x.WithStreamId(stream).WithSymbol(x.Data.First().Symbol)), false);
return await SubscribeAsync(BaseAddress.AppendPath("swap-market"), subscription, ct).ConfigureAwait(false);
}

Expand All @@ -70,7 +74,7 @@ public async Task<CallResult<UpdateSubscription>> SubscribeToPartialOrderBookUpd
updateInterval.ValidateIntValues(nameof(updateInterval),100, 200, 500, 1000);

var stream = symbol + $"@depth{depth}@{updateInterval}ms";
var subscription = new BingXSubscription<BingXOrderBook>(_logger, stream, stream, onMessage, false);
var subscription = new BingXSubscription<BingXOrderBook>(_logger, stream, stream, x => onMessage(x.WithStreamId(stream).WithSymbol(symbol)), false);
return await SubscribeAsync(BaseAddress.AppendPath("swap-market"), subscription, ct).ConfigureAwait(false);
}

Expand All @@ -81,61 +85,63 @@ public async Task<CallResult<UpdateSubscription>> SubscribeToPartialOrderBookUpd
updateInterval.ValidateIntValues(nameof(updateInterval), 100, 200, 500, 1000);

var stream = $"all@depth{depth}@{updateInterval}ms";
var subscription = new BingXSubscription<BingXOrderBook>(_logger, stream, stream, onMessage, false);
var subscription = new BingXSubscription<BingXOrderBook>(_logger, stream, stream, x => onMessage(x.WithStreamId(stream).WithSymbol(x.Data.Symbol!)), false);
return await SubscribeAsync(BaseAddress.AppendPath("swap-market"), subscription, ct).ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToKlineUpdatesAsync(KlineInterval interval, Action<DataEvent<IEnumerable<BingXFuturesKlineUpdate>>> onMessage, CancellationToken ct = default)
{
var stream = "all@kline_" + EnumConverter.GetString(interval);
var subscription = new BingXSubscription<IEnumerable<BingXFuturesKlineUpdate>>(_logger, stream, stream, onMessage, false);
var subscription = new BingXSubscription<IEnumerable<BingXFuturesKlineUpdate>>(_logger, stream, stream, x => onMessage(x.WithStreamId(stream)), false);
return await SubscribeAsync(BaseAddress.AppendPath("swap-market"), subscription, ct).ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToKlineUpdatesAsync(string symbol, KlineInterval interval, Action<DataEvent<IEnumerable<BingXFuturesKlineUpdate>>> onMessage, CancellationToken ct = default)
{
var stream = symbol + "@kline_" + EnumConverter.GetString(interval);
var subscription = new BingXSubscription<IEnumerable<BingXFuturesKlineUpdate>>(_logger, stream, stream, onMessage, false);
var subscription = new BingXSubscription<IEnumerable<BingXFuturesKlineUpdate>>(_logger, stream, stream, x => onMessage(x.WithStreamId(stream).WithSymbol(symbol)), false);
return await SubscribeAsync(BaseAddress.AppendPath("swap-market"), subscription, ct).ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToTickerUpdatesAsync(string symbol, Action<DataEvent<BingXFuturesTickerUpdate>> onMessage, CancellationToken ct = default)
{
var subscription = new BingXSubscription<BingXFuturesTickerUpdate>(_logger, symbol + "@ticker", symbol + "@ticker", onMessage, false);
var stream = symbol + "@ticker";
var subscription = new BingXSubscription<BingXFuturesTickerUpdate>(_logger, stream, stream, x => onMessage(x.WithStreamId(stream).WithSymbol(x.Data.Symbol)), false);
return await SubscribeAsync(BaseAddress.AppendPath("swap-market"), subscription, ct).ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToTickerUpdatesAsync(Action<DataEvent<BingXFuturesTickerUpdate>> onMessage, CancellationToken ct = default)
{
var subscription = new BingXSubscription<BingXFuturesTickerUpdate>(_logger, "all@ticker", "all@ticker", onMessage, false);
var stream = "all@ticker";
var subscription = new BingXSubscription<BingXFuturesTickerUpdate>(_logger, stream, stream, x => onMessage(x.WithStreamId(stream).WithSymbol(x.Data.Symbol)), false);
return await SubscribeAsync(BaseAddress.AppendPath("swap-market"), subscription, ct).ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToPriceUpdatesAsync(string symbol, Action<DataEvent<BingXPriceUpdate>> onMessage, CancellationToken ct = default)
{
var stream = symbol + "@lastPrice";
var subscription = new BingXSubscription<BingXPriceUpdate>(_logger, stream, stream, onMessage, false);
var subscription = new BingXSubscription<BingXPriceUpdate>(_logger, stream, stream, x => onMessage(x.WithStreamId(stream).WithSymbol(x.Data.Symbol)), false);
return await SubscribeAsync(BaseAddress.AppendPath("swap-market"), subscription, ct).ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToMarkPriceUpdatesAsync(string symbol, Action<DataEvent<BingXMarkPriceUpdate>> onMessage, CancellationToken ct = default)
{
var stream = symbol + "@markPrice";
var subscription = new BingXSubscription<BingXMarkPriceUpdate>(_logger, stream, stream, onMessage, false);
var subscription = new BingXSubscription<BingXMarkPriceUpdate>(_logger, stream, stream, x => onMessage(x.WithStreamId(stream).WithSymbol(x.Data.Symbol)), false);
return await SubscribeAsync(BaseAddress.AppendPath("swap-market"), subscription, ct).ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToBookPriceUpdatesAsync(string symbol, Action<DataEvent<BingXBookTickerUpdate>> onMessage, CancellationToken ct = default)
{
var stream = symbol + "@bookTicker";
var subscription = new BingXSubscription<BingXBookTickerUpdate>(_logger, stream, stream, onMessage, false);
var subscription = new BingXSubscription<BingXBookTickerUpdate>(_logger, stream, stream, x => onMessage(x.WithStreamId(stream).WithSymbol(x.Data.Symbol)), false);
return await SubscribeAsync(BaseAddress.AppendPath("swap-market"), subscription, ct).ConfigureAwait(false);
}

Expand Down
17 changes: 10 additions & 7 deletions BingX.Net/Clients/SpotApi/BingXSocketClientSpotApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using CryptoExchange.Net.Clients;
using CryptoExchange.Net.Converters.SystemTextJson;
using BingX.Net.Enums;
using System.Runtime.InteropServices.ComTypes;

namespace BingX.Net.Clients.SpotApi
{
Expand Down Expand Up @@ -62,15 +63,16 @@ protected override AuthenticationProvider CreateAuthenticationProvider(ApiCreden
/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToTradeUpdatesAsync(string symbol, Action<DataEvent<BingXTradeUpdate>> onMessage, CancellationToken ct = default)
{
var subscription = new BingXSubscription<BingXTradeUpdate>(_logger, symbol + "@trade", symbol + "@trade", onMessage, false);
var stream = symbol + "@trade";
var subscription = new BingXSubscription<BingXTradeUpdate>(_logger, stream, stream, x => onMessage(x.WithStreamId(stream).WithSymbol(x.Data.Symbol)), false);
return await SubscribeAsync(BaseAddress.AppendPath("market"), subscription, ct).ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToKlineUpdatesAsync(string symbol, KlineInterval interval, Action<DataEvent<BingXKlineUpdate>> onMessage, CancellationToken ct = default)
{
var stream = symbol + "@kline_" + KlineIntervalToWebsocketString(interval);
var subscription = new BingXSubscription<BingXKlineUpdate>(_logger, stream, stream, onMessage, false);
var subscription = new BingXSubscription<BingXKlineUpdate>(_logger, stream, stream, x => onMessage(x.WithStreamId(stream).WithSymbol(x.Data.Symbol)), false);
return await SubscribeAsync(BaseAddress.AppendPath("market"), subscription, ct).ConfigureAwait(false);
}

Expand All @@ -80,38 +82,39 @@ public async Task<CallResult<UpdateSubscription>> SubscribeToPartialOrderBookUpd
depth.ValidateIntValues(nameof(depth), 5, 10, 20, 50, 100);

var stream = symbol + "@depth" + depth;
var subscription = new BingXSubscription<BingXOrderBook>(_logger, stream, stream, onMessage, false);
var subscription = new BingXSubscription<BingXOrderBook>(_logger, stream, stream, x => onMessage(x.WithStreamId(stream).WithSymbol(symbol)), false);
return await SubscribeAsync(BaseAddress.AppendPath("market"), subscription, ct).ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToTickerUpdatesAsync(string symbol, Action<DataEvent<BingXTickerUpdate>> onMessage, CancellationToken ct = default)
{
var subscription = new BingXSubscription<BingXTickerUpdate>(_logger, symbol + "@ticker", "24hTicker" + symbol, onMessage, false);
var stream = symbol + "@ticker";
var subscription = new BingXSubscription<BingXTickerUpdate>(_logger, stream, "24hTicker" + symbol, x => onMessage(x.WithStreamId(stream).WithSymbol(x.Data.Symbol)), false);
return await SubscribeAsync(BaseAddress.AppendPath("market"), subscription, ct).ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToPriceUpdatesAsync(string symbol, Action<DataEvent<BingXPriceUpdate>> onMessage, CancellationToken ct = default)
{
var stream = symbol + "@lastPrice";
var subscription = new BingXSubscription<BingXPriceUpdate>(_logger, stream, stream, onMessage, false);
var subscription = new BingXSubscription<BingXPriceUpdate>(_logger, stream, stream, x => onMessage(x.WithStreamId(stream).WithSymbol(x.Data.Symbol)), false);
return await SubscribeAsync(BaseAddress.AppendPath("market"), subscription, ct).ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToBookPriceUpdatesAsync(string symbol, Action<DataEvent<BingXBookTickerUpdate>> onMessage, CancellationToken ct = default)
{
var stream = symbol + "@bookTicker";
var subscription = new BingXSubscription<BingXBookTickerUpdate>(_logger, stream, stream, onMessage, false);
var subscription = new BingXSubscription<BingXBookTickerUpdate>(_logger, stream, stream, x => onMessage(x.WithStreamId(stream).WithSymbol(x.Data.Symbol)), false);
return await SubscribeAsync(BaseAddress.AppendPath("market"), subscription, ct).ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToOrderUpdatesAsync(string listenKey, Action<DataEvent<BingXOrderUpdate>> onMessage, CancellationToken ct = default)
{
var stream = "spot.executionReport";
var subscription = new BingXSubscription<BingXOrderUpdate>(_logger, stream, stream, onMessage, false);
var subscription = new BingXSubscription<BingXOrderUpdate>(_logger, stream, stream, x => onMessage(x.WithStreamId(stream).WithSymbol(x.Data.Symbol)), false);
return await SubscribeAsync(BaseAddress.AppendPath("market") + "?listenKey=" + listenKey, subscription, ct).ConfigureAwait(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public BingXBalanceSubscription(ILogger logger, Action<DataEvent<BingXBalanceUpd
public override CallResult DoHandleMessage(SocketConnection connection, DataEvent<object> message)
{
var update = (BingXBalanceUpdate)message.Data;
_handler.Invoke(message.As(update!, update.Event, SocketUpdateType.Update));
_handler.Invoke(message.As(update!, update.Event, null, SocketUpdateType.Update));
return new CallResult(null);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public override CallResult DoHandleMessage(SocketConnection connection, DataEven
klineUpdate.Symbol = update.Symbol!;
}

_handler.Invoke(message.As(update.Data!, update.DataType, SocketUpdateType.Update));
_handler.Invoke(message.As(update.Data!, update.DataType, update.Symbol, SocketUpdateType.Update));
return new CallResult(null);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,19 @@ public override CallResult DoHandleMessage(SocketConnection connection, DataEven
{
if (message.Data is BingXConfigUpdate configUpdate)
{
_configHandler?.Invoke(message.As(configUpdate, configUpdate.Configuration.Symbol, SocketUpdateType.Update));
_configHandler?.Invoke(message.As(configUpdate, configUpdate.Event, configUpdate.Configuration.Symbol, SocketUpdateType.Update));
}
else if (message.Data is BingXFuturesAccountUpdate accountUpdate)
{
_accountHandler?.Invoke(message.As(accountUpdate, accountUpdate.Update.Trigger, SocketUpdateType.Update));
_accountHandler?.Invoke(message.As(accountUpdate, accountUpdate.Event, null, SocketUpdateType.Update));
}
else if (message.Data is BingXFuturesOrderUpdateWrapper orderUpdate)
{
_orderHandler?.Invoke(message.As(orderUpdate.Data, orderUpdate.Data.Symbol, SocketUpdateType.Update));
_orderHandler?.Invoke(message.As(orderUpdate.Data, orderUpdate.Event, orderUpdate.Data.Symbol, SocketUpdateType.Update));
}
else if (message.Data is BingXListenKeyExpiredUpdate listenKeyUpdate)
{
_listenkeyHandler?.Invoke(message.As(listenKeyUpdate!, listenKeyUpdate!.ListenKey, SocketUpdateType.Update));
_listenkeyHandler?.Invoke(message.As(listenKeyUpdate!, listenKeyUpdate.Event, null, SocketUpdateType.Update));
}

return new CallResult(null);
Expand Down

0 comments on commit 1400eb5

Please sign in to comment.