Skip to content

Commit

Permalink
Feature/cryptoclients update (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
JKorf authored Apr 28, 2024
1 parent b8f5b42 commit 9d669dd
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 31 deletions.
3 changes: 3 additions & 0 deletions Kucoin.Net/Clients/FuturesApi/KucoinRestClientFuturesApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ internal KucoinRestClientFuturesApi(ILogger logger, HttpClient? httpClient, Kuco
protected override AuthenticationProvider CreateAuthenticationProvider(ApiCredentials credentials)
=> new KucoinAuthenticationProvider((KucoinApiCredentials)credentials);

/// <inheritdoc />
public override string FormatSymbol(string baseAsset, string quoteAsset) => baseAsset.ToUpperInvariant() + quoteAsset.ToUpperInvariant();

internal async Task<WebCallResult> SendAsync(RequestDefinition definition, ParameterCollection? parameters, CancellationToken cancellationToken, int? weight = null)
{
var result = await base.SendAsync<KucoinResult>(BaseAddress, definition, parameters, cancellationToken, null, weight).ConfigureAwait(false);
Expand Down
3 changes: 3 additions & 0 deletions Kucoin.Net/Clients/FuturesApi/KucoinSocketClientFuturesApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public override string GetListenerIdentifier(IMessageAccessor message)
protected override AuthenticationProvider CreateAuthenticationProvider(ApiCredentials credentials)
=> new KucoinAuthenticationProvider((KucoinApiCredentials)credentials);

/// <inheritdoc />
public override string FormatSymbol(string baseAsset, string quoteAsset) => baseAsset.ToUpperInvariant() + quoteAsset.ToUpperInvariant();

/// <inheritdoc />
protected override Query? GetAuthenticationRequest() => null;

Expand Down
32 changes: 3 additions & 29 deletions Kucoin.Net/Clients/SpotApi/KucoinRestClientSpotApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ internal KucoinRestClientSpotApi(ILogger logger, HttpClient? httpClient, KucoinR
protected override AuthenticationProvider CreateAuthenticationProvider(ApiCredentials credentials)
=> new KucoinAuthenticationProvider((KucoinApiCredentials)credentials);

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

#region common interface

/// <summary>
Expand Down Expand Up @@ -372,35 +375,6 @@ internal async Task<WebCallResult<T>> SendAsync<T>(RequestDefinition definition,
return result.As(result.Data.Data);
}

internal async Task<WebCallResult> Execute(Uri uri, HttpMethod method, CancellationToken ct, Dictionary<string, object>? parameters = null, bool signed = false, HttpMethodParameterPosition? parameterPosition = null)
{
var result = await SendRequestAsync<KucoinResult<object>>(uri, method, ct, parameters, signed, parameterPosition: parameterPosition, requestWeight: 0).ConfigureAwait(false);
if (!result)
return result.AsDatalessError(result.Error!);

if (result.Data.Code != 200000 && result.Data.Code != 200)
return result.AsDatalessError(new ServerError(result.Data.Code, result.Data.Message ?? "-"));

return result.AsDataless();
}

internal async Task<WebCallResult<T>> Execute<T>(Uri uri, HttpMethod method, CancellationToken ct, Dictionary<string, object>? parameters = null, bool signed = false, HttpMethodParameterPosition? parameterPosition = null)
{
var result = await SendRequestAsync<KucoinResult<T>>(uri, method, ct, parameters, signed, parameterPosition: parameterPosition, requestWeight: 0).ConfigureAwait(false);
if (!result)
return result.AsError<T>(result.Error!);

if (result.Data.Code != 200000 && result.Data.Code != 200)
return result.AsError<T>(new ServerError(result.Data.Code, result.Data.Message ?? "-"));

return result.As(result.Data.Data);
}

internal Uri GetUri(string path, int apiVersion = 1)
{
return new Uri(BaseAddress.AppendPath("api").AppendPath("v" + apiVersion, path));
}

/// <inheritdoc />
protected override ServerRateLimitError ParseRateLimitResponse(int httpStatusCode, IEnumerable<KeyValuePair<string, IEnumerable<string>>> responseHeaders, IMessageAccessor accessor)
{
Expand Down
3 changes: 3 additions & 0 deletions Kucoin.Net/Clients/SpotApi/KucoinSocketClientSpotApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ internal KucoinSocketClientSpotApi(ILogger logger, KucoinSocketClient baseClient
protected override AuthenticationProvider CreateAuthenticationProvider(ApiCredentials credentials)
=> new KucoinAuthenticationProvider((KucoinApiCredentials)credentials);

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

/// <inheritdoc />
public override string GetListenerIdentifier(IMessageAccessor message)
{
Expand Down
2 changes: 1 addition & 1 deletion Kucoin.Net/ExtensionMethods/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static IServiceCollection AddKucoin(

services.AddTransient<ICryptoRestClient, CryptoRestClient>();
services.AddTransient<ICryptoSocketClient, CryptoSocketClient>();
services.AddSingleton<IKucoinOrderBookFactory, KucoinOrderBookFactory>();
services.AddTransient<IKucoinOrderBookFactory, KucoinOrderBookFactory>();
services.AddTransient(x => x.GetRequiredService<IKucoinRestClient>().SpotApi.CommonSpotClient);
services.AddTransient(x => x.GetRequiredService<IKucoinRestClient>().FuturesApi.CommonFuturesClient);
if (socketClientLifeTime == null)
Expand Down
10 changes: 10 additions & 0 deletions Kucoin.Net/Interfaces/IKucoinOrderBookFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ namespace Kucoin.Net.Interfaces
/// </summary>
public interface IKucoinOrderBookFactory
{
/// <summary>
/// Spot order book factory methods
/// </summary>
public IOrderBookFactory<KucoinOrderBookOptions> Spot { get; }

/// <summary>
/// Futures order book factory methods
/// </summary>
public IOrderBookFactory<KucoinOrderBookOptions> Futures { get; }

/// <summary>
/// Create a futures ISymbolOrderBook instance for the symbol
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Kucoin.Net/Kucoin.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,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>
43 changes: 43 additions & 0 deletions Kucoin.Net/Kucoin.Net.xml

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

12 changes: 12 additions & 0 deletions Kucoin.Net/KucoinExchange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ public static class KucoinExchange
/// </summary>
public static string ExchangeName => "Kucoin";

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

/// <summary>
/// Urls to the API documentation
/// </summary>
public static string[] ApiDocsUrl { get; } = new[] {
"https://www.kucoin.com/docs/beginners/introduction"
};

/// <summary>
/// Rate limiter configuration for the Kucoin API
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions Kucoin.Net/Objects/Options/KucoinOrderBookOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public class KucoinOrderBookOptions : OrderBookOptions
/// </summary>
public TimeSpan? InitialDataTimeout { get; set; }

/// <summary>
/// API credentials to use. The Kucoin order book endpoint requires authentication
/// </summary>
public KucoinApiCredentials? ApiCredentials { get; set; }

internal KucoinOrderBookOptions Copy()
{
var result = Copy<KucoinOrderBookOptions>();
Expand Down
10 changes: 10 additions & 0 deletions Kucoin.Net/SymbolOrderBooks/KucoinOrderBookFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CryptoExchange.Net.Interfaces;
using CryptoExchange.Net.OrderBook;
using Kucoin.Net.Interfaces;
using Kucoin.Net.Interfaces.Clients;
using Kucoin.Net.Objects.Options;
Expand All @@ -15,13 +16,22 @@ public class KucoinOrderBookFactory : IKucoinOrderBookFactory
{
private readonly IServiceProvider _serviceProvider;

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

/// <inheritdoc />
public IOrderBookFactory<KucoinOrderBookOptions> Futures { get; }

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

Spot = new OrderBookFactory<KucoinOrderBookOptions>((symbol, options) => CreateSpot(symbol, options), (baseAsset, quoteAsset, options) => CreateSpot(baseAsset.ToUpperInvariant() + "-" + quoteAsset.ToUpperInvariant(), options));
Futures = new OrderBookFactory<KucoinOrderBookOptions>((symbol, options) => CreateFutures(symbol, options), (baseAsset, quoteAsset, options) => CreateFutures(baseAsset.ToUpperInvariant() + quoteAsset.ToUpperInvariant(), options));
}

/// <summary>
Expand Down

0 comments on commit 9d669dd

Please sign in to comment.