Skip to content

Commit

Permalink
Feature/cryptoclients update (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
JKorf authored Apr 28, 2024
1 parent 636e33a commit 4a5b305
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 11 deletions.
8 changes: 7 additions & 1 deletion Huobi.Net/Clients/SpotApi/HuobiRestClientSpotApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ internal HuobiRestClientSpotApi(ILogger logger, HttpClient? httpClient, HuobiRes
}
#endregion

/// <inheritdoc />
public override string FormatSymbol(string baseAsset, string quoteAsset) => $"{baseAsset.ToLowerInvariant()}{quoteAsset.ToLowerInvariant()}";

/// <inheritdoc />
protected override AuthenticationProvider CreateAuthenticationProvider(ApiCredentials credentials)
=> new HuobiAuthenticationProvider(credentials, ClientOptions.SignPublicRequests);
Expand Down Expand Up @@ -196,9 +199,12 @@ async Task<WebCallResult<Ticker>> IBaseRestClient.GetTickerAsync(string symbol,
return tickers.As<Ticker>(null);

var ticker = tickers.Data.Ticks.SingleOrDefault(s => s.Symbol == symbol);
if (ticker == null)
return tickers.AsError<Ticker>(new ServerError("Symbol not found"));

return tickers.As(new Ticker
{
SourceObject =ticker,
SourceObject = ticker,
HighPrice = ticker.HighPrice,
Symbol = ticker.Symbol,
LastPrice = ticker.ClosePrice,
Expand Down
8 changes: 4 additions & 4 deletions Huobi.Net/Clients/SpotApi/HuobiSocketClientSpotApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ internal HuobiSocketClientSpotApi(ILogger logger, HuobiSocketOptions options)

#endregion

/// <inheritdoc />
public override string FormatSymbol(string baseAsset, string quoteAsset) => $"{baseAsset.ToLowerInvariant()}{quoteAsset.ToLowerInvariant()}";

/// <inheritdoc />
public override string? GetListenerIdentifier(IMessageAccessor message)
{
Expand Down Expand Up @@ -79,10 +82,7 @@ public override ReadOnlyMemory<byte> PreprocessStreamMessage(WebSocketMessageTyp
if (type != WebSocketMessageType.Binary)
return data;

using var decompressedStream = new MemoryStream();
using var deflateStream = new GZipStream(new MemoryStream(data.ToArray()), CompressionMode.Decompress);
deflateStream.CopyTo(decompressedStream);
return new ReadOnlyMemory<byte>(decompressedStream.GetBuffer(), 0, (int)decompressedStream.Length);
return data.DecompressGzip();
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ internal HuobiClientUsdtMarginSwapApi(ILogger log, HttpClient? httpClient, Huobi
}
#endregion

/// <inheritdoc />
public override string FormatSymbol(string baseAsset, string quoteAsset) => $"{baseAsset.ToUpperInvariant()}-{quoteAsset.ToUpperInvariant()}";

/// <inheritdoc />
protected override AuthenticationProvider CreateAuthenticationProvider(ApiCredentials credentials)
=> new HuobiAuthenticationProvider(credentials, ClientOptions.SignPublicRequests);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ internal HuobiSocketClientUsdtMarginSwapApi(ILogger logger, HuobiSocketOptions o

#endregion

/// <inheritdoc />
public override string FormatSymbol(string baseAsset, string quoteAsset) => $"{baseAsset.ToUpperInvariant()}-{quoteAsset.ToUpperInvariant()}";

/// <inheritdoc />
public override ReadOnlyMemory<byte> PreprocessStreamMessage(WebSocketMessageType type, ReadOnlyMemory<byte> data)
{
if (type != WebSocketMessageType.Binary)
return data;

using var decompressedStream = new MemoryStream();
using var deflateStream = new GZipStream(new MemoryStream(data.ToArray()), CompressionMode.Decompress);
deflateStream.CopyTo(decompressedStream);
return new ReadOnlyMemory<byte>(decompressedStream.GetBuffer(), 0, (int)decompressedStream.Length);
return data.DecompressGzip();
}

/// <inheritdoc />
Expand Down
2 changes: 1 addition & 1 deletion Huobi.Net/ExtensionMethods/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static IServiceCollection AddHuobi(

services.AddTransient<ICryptoRestClient, CryptoRestClient>();
services.AddTransient<ICryptoSocketClient, CryptoSocketClient>();
services.AddSingleton<IHuobiOrderBookFactory, HuobiOrderBookFactory>();
services.AddTransient<IHuobiOrderBookFactory, HuobiOrderBookFactory>();
services.AddTransient(x => x.GetRequiredService<IHuobiRestClient>().SpotApi.CommonSpotClient);
if (socketClientLifeTime == null)
services.AddSingleton<IHuobiSocketClient, HuobiSocketClient>();
Expand Down
2 changes: 1 addition & 1 deletion Huobi.Net/Huobi.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="CryptoExchange.Net" Version="7.3.3" />
<PackageReference Include="CryptoExchange.Net" Version="7.4.0" />
</ItemGroup>
</Project>
40 changes: 40 additions & 0 deletions Huobi.Net/Huobi.Net.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions Huobi.Net/HuobiExchange.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Huobi.Net
{
/// <summary>
/// Huobi exchange information and configuration
/// </summary>
public static class HuobiExchange
{
/// <summary>
/// Exchange name
/// </summary>
public static string ExchangeName => "Huobi";

/// <summary>
/// Url to the main website
/// </summary>
public static string Url { get; } = "https://www.huobi.com";

/// <summary>
/// Urls to the API documentation
/// </summary>
public static string[] ApiDocsUrl { get; } = new[] {
"https://huobiapi.github.io/docs/spot/v1/en/#change-log"
};
}
}
5 changes: 5 additions & 0 deletions Huobi.Net/Interfaces/IHuobiOrderBookFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ namespace Huobi.Net.Interfaces
/// </summary>
public interface IHuobiOrderBookFactory
{
/// <summary>
/// Spot order book factory methods
/// </summary>
public IOrderBookFactory<HuobiOrderBookOptions> Spot { get; }

/// <summary>
/// Create a SymbolOrderBook for the Spot API
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions Huobi.Net/SymbolOrderBooks/HuobiOrderBookFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CryptoExchange.Net.Interfaces;
using CryptoExchange.Net.OrderBook;
using Huobi.Net.Interfaces;
using Huobi.Net.Interfaces.Clients;
using Huobi.Net.Objects.Options;
Expand All @@ -13,13 +14,18 @@ public class HuobiOrderBookFactory : IHuobiOrderBookFactory
{
private readonly IServiceProvider _serviceProvider;

/// <inheritdoc />
public IOrderBookFactory<HuobiOrderBookOptions> Spot { get; }

/// <summary>
/// ctor
/// </summary>
/// <param name="serviceProvider">Service provider for resolving logging and clients</param>
public HuobiOrderBookFactory(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;

Spot = new OrderBookFactory<HuobiOrderBookOptions>((symbol, options) => CreateSpot(symbol, options), (baseAsset, quoteAsset, options) => CreateSpot(baseAsset.ToLowerInvariant() + quoteAsset.ToLowerInvariant(), options));
}

/// <inheritdoc />
Expand Down

0 comments on commit 4a5b305

Please sign in to comment.