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 8fac2c5 commit fbfa773
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 40 deletions.
16 changes: 8 additions & 8 deletions Huobi.Net/Clients/SpotApi/HuobiSocketClientSpotApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public async Task<CallResult<UpdateSubscription>> SubscribeToKlineUpdatesAsync(s
{
symbol = symbol.ValidateHuobiSymbol();

var subscription = new HuobiSubscription<HuobiKline>(_logger, $"market.{symbol}.kline.{JsonConvert.SerializeObject(period, new PeriodConverter(false))}", onData, false);
var subscription = new HuobiSubscription<HuobiKline>(_logger, $"market.{symbol}.kline.{JsonConvert.SerializeObject(period, new PeriodConverter(false))}", x => onData(x.WithSymbol(symbol)), false);
return await SubscribeAsync(BaseAddress.AppendPath("ws"), subscription, ct).ConfigureAwait(false);
}

Expand Down Expand Up @@ -147,7 +147,7 @@ public async Task<CallResult<UpdateSubscription>> SubscribeToPartialOrderBookUpd
symbol = symbol.ValidateHuobiSymbol();
mergeStep.ValidateIntBetween(nameof(mergeStep), 0, 5);

var subscription = new HuobiSubscription<HuobiOrderBook>(_logger, $"market.{symbol}.depth.step{mergeStep}", onData, false);
var subscription = new HuobiSubscription<HuobiOrderBook>(_logger, $"market.{symbol}.depth.step{mergeStep}", x => onData(x.WithSymbol(symbol)), false);
return await SubscribeAsync(BaseAddress.AppendPath("ws"), subscription, ct).ConfigureAwait(false);
}

Expand All @@ -157,7 +157,7 @@ public async Task<CallResult<UpdateSubscription>> SubscribeToPartialOrderBookUpd
symbol = symbol.ValidateHuobiSymbol();
levels.ValidateIntValues(nameof(levels), 5, 10, 20);

var subscription = new HuobiSubscription<HuobiOrderBook>(_logger, $"market.{symbol}.mbp.refresh.{levels}", onData, false);
var subscription = new HuobiSubscription<HuobiOrderBook>(_logger, $"market.{symbol}.mbp.refresh.{levels}", x => onData(x.WithSymbol(symbol)), false);
return await SubscribeAsync(BaseAddress.AppendPath("ws"), subscription, ct).ConfigureAwait(false);
}

Expand All @@ -167,7 +167,7 @@ public async Task<CallResult<UpdateSubscription>> SubscribeToOrderBookChangeUpda
symbol = symbol.ValidateHuobiSymbol();
levels.ValidateIntValues(nameof(levels), 5, 20, 150, 400);

var subscription = new HuobiSubscription<HuobiIncementalOrderBook>(_logger, $"market.{symbol}.mbp.{levels}", onData, false);
var subscription = new HuobiSubscription<HuobiIncementalOrderBook>(_logger, $"market.{symbol}.mbp.{levels}", x => onData(x.WithSymbol(symbol)), false);
return await SubscribeAsync(BaseAddress.AppendPath("feed"), subscription, ct).ConfigureAwait(false);
}

Expand All @@ -185,7 +185,7 @@ public async Task<CallResult<IEnumerable<HuobiSymbolTradeDetails>>> GetTradeHist
public async Task<CallResult<UpdateSubscription>> SubscribeToTradeUpdatesAsync(string symbol, Action<DataEvent<HuobiSymbolTrade>> onData, CancellationToken ct = default)
{
symbol = symbol.ValidateHuobiSymbol();
var subscription = new HuobiSubscription<HuobiSymbolTrade>(_logger, $"market.{symbol}.trade.detail", onData, false);
var subscription = new HuobiSubscription<HuobiSymbolTrade>(_logger, $"market.{symbol}.trade.detail", x => onData(x.WithSymbol(symbol)), false);
return await SubscribeAsync(BaseAddress.AppendPath("ws"), subscription, ct).ConfigureAwait(false);
}

