Skip to content

Commit

Permalink
Updated client order id logic, added AllowAppendingClientOrderId option
Browse files Browse the repository at this point in the history
  • Loading branch information
JKorf committed Dec 2, 2024
1 parent 6a78853 commit d32ad4e
Show file tree
Hide file tree
Showing 18 changed files with 109 additions and 46 deletions.
4 changes: 0 additions & 4 deletions CoinEx.Net/Clients/FuturesApi/CoinExRestClientFuturesApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ internal partial class CoinExRestClientFuturesApi : RestApiClient, ICoinExRestCl
public ICoinExRestClientFuturesApiTrading Trading { get; }
#endregion

internal readonly string _brokerId;

#region ctor
internal CoinExRestClientFuturesApi(ILogger logger, HttpClient? httpClient, CoinExRestOptions options) :
base(logger, httpClient, options.Environment.RestBaseAddress, options, options.FuturesOptions)
Expand All @@ -53,8 +51,6 @@ internal CoinExRestClientFuturesApi(ILogger logger, HttpClient? httpClient, Coin

ParameterPositions[HttpMethod.Delete] = HttpMethodParameterPosition.InUri;

_brokerId = !string.IsNullOrEmpty(options.BrokerId) ? options.BrokerId! : "147866029";

}
#endregion

