#include "execution_gateway_provider.h" #include "binance/cross_margin/cross_margin_gateway.h" #include "binance/gateway_config.h" #include "binance/spots/spots_gateway.h" #include "binance/usds_futures/usds_futures_gateway.h" #include "okx_execution/okx_execution_gateway.h" #include "onixs_fix/OnixFIXAdapter.h" #include "python_interop/plugin_manager.h" #include "spark/api/domain/Venue.h" #include "spark/domain/DomainSecurityUtils.h" #include "spark/domain_model/Security.pb.h" #include "spark/domain_model/Venue.h" #include "spark/io/JsonReader.h" #include "spark/orderman/gateways/CCAPIExecutionGateway.h" #include "spark/orderman/gateways/FakeExecutionGateway.h" #include "spark/orderman/gateways/GLBXExecutionGateway.h" #include "spark/orderman/gateways/GLBXiLink3ExecutionGateway.h" #include "spark/orderman/gateways/GeneralExecutionGateway.h" #include "spark/orderman/gateways/ICEExecutionGateway.h" #include "spark/orderman/gateways/IExecutionGenericAdapter.h" #include "spark/orderman/gateways/sim/SimExecutionGatewayFactory.h" #include "spark/orderman/gateways/sim/SimExecutionGatewayUniqueWrapper.h" #include "spark/utils/PBUtils.h" #include "spark/utils/bts_assert.h" #include "spark/utils/type_name.h" #include "sparkd/execution/py_execution_gateway_adapter.h" #include "sparkd/execution/py_execution_gateway_config.h" namespace bts::orderman { #define _LOG_FN(level, fn, ...) \ LOG(OrderMan, level, "ExecutionGatewayProvider::" fn ". " __VA_ARGS__) #define INFO_FN(fn, ...) _LOG_FN(Info, fn, __VA_ARGS__) #define WARN_FN(fn, ...) _LOG_FN(Warn, fn, __VA_ARGS__) #define ERROR_FN(fn, ...) _LOG_FN(Error, fn, __VA_ARGS__) using bts::io::JsonReader; using nlohmann::json; using std::function; using std::optional; using std::shared_ptr; using std::string; using std::vector; template static std::unique_ptr make_gw( const config::JetFIX::Connection &conn, Args &&...args) { LOG(OrderMan, Info, R"([create_exe_gw.json]){{ "name": "{}", "venue": "{}", "type": "{}", "ts": {} }})", conn.name(), api::to_pretty(conn.venue()), std::string(utils::cached_type_name()), core::TimeStamp::now_ns()); return std::make_unique(std::forward(args)...); } struct ExecutionGatewayProvider::PImpl { PImpl( trace::PerfLog *l, md::SecurityOracle o, config::JetFIX config, function(const string &)> load_config_json, shared_ptr py_plugin_man) : l(l), o(std::move(o)), config(std::move(config)), load_config_json(std::move(load_config_json)), py_plugin_man(std::move(py_plugin_man)) {} trace::PerfLog *l; md::SecurityOracle o; config::JetFIX config; function(const string &)> load_config_json; shared_ptr py_plugin_man; template T parse_config_json(const string &name) { auto j = load_config_json(name); bts_assert_msgf( j, "Failed to load execution gateway config. name: {}", name); JsonReader jr{&*j}; T res; bts_assert_msgf( jr.read(res), "Failed to parse execution gateway config. name: {}", name); return res; } std::unique_ptr create_gateway( const config::JetFIX::Connection &conn) { INFO_FN("create_gateway", "conn: {}", utils::pb::to_string(conn)); // ************************************* // Simple hack to integrate python execution gateways: // If a json file exists with the same name as the jetfix connection, // but with a "py_" prefix, assume that file defines python gateway // config for that connection. // ************************************* if (SimExecutionGatewayFactory::is_sim_execution_on(conn.venue())) { return make_gw(conn, conn.venue()); } auto py_cfg_j = load_config_json(fmt::format("py_{}", conn.name())); if (py_cfg_j) { auto py_cfg = py_cfg_j->get(); bts_assert( domain::DomainSecurityUtils::convert(conn.venue()) == py_cfg.venue); return make_gw( conn, o, std::move(py_cfg), conn, py_plugin_man); } switch (conn.venue()) { case api::VENUE_ICE: case api::VENUE_SIMICE: return make_gw(conn, conn, *l, o); case api::VENUE_GLBX: if (conn.use_ilink3()) { make_gw(conn, conn, *l, o); } return make_gw(conn, conn, *l, o); case api::VENUE_DERIBIT: return make_gw(conn, conn.venue(), o); case api::VENUE_CBOE: return make_gw( conn, conn.venue(), std::make_unique(o), o); case api::VENUE_OKX: return make_gw(conn, o); case api::VENUE_BINANCE: { auto cfg = parse_config_json(conn.name()); cfg.expand_env(); cfg.deduce_key_type(); if (cfg.use_cross_margin_api) { return make_gw( conn, o, std::move(cfg), conn); } return make_gw( conn, o, std::move(cfg), conn); } case api::VENUE_BINANCE_USDS_FUTURES: { auto cfg = parse_config_json(conn.name()); cfg.expand_env(); if (cfg.use_cross_margin_api) { WARN_FN( "create_gateway(BinanceUsdsFutures)", "Config has 'use_cross_margin_api' set to true, but this is " "only supported for spots (using regular api)."); } return make_gw( conn, o, std::move(cfg), conn); } default: return make_gw(conn, o); } } std::vector create() { std::vector gateways; for (auto c : config.connection()) { auto &gw = gateways.emplace_back(); gw.connection = std::move(c); gw.gateway = create_gateway(gw.connection); } return gateways; } }; ExecutionGatewayProvider::ExecutionGatewayProvider( trace::PerfLog *l, md::SecurityOracle o, config::JetFIX config, std::function(const std::string &)> load_config_json, shared_ptr py_plugin_man) : me_(std::make_unique( l, std::move(o), std::move(config), std::move(load_config_json), std::move(py_plugin_man))) {} ExecutionGatewayProvider::~ExecutionGatewayProvider() = default; std::vector ExecutionGatewayProvider::create() { return me_->create(); } } // namespace bts::orderman