From 5b9fc54ea618dff833fbbded1031121d05b5c4e7 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Wed, 26 Feb 2025 18:35:41 +0000 Subject: [PATCH] fix: Reject orders w/ invalid TimeInForce at binance Do this internally (before sending them to the exchange) --- .../spots/execution_gateway.cpp | 16 ++++++++++++++++ .../binance_execution/type_converters.h | 5 ++++- .../usds_futures/execution_gateway.cpp | 15 +++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/lib_binance_execution/binance_execution/spots/execution_gateway.cpp b/src/lib_binance_execution/binance_execution/spots/execution_gateway.cpp index 0bed1813c..72961ed1e 100644 --- a/src/lib_binance_execution/binance_execution/spots/execution_gateway.cpp +++ b/src/lib_binance_execution/binance_execution/spots/execution_gateway.cpp @@ -1,4 +1,5 @@ #include "execution_gateway.h" +#include "binance_execution/enums/time_in_force.h" #include "binance_execution/rest_api_result.h" #include "binance_execution/spots/messages/modify_order_request.h" #include "binance_execution/spots/messages/order_record.h" @@ -127,6 +128,20 @@ static void validate(bts::api::Side side, string &error) { } } +static void validate(bts::api::orderman::TimeInForce bts_tif, string &error) { + const auto tif = convert_time_in_force(bts_tif); + switch (tif.value) { + case TimeInForce::GTC: + case TimeInForce::IOC: + case TimeInForce::FOK: + break; + default: + append_error( + error, fmt::format("invalid TimeInForce '{}'.", bts_tif).c_str()); + break; + } +} + // TODO: Store exch order id in EMNativeOrder (until then, it'll always be 0). // We can ignore this for now and use "orig client id" in cancel/replace requests. static constexpr const bool VALIDATE_EXCH_ORDER_ID = false; @@ -137,6 +152,7 @@ static bool validate(const CreateOrderMsg &m, string &error) { validate(m.key, error); validate(m.quantity, error); validate(m.side, error); + validate(m.time_in_force, error); return error.size() == orig_sz; } diff --git a/src/lib_binance_execution/binance_execution/type_converters.h b/src/lib_binance_execution/binance_execution/type_converters.h index 725ecddb8..e2da85ff2 100644 --- a/src/lib_binance_execution/binance_execution/type_converters.h +++ b/src/lib_binance_execution/binance_execution/type_converters.h @@ -142,6 +142,8 @@ inline bts::api::orderman::TimeInForce convert_time_in_force(TimeInForce tif) { switch (tif.value) { case TimeInForce::GTC: return bts::api::orderman::TIME_IN_FORCE_GTC; + case TimeInForce::GTD: + return bts::api::orderman::TIME_IN_FORCE_DAY; case TimeInForce::IOC: case TimeInForce::FOK: return bts::api::orderman::TIME_IN_FORCE_IOC; @@ -202,8 +204,9 @@ inline OrderType convert_order_type(bts::api::orderman::NativeOrderType tif) { inline TimeInForce convert_time_in_force(bts::api::orderman::TimeInForce tif) { switch (tif) { case bts::api::orderman::TIME_IN_FORCE_GTC: - case bts::api::orderman::TIME_IN_FORCE_DAY: return TimeInForce::GTC; + case bts::api::orderman::TIME_IN_FORCE_DAY: + return TimeInForce::GTD; case bts::api::orderman::TIME_IN_FORCE_IOC: return TimeInForce::IOC; case bts::api::orderman::TIME_IN_FORCE_UNDEFINED: diff --git a/src/lib_binance_execution/binance_execution/usds_futures/execution_gateway.cpp b/src/lib_binance_execution/binance_execution/usds_futures/execution_gateway.cpp index 4dcf77e51..ba12f594e 100644 --- a/src/lib_binance_execution/binance_execution/usds_futures/execution_gateway.cpp +++ b/src/lib_binance_execution/binance_execution/usds_futures/execution_gateway.cpp @@ -126,6 +126,20 @@ static void validate(bts::api::Side side, string &error) { } } +static void validate(bts::api::orderman::TimeInForce bts_tif, string &error) { + const auto tif = convert_time_in_force(bts_tif); + switch (tif.value) { + case TimeInForce::GTC: + case TimeInForce::IOC: + case TimeInForce::FOK: + break; + default: + append_error( + error, fmt::format("invalid TimeInForce '{}'.", bts_tif).c_str()); + break; + } +} + // TODO: Store exch order id in EMNativeOrder (until then, it'll always be 0). // We can ignore this for now and use "orig client id" in cancel/replace requests. static constexpr const bool VALIDATE_EXCH_ORDER_ID = false; @@ -136,6 +150,7 @@ static bool validate(const CreateOrderMsg &m, string &error) { validate(m.key, error); validate(m.quantity, error); validate(m.side, error); + validate(m.time_in_force, error); return error.size() == orig_sz; }