Skip to content

Commit

Permalink
Add clientOrderId parameter to CancelOrderAsync() (spot market) (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
alokym86 authored Oct 17, 2024
1 parent 957d5b5 commit 9e5596d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 12 deletions.
6 changes: 4 additions & 2 deletions GateIo.Net/Clients/SpotApi/GateIoRestClientSpotApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,15 @@ async Task<WebCallResult<IEnumerable<Order>>> IBaseRestClient.GetClosedOrdersAsy

async Task<WebCallResult<OrderId>> IBaseRestClient.CancelOrderAsync(string orderId, string? symbol, CancellationToken ct)
{
if (!long.TryParse(orderId, out var id))
var clientOrderId = orderId.StartsWith("t-") ? orderId : null;

if (!long.TryParse(orderId, out var id) && clientOrderId is null)
throw new ArgumentException("Order id invalid", nameof(orderId));

if (string.IsNullOrWhiteSpace(symbol))
throw new ArgumentException(nameof(symbol) + " required for Gate.io " + nameof(ISpotClient.CancelOrderAsync), nameof(symbol));

var order = await Trading.CancelOrderAsync(symbol!, id, ct: ct).ConfigureAwait(false);
var order = await Trading.CancelOrderAsync(symbol!, orderId: id == 0 ? null : id, clientOrderId: clientOrderId, ct: ct).ConfigureAwait(false);
if (!order)
return order.As<OrderId>(null);

Expand Down
6 changes: 4 additions & 2 deletions GateIo.Net/Clients/SpotApi/GateIoRestClientSpotApiShared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -431,10 +431,12 @@ async Task<ExchangeWebResult<SharedId>> ISpotOrderRestClient.CancelSpotOrderAsyn
if (validationError != null)
return new ExchangeWebResult<SharedId>(Exchange, validationError);

string? clientOrderId = null;

if (!long.TryParse(request.OrderId, out var orderId))
return new ExchangeWebResult<SharedId>(Exchange, new ArgumentError("Invalid order id"));
clientOrderId = $"t-{request.OrderId}";

var order = await Trading.CancelOrderAsync(request.Symbol.GetSymbol(FormatSymbol), orderId).ConfigureAwait(false);
var order = await Trading.CancelOrderAsync(request.Symbol.GetSymbol(FormatSymbol), orderId == 0 ? null : orderId, clientOrderId).ConfigureAwait(false);
if (!order)
return order.AsExchangeResult<SharedId>(Exchange, null, default);

Expand Down
7 changes: 5 additions & 2 deletions GateIo.Net/Clients/SpotApi/GateIoRestClientSpotApiTrading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,17 @@ public async Task<WebCallResult<IEnumerable<GateIoOrderOperation>>> EditMultiple
/// <inheritdoc />
public async Task<WebCallResult<GateIoOrder>> CancelOrderAsync(
string symbol,
long orderId,
long? orderId = null,
string? clientOrderId = null,
SpotAccountType? accountType = null,
CancellationToken ct = default)
{
var id = orderId?.ToString() ?? clientOrderId ?? throw new ArgumentException($"Either {nameof(orderId)} or {nameof(clientOrderId)} must be provided"); ;

var parameters = new ParameterCollection();
parameters.Add("currency_pair", symbol);
parameters.AddOptionalEnum("account", accountType);
var request = _definitions.GetOrCreate(HttpMethod.Delete, "/api/v4/spot/orders/" + orderId, GateIoExchange.RateLimiter.RestSpotOrderCancelation, 1, true);
var request = _definitions.GetOrCreate(HttpMethod.Delete, "/api/v4/spot/orders/" + id, GateIoExchange.RateLimiter.RestSpotOrderCancelation, 1, true);
var result = await _baseClient.SendAsync<GateIoOrder>(request, parameters, ct).ConfigureAwait(false);
if (result)
_baseClient.InvokeOrderCanceled(new CryptoExchange.Net.CommonObjects.OrderId
Expand Down
8 changes: 6 additions & 2 deletions GateIo.Net/Clients/SpotApi/GateIoSocketClientSpotApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,16 @@ public async Task<CallResult<GateIoOrder>> EditOrderAsync(string symbol,
}

/// <inheritdoc />
public async Task<CallResult<GateIoOrder>> CancelOrderAsync(string symbol, long orderId, SpotAccountType? accountType = null, CancellationToken ct = default)
public async Task<CallResult<GateIoOrder>> CancelOrderAsync(string symbol,
long? orderId = null,
string? clientOrderId = null,
SpotAccountType? accountType = null,
CancellationToken ct = default)
{
var id = ExchangeHelpers.NextId();
var query = new GateIoRequestQuery<GateIoSpotGetOrderRequest, GateIoOrder>(id, "spot.order_cancel", "api", new GateIoSpotGetOrderRequest
{
OrderId = orderId.ToString(),
OrderId = orderId?.ToString() ?? clientOrderId ?? throw new ArgumentException($"Either {nameof(orderId)} or {nameof(clientOrderId)} must be provided"),
Symbol = symbol,
AccountType = accountType
}, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,15 @@ Task<WebCallResult<GateIoOrder>> EditOrderAsync(
/// <para><a href="https://www.gate.io/docs/developers/apiv4/#cancel-a-single-order" /></para>
/// </summary>
/// <param name="symbol">Symbol of the order, for example `ETH_USDT`</param>
/// <param name="orderId">Order id</param>
/// <param name="orderId">Order id, either `orderId` or `clientOrderId` required</param>
/// <param name="clientOrderId">user custom ID (i.e., t-123c456f), either `orderId` or `clientOrderId` required</param>
/// <param name="accountType">Account type</param>
/// <param name="ct">Cancellation token</param>
/// <returns></returns>
Task<WebCallResult<GateIoOrder>> CancelOrderAsync(
string symbol,
long orderId,
long? orderId = null,
string? clientOrderId = null,
SpotAccountType? accountType = null,
CancellationToken ct = default);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,16 @@ Task<CallResult<GateIoOrder>> PlaceOrderAsync(string symbol,
/// <para><a href="https://www.gate.io/docs/developers/apiv4/ws/en/#order-cancel" /></para>
/// </summary>
/// <param name="symbol">Symbol, for example `ETH_USDT`</param>
/// <param name="orderId">Order id</param>
/// <param name="orderId">Order id, either `orderId` or `clientOrderId` required</param>
/// <param name="clientOrderId">user custom ID (i.e., t-123c456f), either `orderId` or `clientOrderId` required</param>
/// <param name="accountType">Account type</param>
/// <param name="ct">Cancellation token</param>
/// <returns></returns>
Task<CallResult<GateIoOrder>> CancelOrderAsync(string symbol, long orderId, SpotAccountType? accountType = null, CancellationToken ct = default);
Task<CallResult<GateIoOrder>> CancelOrderAsync(string symbol,
long? orderId,
string? clientOrderId = null,
SpotAccountType? accountType = null,
CancellationToken ct = default);

/// <summary>
/// Cancel multiple orders
Expand Down

0 comments on commit 9e5596d

Please sign in to comment.