Skip to content

Commit

Permalink
CryptoExchange.Net ratelimit update (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
JKorf authored Apr 17, 2024
1 parent f4b637d commit 9ce58d6
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Huobi.Net.UnitTests/JsonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IRateLimiter>();
x.RateLimiterEnabled = false;
}));

[Test]
Expand Down
5 changes: 3 additions & 2 deletions Huobi.Net.UnitTests/TestImplementations/TestSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class TestSocket: IWebsocket
#pragma warning disable 0067
public event Func<Task> OnReconnected;
public event Func<Task> OnReconnecting;
public event Func<int, Task> OnRequestRateLimited;
#pragma warning restore 0067
public event Func<int, Task> OnRequestSent;
public event Action<WebSocketMessageType, ReadOnlyMemory<byte>> OnStreamMessage;
Expand Down Expand Up @@ -49,10 +50,10 @@ public class TestSocket: IWebsocket
public TimeSpan KeepAliveInterval { get; set; }
public Func<Task<Uri>> GetReconnectionUrl { get; set; }

public Task<bool> ConnectAsync()
public Task<CallResult> 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)
Expand Down
8 changes: 4 additions & 4 deletions Huobi.Net/Clients/SpotApi/HuobiRestClientSpotApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected override AuthenticationProvider CreateAuthenticationProvider(ApiCreden

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

Expand All @@ -85,7 +85,7 @@ internal async Task<WebCallResult<T>> SendHuobiV2Request<T>(Uri uri, HttpMethod

internal async Task<WebCallResult<(T, DateTime)>> SendHuobiTimestampRequest<T>(Uri uri, HttpMethod method, CancellationToken cancellationToken, Dictionary<string, object>? parameters = null, bool signed = false)
{
var result = await SendRequestAsync<HuobiBasicResponse<T>>(uri, method, cancellationToken, parameters, signed).ConfigureAwait(false);
var result = await SendRequestAsync<HuobiBasicResponse<T>>(uri, method, cancellationToken, parameters, signed, requestWeight: 0).ConfigureAwait(false);
if (!result || result.Data == null)
return result.AsError<(T, DateTime)>(result.Error!);

Expand All @@ -95,9 +95,9 @@ internal async Task<WebCallResult<T>> SendHuobiV2Request<T>(Uri uri, HttpMethod
return result.As((result.Data.Data, result.Data.Timestamp));
}

internal async Task<WebCallResult<T>> SendHuobiRequest<T>(Uri uri, HttpMethod method, CancellationToken cancellationToken, Dictionary<string, object>? parameters = null, bool signed = false, int? weight = 1, bool ignoreRatelimit = false)
internal async Task<WebCallResult<T>> SendHuobiRequest<T>(Uri uri, HttpMethod method, CancellationToken cancellationToken, Dictionary<string, object>? parameters = null, bool signed = false)
{
var result = await SendRequestAsync<HuobiBasicResponse<T>>(uri, method, cancellationToken, parameters, signed, requestWeight: weight ?? 1, ignoreRatelimit: ignoreRatelimit).ConfigureAwait(false);
var result = await SendRequestAsync<HuobiBasicResponse<T>>(uri, method, cancellationToken, parameters, signed, requestWeight: 0).ConfigureAwait(false);
if (!result || result.Data == null)
return result.AsError<T>(result.Error!);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public async Task<WebCallResult<IEnumerable<HuobiAssetInfo>>> GetAssetDetailsAsy
/// <inheritdoc />
public async Task<WebCallResult<DateTime>> GetServerTimeAsync(CancellationToken ct = default)
{
var result = await _baseClient.SendHuobiRequest<string>(_baseClient.GetUrl(ServerTimeEndpoint, "1"), HttpMethod.Get, ct, ignoreRatelimit: true).ConfigureAwait(false);
var result = await _baseClient.SendHuobiRequest<string>(_baseClient.GetUrl(ServerTimeEndpoint, "1"), HttpMethod.Get, ct).ConfigureAwait(false);
if (!result)
return result.AsError<DateTime>(result.Error!);
var time = (DateTime)JsonConvert.DeserializeObject(result.Data, typeof(DateTime), new DateTimeConverter())!;
Expand Down
16 changes: 8 additions & 8 deletions Huobi.Net/Clients/SpotApi/HuobiRestClientSpotApiTrading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public async Task<WebCallResult<IEnumerable<HuobiOpenOrder>>> GetOpenOrdersAsync
parameters.AddOptionalParameter("side", side == null ? null : JsonConvert.SerializeObject(side, new OrderSideConverter(false)));
parameters.AddOptionalParameter("size", limit);

return await _baseClient.SendHuobiRequest<IEnumerable<HuobiOpenOrder>>(_baseClient.GetUrl(OpenOrdersEndpoint, "1"), HttpMethod.Get, ct, parameters, true, weight: 2).ConfigureAwait(false);
return await _baseClient.SendHuobiRequest<IEnumerable<HuobiOpenOrder>>(_baseClient.GetUrl(OpenOrdersEndpoint, "1"), HttpMethod.Get, ct, parameters, true).ConfigureAwait(false);
}

/// <inheritdoc />
Expand Down Expand Up @@ -121,7 +121,7 @@ public async Task<WebCallResult<HuobiBatchCancelResult>> 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<HuobiBatchCancelResult>(_baseClient.GetUrl(CancelOrdersEndpoint, "1"), HttpMethod.Post, ct, parameters, true, weight: 2).ConfigureAwait(false);
return await _baseClient.SendHuobiRequest<HuobiBatchCancelResult>(_baseClient.GetUrl(CancelOrdersEndpoint, "1"), HttpMethod.Post, ct, parameters, true).ConfigureAwait(false);
}

/// <inheritdoc />
Expand All @@ -139,7 +139,7 @@ public async Task<WebCallResult<HuobiByCriteriaCancelResult>> CancelOrdersByCrit
/// <inheritdoc />
public async Task<WebCallResult<HuobiOrder>> GetOrderAsync(long orderId, CancellationToken ct = default)
{
return await _baseClient.SendHuobiRequest<HuobiOrder>(_baseClient.GetUrl(OrderInfoEndpoint.FillPathParameters(orderId.ToString(CultureInfo.InvariantCulture)), "1"), HttpMethod.Get, ct, signed: true, weight: 2).ConfigureAwait(false);
return await _baseClient.SendHuobiRequest<HuobiOrder>(_baseClient.GetUrl(OrderInfoEndpoint.FillPathParameters(orderId.ToString(CultureInfo.InvariantCulture)), "1"), HttpMethod.Get, ct, signed: true).ConfigureAwait(false);
}

/// <inheritdoc />
Expand All @@ -150,13 +150,13 @@ public async Task<WebCallResult<HuobiOrder>> GetOrderByClientOrderIdAsync(string
{ "clientOrderId", clientOrderId }
};

return await _baseClient.SendHuobiRequest<HuobiOrder>(_baseClient.GetUrl(ClientOrderInfoEndpoint, "1"), HttpMethod.Get, ct, parameters: parameters, signed: true, weight: 2).ConfigureAwait(false);
return await _baseClient.SendHuobiRequest<HuobiOrder>(_baseClient.GetUrl(ClientOrderInfoEndpoint, "1"), HttpMethod.Get, ct, parameters: parameters, signed: true).ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<WebCallResult<IEnumerable<HuobiOrderTrade>>> GetOrderTradesAsync(long orderId, CancellationToken ct = default)
{
return await _baseClient.SendHuobiRequest<IEnumerable<HuobiOrderTrade>>(_baseClient.GetUrl(OrderTradesEndpoint.FillPathParameters(orderId.ToString(CultureInfo.InvariantCulture)), "1"), HttpMethod.Get, ct, signed: true, weight: 2).ConfigureAwait(false);
return await _baseClient.SendHuobiRequest<IEnumerable<HuobiOrderTrade>>(_baseClient.GetUrl(OrderTradesEndpoint.FillPathParameters(orderId.ToString(CultureInfo.InvariantCulture)), "1"), HttpMethod.Get, ct, signed: true).ConfigureAwait(false);
}

/// <inheritdoc />
Expand All @@ -180,7 +180,7 @@ public async Task<WebCallResult<IEnumerable<HuobiOrder>>> GetClosedOrdersAsync(s
parameters.AddOptionalParameter("direct", direction == null ? null : JsonConvert.SerializeObject(direction, new FilterDirectionConverter(false)));
parameters.AddOptionalParameter("size", limit);

return await _baseClient.SendHuobiRequest<IEnumerable<HuobiOrder>>(_baseClient.GetUrl(OrdersEndpoint, "1"), HttpMethod.Get, ct, parameters, true, weight: 2).ConfigureAwait(false);
return await _baseClient.SendHuobiRequest<IEnumerable<HuobiOrder>>(_baseClient.GetUrl(OrdersEndpoint, "1"), HttpMethod.Get, ct, parameters, true).ConfigureAwait(false);
}

/// <inheritdoc />
Expand All @@ -199,7 +199,7 @@ public async Task<WebCallResult<IEnumerable<HuobiOrderTrade>>> GetUserTradesAsyn
parameters.AddOptionalParameter("direct", direction == null ? null : JsonConvert.SerializeObject(direction, new FilterDirectionConverter(false)));
parameters.AddOptionalParameter("size", limit);

return await _baseClient.SendHuobiRequest<IEnumerable<HuobiOrderTrade>>(_baseClient.GetUrl(SymbolTradesEndpoint, "1"), HttpMethod.Get, ct, parameters, true, weight: 5).ConfigureAwait(false);
return await _baseClient.SendHuobiRequest<IEnumerable<HuobiOrderTrade>>(_baseClient.GetUrl(SymbolTradesEndpoint, "1"), HttpMethod.Get, ct, parameters, true).ConfigureAwait(false);
}

/// <inheritdoc />
Expand All @@ -213,7 +213,7 @@ public async Task<WebCallResult<IEnumerable<HuobiOrder>>> GetHistoricalOrdersAsy
parameters.AddOptionalParameter("direct", direction == null ? null : JsonConvert.SerializeObject(direction, new FilterDirectionConverter(false)));
parameters.AddOptionalParameter("size", limit);

var result = await _baseClient.SendHuobiRequest<IEnumerable<HuobiOrder>>(_baseClient.GetUrl(HistoryOrdersEndpoint, "1"), HttpMethod.Get, ct, parameters, true, weight: 5).ConfigureAwait(false);
var result = await _baseClient.SendHuobiRequest<IEnumerable<HuobiOrder>>(_baseClient.GetUrl(HistoryOrdersEndpoint, "1"), HttpMethod.Get, ct, parameters, true).ConfigureAwait(false);
if (!result)
return result.AsError<IEnumerable<HuobiOrder>>(result.Error!);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,19 @@ internal Uri GetUrl(string endpoint, string? version = null)
return new Uri(BaseAddress.AppendPath($"v{version}", endpoint));
}

internal async Task<WebCallResult<DateTime>> SendTimestampRequestAsync(Uri uri, HttpMethod method, CancellationToken cancellationToken, Dictionary<string, object>? parameters = null, bool signed = false, int? weight = 1, bool ignoreRatelimit = false)
internal async Task<WebCallResult<DateTime>> SendTimestampRequestAsync(Uri uri, HttpMethod method, CancellationToken cancellationToken, Dictionary<string, object>? parameters = null, bool signed = false)
{
var result = await SendRequestAsync<HuobiBasicResponse<string>>(uri, method, cancellationToken, parameters, signed, requestWeight: weight ?? 1, ignoreRatelimit: ignoreRatelimit).ConfigureAwait(false);
var result = await SendRequestAsync<HuobiBasicResponse<string>>(uri, method, cancellationToken, parameters, signed, requestWeight: 0).ConfigureAwait(false);
if (!result || result.Data == null)
return result.AsError<DateTime>(result.Error!);

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


internal async Task<WebCallResult<T>> SendHuobiRequest<T>(Uri uri, HttpMethod method, CancellationToken cancellationToken, Dictionary<string, object>? parameters = null, bool signed = false, int? weight = 1, bool ignoreRatelimit = false)
internal async Task<WebCallResult<T>> SendHuobiRequest<T>(Uri uri, HttpMethod method, CancellationToken cancellationToken, Dictionary<string, object>? parameters = null, bool signed = false)
{
var result = await SendRequestAsync<HuobiBasicResponse<T>>(uri, method, cancellationToken, parameters, signed, requestWeight: weight ?? 1, ignoreRatelimit: ignoreRatelimit).ConfigureAwait(false);
var result = await SendRequestAsync<HuobiBasicResponse<T>>(uri, method, cancellationToken, parameters, signed, requestWeight: 0).ConfigureAwait(false);
if (!result || result.Data == null)
return result.AsError<T>(result.Error!);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal HuobiClientUsdtMarginSwapApiExchangeData(HuobiClientUsdtMarginSwapApi b
/// <inheritdoc />
public async Task<WebCallResult<DateTime>> 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);
}

/// <inheritdoc />
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.2.0" />
<PackageReference Include="CryptoExchange.Net" Version="7.3.0" />
</ItemGroup>
</Project>
11 changes: 1 addition & 10 deletions Huobi.Net/Objects/Options/HuobiRestOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,7 @@ public class HuobiRestOptions : RestExchangeOptions<HuobiEnvironment>
/// <summary>
/// Spot API options
/// </summary>
public RestApiOptions SpotOptions { get; private set; } = new RestApiOptions()
{
RateLimiters = new List<IRateLimiter>
{
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();

/// <summary>
/// USDT margin swap API options
Expand Down

0 comments on commit 9ce58d6

Please sign in to comment.