From 9ce58d6bd372ffc104375331ac049783ee5e95ba Mon Sep 17 00:00:00 2001 From: Jan Korf Date: Wed, 17 Apr 2024 14:25:15 +0200 Subject: [PATCH] CryptoExchange.Net ratelimit update (#108) --- Huobi.Net.UnitTests/JsonTests.cs | 2 +- .../TestImplementations/TestSocket.cs | 5 +++-- .../Clients/SpotApi/HuobiRestClientSpotApi.cs | 8 ++++---- .../HuobiRestClientSpotApiExchangeData.cs | 2 +- .../SpotApi/HuobiRestClientSpotApiTrading.cs | 16 ++++++++-------- .../HuobiClientUsdtMarginSwapApi.cs | 8 ++++---- .../HuobiClientUsdtMarginSwapApiExchangeData.cs | 2 +- Huobi.Net/Huobi.Net.csproj | 2 +- Huobi.Net/Objects/Options/HuobiRestOptions.cs | 11 +---------- 9 files changed, 24 insertions(+), 32 deletions(-) diff --git a/Huobi.Net.UnitTests/JsonTests.cs b/Huobi.Net.UnitTests/JsonTests.cs index e454b41b..56ecbcfc 100644 --- a/Huobi.Net.UnitTests/JsonTests.cs +++ b/Huobi.Net.UnitTests/JsonTests.cs @@ -18,7 +18,7 @@ public class JsonTests { x.ApiCredentials = new CryptoExchange.Net.Authentication.ApiCredentials("123", "123"); x.SpotOptions.OutputOriginalData = true; - x.SpotOptions.RateLimiters = new List(); + x.RateLimiterEnabled = false; })); [Test] diff --git a/Huobi.Net.UnitTests/TestImplementations/TestSocket.cs b/Huobi.Net.UnitTests/TestImplementations/TestSocket.cs index f3eefded..674603a7 100644 --- a/Huobi.Net.UnitTests/TestImplementations/TestSocket.cs +++ b/Huobi.Net.UnitTests/TestImplementations/TestSocket.cs @@ -19,6 +19,7 @@ public class TestSocket: IWebsocket #pragma warning disable 0067 public event Func OnReconnected; public event Func OnReconnecting; + public event Func OnRequestRateLimited; #pragma warning restore 0067 public event Func OnRequestSent; public event Action> OnStreamMessage; @@ -49,10 +50,10 @@ public class TestSocket: IWebsocket public TimeSpan KeepAliveInterval { get; set; } public Func> GetReconnectionUrl { get; set; } - public Task ConnectAsync() + public Task ConnectAsync() { Connected = CanConnect; - return Task.FromResult(CanConnect); + return Task.FromResult(CanConnect ? new CallResult(null) : new CallResult(new CantConnectError())); } public void Send(int requestId, string data, int weight) diff --git a/Huobi.Net/Clients/SpotApi/HuobiRestClientSpotApi.cs b/Huobi.Net/Clients/SpotApi/HuobiRestClientSpotApi.cs index 5ecf98ee..271e83ac 100644 --- a/Huobi.Net/Clients/SpotApi/HuobiRestClientSpotApi.cs +++ b/Huobi.Net/Clients/SpotApi/HuobiRestClientSpotApi.cs @@ -73,7 +73,7 @@ protected override AuthenticationProvider CreateAuthenticationProvider(ApiCreden internal async Task> SendHuobiV2Request(Uri uri, HttpMethod method, CancellationToken cancellationToken, Dictionary? parameters = null, bool signed = false) { - var result = await SendRequestAsync>(uri, method, cancellationToken, parameters, signed).ConfigureAwait(false); + var result = await SendRequestAsync>(uri, method, cancellationToken, parameters, signed, requestWeight: 0).ConfigureAwait(false); if (!result || result.Data == null) return result.AsError(result.Error!); @@ -85,7 +85,7 @@ internal async Task> SendHuobiV2Request(Uri uri, HttpMethod internal async Task> SendHuobiTimestampRequest(Uri uri, HttpMethod method, CancellationToken cancellationToken, Dictionary? parameters = null, bool signed = false) { - var result = await SendRequestAsync>(uri, method, cancellationToken, parameters, signed).ConfigureAwait(false); + var result = await SendRequestAsync>(uri, method, cancellationToken, parameters, signed, requestWeight: 0).ConfigureAwait(false); if (!result || result.Data == null) return result.AsError<(T, DateTime)>(result.Error!); @@ -95,9 +95,9 @@ internal async Task> SendHuobiV2Request(Uri uri, HttpMethod return result.As((result.Data.Data, result.Data.Timestamp)); } - internal async Task> SendHuobiRequest(Uri uri, HttpMethod method, CancellationToken cancellationToken, Dictionary? parameters = null, bool signed = false, int? weight = 1, bool ignoreRatelimit = false) + internal async Task> SendHuobiRequest(Uri uri, HttpMethod method, CancellationToken cancellationToken, Dictionary? parameters = null, bool signed = false) { - var result = await SendRequestAsync>(uri, method, cancellationToken, parameters, signed, requestWeight: weight ?? 1, ignoreRatelimit: ignoreRatelimit).ConfigureAwait(false); + var result = await SendRequestAsync>(uri, method, cancellationToken, parameters, signed, requestWeight: 0).ConfigureAwait(false); if (!result || result.Data == null) return result.AsError(result.Error!); diff --git a/Huobi.Net/Clients/SpotApi/HuobiRestClientSpotApiExchangeData.cs b/Huobi.Net/Clients/SpotApi/HuobiRestClientSpotApiExchangeData.cs index a3e6d7c6..f24570af 100644 --- a/Huobi.Net/Clients/SpotApi/HuobiRestClientSpotApiExchangeData.cs +++ b/Huobi.Net/Clients/SpotApi/HuobiRestClientSpotApiExchangeData.cs @@ -193,7 +193,7 @@ public async Task>> GetAssetDetailsAsy /// public async Task> GetServerTimeAsync(CancellationToken ct = default) { - var result = await _baseClient.SendHuobiRequest(_baseClient.GetUrl(ServerTimeEndpoint, "1"), HttpMethod.Get, ct, ignoreRatelimit: true).ConfigureAwait(false); + var result = await _baseClient.SendHuobiRequest(_baseClient.GetUrl(ServerTimeEndpoint, "1"), HttpMethod.Get, ct).ConfigureAwait(false); if (!result) return result.AsError(result.Error!); var time = (DateTime)JsonConvert.DeserializeObject(result.Data, typeof(DateTime), new DateTimeConverter())!; diff --git a/Huobi.Net/Clients/SpotApi/HuobiRestClientSpotApiTrading.cs b/Huobi.Net/Clients/SpotApi/HuobiRestClientSpotApiTrading.cs index e8974446..ce4943ee 100644 --- a/Huobi.Net/Clients/SpotApi/HuobiRestClientSpotApiTrading.cs +++ b/Huobi.Net/Clients/SpotApi/HuobiRestClientSpotApiTrading.cs @@ -88,7 +88,7 @@ public async Task>> GetOpenOrdersAsync parameters.AddOptionalParameter("side", side == null ? null : JsonConvert.SerializeObject(side, new OrderSideConverter(false))); parameters.AddOptionalParameter("size", limit); - return await _baseClient.SendHuobiRequest>(_baseClient.GetUrl(OpenOrdersEndpoint, "1"), HttpMethod.Get, ct, parameters, true, weight: 2).ConfigureAwait(false); + return await _baseClient.SendHuobiRequest>(_baseClient.GetUrl(OpenOrdersEndpoint, "1"), HttpMethod.Get, ct, parameters, true).ConfigureAwait(false); } /// @@ -121,7 +121,7 @@ public async Task> CancelOrdersAsync(IEnum parameters.AddOptionalParameter("order-ids", orderIds?.Select(s => s.ToString(CultureInfo.InvariantCulture))); parameters.AddOptionalParameter("client-order-ids", clientOrderIds?.Select(s => s.ToString(CultureInfo.InvariantCulture))); - return await _baseClient.SendHuobiRequest(_baseClient.GetUrl(CancelOrdersEndpoint, "1"), HttpMethod.Post, ct, parameters, true, weight: 2).ConfigureAwait(false); + return await _baseClient.SendHuobiRequest(_baseClient.GetUrl(CancelOrdersEndpoint, "1"), HttpMethod.Post, ct, parameters, true).ConfigureAwait(false); } /// @@ -139,7 +139,7 @@ public async Task> CancelOrdersByCrit /// public async Task> GetOrderAsync(long orderId, CancellationToken ct = default) { - return await _baseClient.SendHuobiRequest(_baseClient.GetUrl(OrderInfoEndpoint.FillPathParameters(orderId.ToString(CultureInfo.InvariantCulture)), "1"), HttpMethod.Get, ct, signed: true, weight: 2).ConfigureAwait(false); + return await _baseClient.SendHuobiRequest(_baseClient.GetUrl(OrderInfoEndpoint.FillPathParameters(orderId.ToString(CultureInfo.InvariantCulture)), "1"), HttpMethod.Get, ct, signed: true).ConfigureAwait(false); } /// @@ -150,13 +150,13 @@ public async Task> GetOrderByClientOrderIdAsync(string { "clientOrderId", clientOrderId } }; - return await _baseClient.SendHuobiRequest(_baseClient.GetUrl(ClientOrderInfoEndpoint, "1"), HttpMethod.Get, ct, parameters: parameters, signed: true, weight: 2).ConfigureAwait(false); + return await _baseClient.SendHuobiRequest(_baseClient.GetUrl(ClientOrderInfoEndpoint, "1"), HttpMethod.Get, ct, parameters: parameters, signed: true).ConfigureAwait(false); } /// public async Task>> GetOrderTradesAsync(long orderId, CancellationToken ct = default) { - return await _baseClient.SendHuobiRequest>(_baseClient.GetUrl(OrderTradesEndpoint.FillPathParameters(orderId.ToString(CultureInfo.InvariantCulture)), "1"), HttpMethod.Get, ct, signed: true, weight: 2).ConfigureAwait(false); + return await _baseClient.SendHuobiRequest>(_baseClient.GetUrl(OrderTradesEndpoint.FillPathParameters(orderId.ToString(CultureInfo.InvariantCulture)), "1"), HttpMethod.Get, ct, signed: true).ConfigureAwait(false); } /// @@ -180,7 +180,7 @@ public async Task>> GetClosedOrdersAsync(s parameters.AddOptionalParameter("direct", direction == null ? null : JsonConvert.SerializeObject(direction, new FilterDirectionConverter(false))); parameters.AddOptionalParameter("size", limit); - return await _baseClient.SendHuobiRequest>(_baseClient.GetUrl(OrdersEndpoint, "1"), HttpMethod.Get, ct, parameters, true, weight: 2).ConfigureAwait(false); + return await _baseClient.SendHuobiRequest>(_baseClient.GetUrl(OrdersEndpoint, "1"), HttpMethod.Get, ct, parameters, true).ConfigureAwait(false); } /// @@ -199,7 +199,7 @@ public async Task>> GetUserTradesAsyn parameters.AddOptionalParameter("direct", direction == null ? null : JsonConvert.SerializeObject(direction, new FilterDirectionConverter(false))); parameters.AddOptionalParameter("size", limit); - return await _baseClient.SendHuobiRequest>(_baseClient.GetUrl(SymbolTradesEndpoint, "1"), HttpMethod.Get, ct, parameters, true, weight: 5).ConfigureAwait(false); + return await _baseClient.SendHuobiRequest>(_baseClient.GetUrl(SymbolTradesEndpoint, "1"), HttpMethod.Get, ct, parameters, true).ConfigureAwait(false); } /// @@ -213,7 +213,7 @@ public async Task>> GetHistoricalOrdersAsy parameters.AddOptionalParameter("direct", direction == null ? null : JsonConvert.SerializeObject(direction, new FilterDirectionConverter(false))); parameters.AddOptionalParameter("size", limit); - var result = await _baseClient.SendHuobiRequest>(_baseClient.GetUrl(HistoryOrdersEndpoint, "1"), HttpMethod.Get, ct, parameters, true, weight: 5).ConfigureAwait(false); + var result = await _baseClient.SendHuobiRequest>(_baseClient.GetUrl(HistoryOrdersEndpoint, "1"), HttpMethod.Get, ct, parameters, true).ConfigureAwait(false); if (!result) return result.AsError>(result.Error!); diff --git a/Huobi.Net/Clients/UsdtMarginSwapApi/HuobiClientUsdtMarginSwapApi.cs b/Huobi.Net/Clients/UsdtMarginSwapApi/HuobiClientUsdtMarginSwapApi.cs index 822ad52d..62f66623 100644 --- a/Huobi.Net/Clients/UsdtMarginSwapApi/HuobiClientUsdtMarginSwapApi.cs +++ b/Huobi.Net/Clients/UsdtMarginSwapApi/HuobiClientUsdtMarginSwapApi.cs @@ -79,9 +79,9 @@ internal Uri GetUrl(string endpoint, string? version = null) return new Uri(BaseAddress.AppendPath($"v{version}", endpoint)); } - internal async Task> SendTimestampRequestAsync(Uri uri, HttpMethod method, CancellationToken cancellationToken, Dictionary? parameters = null, bool signed = false, int? weight = 1, bool ignoreRatelimit = false) + internal async Task> SendTimestampRequestAsync(Uri uri, HttpMethod method, CancellationToken cancellationToken, Dictionary? parameters = null, bool signed = false) { - var result = await SendRequestAsync>(uri, method, cancellationToken, parameters, signed, requestWeight: weight ?? 1, ignoreRatelimit: ignoreRatelimit).ConfigureAwait(false); + var result = await SendRequestAsync>(uri, method, cancellationToken, parameters, signed, requestWeight: 0).ConfigureAwait(false); if (!result || result.Data == null) return result.AsError(result.Error!); @@ -89,9 +89,9 @@ internal async Task> SendTimestampRequestAsync(Uri uri, } - internal async Task> SendHuobiRequest(Uri uri, HttpMethod method, CancellationToken cancellationToken, Dictionary? parameters = null, bool signed = false, int? weight = 1, bool ignoreRatelimit = false) + internal async Task> SendHuobiRequest(Uri uri, HttpMethod method, CancellationToken cancellationToken, Dictionary? parameters = null, bool signed = false) { - var result = await SendRequestAsync>(uri, method, cancellationToken, parameters, signed, requestWeight: weight ?? 1, ignoreRatelimit: ignoreRatelimit).ConfigureAwait(false); + var result = await SendRequestAsync>(uri, method, cancellationToken, parameters, signed, requestWeight: 0).ConfigureAwait(false); if (!result || result.Data == null) return result.AsError(result.Error!); diff --git a/Huobi.Net/Clients/UsdtMarginSwapApi/HuobiClientUsdtMarginSwapApiExchangeData.cs b/Huobi.Net/Clients/UsdtMarginSwapApi/HuobiClientUsdtMarginSwapApiExchangeData.cs index a57eeb2b..acb2f03d 100644 --- a/Huobi.Net/Clients/UsdtMarginSwapApi/HuobiClientUsdtMarginSwapApiExchangeData.cs +++ b/Huobi.Net/Clients/UsdtMarginSwapApi/HuobiClientUsdtMarginSwapApiExchangeData.cs @@ -28,7 +28,7 @@ internal HuobiClientUsdtMarginSwapApiExchangeData(HuobiClientUsdtMarginSwapApi b /// public async Task> GetServerTimeAsync(CancellationToken ct = default) { - return await _baseClient.SendTimestampRequestAsync(_baseClient.GetUrl("api/v1/timestamp"), HttpMethod.Get, ct, ignoreRatelimit: true).ConfigureAwait(false); + return await _baseClient.SendTimestampRequestAsync(_baseClient.GetUrl("api/v1/timestamp"), HttpMethod.Get, ct).ConfigureAwait(false); } /// diff --git a/Huobi.Net/Huobi.Net.csproj b/Huobi.Net/Huobi.Net.csproj index 0fb6a625..1a28da29 100644 --- a/Huobi.Net/Huobi.Net.csproj +++ b/Huobi.Net/Huobi.Net.csproj @@ -52,6 +52,6 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + \ No newline at end of file diff --git a/Huobi.Net/Objects/Options/HuobiRestOptions.cs b/Huobi.Net/Objects/Options/HuobiRestOptions.cs index cca2e6d3..31c8cad4 100644 --- a/Huobi.Net/Objects/Options/HuobiRestOptions.cs +++ b/Huobi.Net/Objects/Options/HuobiRestOptions.cs @@ -32,16 +32,7 @@ public class HuobiRestOptions : RestExchangeOptions /// /// Spot API options /// - public RestApiOptions SpotOptions { get; private set; } = new RestApiOptions() - { - RateLimiters = new List - { - new RateLimiter() - .AddPartialEndpointLimit("/v1/order", 100, TimeSpan.FromSeconds(2), null, true, true) - .AddApiKeyLimit(10, TimeSpan.FromSeconds(1), true, true) - .AddTotalRateLimit(10, TimeSpan.FromSeconds(1)) - } - }; + public RestApiOptions SpotOptions { get; private set; } = new RestApiOptions(); /// /// USDT margin swap API options