Expand All @@ -207,7 +207,7 @@ public async Task<CallResult<HuobiSymbolDetails>> GetSymbolDetailsAsync(string s
public async Task<CallResult<UpdateSubscription>> SubscribeToSymbolDetailUpdatesAsync(string symbol, Action<DataEvent<HuobiSymbolDetails>> onData, CancellationToken ct = default)
{
symbol = symbol.ValidateHuobiSymbol();
var subscription = new HuobiSubscription<HuobiSymbolDetails>(_logger, $"market.{symbol}.detail", onData, false);
var subscription = new HuobiSubscription<HuobiSymbolDetails>(_logger, $"market.{symbol}.detail", x => onData(x.WithSymbol(symbol)), false);
return await SubscribeAsync(BaseAddress.AppendPath("ws"), subscription, ct).ConfigureAwait(false);
}

Expand All @@ -221,14 +221,14 @@ public async Task<CallResult<UpdateSubscription>> SubscribeToTickerUpdatesAsync(
/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToTickerUpdatesAsync(string symbol, Action<DataEvent<HuobiSymbolTick>> onData, CancellationToken ct = default)
{
var subscription = new HuobiSubscription<HuobiSymbolTick>(_logger, $"market.{symbol}.ticker", onData, false);
var subscription = new HuobiSubscription<HuobiSymbolTick>(_logger, $"market.{symbol}.ticker", x => onData(x.WithSymbol(symbol)), false);
return await SubscribeAsync(BaseAddress.AppendPath("ws"), subscription, ct).ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToBestOfferUpdatesAsync(string symbol, Action<DataEvent<HuobiBestOffer>> onData, CancellationToken ct = default)
{
var subscription = new HuobiSubscription<HuobiBestOffer>(_logger, $"market.{symbol}.bbo", onData, false);
var subscription = new HuobiSubscription<HuobiBestOffer>(_logger, $"market.{symbol}.bbo", x => onData(x.WithSymbol(symbol)), false);
return await SubscribeAsync(BaseAddress.AppendPath("ws"), subscription, ct).ConfigureAwait(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,77 +78,77 @@ protected override AuthenticationProvider CreateAuthenticationProvider(ApiCreden
/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToKlineUpdatesAsync(string contractCode, KlineInterval period, Action<DataEvent<HuobiKline>> onData, CancellationToken ct = default)
{
var subscription = new HuobiSubscription<HuobiKline>(_logger, $"market.{contractCode.ToUpperInvariant()}.kline.{JsonConvert.SerializeObject(period, new PeriodConverter(false))}", onData, false);
var subscription = new HuobiSubscription<HuobiKline>(_logger, $"market.{contractCode.ToUpperInvariant()}.kline.{JsonConvert.SerializeObject(period, new PeriodConverter(false))}", x => onData(x.WithSymbol(contractCode)), false);
return await SubscribeAsync(BaseAddress.AppendPath("linear-swap-ws"), subscription, ct).ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToOrderBookUpdatesAsync(string contractCode, int mergeStep, Action<DataEvent<HuobiUsdtMarginSwapIncementalOrderBook>> onData, CancellationToken ct = default)
{
var subscription = new HuobiSubscription<HuobiUsdtMarginSwapIncementalOrderBook>(_logger, $"market.{contractCode.ToUpperInvariant()}.depth.step" + mergeStep, onData, false);
var subscription = new HuobiSubscription<HuobiUsdtMarginSwapIncementalOrderBook>(_logger, $"market.{contractCode.ToUpperInvariant()}.depth.step" + mergeStep, x => onData(x.WithSymbol(contractCode)), false);
return await SubscribeAsync(BaseAddress.AppendPath("linear-swap-ws"), subscription, ct).ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToIncrementalOrderBookUpdatesAsync(string contractCode, bool snapshot, int limit, Action<DataEvent<HuobiUsdtMarginSwapIncementalOrderBook>> onData, CancellationToken ct = default)
{
var subscription = new HuobiSubscription<HuobiUsdtMarginSwapIncementalOrderBook>(_logger, $"market.{contractCode.ToUpperInvariant()}.depth.size_{limit}.high_freq", onData, false);
var subscription = new HuobiSubscription<HuobiUsdtMarginSwapIncementalOrderBook>(_logger, $"market.{contractCode.ToUpperInvariant()}.depth.size_{limit}.high_freq", x => onData(x.WithSymbol(contractCode)), false);
return await SubscribeAsync(BaseAddress.AppendPath("linear-swap-ws"), subscription, ct).ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToSymbolTickerUpdatesAsync(string contractCode, Action<DataEvent<HuobiSymbolTickUpdate>> onData, CancellationToken ct = default)
{
var subscription = new HuobiSubscription<HuobiSymbolTickUpdate>(_logger, $"market.{contractCode.ToUpperInvariant()}.detail", onData, false);
var subscription = new HuobiSubscription<HuobiSymbolTickUpdate>(_logger, $"market.{contractCode.ToUpperInvariant()}.detail", x => onData(x.WithSymbol(contractCode)), false);
return await SubscribeAsync(BaseAddress.AppendPath("linear-swap-ws"), subscription, ct).ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToBestOfferUpdatesAsync(string contractCode, Action<DataEvent<HuobiBestOfferUpdate>> onData, CancellationToken ct = default)
{
var subscription = new HuobiSubscription<HuobiBestOfferUpdate>(_logger, $"market.{contractCode.ToUpperInvariant()}.bbo", onData, false);
var subscription = new HuobiSubscription<HuobiBestOfferUpdate>(_logger, $"market.{contractCode.ToUpperInvariant()}.bbo", x => onData(x.WithSymbol(contractCode)), false);
return await SubscribeAsync(BaseAddress.AppendPath("linear-swap-ws"), subscription, ct).ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToTradeUpdatesAsync(string contractCode, Action<DataEvent<HuobiUsdtMarginSwapTradesUpdate>> onData, CancellationToken ct = default)
{
var subscription = new HuobiSubscription<HuobiUsdtMarginSwapTradesUpdate>(_logger, $"market.{contractCode.ToUpperInvariant()}.trade.detail", onData, false);
var subscription = new HuobiSubscription<HuobiUsdtMarginSwapTradesUpdate>(_logger, $"market.{contractCode.ToUpperInvariant()}.trade.detail", x => onData(x.WithSymbol(contractCode)), false);
return await SubscribeAsync(BaseAddress.AppendPath("linear-swap-ws"), subscription, ct).ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToIndexKlineUpdatesAsync(string contractCode, KlineInterval period, Action<DataEvent<HuobiKline>> onData, CancellationToken ct = default)
{
var subscription = new HuobiSubscription<HuobiKline>(_logger, $"market.{contractCode.ToUpperInvariant()}.index.{JsonConvert.SerializeObject(period, new PeriodConverter(false))}", onData, false);
var subscription = new HuobiSubscription<HuobiKline>(_logger, $"market.{contractCode.ToUpperInvariant()}.index.{JsonConvert.SerializeObject(period, new PeriodConverter(false))}", x => onData(x.WithSymbol(contractCode)), false);
return await SubscribeAsync(BaseAddress.AppendPath("ws_index"), subscription, ct).ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToPremiumIndexKlineUpdatesAsync(string contractCode, KlineInterval period, Action<DataEvent<HuobiKline>> onData, CancellationToken ct = default)
{
var subscription = new HuobiSubscription<HuobiKline>(_logger, $"market.{contractCode.ToUpperInvariant()}.premium_index.{JsonConvert.SerializeObject(period, new PeriodConverter(false))}", onData, false);
var subscription = new HuobiSubscription<HuobiKline>(_logger, $"market.{contractCode.ToUpperInvariant()}.premium_index.{JsonConvert.SerializeObject(period, new PeriodConverter(false))}", x => onData(x.WithSymbol(contractCode)), false);
return await SubscribeAsync(BaseAddress.AppendPath("ws_index"), subscription, ct).ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToEstimatedFundingRateKlineUpdatesAsync(string contractCode, KlineInterval period, Action<DataEvent<HuobiKline>> onData, CancellationToken ct = default)
{
var subscription = new HuobiSubscription<HuobiKline>(_logger, $"market.{contractCode.ToUpperInvariant()}.estimated_rate.{JsonConvert.SerializeObject(period, new PeriodConverter(false))}", onData, false);
var subscription = new HuobiSubscription<HuobiKline>(_logger, $"market.{contractCode.ToUpperInvariant()}.estimated_rate.{JsonConvert.SerializeObject(period, new PeriodConverter(false))}", x => onData(x.WithSymbol(contractCode)), false);
return await SubscribeAsync(BaseAddress.AppendPath("ws_index"), subscription, ct).ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToBasisUpdatesAsync(string contractCode, KlineInterval period, string priceType, Action<DataEvent<HuobiUsdtMarginSwapBasisUpdate>> onData, CancellationToken ct = default)
{
var subscription = new HuobiSubscription<HuobiUsdtMarginSwapBasisUpdate>(_logger, $"market.{contractCode.ToUpperInvariant()}.basis.{JsonConvert.SerializeObject(period, new PeriodConverter(false))}.{priceType}", onData, false);
var subscription = new HuobiSubscription<HuobiUsdtMarginSwapBasisUpdate>(_logger, $"market.{contractCode.ToUpperInvariant()}.basis.{JsonConvert.SerializeObject(period, new PeriodConverter(false))}.{priceType}", x => onData(x.WithSymbol(contractCode)), false);
return await SubscribeAsync(BaseAddress.AppendPath("ws_index"), subscription, ct).ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToMarkPriceKlineUpdatesAsync(string contractCode, KlineInterval period, Action<DataEvent<HuobiKline>> onData, CancellationToken ct = default)
{
var subscription = new HuobiSubscription<HuobiKline>(_logger, $"market.{contractCode.ToUpperInvariant()}.mark_price.{JsonConvert.SerializeObject(period, new PeriodConverter(false))}", onData, false);
var subscription = new HuobiSubscription<HuobiKline>(_logger, $"market.{contractCode.ToUpperInvariant()}.mark_price.{JsonConvert.SerializeObject(period, new PeriodConverter(false))}", x => onData(x.WithSymbol(contractCode)), false);
return await SubscribeAsync(BaseAddress.AppendPath("ws_index"), subscription, ct).ConfigureAwait(false);
}

Expand Down
4 changes: 2 additions & 2 deletions Huobi.Net/Huobi.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>
<Nullable>enable</Nullable>
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
21 changes: 11 additions & 10 deletions Huobi.Net/HuobiAuthenticationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,37 @@ public HuobiAuthenticationProvider(ApiCredentials credentials, bool signPublicRe
_signPublicRequests = signPublicRequests;
}

public override void AuthenticateRequest(RestApiClient apiClient,
public override void AuthenticateRequest(
RestApiClient apiClient,
Uri uri,
HttpMethod method,
IDictionary<string, object> uriParams,
IDictionary<string, object> bodyParams,
IDictionary<string, object> uriParameters,
IDictionary<string, object> bodyParameters,
Dictionary<string, string> headers,
bool auth,
ArrayParametersSerialization arraySerialization,
HttpMethodParameterPosition parameterPosition,
RequestBodyFormat bodyFormat)
RequestBodyFormat requestBodyFormat)
{
if (!auth && !_signPublicRequests)
return;

// These are always in the uri
uriParams.Add("AccessKeyId", _credentials.Key!.GetString());
uriParams.Add("SignatureMethod", "HmacSHA256");
uriParams.Add("SignatureVersion", 2);
uriParams.Add("Timestamp", GetTimestamp(apiClient).ToString("yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture));
uriParameters.Add("AccessKeyId", _credentials.Key!.GetString());
uriParameters.Add("SignatureMethod", "HmacSHA256");
uriParameters.Add("SignatureVersion", 2);
uriParameters.Add("Timestamp", GetTimestamp(apiClient).ToString("yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture));

var absolutePath = uri.AbsolutePath;
if (absolutePath.StartsWith("/api"))
// Russian api has /api prefix which shouldn't be part of the signature
absolutePath = absolutePath.Substring(4);

var sortedParameters = uriParams.OrderBy(kv => Encoding.UTF8.GetBytes(WebUtility.UrlEncode(kv.Key)!), new ByteOrderComparer());
var sortedParameters = uriParameters.OrderBy(kv => Encoding.UTF8.GetBytes(WebUtility.UrlEncode(kv.Key)!), new ByteOrderComparer());
var paramString = uri.SetParameters(sortedParameters, arraySerialization).Query.Replace("?", "");
paramString = new Regex(@"%[a-f0-9]{2}").Replace(paramString, m => m.Value.ToUpperInvariant());
var signData = $"{method}\n{uri.Host}\n{absolutePath}\n{paramString}";
uriParams.Add("Signature", SignHMACSHA256(signData, SignOutputType.Base64));
uriParameters.Add("Signature", SignHMACSHA256(signData, SignOutputType.Base64));
}

public HuobiAuthParams GetWebsocketAuthentication(Uri uri)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public HuobiAccountSubscription(ILogger logger, string topic, Action<DataEvent<H
public override CallResult DoHandleMessage(SocketConnection connection, DataEvent<object> message)
{
var update = (HuobiDataEvent<HuobiAccountUpdate>)message.Data;
_handler.Invoke(message.As(update.Data, update.Channel));
_handler.Invoke(message.As(update.Data, update.Channel, null, SocketUpdateType.Update));
return new CallResult(null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public override CallResult DoHandleMessage(SocketConnection connection, DataEven
{
var data = message.Data;
if (data is HuobiDataEvent<HuobiTradeUpdate> tradeEvent)
_onOrderMatch?.Invoke(message.As(tradeEvent.Data, tradeEvent.Channel));
_onOrderMatch?.Invoke(message.As(tradeEvent.Data, tradeEvent.Channel, tradeEvent.Data.Symbol, SocketUpdateType.Update));
if (data is HuobiDataEvent<HuobiOrderCancelationUpdate> cancelEvent)
_onOrderCancel?.Invoke(message.As(cancelEvent.Data, cancelEvent.Channel));
_onOrderCancel?.Invoke(message.As(cancelEvent.Data, cancelEvent.Channel, cancelEvent.Data.Symbol, SocketUpdateType.Update));
return new CallResult(null);
}

Expand Down
Loading

0 comments on commit fbfa773

Please sign in to comment.