From d010ba7ae7740f1254c660dc6d072dc0bbfd29fe Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 3 Mar 2025 15:05:06 +0000 Subject: [PATCH] refactor: Abstract out common binance gw code For all sub-venues to use --- ...pots_gateway_config.h => gateway_config.h} | 8 +- .../listen_key_requestor.cpp | 25 +- .../binance_execution/listen_key_requestor.h | 36 +++ .../messages/listen_key_request.h | 12 + .../messages/listen_key_response.h | 7 +- .../{spots => }/order_key_map.h | 4 +- .../spots/messages/cancel_order_request.cpp | 8 +- .../spots/messages/cancel_order_request.h | 6 +- .../spots/messages/create_order_request.cpp | 6 +- .../spots/messages/listen_key_request.h | 15 -- .../spots/messages/modify_order_request.cpp | 8 +- .../spots/messages/modify_order_request.h | 2 +- .../binance_execution/spots/rest_api_client.h | 4 +- .../binance_execution/spots/spots_gateway.cpp | 76 ++---- .../binance_execution/spots/spots_gateway.h | 8 +- .../spots/type_converters.cpp | 48 ---- .../binance_execution/spots/type_converters.h | 14 -- .../user_data_streams/listen_key_requestor.h | 30 --- .../binance_execution/type_validators.cpp | 69 ++++++ .../binance_execution/type_validators.h | 62 +++++ .../messages/cancel_order_request.cpp | 7 +- .../messages/create_order_request.cpp | 6 +- .../messages/listen_key_request.h | 15 -- .../messages/listen_key_response.h | 17 -- .../usds_futures/rest_api_client.h | 4 +- .../usds_futures/type_converters.cpp | 226 ------------------ .../usds_futures/type_converters.h | 63 ----- .../usds_futures/usds_futures_gateway.cpp | 69 +----- .../usds_futures/usds_futures_gateway.h | 7 +- .../usds_futures_gateway_config.h | 26 -- .../listen_key_requestor.cpp | 84 ------- .../user_data_streams/listen_key_requestor.h | 30 --- .../execution/execution_gateway_provider.cpp | 11 +- 33 files changed, 275 insertions(+), 738 deletions(-) rename src/lib_binance_execution/binance_execution/{spots/spots_gateway_config.h => gateway_config.h} (74%) rename src/lib_binance_execution/binance_execution/{spots/user_data_streams => }/listen_key_requestor.cpp (73%) create mode 100644 src/lib_binance_execution/binance_execution/listen_key_requestor.h create mode 100644 src/lib_binance_execution/binance_execution/messages/listen_key_request.h rename src/lib_binance_execution/binance_execution/{spots => }/messages/listen_key_response.h (51%) rename src/lib_binance_execution/binance_execution/{spots => }/order_key_map.h (96%) delete mode 100644 src/lib_binance_execution/binance_execution/spots/messages/listen_key_request.h delete mode 100644 src/lib_binance_execution/binance_execution/spots/type_converters.cpp delete mode 100644 src/lib_binance_execution/binance_execution/spots/type_converters.h delete mode 100644 src/lib_binance_execution/binance_execution/spots/user_data_streams/listen_key_requestor.h create mode 100644 src/lib_binance_execution/binance_execution/type_validators.cpp create mode 100644 src/lib_binance_execution/binance_execution/type_validators.h delete mode 100644 src/lib_binance_execution/binance_execution/usds_futures/messages/listen_key_request.h delete mode 100644 src/lib_binance_execution/binance_execution/usds_futures/messages/listen_key_response.h delete mode 100644 src/lib_binance_execution/binance_execution/usds_futures/type_converters.cpp delete mode 100644 src/lib_binance_execution/binance_execution/usds_futures/type_converters.h delete mode 100644 src/lib_binance_execution/binance_execution/usds_futures/usds_futures_gateway_config.h delete mode 100644 src/lib_binance_execution/binance_execution/usds_futures/user_data_streams/listen_key_requestor.cpp delete mode 100644 src/lib_binance_execution/binance_execution/usds_futures/user_data_streams/listen_key_requestor.h diff --git a/src/lib_binance_execution/binance_execution/spots/spots_gateway_config.h b/src/lib_binance_execution/binance_execution/gateway_config.h similarity index 74% rename from src/lib_binance_execution/binance_execution/spots/spots_gateway_config.h rename to src/lib_binance_execution/binance_execution/gateway_config.h index 52d60f070..f19d19842 100644 --- a/src/lib_binance_execution/binance_execution/spots/spots_gateway_config.h +++ b/src/lib_binance_execution/binance_execution/gateway_config.h @@ -3,9 +3,9 @@ #include #include -namespace binance_execution::spots { +namespace binance_execution { -struct SpotsGatewayConfig { +struct GatewayConfig { std::string api_key; std::string secret_key; std::string account; @@ -15,7 +15,7 @@ struct SpotsGatewayConfig { }; NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE( - SpotsGatewayConfig, + GatewayConfig, api_key, secret_key, account, @@ -23,4 +23,4 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE( ws_endpoint, ws_stream_endpoint); -} // namespace binance_execution::spots +} // namespace binance_execution diff --git a/src/lib_binance_execution/binance_execution/spots/user_data_streams/listen_key_requestor.cpp b/src/lib_binance_execution/binance_execution/listen_key_requestor.cpp similarity index 73% rename from src/lib_binance_execution/binance_execution/spots/user_data_streams/listen_key_requestor.cpp rename to src/lib_binance_execution/binance_execution/listen_key_requestor.cpp index e457373e8..bf8c1740e 100644 --- a/src/lib_binance_execution/binance_execution/spots/user_data_streams/listen_key_requestor.cpp +++ b/src/lib_binance_execution/binance_execution/listen_key_requestor.cpp @@ -1,12 +1,11 @@ #include "listen_key_requestor.h" -#include "binance_execution/spots/rest_api_client.h" #include "lib/core/TimeStamp.h" #include "lib/io/json_formatter.h" #include #include -namespace binance_execution::spots { +namespace binance_execution { using bts::core::Delegate; using bts::core::TimeStamp; @@ -16,16 +15,16 @@ using std::string; using std::thread; struct ListenKeyRequestor::PImpl { - RestApiClient rest_api_client; + std::function(const ListenKeyRequest &)> + get_listen_key; + std::function(const ListenKeyRequest &)> + refresh_listen_key; string listen_key; TimeStamp listen_key_timestamp; optional listen_key_thread; Delegate callback; bool is_running = false; - PImpl(RestApiClient rest_api_client) - : rest_api_client(std::move(rest_api_client)) {} - ~PImpl() { stop(); } void start() { @@ -47,7 +46,7 @@ struct ListenKeyRequestor::PImpl { bts::core::TimeSpan::from_seconds(60 * 30); const auto now = bts::core::TimeStamp::now(); if (listen_key.empty()) { - const auto &res = rest_api_client.get_listen_key(); + const auto &res = get_listen_key({}); if (!res.ok()) { continue; } @@ -59,7 +58,7 @@ struct ListenKeyRequestor::PImpl { callback(listen_key); } if ((now - listen_key_timestamp) > refresh_period) { - const auto &res = rest_api_client.refresh_listen_key(); + const auto &res = refresh_listen_key({}); bts_assert_msgf( res.ok(), "Failed to refresh listen key. {}", json_formatter(res)); @@ -70,8 +69,12 @@ struct ListenKeyRequestor::PImpl { } }; -ListenKeyRequestor::ListenKeyRequestor(RestApiClient rest_api_client) - : me_(std::make_unique(std::move(rest_api_client))) {} +ListenKeyRequestor::ListenKeyRequestor( + std::function(const ListenKeyRequest &)> + get_listen_key, + std::function(const ListenKeyRequest &)> + refresh_listen_key) + : me_(std::make_unique(get_listen_key, refresh_listen_key)) {} ListenKeyRequestor::~ListenKeyRequestor() = default; @@ -81,4 +84,4 @@ Delegate &ListenKeyRequestor::callback() { return me_->callback; } -} // namespace binance_execution::spots +} // namespace binance_execution diff --git a/src/lib_binance_execution/binance_execution/listen_key_requestor.h b/src/lib_binance_execution/binance_execution/listen_key_requestor.h new file mode 100644 index 000000000..b258fc4c5 --- /dev/null +++ b/src/lib_binance_execution/binance_execution/listen_key_requestor.h @@ -0,0 +1,36 @@ +#pragma once + +#include "binance_execution/messages/listen_key_request.h" +#include "binance_execution/messages/listen_key_response.h" +#include "binance_execution/rest_api_result.h" +#include "lib/utils/Delegate.h" + +#include +#include + +namespace binance_execution { + +/** + * Requests the listen key for user data stream subscriptions, and periodically + * refreshes it to keep subscriptions alive. + */ +class ListenKeyRequestor { + public: + explicit ListenKeyRequestor( + std::function(const ListenKeyRequest &)> + get_listen_key, + std::function(const ListenKeyRequest &)> + refresh_listen_key); + ~ListenKeyRequestor(); + + void start(); + void stop(); + + bts::core::Delegate &callback(); + + private: + struct PImpl; + std::unique_ptr me_; +}; + +} // namespace binance_execution diff --git a/src/lib_binance_execution/binance_execution/messages/listen_key_request.h b/src/lib_binance_execution/binance_execution/messages/listen_key_request.h new file mode 100644 index 000000000..f2c6d6338 --- /dev/null +++ b/src/lib_binance_execution/binance_execution/messages/listen_key_request.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +namespace binance_execution { + +struct ListenKeyRequest {}; + +inline void to_query_params( + std::string & /*qps*/, const ListenKeyRequest & /*req*/) {} + +} // namespace binance_execution diff --git a/src/lib_binance_execution/binance_execution/spots/messages/listen_key_response.h b/src/lib_binance_execution/binance_execution/messages/listen_key_response.h similarity index 51% rename from src/lib_binance_execution/binance_execution/spots/messages/listen_key_response.h rename to src/lib_binance_execution/binance_execution/messages/listen_key_response.h index faebd8f54..b67194cd7 100644 --- a/src/lib_binance_execution/binance_execution/spots/messages/listen_key_response.h +++ b/src/lib_binance_execution/binance_execution/messages/listen_key_response.h @@ -3,15 +3,12 @@ #include #include -namespace binance_execution::spots { +namespace binance_execution { -/** - * @a https://developers.binance.com/docs/derivatives/usds-margined-futures/user-data-streams - */ struct ListenKeyResponse { std::string listenKey; }; NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(ListenKeyResponse, listenKey); -} // namespace binance_execution::spots +} // namespace binance_execution diff --git a/src/lib_binance_execution/binance_execution/spots/order_key_map.h b/src/lib_binance_execution/binance_execution/order_key_map.h similarity index 96% rename from src/lib_binance_execution/binance_execution/spots/order_key_map.h rename to src/lib_binance_execution/binance_execution/order_key_map.h index ce2459c37..b9f1697a5 100644 --- a/src/lib_binance_execution/binance_execution/spots/order_key_map.h +++ b/src/lib_binance_execution/binance_execution/order_key_map.h @@ -7,7 +7,7 @@ #include #include -namespace binance_execution::spots { +namespace binance_execution { struct OrderKeyMap { enum KeyType { @@ -60,4 +60,4 @@ struct OrderKeyMap { std::mutex instance_mx; }; -} // namespace binance_execution::spots +} // namespace binance_execution diff --git a/src/lib_binance_execution/binance_execution/spots/messages/cancel_order_request.cpp b/src/lib_binance_execution/binance_execution/spots/messages/cancel_order_request.cpp index 56828f5cf..3abed8fd6 100644 --- a/src/lib_binance_execution/binance_execution/spots/messages/cancel_order_request.cpp +++ b/src/lib_binance_execution/binance_execution/spots/messages/cancel_order_request.cpp @@ -1,8 +1,8 @@ #include "cancel_order_request.h" #include "binance_execution/error_list.h" -#include "binance_execution/spots/order_key_map.h" -#include "binance_execution/spots/type_converters.h" +#include "binance_execution/order_key_map.h" #include "binance_execution/type_converters.h" +#include "binance_execution/type_validators.h" #include "lib/core/TimeStamp.h" #include "lib/orderman/CancelOrderMsg.h" #include "lib/utils/checked_arithmetic.h" @@ -14,7 +14,9 @@ bool CancelOrderRequest::init( OrderKeyMap &key_map, std::string &error) { const std::size_t orig_error_sz = error.size(); - convert_sec_name(m.security, symbol, error); + if (validate(m.security, bts::api::VENUE_BINANCE, error)) { + symbol = m.security->binance().security_desc(); + } orderId = bts::system::math::checked_ultol(m.exchange_order_id.value()); origClientOrderId = key_map.get_exch_key(m.key); // The exchange accepts either orderId, or origClientOrderId (only one diff --git a/src/lib_binance_execution/binance_execution/spots/messages/cancel_order_request.h b/src/lib_binance_execution/binance_execution/spots/messages/cancel_order_request.h index 3edd57fe3..b2856c628 100644 --- a/src/lib_binance_execution/binance_execution/spots/messages/cancel_order_request.h +++ b/src/lib_binance_execution/binance_execution/spots/messages/cancel_order_request.h @@ -8,9 +8,11 @@ namespace bts::orderman { struct CancelOrderMsg; } -namespace binance_execution::spots { - +namespace binance_execution { struct OrderKeyMap; +} + +namespace binance_execution::spots { /** * @a https://developers.binance.com/docs/binance-spot-api-docs/rest-api/trading-endpoints#cancel-order-trade diff --git a/src/lib_binance_execution/binance_execution/spots/messages/create_order_request.cpp b/src/lib_binance_execution/binance_execution/spots/messages/create_order_request.cpp index b5a9292e0..dd0a8b044 100644 --- a/src/lib_binance_execution/binance_execution/spots/messages/create_order_request.cpp +++ b/src/lib_binance_execution/binance_execution/spots/messages/create_order_request.cpp @@ -2,8 +2,8 @@ #include "binance_execution/enums/new_order_response_type.h" #include "binance_execution/price.h" #include "binance_execution/quantity.h" -#include "binance_execution/spots/type_converters.h" #include "binance_execution/type_converters.h" +#include "binance_execution/type_validators.h" #include "lib/orderman/CreateOrderMsg.h" namespace binance_execution::spots { @@ -12,7 +12,9 @@ bool CreateOrderRequest::init( const bts::orderman::CreateOrderMsg &m, std::string &error) { const std::size_t orig_error_sz = error.size(); newClientOrderId = std::to_string(m.key.value()); - convert_sec_name(m.security, symbol, error); + if (validate(m.security, bts::api::VENUE_BINANCE, error)) { + symbol = m.security->binance().security_desc(); + } convert_side(m.side, side, error); price = m.price.value(); quantity = m.quantity.full_value(); diff --git a/src/lib_binance_execution/binance_execution/spots/messages/listen_key_request.h b/src/lib_binance_execution/binance_execution/spots/messages/listen_key_request.h deleted file mode 100644 index ba4643cdd..000000000 --- a/src/lib_binance_execution/binance_execution/spots/messages/listen_key_request.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include - -namespace binance_execution::spots { - -/** - * @a https://developers.binance.com/docs/derivatives/usds-margined-futures/user-data-streams - */ -struct ListenKeyRequest {}; - -inline void to_query_params( - std::string & /*qps*/, const ListenKeyRequest & /*req*/) {} - -} // namespace binance_execution::spots diff --git a/src/lib_binance_execution/binance_execution/spots/messages/modify_order_request.cpp b/src/lib_binance_execution/binance_execution/spots/messages/modify_order_request.cpp index 63c4580bb..69a68da84 100644 --- a/src/lib_binance_execution/binance_execution/spots/messages/modify_order_request.cpp +++ b/src/lib_binance_execution/binance_execution/spots/messages/modify_order_request.cpp @@ -2,11 +2,11 @@ #include "binance_execution/enums/new_order_response_type.h" #include "binance_execution/enums/time_in_force.h" #include "binance_execution/error_list.h" +#include "binance_execution/order_key_map.h" #include "binance_execution/price.h" #include "binance_execution/quantity.h" -#include "binance_execution/spots/order_key_map.h" -#include "binance_execution/spots/type_converters.h" #include "binance_execution/type_converters.h" +#include "binance_execution/type_validators.h" #include "lib/io/JsonReader.h" #include "lib/orderman/CancelReplaceOrderMsg.h" @@ -202,7 +202,9 @@ bool ModifyOrderRequest::init( if (quantity == 0) { ErrorList::append(error, "quantity is 0"); } - convert_sec_name(m.create_fields.security, symbol, error); + if (validate(m.create_fields.security, bts::api::VENUE_BINANCE, error)) { + symbol = m.create_fields.security->binance().security_desc(); + } convert_side(m.create_fields.side, side, error); convert_order_type(m.create_fields.order_type, type, error); cancelReplaceMode = CancelReplaceMode::STOP_ON_FAILURE; diff --git a/src/lib_binance_execution/binance_execution/spots/messages/modify_order_request.h b/src/lib_binance_execution/binance_execution/spots/messages/modify_order_request.h index b1be30af4..8489c75dd 100644 --- a/src/lib_binance_execution/binance_execution/spots/messages/modify_order_request.h +++ b/src/lib_binance_execution/binance_execution/spots/messages/modify_order_request.h @@ -8,7 +8,7 @@ #include "binance_execution/enums/self_trade_prevention_mode.h" #include "binance_execution/enums/side.h" #include "binance_execution/enums/time_in_force.h" -#include "binance_execution/spots/order_key_map.h" +#include "binance_execution/order_key_map.h" #include "lib/orderman/CancelReplaceOrderMsg.h" #include diff --git a/src/lib_binance_execution/binance_execution/spots/rest_api_client.h b/src/lib_binance_execution/binance_execution/spots/rest_api_client.h index a85ccce72..ebac7b78a 100644 --- a/src/lib_binance_execution/binance_execution/spots/rest_api_client.h +++ b/src/lib_binance_execution/binance_execution/spots/rest_api_client.h @@ -1,11 +1,11 @@ #pragma once +#include "binance_execution/messages/listen_key_request.h" +#include "binance_execution/messages/listen_key_response.h" #include "binance_execution/rest_api_result.h" #include "binance_execution/spots/messages/all_orders_request.h" #include "binance_execution/spots/messages/cancel_order_request.h" #include "binance_execution/spots/messages/create_order_request.h" -#include "binance_execution/spots/messages/listen_key_request.h" -#include "binance_execution/spots/messages/listen_key_response.h" #include "binance_execution/spots/messages/modify_order_request.h" #include "binance_execution/spots/messages/modify_order_response.h" #include "binance_execution/spots/messages/order_record.h" diff --git a/src/lib_binance_execution/binance_execution/spots/spots_gateway.cpp b/src/lib_binance_execution/binance_execution/spots/spots_gateway.cpp index cd4d4ca85..70cdd366b 100644 --- a/src/lib_binance_execution/binance_execution/spots/spots_gateway.cpp +++ b/src/lib_binance_execution/binance_execution/spots/spots_gateway.cpp @@ -1,19 +1,17 @@ #include "spots_gateway.h" #include "binance_execution/execution_report_store.h" +#include "binance_execution/gateway_config.h" +#include "binance_execution/listen_key_requestor.h" +#include "binance_execution/order_key_map.h" #include "binance_execution/rest_api_result.h" #include "binance_execution/spots/messages/modify_order_request.h" -#include "binance_execution/spots/order_key_map.h" #include "binance_execution/spots/rest_api_client.h" -#include "binance_execution/spots/spots_gateway_config.h" -#include "binance_execution/spots/type_converters.h" -#include "binance_execution/spots/user_data_streams/listen_key_requestor.h" #include "binance_execution/spots/user_data_streams/user_data_subscription.h" -#include "binance_execution/type_converters.h" +#include "binance_execution/type_validators.h" #include "lib/api/service_helpers/AdminServiceUtils.h" #include "lib/api/services/AdminService.pb.h" #include "lib/api/services/OrderService.h" #include "lib/config/protos/jetfix.pb.h" -#include "lib/core/TimeStamp.h" #include "lib/domain_model/Security.h" #include "lib/domain_model/Security.pb.h" #include "lib/domain_model/Side.h" @@ -75,55 +73,6 @@ using std::string; using std::uint32_t; using std::vector; -static void validate(bts::orderman::OrderKey key, string &error) { - if (key.value() == 0) { - ErrorList::append(error, "order key is 0"); - } -} - -static void validate(bts::orderman::ExchangeOrderID64 key, string &error) { - if (key.value() == 0) { - ErrorList::append(error, "exch order key is 0"); - } -} - -static void validate(bts::orderman::ExchangeOrderIDVar key, string &error) { - if (std::holds_alternative(key)) { - validate(std::get(key), error); - } else { - ErrorList::append(error, "exch order id is undefined or not an integer"); - } -} - -static void validate(bts::Quantity qty, string &error) { - if (qty.full_value() == 0) { - ErrorList::append(error, "quantity is 0"); - } -} - -static void validate(const bts::api::Security *sec, string &error) { - if (sec == nullptr) { - ErrorList::append(error, "security is null"); - } else if (!sec->has_binance()) { - ErrorList::append(error, "security not of Binance"); - } -} - -static bool validate(const bts::orderman::ExecutionReport &er, string &error) { - const size_t orig_sz = error.size(); - validate(er.order_key, error); - validate(er.quantity, error); - validate(er.exchange_order_id, error); - if (er.account_name.value().empty()) { - ErrorList::append(error, "account is empty"); - } - validate(er.security, error); - if (er.cfg == nullptr) { - ErrorList::append(error, "jetfix config is null"); - } - return error.size() == orig_sz; -} - struct ExecutionGatewayCallbacks { bts::orderman::OnExeReport exe_report; bts::orderman::OnGwStatusChange gw_status_change; @@ -135,7 +84,7 @@ struct ExecutionGatewayCallbacks { struct SpotsGateway::PImpl { explicit PImpl( bts::md::SecurityOracle sec_oracle, - SpotsGatewayConfig config, + GatewayConfig config, bts::config::JetFIX_Connection jetfix_connection) : sec_oracle(std::move(sec_oracle)), config(std::move(config)), jetfix_connection(std::make_shared( @@ -144,7 +93,13 @@ struct SpotsGateway::PImpl { this->config.rest_endpoint, this->config.api_key, this->config.secret_key), - listen_key_requestor(this->rest_api_client), + listen_key_requestor( + [this](const auto &req) { + return rest_api_client.get_listen_key(req); + }, + [this](const auto &req) { + return rest_api_client.refresh_listen_key(req); + }), key_map(std::make_shared()), orders( [this](const std::string &exch_key) { @@ -160,7 +115,7 @@ struct SpotsGateway::PImpl { static constexpr const char *CANCEL_REPLACE_CANCEL_SUFFIX = "crc"; bts::md::SecurityOracle sec_oracle; - SpotsGatewayConfig config; + GatewayConfig config; std::shared_ptr jetfix_connection; RestApiClient rest_api_client; @@ -441,7 +396,7 @@ struct SpotsGateway::PImpl { SpotsGateway::SpotsGateway( bts::md::SecurityOracle sec_oracle, - SpotsGatewayConfig session_cfg, + GatewayConfig session_cfg, bts::config::JetFIX::Connection jetfix_connection) : me_(std::make_unique( std::move(sec_oracle), @@ -505,8 +460,7 @@ void SpotsGateway::send_msgs( } } -void SpotsGateway::send_fix( - const bts::jetfix::OutFIXPtr &, bts::trace::CorID) { +void SpotsGateway::send_fix(const bts::jetfix::OutFIXPtr &, bts::trace::CorID) { bts_assert_msg(false, "ExecutionGateway does not support fix"); } diff --git a/src/lib_binance_execution/binance_execution/spots/spots_gateway.h b/src/lib_binance_execution/binance_execution/spots/spots_gateway.h index 153e951ad..71e77105a 100644 --- a/src/lib_binance_execution/binance_execution/spots/spots_gateway.h +++ b/src/lib_binance_execution/binance_execution/spots/spots_gateway.h @@ -12,15 +12,17 @@ namespace bts::md { class SecurityOracle; } -namespace binance_execution::spots { +namespace binance_execution { +struct GatewayConfig; +} -struct SpotsGatewayConfig; +namespace binance_execution::spots { class SpotsGateway : public bts::orderman::IExecutionGateway { public: explicit SpotsGateway( bts::md::SecurityOracle sec_oracle, - SpotsGatewayConfig session_cfg, + GatewayConfig session_cfg, bts::config::JetFIX_Connection jetfix_connection); ~SpotsGateway() override; diff --git a/src/lib_binance_execution/binance_execution/spots/type_converters.cpp b/src/lib_binance_execution/binance_execution/spots/type_converters.cpp deleted file mode 100644 index 8d313ee2b..000000000 --- a/src/lib_binance_execution/binance_execution/spots/type_converters.cpp +++ /dev/null @@ -1,48 +0,0 @@ -#include "type_converters.h" -#include "binance_execution/error_list.h" -#include "binance_execution/spots/messages/cancel_order_request.h" -#include "binance_execution/spots/messages/create_order_request.h" -#include "binance_execution/spots/messages/modify_order_request.h" -#include "binance_execution/spots/messages/order_record.h" -#include "binance_execution/spots/order_key_map.h" -#include "binance_execution/type_converters.h" -#include "lib/api/services/AdminService.pb.h" -#include "lib/domain_model/Security.pb.h" -#include "lib/domain_model/common.h" -#include "lib/orderman/CancelOrderMsg.h" -#include "lib/orderman/CancelReplaceOrderMsg.h" -#include "lib/orderman/CreateOrderMsg.h" -#include "lib/utils/checked_arithmetic.h" -#include "lib/utils/string_utils.h" -#include -#include -#include - -namespace binance_execution::spots { - -using bts::system::string_utils::copy_to_array; -using bts::system::string_utils::parse_uint; -using std::string; -using std::vector; - -bool validate_security(const bts::api::Security *sec, string &error) { - if (sec == nullptr) { - ErrorList::append(error, "security is null"); - } else if (!sec->has_binance()) { - ErrorList::append(error, "security not of Binance (spots)"); - } else { - return true; - } - return false; -} - -bool convert_sec_name( - const bts::api::Security *sec, std::string &name, std::string &error) { - if (!validate_security(sec, error)) { - return false; - } - name = sec->binance().security_desc(); - return true; -} - -} // namespace binance_execution::spots diff --git a/src/lib_binance_execution/binance_execution/spots/type_converters.h b/src/lib_binance_execution/binance_execution/spots/type_converters.h deleted file mode 100644 index 6ff2a8378..000000000 --- a/src/lib_binance_execution/binance_execution/spots/type_converters.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#include "lib/domain_model/Security.pb.h" - -#include - -namespace binance_execution::spots { - -bool validate_security(const bts::api::Security *sec, std::string &error); - -bool convert_sec_name( - const bts::api::Security *sec, std::string &name, std::string &error); - -} // namespace binance_execution::spots diff --git a/src/lib_binance_execution/binance_execution/spots/user_data_streams/listen_key_requestor.h b/src/lib_binance_execution/binance_execution/spots/user_data_streams/listen_key_requestor.h deleted file mode 100644 index d7555638d..000000000 --- a/src/lib_binance_execution/binance_execution/spots/user_data_streams/listen_key_requestor.h +++ /dev/null @@ -1,30 +0,0 @@ -#pragma once - -#include "binance_execution/spots/rest_api_client.h" -#include "lib/utils/Delegate.h" -#include - -namespace binance_execution::spots { - -/** - * Requests the listen key for user data stream subscriptions, and periodically - * refreshes it to keep subscriptions alive. - * - * @a https://developers.binance.com/docs/derivatives/usds-margined-futures/user-data-streams - */ -class ListenKeyRequestor { - public: - explicit ListenKeyRequestor(RestApiClient rest_api_client); - ~ListenKeyRequestor(); - - void start(); - void stop(); - - bts::core::Delegate &callback(); - - private: - struct PImpl; - std::unique_ptr me_; -}; - -} // namespace binance_execution::spots diff --git a/src/lib_binance_execution/binance_execution/type_validators.cpp b/src/lib_binance_execution/binance_execution/type_validators.cpp new file mode 100644 index 000000000..7f8f01de6 --- /dev/null +++ b/src/lib_binance_execution/binance_execution/type_validators.cpp @@ -0,0 +1,69 @@ +#include "type_validators.h" + +namespace binance_execution { + +bool validate( + const bts::api::Security *sec, bts::api::Venue venue, std::string &error) { + if (sec == nullptr) { + ErrorList::append(error, "security is null"); + return false; + } + switch (venue) { + case bts::api::VENUE_BINANCE: + if (!sec->has_binance()) { + ErrorList::append(error, "security not of Binance"); + return false; + } + break; + case bts::api::VENUE_BINANCE_USDS_FUTURES: + if (!sec->has_binance_usds_futures()) { + ErrorList::append(error, "security not of BinanceUsdsFutures"); + return false; + } + break; + case bts::api::VENUE_BINANCE_COIN_FUTURES: + if (!sec->has_binance_coin_futures()) { + ErrorList::append(error, "security not of BinanceCoinFutures"); + return false; + } + break; + case bts::api::VENUE_BINANCE_US: + if (!sec->has_binance_us()) { + ErrorList::append(error, "security not of BinanceUs"); + return false; + } + break; + case bts::api::VENUE_UNSET: + switch (sec->ExchangeData_case()) { + case bts::api::Security::kBinance: + case bts::api::Security::kBinanceCoinFutures: + case bts::api::Security::kBinanceUsdsFutures: + case bts::api::Security::kBinanceUs: + break; + default: + ErrorList::append(error, "security not of any Binance sub-venue"); + return false; + } + break; + default: + return false; + } + return true; +} + +bool validate(const bts::orderman::ExecutionReport &er, std::string &error) { + const size_t orig_sz = error.size(); + validate(er.order_key, error); + validate(er.quantity, error); + validate(er.exchange_order_id, error); + if (er.account_name.value().empty()) { + ErrorList::append(error, "account is empty"); + } + validate(er.security, error); + if (er.cfg == nullptr) { + ErrorList::append(error, "jetfix config is null"); + } + return error.size() == orig_sz; +} + +} // namespace binance_execution diff --git a/src/lib_binance_execution/binance_execution/type_validators.h b/src/lib_binance_execution/binance_execution/type_validators.h new file mode 100644 index 000000000..6efce4d03 --- /dev/null +++ b/src/lib_binance_execution/binance_execution/type_validators.h @@ -0,0 +1,62 @@ +#pragma once + +#include "binance_execution/error_list.h" +#include "lib/domain_model/Security.pb.h" +#include "lib/orderman/orderman_utils/ExecutionReport.h" +#include "lib/orderman/orderman_utils/orderman_common.h" + +#include + +namespace binance_execution { + +bool validate(bts::orderman::OrderKey key, std::string &error); +bool validate(bts::orderman::ExchangeOrderID64 key, std::string &error); +bool validate(bts::orderman::ExchangeOrderIDVar key, std::string &error); +bool validate(bts::Quantity qty, std::string &error); +bool validate( + const bts::api::Security *sec, bts::api::Venue venue, std::string &error); +bool validate(const bts::orderman::ExecutionReport &er, std::string &error); + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +inline bool validate(bts::orderman::OrderKey key, std::string &error) { + if (key.value() == 0) { + ErrorList::append(error, "order key is 0"); + return false; + } + return true; +} + +inline bool validate(bts::orderman::ExchangeOrderID64 key, std::string &error) { + if (key.value() == 0) { + ErrorList::append(error, "exch order key is 0"); + return false; + } + return true; +} + +inline bool validate( + bts::orderman::ExchangeOrderIDVar key, std::string &error) { + if (std::holds_alternative(key)) { + return validate(std::get(key), error); + } else { + ErrorList::append(error, "exch order id is undefined or not an integer"); + } + return false; +} + +inline bool validate(bts::Quantity qty, std::string &error) { + if (qty.full_value() == 0) { + ErrorList::append(error, "quantity is 0"); + return false; + } + return true; +} + +inline bool validate(const bts::api::Security *sec, std::string &error) { + return validate(sec, bts::api::VENUE_UNSET, error); +} + +} // namespace binance_execution diff --git a/src/lib_binance_execution/binance_execution/usds_futures/messages/cancel_order_request.cpp b/src/lib_binance_execution/binance_execution/usds_futures/messages/cancel_order_request.cpp index 835b9071d..6adb6f722 100644 --- a/src/lib_binance_execution/binance_execution/usds_futures/messages/cancel_order_request.cpp +++ b/src/lib_binance_execution/binance_execution/usds_futures/messages/cancel_order_request.cpp @@ -1,7 +1,8 @@ #include "cancel_order_request.h" #include "binance_execution/error_list.h" #include "binance_execution/type_converters.h" -#include "binance_execution/usds_futures/type_converters.h" +#include "binance_execution/type_validators.h" +#include "lib/domain_model/Security.pb.h" #include "lib/orderman/CancelOrderMsg.h" #include "lib/utils/checked_arithmetic.h" @@ -10,7 +11,9 @@ namespace binance_execution::usds_futures { bool CancelOrderRequest::init( const bts::orderman::CancelOrderMsg &m, std::string &error) { const auto orig_error_sz = error.size(); - convert_sec_name(m.security, symbol, error); + if (validate(m.security, bts::api::VENUE_BINANCE_USDS_FUTURES, error)) { + symbol = m.security->binance_usds_futures().security_desc(); + } orderId = bts::system::math::checked_ultol(m.exchange_order_id.value()); origClientOrderId = std::to_string(m.key.value()); // The exchange accepts either orderId, or origClientOrderId (only one diff --git a/src/lib_binance_execution/binance_execution/usds_futures/messages/create_order_request.cpp b/src/lib_binance_execution/binance_execution/usds_futures/messages/create_order_request.cpp index 9c71da7f2..206a9786d 100644 --- a/src/lib_binance_execution/binance_execution/usds_futures/messages/create_order_request.cpp +++ b/src/lib_binance_execution/binance_execution/usds_futures/messages/create_order_request.cpp @@ -1,6 +1,6 @@ #include "create_order_request.h" #include "binance_execution/type_converters.h" -#include "binance_execution/usds_futures/type_converters.h" +#include "binance_execution/type_validators.h" #include "lib/orderman/CreateOrderMsg.h" namespace binance_execution::usds_futures { @@ -9,7 +9,9 @@ bool CreateOrderRequest::init( const bts::orderman::CreateOrderMsg &m, std::string &error) { const auto orig_error_sz = error.size(); newClientOrderId = std::to_string(m.key.value()); - convert_sec_name(m.security, symbol, error); + if (validate(m.security, bts::api::VENUE_BINANCE_USDS_FUTURES, error)) { + symbol = m.security->binance_usds_futures().security_desc(); + } side = m.side == bts::api::SIDE_BUY ? Side::BUY : Side::SELL; price = m.price.value(); quantity = m.quantity.full_value(); diff --git a/src/lib_binance_execution/binance_execution/usds_futures/messages/listen_key_request.h b/src/lib_binance_execution/binance_execution/usds_futures/messages/listen_key_request.h deleted file mode 100644 index 7d2d50769..000000000 --- a/src/lib_binance_execution/binance_execution/usds_futures/messages/listen_key_request.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include - -namespace binance_execution::usds_futures { - -/** - * @a https://developers.binance.com/docs/derivatives/usds-margined-futures/user-data-streams - */ -struct ListenKeyRequest {}; - -inline void to_query_params( - std::string & /*qps*/, const ListenKeyRequest & /*req*/) {} - -} // namespace binance_execution::usds_futures diff --git a/src/lib_binance_execution/binance_execution/usds_futures/messages/listen_key_response.h b/src/lib_binance_execution/binance_execution/usds_futures/messages/listen_key_response.h deleted file mode 100644 index 9ff34b6fb..000000000 --- a/src/lib_binance_execution/binance_execution/usds_futures/messages/listen_key_response.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include -#include - -namespace binance_execution::usds_futures { - -/** - * @a https://developers.binance.com/docs/derivatives/usds-margined-futures/user-data-streams - */ -struct ListenKeyResponse { - std::string listenKey; -}; - -NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(ListenKeyResponse, listenKey); - -} // namespace binance_execution::usds_futures diff --git a/src/lib_binance_execution/binance_execution/usds_futures/rest_api_client.h b/src/lib_binance_execution/binance_execution/usds_futures/rest_api_client.h index 6f4a1de13..a96ea0b06 100644 --- a/src/lib_binance_execution/binance_execution/usds_futures/rest_api_client.h +++ b/src/lib_binance_execution/binance_execution/usds_futures/rest_api_client.h @@ -1,13 +1,13 @@ #pragma once +#include "binance_execution/messages/listen_key_request.h" +#include "binance_execution/messages/listen_key_response.h" #include "binance_execution/rest_api_result.h" #include "binance_execution/usds_futures/messages/all_orders_request.h" #include "binance_execution/usds_futures/messages/cancel_order_request.h" #include "binance_execution/usds_futures/messages/cancel_order_response.h" #include "binance_execution/usds_futures/messages/create_order_request.h" #include "binance_execution/usds_futures/messages/create_order_response.h" -#include "binance_execution/usds_futures/messages/listen_key_request.h" -#include "binance_execution/usds_futures/messages/listen_key_response.h" #include "binance_execution/usds_futures/messages/modify_order_request.h" #include "binance_execution/usds_futures/messages/modify_order_response.h" #include "binance_execution/usds_futures/messages/order_record.h" diff --git a/src/lib_binance_execution/binance_execution/usds_futures/type_converters.cpp b/src/lib_binance_execution/binance_execution/usds_futures/type_converters.cpp deleted file mode 100644 index 8ae5c61fd..000000000 --- a/src/lib_binance_execution/binance_execution/usds_futures/type_converters.cpp +++ /dev/null @@ -1,226 +0,0 @@ -#include "type_converters.h" -#include "binance_execution/error_list.h" -#include "binance_execution/type_converters.h" -#include "binance_execution/usds_futures/messages/cancel_order_request.h" -#include "binance_execution/usds_futures/messages/create_order_request.h" -#include "binance_execution/usds_futures/messages/modify_order_request.h" -#include "lib/api/services/AdminService.pb.h" -#include "lib/orderman/CancelOrderMsg.h" -#include "lib/orderman/CancelReplaceOrderMsg.h" -#include "lib/orderman/CreateOrderMsg.h" -#include "lib/utils/checked_arithmetic.h" -#include "lib/utils/string_utils.h" - -namespace binance_execution::usds_futures { - -using bts::system::math::checked_ultol; -using bts::system::string_utils::copy_to_array; -using bts::system::string_utils::parse_uint; -using std::string; -using std::vector; - -bool validate_security(const bts::api::Security *sec, string &error) { - if (sec == nullptr) { - ErrorList::append(error, "security is null"); - } else if (!sec->has_binance_usds_futures()) { - ErrorList::append(error, "security not of Binance Usds Futures"); - } else { - return true; - } - return false; -} - -bool convert_sec_name( - const bts::api::Security *sec, std::string &name, std::string &error) { - if (!validate_security(sec, error)) { - return false; - } - name = sec->binance_usds_futures().security_desc(); - return true; -} - -bool convert_execution_report( - const user_data_streams::OrderUpdate &u, - bts::api::Venue venue, - const std::string &account, - const bts::config::JetFIX_Connection *jetfix_cfg, - bts::orderman::ExecutionReport &er) { - copy_to_array<12>(account, er.account_name.value()); - er.exchange = venue; - er.transact_time = bts::orderman::ExchangeTimeStamp64{ - convert_time_stamp(u.transaction_time).nanos()}; - if (!parse_uint(u.data.client_order_id, er.order_key.value())) { - return false; - } - bts_assert(bts::system::math::checked_ltoul( - u.data.order_id, - er.exchange_order_id.emplace() - .value())); - er.security = nullptr; - er.leg_security = nullptr; - er.side = - u.data.side == Side::BUY ? bts::api::SIDE_BUY : bts::api::SIDE_SELL; - er.time_in_force = convert_time_in_force(u.data.time_in_force); - er.order_type = convert_order_type(u.data.order_type); - er.exec_type = convert_exec_type(u.data.execution_type, u.data.order_status); - er.order_status = convert_order_status(u.data.order_status); - er.manual_order_indicator = bts::api::orderman::MANUAL_ORDER_INDICATOR_NO; - er.price.value() = u.data.orig_price; - er.quantity = u.data.orig_quantity; - er.filled_quantity = u.data.total_filled_qty; - er.last_shares = u.data.last_filled_qty; - // er.max_show = u.data.; - er.last_price.value() = u.data.last_filled_price; - er.avg_price.value() = u.data.avg_price; - er.stop_price.value() = u.data.stop_price; - copy_to_array<40>( - std::to_string(u.data.trade_id), er.exchange_fill_id.value()); - // er.msg = u.reject_reason; - er.cfg = jetfix_cfg; - // Irrelevant fields - er.ice_cl_ord_id = 0; - er.ice_company_id = 0; - er.memo = ""; - return true; -} - -bool convert_execution_report( - const CreateOrderResponse &res, - bts::api::Venue venue, - const std::string &account, - const bts::config::JetFIX_Connection *jetfix_cfg, - bts::orderman::ExecutionReport &er) { - copy_to_array<12>(account, er.account_name.value()); - er.exchange = venue; - er.transact_time = bts::orderman::ExchangeTimeStamp64{ - convert_time_stamp(res.updateTime).nanos()}; - if (!parse_uint(res.clientOrderId, er.order_key.value())) { - return false; - } - bts_assert(bts::system::math::checked_ltoul( - res.orderId, - er.exchange_order_id.emplace() - .value())); - er.security = nullptr; - er.leg_security = nullptr; - er.side = res.side == Side::BUY ? bts::api::SIDE_BUY : bts::api::SIDE_SELL; - er.time_in_force = convert_time_in_force(res.timeInForce); - er.order_type = convert_order_type(res.type); - er.exec_type = bts::api::orderman::EXEC_TYPE_NEW; - er.order_status = convert_order_status(res.status); - er.manual_order_indicator = bts::api::orderman::MANUAL_ORDER_INDICATOR_NO; - er.price.value() = res.price; - er.quantity = res.origQty; - er.filled_quantity = res.executedQty; - er.last_shares = res.executedQty; - er.max_show = 0; - er.last_price.value() = res.avgPrice; - er.avg_price.value() = res.avgPrice; - er.stop_price.value() = res.stopPrice; - er.msg = ""; - er.cfg = jetfix_cfg; - // Irrelevant fields - er.ice_cl_ord_id = 0; - er.ice_company_id = 0; - er.memo = ""; - return true; -} - -void convert_positions( - bts::api::Venue venue, - const bts::api::admin::BTSAccount &account, - const vector &acct_upds, - std::function get_security, - vector &dest) { - for (const auto &acct_upd : acct_upds) { - for (const auto &pos : acct_upd.data.positions) { - bts_assert(pos.position_amount >= 0); - auto it = std::lower_bound( - dest.begin(), dest.end(), pos.symbol, - [](const bts::orderman::ExchangePosition::UnitPosition &u, - const string &ticker) { return u.exchange_sec_desc < ticker; }); - if (it == dest.end() || it->exchange_sec_desc != pos.symbol) { - const auto *sec = get_security(venue, pos.symbol); - if (sec == nullptr) { - continue; - } - bts::orderman::ExchangePosition::UnitPosition new_pos; - new_pos.exchange_sec_desc = pos.symbol; - new_pos.security = sec; - new_pos.account_id = account.id(); - new_pos.account_name = account.name(); - it = dest.insert(it, new_pos); - } - it->position = pos.position_side == PositionSide::SHORT - ? -pos.position_amount - : pos.position_amount; - it->avg_filled_price.value() = pos.break_even_price; - } - } -} - -bool convert_create_request( - const bts::orderman::CreateOrderMsg &m, - CreateOrderRequest &req, - std::string &error) { - const auto orig_error_sz = error.size(); - req.newClientOrderId = std::to_string(m.key.value()); - convert_sec_name(m.security, req.symbol, error); - req.side = m.side == bts::api::SIDE_BUY ? Side::BUY : Side::SELL; - req.price = m.price.value(); - req.quantity = m.quantity.full_value(); - req.type = convert_order_type(m.order_type); - req.timeInForce = convert_time_in_force(m.time_in_force); - req.timestamp = convert_time_stamp(bts::core::TimeStamp::now()); - return error.size() == orig_error_sz; -} - -bool convert_modify_request( - const bts::orderman::CancelReplaceOrderMsg &m, - ModifyOrderRequest &req, - std::string &error) { - const auto orig_error_sz = error.size(); - const auto &cf = m.create_fields; - if (cf.security == nullptr) { - ErrorList::append(error, "security is null"); - } else if (!cf.security->has_binance_usds_futures()) { - ErrorList::append(error, "security not of Binance Usds Futures"); - } else { - req.symbol = cf.security->binance_usds_futures().security_desc(); - } - req.orderId = checked_ultol(m.exchange_order_id.value()); - req.origClientOrderId = std::to_string(cf.key.value()); - // The exchange accepts either orderId, or origClientOrderId (only one - // need be populated). - if (req.orderId == 0 && req.origClientOrderId.empty()) { - ErrorList::append( - error, "Either orderId or origClientOrderId must be defined"); - } - bts_assert(req.orderId != 0 || !req.origClientOrderId.empty()); - req.price = cf.price.value(); - req.priceMatch = PriceMatchMode::NONE; - req.quantity = cf.quantity.full_value(); - req.side = convert_side(cf.side); - return error.size() == orig_error_sz; -} - -bool convert_cancel_request( - const bts::orderman::CancelOrderMsg &m, - CancelOrderRequest &req, - std::string &error) { - const auto orig_error_sz = error.size(); - convert_sec_name(m.security, req.symbol, error); - req.orderId = bts::system::math::checked_ultol(m.exchange_order_id.value()); - req.origClientOrderId = std::to_string(m.key.value()); - // The exchange accepts either orderId, or origClientOrderId (only one - // need be populated). - if (req.orderId == 0 && req.origClientOrderId.empty()) { - ErrorList::append( - error, "Either orderId or origClientOrderId must be defined"); - } - req.symbol = m.security->binance_usds_futures().security_desc(); - return error.size() == orig_error_sz; -} - -} // namespace binance_execution::usds_futures diff --git a/src/lib_binance_execution/binance_execution/usds_futures/type_converters.h b/src/lib_binance_execution/binance_execution/usds_futures/type_converters.h deleted file mode 100644 index d7131343c..000000000 --- a/src/lib_binance_execution/binance_execution/usds_futures/type_converters.h +++ /dev/null @@ -1,63 +0,0 @@ -#pragma once - -#include "binance_execution/usds_futures/messages/create_order_response.h" -#include "binance_execution/usds_futures/user_data_streams/account_update.h" -#include "binance_execution/usds_futures/user_data_streams/order_update.h" -#include "lib/config/protos/jetfix.pb.h" -#include "lib/orderman/orderman_utils/ExchangePosition.h" -#include "lib/orderman/orderman_utils/ExecutionReport.h" - -namespace bts::api::admin { -class BTSAccount; -} - -namespace bts::orderman { -struct CreateOrderMsg; -struct CancelReplaceOrderMsg; -struct CancelOrderMsg; -} // namespace bts::orderman - -namespace binance_execution::usds_futures { - -struct ModifyOrderRequest; -struct CreateOrderRequest; -struct CancelOrderRequest; - -bool convert_sec_name( - const bts::api::Security *sec, std::string &name, std::string &error); - -bool convert_execution_report( - const user_data_streams::OrderUpdate &u, - bts::api::Venue venue, - const std::string &account, - const bts::config::JetFIX_Connection *jetfix_cfg, - bts::orderman::ExecutionReport &er); -bool convert_execution_report( - const CreateOrderResponse &res, - bts::api::Venue venue, - const std::string &account, - const bts::config::JetFIX_Connection *jetfix_cfg, - bts::orderman::ExecutionReport &er); - -bool convert_create_request( - const bts::orderman::CreateOrderMsg &m, - CreateOrderRequest &req, - std::string &error); -bool convert_modify_request( - const bts::orderman::CancelReplaceOrderMsg &msg, - ModifyOrderRequest &req, - std::string &error); -bool convert_cancel_request( - const bts::orderman::CancelOrderMsg &msg, - CancelOrderRequest &req, - std::string &error); - -void convert_positions( - bts::api::Venue venue, - const bts::api::admin::BTSAccount &account, - const std::vector &acct_upds, - std::function get_security, - std::vector &dest); - -} // namespace binance_execution::usds_futures diff --git a/src/lib_binance_execution/binance_execution/usds_futures/usds_futures_gateway.cpp b/src/lib_binance_execution/binance_execution/usds_futures/usds_futures_gateway.cpp index 5b22c78a0..4bfca59af 100644 --- a/src/lib_binance_execution/binance_execution/usds_futures/usds_futures_gateway.cpp +++ b/src/lib_binance_execution/binance_execution/usds_futures/usds_futures_gateway.cpp @@ -1,13 +1,13 @@ #include "usds_futures_gateway.h" #include "binance_execution/enums/order_status.h" -#include "binance_execution/error_list.h" #include "binance_execution/execution_report_store.h" +#include "binance_execution/gateway_config.h" +#include "binance_execution/listen_key_requestor.h" #include "binance_execution/rest_api_result.h" +#include "binance_execution/type_validators.h" #include "binance_execution/usds_futures/messages/create_order_request.h" #include "binance_execution/usds_futures/messages/modify_order_request.h" #include "binance_execution/usds_futures/rest_api_client.h" -#include "binance_execution/usds_futures/type_converters.h" -#include "binance_execution/usds_futures/user_data_streams/listen_key_requestor.h" #include "binance_execution/usds_futures/user_data_streams/user_data_subscription.h" #include "lib/api/service_helpers/AdminServiceUtils.h" #include "lib/api/services/AdminService.pb.h" @@ -77,55 +77,6 @@ using std::string; using std::uint32_t; using std::vector; -static void validate(bts::orderman::OrderKey key, string &error) { - if (key.value() == 0) { - ErrorList::append(error, "order key is 0"); - } -} - -static void validate(bts::orderman::ExchangeOrderID64 key, string &error) { - if (key.value() == 0) { - ErrorList::append(error, "exch order key is 0"); - } -} - -static void validate(bts::orderman::ExchangeOrderIDVar key, string &error) { - if (std::holds_alternative(key)) { - validate(std::get(key), error); - } else { - ErrorList::append(error, "exch order id is undefined or not an integer"); - } -} - -static void validate(bts::Quantity qty, string &error) { - if (qty.full_value() == 0) { - ErrorList::append(error, "quantity is 0"); - } -} - -static void validate(const bts::api::Security *sec, string &error) { - if (sec == nullptr) { - ErrorList::append(error, "security is null"); - } else if (!sec->has_binance_usds_futures()) { - ErrorList::append(error, "security not of Binance Usds Futures"); - } -} - -static bool validate(const bts::orderman::ExecutionReport &er, string &error) { - const size_t orig_sz = error.size(); - validate(er.order_key, error); - validate(er.quantity, error); - validate(er.exchange_order_id, error); - if (er.account_name.value().empty()) { - ErrorList::append(error, "account is empty"); - } - validate(er.security, error); - if (er.cfg == nullptr) { - ErrorList::append(error, "jetfix config is null"); - } - return error.size() == orig_sz; -} - struct ExecutionGatewayCallbacks { bts::orderman::OnExeReport exe_report; bts::orderman::OnGwStatusChange gw_status_change; @@ -137,7 +88,7 @@ struct ExecutionGatewayCallbacks { struct UsdsFuturesGateway::PImpl { explicit PImpl( bts::md::SecurityOracle sec_oracle, - UsdsFuturesGatewayConfig config, + GatewayConfig config, bts::config::JetFIX::Connection jetfix_connection) : sec_oracle(std::move(sec_oracle)), config(std::move(config)), jetfix_connection(std::make_shared( @@ -146,7 +97,13 @@ struct UsdsFuturesGateway::PImpl { this->config.rest_endpoint, this->config.api_key, this->config.secret_key), - listen_key_requestor(this->rest_api_client), + listen_key_requestor( + [this](const auto &req) { + return rest_api_client.get_listen_key(req); + }, + [this](const auto &req) { + return rest_api_client.refresh_listen_key(req); + }), orders( [](const std::string &exch_key) { std::uint32_t k; @@ -161,7 +118,7 @@ struct UsdsFuturesGateway::PImpl { } bts::md::SecurityOracle sec_oracle; - UsdsFuturesGatewayConfig config; + GatewayConfig config; std::shared_ptr jetfix_connection; RestApiClient rest_api_client; @@ -403,7 +360,7 @@ struct UsdsFuturesGateway::PImpl { UsdsFuturesGateway::UsdsFuturesGateway( bts::md::SecurityOracle sec_oracle, - UsdsFuturesGatewayConfig session_cfg, + GatewayConfig session_cfg, bts::config::JetFIX::Connection jetfix_connection) : me_(std::make_unique( std::move(sec_oracle), diff --git a/src/lib_binance_execution/binance_execution/usds_futures/usds_futures_gateway.h b/src/lib_binance_execution/binance_execution/usds_futures/usds_futures_gateway.h index d6ecaa11b..9e54bd257 100644 --- a/src/lib_binance_execution/binance_execution/usds_futures/usds_futures_gateway.h +++ b/src/lib_binance_execution/binance_execution/usds_futures/usds_futures_gateway.h @@ -1,19 +1,22 @@ #pragma once -#include "binance_execution/usds_futures/usds_futures_gateway_config.h" #include "lib/config/protos/jetfix.pb.h" #include "lib/md/SecurityOracle.h" #include "lib/orderman/gateways/IExecutionGateway.h" #include +namespace binance_execution { +struct GatewayConfig; +} + namespace binance_execution::usds_futures { class UsdsFuturesGateway : public bts::orderman::IExecutionGateway { public: explicit UsdsFuturesGateway( bts::md::SecurityOracle sec_oracle, - UsdsFuturesGatewayConfig session_cfg, + GatewayConfig session_cfg, bts::config::JetFIX::Connection jetfix_connection); ~UsdsFuturesGateway() override; diff --git a/src/lib_binance_execution/binance_execution/usds_futures/usds_futures_gateway_config.h b/src/lib_binance_execution/binance_execution/usds_futures/usds_futures_gateway_config.h deleted file mode 100644 index 158b74654..000000000 --- a/src/lib_binance_execution/binance_execution/usds_futures/usds_futures_gateway_config.h +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once - -#include -#include - -namespace binance_execution::usds_futures { - -struct UsdsFuturesGatewayConfig { - std::string api_key; - std::string secret_key; - std::string account; - std::string rest_endpoint; - std::string ws_endpoint; - std::string ws_stream_endpoint; -}; - -NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE( - UsdsFuturesGatewayConfig, - api_key, - secret_key, - account, - rest_endpoint, - ws_endpoint, - ws_stream_endpoint); - -} // namespace binance_execution::usds_futures diff --git a/src/lib_binance_execution/binance_execution/usds_futures/user_data_streams/listen_key_requestor.cpp b/src/lib_binance_execution/binance_execution/usds_futures/user_data_streams/listen_key_requestor.cpp deleted file mode 100644 index 2724943fb..000000000 --- a/src/lib_binance_execution/binance_execution/usds_futures/user_data_streams/listen_key_requestor.cpp +++ /dev/null @@ -1,84 +0,0 @@ -#include "listen_key_requestor.h" -#include "binance_execution/usds_futures/rest_api_client.h" -#include "lib/core/TimeStamp.h" -#include "lib/io/json_formatter.h" - -#include -#include - -namespace binance_execution::usds_futures { - -using bts::core::Delegate; -using bts::core::TimeStamp; -using bts::io::json_formatter; -using std::optional; -using std::string; -using std::thread; - -struct ListenKeyRequestor::PImpl { - RestApiClient rest_api_client; - string listen_key; - TimeStamp listen_key_timestamp; - optional listen_key_thread; - Delegate callback; - bool is_running = false; - - PImpl(RestApiClient rest_api_client) - : rest_api_client(std::move(rest_api_client)) {} - - ~PImpl() { stop(); } - - void start() { - bts_assert(!is_running); - is_running = true; - listen_key_thread.emplace([this]() { run(); }); - } - - void stop() { - is_running = false; - if (listen_key_thread) { - listen_key_thread->join(); - } - } - - void run() { - while (is_running) { - static const auto refresh_period = - bts::core::TimeSpan::from_seconds(60 * 30); - const auto now = bts::core::TimeStamp::now(); - if (listen_key.empty()) { - const auto &res = rest_api_client.get_listen_key(); - if (!res.ok()) { - continue; - } - bts_assert_msgf( - res.ok(), "Failed to get listen key. {}", json_formatter(res)); - listen_key = res.response->listenKey; - bts_assert(!listen_key.empty()); - listen_key_timestamp = now; - callback(listen_key); - } - if ((now - listen_key_timestamp) > refresh_period) { - const auto &res = rest_api_client.refresh_listen_key(); - bts_assert_msgf( - res.ok(), "Failed to refresh listen key. {}", - json_formatter(res)); - listen_key_timestamp = now; - } - std::this_thread::sleep_for(std::chrono::seconds(2)); - } - } -}; - -ListenKeyRequestor::ListenKeyRequestor(RestApiClient rest_api_client) - : me_(std::make_unique(std::move(rest_api_client))) {} - -ListenKeyRequestor::~ListenKeyRequestor() = default; - -void ListenKeyRequestor::start() { me_->start(); } -void ListenKeyRequestor::stop() { me_->stop(); } -Delegate &ListenKeyRequestor::callback() { - return me_->callback; -} - -} // namespace binance_execution::usds_futures diff --git a/src/lib_binance_execution/binance_execution/usds_futures/user_data_streams/listen_key_requestor.h b/src/lib_binance_execution/binance_execution/usds_futures/user_data_streams/listen_key_requestor.h deleted file mode 100644 index 7a6740aae..000000000 --- a/src/lib_binance_execution/binance_execution/usds_futures/user_data_streams/listen_key_requestor.h +++ /dev/null @@ -1,30 +0,0 @@ -#pragma once - -#include "binance_execution/usds_futures/rest_api_client.h" -#include "lib/utils/Delegate.h" -#include - -namespace binance_execution::usds_futures { - -/** - * Requests the listen key for user data stream subscriptions, and periodically - * refreshes it to keep subscriptions alive. - * - * @a https://developers.binance.com/docs/derivatives/usds-margined-futures/user-data-streams - */ -class ListenKeyRequestor { - public: - explicit ListenKeyRequestor(RestApiClient rest_api_client); - ~ListenKeyRequestor(); - - void start(); - void stop(); - - bts::core::Delegate &callback(); - - private: - struct PImpl; - std::unique_ptr me_; -}; - -} // namespace binance_execution::usds_futures diff --git a/src/server/sparkd/execution/execution_gateway_provider.cpp b/src/server/sparkd/execution/execution_gateway_provider.cpp index 8afdd6086..00dbfef55 100644 --- a/src/server/sparkd/execution/execution_gateway_provider.cpp +++ b/src/server/sparkd/execution/execution_gateway_provider.cpp @@ -1,8 +1,7 @@ #include "execution_gateway_provider.h" +#include "binance_execution/gateway_config.h" #include "binance_execution/spots/spots_gateway.h" -#include "binance_execution/spots/spots_gateway_config.h" #include "binance_execution/usds_futures/usds_futures_gateway.h" -#include "binance_execution/usds_futures/usds_futures_gateway_config.h" #include "lib/domain_model/Security.pb.h" #include "lib/io/JsonReader.h" #include "lib/orderman/gateways/CCAPIExecutionGateway.h" @@ -89,15 +88,13 @@ struct ExecutionGatewayProvider::PImpl { } case api::VENUE_BINANCE: { const auto cfg = - parse_config_json( - conn.name()); + parse_config_json(conn.name()); return std::make_unique( o, std::move(cfg), conn); } case api::VENUE_BINANCE_USDS_FUTURES: { - auto cfg = parse_config_json< - binance_execution::usds_futures::UsdsFuturesGatewayConfig>( - conn.name()); + auto cfg = + parse_config_json(conn.name()); return std::make_unique< binance_execution::usds_futures::UsdsFuturesGateway>( o, std::move(cfg), conn);