Expand Down
25 changes: 20 additions & 5 deletions CoinEx.Net/Clients/FuturesApi/CoinExRestClientFuturesApiTrading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public async Task<WebCallResult<CoinExFuturesOrder>> PlaceOrderAsync(
SelfTradePreventionMode? stpMode = null,
CancellationToken ct = default)
{
clientOrderId ??= ExchangeHelpers.AppendRandomString("x-" + _baseClient._brokerId + "-", 32);

clientOrderId = LibraryHelpers.ApplyBrokerId(clientOrderId, CoinExExchange.ClientOrderId, 32, _baseClient.ClientOptions.AllowAppendingClientOrderId);
var parameters = new ParameterCollection()
{
{ "market", symbol }
Expand Down Expand Up @@ -64,7 +64,7 @@ public async Task<WebCallResult<CoinExStopId>> PlaceStopOrderAsync(
SelfTradePreventionMode? stpMode = null,
CancellationToken ct = default)
{
clientOrderId ??= ExchangeHelpers.AppendRandomString("x-" + _baseClient._brokerId + "-", 32);
clientOrderId = LibraryHelpers.ApplyBrokerId(clientOrderId, CoinExExchange.ClientOrderId, 32, _baseClient.ClientOptions.AllowAppendingClientOrderId);

var parameters = new ParameterCollection()
{
Expand All @@ -89,7 +89,7 @@ public async Task<WebCallResult<IEnumerable<CoinExBatchResult<CoinExFuturesOrder
CancellationToken ct = default)
{
foreach (var order in requests)
order.ClientOrderId ??= ExchangeHelpers.AppendRandomString("x-" + _baseClient._brokerId + "-", 32);
order.ClientOrderId = LibraryHelpers.ApplyBrokerId(order.ClientOrderId, CoinExExchange.ClientOrderId, 32, _baseClient.ClientOptions.AllowAppendingClientOrderId);

var parameters = new ParameterCollection()
{
Expand All @@ -104,7 +104,7 @@ public async Task<WebCallResult<IEnumerable<CoinExBatchResult<CoinExStopId>>>> P
CancellationToken ct = default)
{
foreach (var order in requests)
order.ClientOrderId ??= ExchangeHelpers.AppendRandomString("x-" + _baseClient._brokerId + "-", 32);
order.ClientOrderId = LibraryHelpers.ApplyBrokerId(order.ClientOrderId, CoinExExchange.ClientOrderId, 32, _baseClient.ClientOptions.AllowAppendingClientOrderId);

var parameters = new ParameterCollection()
{
Expand All @@ -126,6 +126,9 @@ public async Task<WebCallResult<CoinExFuturesOrder>> GetOrderAsync(string symbol
/// <inheritdoc />
public async Task<WebCallResult<CoinExPaginated<CoinExFuturesOrder>>> GetOpenOrdersAsync(string? symbol = null, OrderSide? side = null, string? clientOrderId = null, int? page = null, int? pageSize = null, CancellationToken ct = default)
{
if (clientOrderId != null)
clientOrderId = LibraryHelpers.ApplyBrokerId(clientOrderId, CoinExExchange.ClientOrderId, 32, _baseClient.ClientOptions.AllowAppendingClientOrderId);

var parameters = new ParameterCollection();
parameters.AddEnum("market_type", AccountType.Futures);
parameters.AddOptionalEnum("side", side);
Expand All @@ -152,6 +155,9 @@ public async Task<WebCallResult<CoinExPaginated<CoinExFuturesOrder>>> GetClosedO
/// <inheritdoc />
public async Task<WebCallResult<CoinExPaginated<CoinExStopOrder>>> GetOpenStopOrdersAsync(string? symbol = null, OrderSide? side = null, string? clientOrderId = null, int? page = null, int? pageSize = null, CancellationToken ct = default)
{
if (clientOrderId != null)
clientOrderId = LibraryHelpers.ApplyBrokerId(clientOrderId, CoinExExchange.ClientOrderId, 32, _baseClient.ClientOptions.AllowAppendingClientOrderId);

var parameters = new ParameterCollection();
parameters.AddEnum("market_type", AccountType.Futures);
parameters.AddOptionalEnum("side", side);
Expand All @@ -165,6 +171,9 @@ public async Task<WebCallResult<CoinExPaginated<CoinExStopOrder>>> GetOpenStopOr
/// <inheritdoc />
public async Task<WebCallResult<CoinExPaginated<CoinExStopOrder>>> GetClosedStopOrdersAsync(string? symbol = null, OrderSide? side = null, string? clientOrderId = null, int? page = null, int? pageSize = null, CancellationToken ct = default)
{
if (clientOrderId != null)
clientOrderId = LibraryHelpers.ApplyBrokerId(clientOrderId, CoinExExchange.ClientOrderId, 32, _baseClient.ClientOptions.AllowAppendingClientOrderId);

var parameters = new ParameterCollection();
parameters.AddEnum("market_type", AccountType.Futures);
parameters.AddOptionalEnum("side", side);
Expand Down Expand Up @@ -256,6 +265,8 @@ public async Task<WebCallResult<CoinExStopOrder>> CancelStopOrderAsync(string sy
/// <inheritdoc />
public async Task<WebCallResult<CoinExFuturesOrder>> CancelOrderByClientOrderIdAsync(string symbol, string clientOrderId, CancellationToken ct = default)
{
clientOrderId = LibraryHelpers.ApplyBrokerId(clientOrderId, CoinExExchange.ClientOrderId, 32, _baseClient.ClientOptions.AllowAppendingClientOrderId);

var parameters = new ParameterCollection()
{
{ "market", symbol },
Expand All @@ -269,6 +280,8 @@ public async Task<WebCallResult<CoinExFuturesOrder>> CancelOrderByClientOrderIdA
/// <inheritdoc />
public async Task<WebCallResult<CoinExStopOrder>> CancelStopOrderByClientOrderIdAsync(string symbol, string clientStopOrderId, CancellationToken ct = default)
{
clientStopOrderId = LibraryHelpers.ApplyBrokerId(clientStopOrderId, CoinExExchange.ClientOrderId, 32, _baseClient.ClientOptions.AllowAppendingClientOrderId);

var parameters = new ParameterCollection()
{
{ "market", symbol },
Expand Down Expand Up @@ -359,6 +372,8 @@ public async Task<WebCallResult<CoinExPaginated<CoinExPosition>>> GetPositionHis
/// <inheritdoc />
public async Task<WebCallResult<CoinExFuturesOrder>> ClosePositionAsync(string symbol, OrderTypeV2 orderType, decimal? price = null, decimal? quantity = null, string? clientOrderId = null, bool? hidden = null, CancellationToken ct = default)
{
clientOrderId = LibraryHelpers.ApplyBrokerId(clientOrderId, CoinExExchange.ClientOrderId, 32, _baseClient.ClientOptions.AllowAppendingClientOrderId);

var parameters = new ParameterCollection()
{
{ "market", symbol }
Expand Down
4 changes: 0 additions & 4 deletions CoinEx.Net/Clients/SpotApiV1/CoinExRestClientSpotApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ internal class CoinExRestClientSpotApi : RestApiClient, ICoinExRestClientSpotApi
public ICoinExRestClientSpotApiTrading Trading { get; }
#endregion

internal readonly string _brokerId;

#region ctor
internal CoinExRestClientSpotApi(ILogger logger, HttpClient? httpClient, CoinExRestOptions options) :
base(logger, httpClient, options.Environment.RestBaseAddress, options, options.SpotOptions)
Expand All @@ -64,8 +62,6 @@ internal CoinExRestClientSpotApi(ILogger logger, HttpClient? httpClient, CoinExR

ParameterPositions[HttpMethod.Delete] = HttpMethodParameterPosition.InUri;

_brokerId = !string.IsNullOrEmpty(options.BrokerId) ? options.BrokerId! : "147866029";

}
#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public async Task<WebCallResult<CoinExOrder>> PlaceOrderAsync(
endpoint = PlaceImmediateOrCancelOrderEndpoint;
}

clientOrderId ??= ExchangeHelpers.AppendRandomString("x-" + _baseClient._brokerId + "-", 32);
clientOrderId ??= ExchangeHelpers.AppendRandomString(CoinExExchange.ClientOrderId, 32);

var parameters = new Dictionary<string, object>
{
Expand Down
5 changes: 0 additions & 5 deletions CoinEx.Net/Clients/SpotApiV2/CoinExRestClientSpotApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ internal partial class CoinExRestClientSpotApi : RestApiClient, ICoinExRestClien
public ICoinExRestClientSpotApiTrading Trading { get; }
#endregion

internal readonly string _brokerId;

#region ctor
internal CoinExRestClientSpotApi(ILogger logger, HttpClient? httpClient, CoinExRestOptions options) :
base(logger, httpClient, options.Environment.RestBaseAddress, options, options.SpotOptions)
Expand All @@ -66,9 +64,6 @@ internal CoinExRestClientSpotApi(ILogger logger, HttpClient? httpClient, CoinExR
Trading = new CoinExRestClientSpotApiTrading(this);

ParameterPositions[HttpMethod.Delete] = HttpMethodParameterPosition.InUri;

_brokerId = !string.IsNullOrEmpty(options.BrokerId) ? options.BrokerId! : "147866029";

}
#endregion

Expand Down
38 changes: 28 additions & 10 deletions CoinEx.Net/Clients/SpotApiV2/CoinExRestClientSpotApiTrading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public async Task<WebCallResult<CoinExOrder>> PlaceOrderAsync(
SelfTradePreventionMode? stpMode = null,
CancellationToken ct = default)
{
clientOrderId ??= ExchangeHelpers.AppendRandomString("x-" + _baseClient._brokerId + "-", 32);
clientOrderId = LibraryHelpers.ApplyBrokerId(clientOrderId, CoinExExchange.ClientOrderId, 32, _baseClient.ClientOptions.AllowAppendingClientOrderId);

var parameters = new ParameterCollection()
{
Expand Down Expand Up @@ -72,7 +72,7 @@ public async Task<WebCallResult<CoinExStopId>> PlaceStopOrderAsync(
SelfTradePreventionMode? stpMode = null,
CancellationToken ct = default)
{
clientOrderId ??= ExchangeHelpers.AppendRandomString("x-" + _baseClient._brokerId + "-", 32);
clientOrderId = LibraryHelpers.ApplyBrokerId(clientOrderId, CoinExExchange.ClientOrderId, 32, _baseClient.ClientOptions.AllowAppendingClientOrderId);

var parameters = new ParameterCollection()
{
Expand Down Expand Up @@ -100,7 +100,7 @@ public async Task<WebCallResult<IEnumerable<CoinExBatchOrderResult>>> PlaceMulti
CancellationToken ct = default)
{
foreach(var order in requests)
order.ClientOrderId ??= ExchangeHelpers.AppendRandomString("x-" + _baseClient._brokerId + "-", 32);
order.ClientOrderId = LibraryHelpers.ApplyBrokerId(order.ClientOrderId, CoinExExchange.ClientOrderId, 32, _baseClient.ClientOptions.AllowAppendingClientOrderId);

var parameters = new ParameterCollection()
{
Expand All @@ -121,7 +121,7 @@ public async Task<WebCallResult<IEnumerable<CoinExBatchResult<CoinExStopId>>>> P
CancellationToken ct = default)
{
foreach (var order in requests)
order.ClientOrderId ??= ExchangeHelpers.AppendRandomString("x-" + _baseClient._brokerId + "-", 32);
order.ClientOrderId = LibraryHelpers.ApplyBrokerId(order.ClientOrderId, CoinExExchange.ClientOrderId, 32, _baseClient.ClientOptions.AllowAppendingClientOrderId);

var parameters = new ParameterCollection()
{
Expand Down Expand Up @@ -176,6 +176,9 @@ public async Task<WebCallResult<CoinExPaginated<CoinExOrder>>> GetClosedOrdersAs
/// <inheritdoc />
public async Task<WebCallResult<CoinExPaginated<CoinExStopOrder>>> GetOpenStopOrdersAsync(AccountType accountType, string? symbol = null, OrderSide? side = null, string? clientOrderId = null, int? page = null, int? pageSize = null, CancellationToken ct = default)
{
if (clientOrderId != null)
clientOrderId = LibraryHelpers.ApplyBrokerId(clientOrderId, CoinExExchange.ClientOrderId, 32, _baseClient.ClientOptions.AllowAppendingClientOrderId);

var parameters = new ParameterCollection();
parameters.AddEnum("market_type", accountType);
parameters.AddOptionalEnum("side", side);
Expand All @@ -189,6 +192,9 @@ public async Task<WebCallResult<CoinExPaginated<CoinExStopOrder>>> GetOpenStopOr
/// <inheritdoc />
public async Task<WebCallResult<CoinExPaginated<CoinExStopOrder>>> GetClosedStopOrdersAsync(AccountType accountType, string? symbol = null, OrderSide? side = null, string? clientOrderId = null, int? page = null, int? pageSize = null, CancellationToken ct = default)
{
if (clientOrderId != null)
clientOrderId = LibraryHelpers.ApplyBrokerId(clientOrderId, CoinExExchange.ClientOrderId, 32, _baseClient.ClientOptions.AllowAppendingClientOrderId);

var parameters = new ParameterCollection();
parameters.AddEnum("market_type", accountType);
parameters.AddOptionalEnum("side", side);
Expand Down Expand Up @@ -301,32 +307,44 @@ public async Task<WebCallResult<CoinExStopOrder>> CancelStopOrderAsync(string sy
}

/// <inheritdoc />
public async Task<WebCallResult<CoinExOrder>> CancelOrderByClientOrderIdAsync(string symbol, AccountType accountType, string clientOrderId, CancellationToken ct = default)
public async Task<WebCallResult<IEnumerable<CoinExOrder>>> CancelOrdersByClientOrderIdAsync(string symbol, AccountType accountType, string clientOrderId, CancellationToken ct = default)
{
clientOrderId = LibraryHelpers.ApplyBrokerId(clientOrderId, CoinExExchange.ClientOrderId, 32, _baseClient.ClientOptions.AllowAppendingClientOrderId);

var parameters = new ParameterCollection()
{
{ "market", symbol },
{ "client_id", clientOrderId }
};
parameters.AddEnum("market_type", accountType);
var result = await _baseClient.ExecuteAsync<CoinExOrder>(_baseClient.GetUri("v2/spot/cancel-order-by-client-id"), HttpMethod.Post, ct, parameters, true).ConfigureAwait(false);
var result = await _baseClient.ExecuteAsync<IEnumerable<CoinExOrder>>(_baseClient.GetUri("v2/spot/cancel-order-by-client-id"), HttpMethod.Post, ct, parameters, true).ConfigureAwait(false);
if (result)
_baseClient.InvokeOrderCanceled(new CryptoExchange.Net.CommonObjects.OrderId { Id = result.Data.Id.ToString(), SourceObject = result.Data });
{
foreach(var order in result.Data)
_baseClient.InvokeOrderCanceled(new CryptoExchange.Net.CommonObjects.OrderId { Id = order.Id.ToString(), SourceObject = result.Data });
}

return result;
}

/// <inheritdoc />
public async Task<WebCallResult<CoinExStopOrder>> CancelStopOrderByClientOrderIdAsync(string symbol, AccountType accountType, string clientStopOrderId, CancellationToken ct = default)
public async Task<WebCallResult<IEnumerable<CoinExStopOrder>>> CancelStopOrdersByClientOrderIdAsync(string symbol, AccountType accountType, string clientStopOrderId, CancellationToken ct = default)
{
clientStopOrderId = LibraryHelpers.ApplyBrokerId(clientStopOrderId, CoinExExchange.ClientOrderId, 32, _baseClient.ClientOptions.AllowAppendingClientOrderId);

var parameters = new ParameterCollection()
{
{ "market", symbol },
{ "client_id", clientStopOrderId }
};
parameters.AddEnum("market_type", accountType);
var result = await _baseClient.ExecuteAsync<CoinExStopOrder>(_baseClient.GetUri("v2/spot/cancel-stop-order-by-client-id"), HttpMethod.Post, ct, parameters, true).ConfigureAwait(false);
var result = await _baseClient.ExecuteAsync<IEnumerable<CoinExStopOrder>>(_baseClient.GetUri("v2/spot/cancel-stop-order-by-client-id"), HttpMethod.Post, ct, parameters, true).ConfigureAwait(false);
if (result)
_baseClient.InvokeOrderCanceled(new CryptoExchange.Net.CommonObjects.OrderId { Id = result.Data.StopOrderId.ToString(), SourceObject = result.Data });
{
foreach(var order in result.Data)
_baseClient.InvokeOrderCanceled(new CryptoExchange.Net.CommonObjects.OrderId { Id = order.StopOrderId.ToString(), SourceObject = result.Data });
}

return result;
}

Expand Down
4 changes: 2 additions & 2 deletions CoinEx.Net/CoinEx.Net.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<LangVersion>10.0</LangVersion>
<LangVersion>12.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
Expand Down Expand Up @@ -48,7 +48,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="CryptoExchange.Net" Version="8.4.0" />
<PackageReference Include="CryptoExchange.Net" Version="8.4.2" />
<PackageReference Include="Crc32.NET" Version="1.2.0" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="9.0.0">
<PrivateAssets>all</PrivateAssets>
Expand Down
Loading

0 comments on commit d32ad4e

Please sign in to comment.