From 9d669ddb30f58eaa709342c3595bf18b6bdc46f1 Mon Sep 17 00:00:00 2001 From: Jan Korf Date: Sun, 28 Apr 2024 11:36:16 +0200 Subject: [PATCH] Feature/cryptoclients update (#223) --- .../FuturesApi/KucoinRestClientFuturesApi.cs | 3 ++ .../KucoinSocketClientFuturesApi.cs | 3 ++ .../SpotApi/KucoinRestClientSpotApi.cs | 32 ++------------ .../SpotApi/KucoinSocketClientSpotApi.cs | 3 ++ .../ServiceCollectionExtensions.cs | 2 +- .../Interfaces/IKucoinOrderBookFactory.cs | 10 +++++ Kucoin.Net/Kucoin.Net.csproj | 2 +- Kucoin.Net/Kucoin.Net.xml | 43 +++++++++++++++++++ Kucoin.Net/KucoinExchange.cs | 12 ++++++ .../Objects/Options/KucoinOrderBookOptions.cs | 5 +++ .../KucoinOrderBookFactory.cs | 10 +++++ 11 files changed, 94 insertions(+), 31 deletions(-) diff --git a/Kucoin.Net/Clients/FuturesApi/KucoinRestClientFuturesApi.cs b/Kucoin.Net/Clients/FuturesApi/KucoinRestClientFuturesApi.cs index 6be7c060..d2826069 100644 --- a/Kucoin.Net/Clients/FuturesApi/KucoinRestClientFuturesApi.cs +++ b/Kucoin.Net/Clients/FuturesApi/KucoinRestClientFuturesApi.cs @@ -67,6 +67,9 @@ internal KucoinRestClientFuturesApi(ILogger logger, HttpClient? httpClient, Kuco protected override AuthenticationProvider CreateAuthenticationProvider(ApiCredentials credentials) => new KucoinAuthenticationProvider((KucoinApiCredentials)credentials); + /// + public override string FormatSymbol(string baseAsset, string quoteAsset) => baseAsset.ToUpperInvariant() + quoteAsset.ToUpperInvariant(); + internal async Task SendAsync(RequestDefinition definition, ParameterCollection? parameters, CancellationToken cancellationToken, int? weight = null) { var result = await base.SendAsync(BaseAddress, definition, parameters, cancellationToken, null, weight).ConfigureAwait(false); diff --git a/Kucoin.Net/Clients/FuturesApi/KucoinSocketClientFuturesApi.cs b/Kucoin.Net/Clients/FuturesApi/KucoinSocketClientFuturesApi.cs index d1c08286..018c1f45 100644 --- a/Kucoin.Net/Clients/FuturesApi/KucoinSocketClientFuturesApi.cs +++ b/Kucoin.Net/Clients/FuturesApi/KucoinSocketClientFuturesApi.cs @@ -62,6 +62,9 @@ public override string GetListenerIdentifier(IMessageAccessor message) protected override AuthenticationProvider CreateAuthenticationProvider(ApiCredentials credentials) => new KucoinAuthenticationProvider((KucoinApiCredentials)credentials); + /// + public override string FormatSymbol(string baseAsset, string quoteAsset) => baseAsset.ToUpperInvariant() + quoteAsset.ToUpperInvariant(); + /// protected override Query? GetAuthenticationRequest() => null; diff --git a/Kucoin.Net/Clients/SpotApi/KucoinRestClientSpotApi.cs b/Kucoin.Net/Clients/SpotApi/KucoinRestClientSpotApi.cs index bec03a8a..801ba027 100644 --- a/Kucoin.Net/Clients/SpotApi/KucoinRestClientSpotApi.cs +++ b/Kucoin.Net/Clients/SpotApi/KucoinRestClientSpotApi.cs @@ -69,6 +69,9 @@ internal KucoinRestClientSpotApi(ILogger logger, HttpClient? httpClient, KucoinR protected override AuthenticationProvider CreateAuthenticationProvider(ApiCredentials credentials) => new KucoinAuthenticationProvider((KucoinApiCredentials)credentials); + /// + public override string FormatSymbol(string baseAsset, string quoteAsset) => baseAsset.ToUpperInvariant() + "-" + quoteAsset.ToUpperInvariant(); + #region common interface /// @@ -372,35 +375,6 @@ internal async Task> SendAsync(RequestDefinition definition, return result.As(result.Data.Data); } - internal async Task Execute(Uri uri, HttpMethod method, CancellationToken ct, Dictionary? parameters = null, bool signed = false, HttpMethodParameterPosition? parameterPosition = null) - { - var result = await SendRequestAsync>(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> Execute(Uri uri, HttpMethod method, CancellationToken ct, Dictionary? parameters = null, bool signed = false, HttpMethodParameterPosition? parameterPosition = null) - { - var result = await SendRequestAsync>(uri, method, ct, parameters, signed, parameterPosition: parameterPosition, requestWeight: 0).ConfigureAwait(false); - if (!result) - return result.AsError(result.Error!); - - if (result.Data.Code != 200000 && result.Data.Code != 200) - return result.AsError(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)); - } - /// protected override ServerRateLimitError ParseRateLimitResponse(int httpStatusCode, IEnumerable>> responseHeaders, IMessageAccessor accessor) { diff --git a/Kucoin.Net/Clients/SpotApi/KucoinSocketClientSpotApi.cs b/Kucoin.Net/Clients/SpotApi/KucoinSocketClientSpotApi.cs index 0ffec5b1..d16a3444 100644 --- a/Kucoin.Net/Clients/SpotApi/KucoinSocketClientSpotApi.cs +++ b/Kucoin.Net/Clients/SpotApi/KucoinSocketClientSpotApi.cs @@ -53,6 +53,9 @@ internal KucoinSocketClientSpotApi(ILogger logger, KucoinSocketClient baseClient protected override AuthenticationProvider CreateAuthenticationProvider(ApiCredentials credentials) => new KucoinAuthenticationProvider((KucoinApiCredentials)credentials); + /// + public override string FormatSymbol(string baseAsset, string quoteAsset) => baseAsset.ToUpperInvariant() + "-" + quoteAsset.ToUpperInvariant(); + /// public override string GetListenerIdentifier(IMessageAccessor message) { diff --git a/Kucoin.Net/ExtensionMethods/ServiceCollectionExtensions.cs b/Kucoin.Net/ExtensionMethods/ServiceCollectionExtensions.cs index 7188eadd..18b22bc3 100644 --- a/Kucoin.Net/ExtensionMethods/ServiceCollectionExtensions.cs +++ b/Kucoin.Net/ExtensionMethods/ServiceCollectionExtensions.cs @@ -59,7 +59,7 @@ public static IServiceCollection AddKucoin( services.AddTransient(); services.AddTransient(); - services.AddSingleton(); + services.AddTransient(); services.AddTransient(x => x.GetRequiredService().SpotApi.CommonSpotClient); services.AddTransient(x => x.GetRequiredService().FuturesApi.CommonFuturesClient); if (socketClientLifeTime == null) diff --git a/Kucoin.Net/Interfaces/IKucoinOrderBookFactory.cs b/Kucoin.Net/Interfaces/IKucoinOrderBookFactory.cs index c8f89413..08edcfb6 100644 --- a/Kucoin.Net/Interfaces/IKucoinOrderBookFactory.cs +++ b/Kucoin.Net/Interfaces/IKucoinOrderBookFactory.cs @@ -9,6 +9,16 @@ namespace Kucoin.Net.Interfaces /// public interface IKucoinOrderBookFactory { + /// + /// Spot order book factory methods + /// + public IOrderBookFactory Spot { get; } + + /// + /// Futures order book factory methods + /// + public IOrderBookFactory Futures { get; } + /// /// Create a futures ISymbolOrderBook instance for the symbol /// diff --git a/Kucoin.Net/Kucoin.Net.csproj b/Kucoin.Net/Kucoin.Net.csproj index fb7a6590..165e29ea 100644 --- a/Kucoin.Net/Kucoin.Net.csproj +++ b/Kucoin.Net/Kucoin.Net.csproj @@ -53,6 +53,6 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + \ No newline at end of file diff --git a/Kucoin.Net/Kucoin.Net.xml b/Kucoin.Net/Kucoin.Net.xml index 2ee58569..543aae84 100644 --- a/Kucoin.Net/Kucoin.Net.xml +++ b/Kucoin.Net/Kucoin.Net.xml @@ -32,6 +32,9 @@ + + + @@ -208,6 +211,9 @@ + + + @@ -377,6 +383,9 @@ + + + Return the Kucoin trade symbol name from base and quote asset @@ -691,6 +700,9 @@ + + + @@ -4190,6 +4202,16 @@ Factory for creating Kucoin symbol orderbook instance + + + Spot order book factory methods + + + + + Futures order book factory methods + + Create a futures ISymbolOrderBook instance for the symbol @@ -4250,6 +4272,16 @@ Exchange name + + + Url to the main website + + + + + Urls to the API documentation + + Rate limiter configuration for the Kucoin API @@ -10012,6 +10044,11 @@ After how much time we should consider the connection dropped if no data is received for this time after the initial subscriptions + + + API credentials to use. The Kucoin order book endpoint requires authentication + + @@ -10103,6 +10140,12 @@ Kucoin order book factory + + + + + + ctor diff --git a/Kucoin.Net/KucoinExchange.cs b/Kucoin.Net/KucoinExchange.cs index 16fdcf18..001a3b34 100644 --- a/Kucoin.Net/KucoinExchange.cs +++ b/Kucoin.Net/KucoinExchange.cs @@ -20,6 +20,18 @@ public static class KucoinExchange /// public static string ExchangeName => "Kucoin"; + /// + /// Url to the main website + /// + public static string Url { get; } = "https://www.kucoin.com"; + + /// + /// Urls to the API documentation + /// + public static string[] ApiDocsUrl { get; } = new[] { + "https://www.kucoin.com/docs/beginners/introduction" + }; + /// /// Rate limiter configuration for the Kucoin API /// diff --git a/Kucoin.Net/Objects/Options/KucoinOrderBookOptions.cs b/Kucoin.Net/Objects/Options/KucoinOrderBookOptions.cs index ca2c78a1..02b97a13 100644 --- a/Kucoin.Net/Objects/Options/KucoinOrderBookOptions.cs +++ b/Kucoin.Net/Objects/Options/KucoinOrderBookOptions.cs @@ -23,6 +23,11 @@ public class KucoinOrderBookOptions : OrderBookOptions /// public TimeSpan? InitialDataTimeout { get; set; } + /// + /// API credentials to use. The Kucoin order book endpoint requires authentication + /// + public KucoinApiCredentials? ApiCredentials { get; set; } + internal KucoinOrderBookOptions Copy() { var result = Copy(); diff --git a/Kucoin.Net/SymbolOrderBooks/KucoinOrderBookFactory.cs b/Kucoin.Net/SymbolOrderBooks/KucoinOrderBookFactory.cs index bb219658..3e5538ad 100644 --- a/Kucoin.Net/SymbolOrderBooks/KucoinOrderBookFactory.cs +++ b/Kucoin.Net/SymbolOrderBooks/KucoinOrderBookFactory.cs @@ -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; @@ -15,6 +16,12 @@ public class KucoinOrderBookFactory : IKucoinOrderBookFactory { private readonly IServiceProvider _serviceProvider; + /// + public IOrderBookFactory Spot { get; } + + /// + public IOrderBookFactory Futures { get; } + /// /// ctor /// @@ -22,6 +29,9 @@ public class KucoinOrderBookFactory : IKucoinOrderBookFactory public KucoinOrderBookFactory(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; + + Spot = new OrderBookFactory((symbol, options) => CreateSpot(symbol, options), (baseAsset, quoteAsset, options) => CreateSpot(baseAsset.ToUpperInvariant() + "-" + quoteAsset.ToUpperInvariant(), options)); + Futures = new OrderBookFactory((symbol, options) => CreateFutures(symbol, options), (baseAsset, quoteAsset, options) => CreateFutures(baseAsset.ToUpperInvariant() + quoteAsset.ToUpperInvariant(), options)); } ///