Files
spark/src/app_sparkd/sparkd/execution/execution_gateway_provider.cpp
T
John Colvin 588b86710f refactor: Rename lib_binance_execution lib_binance
Implementing md and execution for the same exchange in separate projects
has proven to be messy and impractical. I'm renaming this project in
advance of a binance md rewrite which will join md and execution
together in the same lib.
2025-07-17 16:25:23 +00:00

184 lines
7.0 KiB
C++

#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 <typename T, typename... Args>
static std::unique_ptr<T> 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<T>()), core::TimeStamp::now_ns());
return std::make_unique<T>(std::forward<Args>(args)...);
}
struct ExecutionGatewayProvider::PImpl {
PImpl(
trace::PerfLog *l,
md::SecurityOracle o,
config::JetFIX config,
function<optional<json>(const string &)> load_config_json,
shared_ptr<python_interop::PluginManager> 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<optional<json>(const string &)> load_config_json;
shared_ptr<python_interop::PluginManager> py_plugin_man;
template <class T>
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<IExecutionGateway> 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<SimExecutionGatewayUniqueWrapper>(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<PyExecutionGatewayConfig>();
bts_assert(
domain::DomainSecurityUtils::convert(conn.venue()) ==
py_cfg.venue);
return make_gw<PyExecutionGatewayAdapter>(
conn, o, std::move(py_cfg), conn, py_plugin_man);
}
switch (conn.venue()) {
case api::VENUE_ICE:
case api::VENUE_SIMICE:
return make_gw<ICEExecutionGateway>(conn, conn, *l, o);
case api::VENUE_GLBX:
if (conn.use_ilink3()) {
make_gw<ilink3::GLBXiLink3ExecutionGateway>(conn, conn, *l, o);
}
return make_gw<GLBXExecutionGateway>(conn, conn, *l, o);
case api::VENUE_DERIBIT:
return make_gw<CCAPIExecutionGateway>(conn, conn.venue(), o);
case api::VENUE_CBOE:
return make_gw<GeneralExecutionGateway>(
conn, conn.venue(), std::make_unique<OnixFIXAdapter>(o), o);
case api::VENUE_OKX:
return make_gw<OkxExecutionGateway>(conn, o);
case api::VENUE_BINANCE: {
auto cfg =
parse_config_json<binance::GatewayConfig>(conn.name());
cfg.expand_env();
cfg.deduce_key_type();
if (cfg.use_cross_margin_api) {
return make_gw<binance::cross_margin::CrossMarginGateway>(
conn, o, std::move(cfg), conn);
}
return make_gw<binance::spots::SpotsGateway>(
conn, o, std::move(cfg), conn);
}
case api::VENUE_BINANCE_USDS_FUTURES: {
auto cfg =
parse_config_json<binance::GatewayConfig>(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<binance::usds_futures::UsdsFuturesGateway>(
conn, o, std::move(cfg), conn);
}
default:
return make_gw<FakeExecutionGateway>(conn, o);
}
}
std::vector<OrderGatewayDefinition> create() {
std::vector<OrderGatewayDefinition> 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<std::optional<nlohmann::json>(const std::string &)>
load_config_json,
shared_ptr<python_interop::PluginManager> py_plugin_man)
: me_(std::make_unique<PImpl>(
l,
std::move(o),
std::move(config),
std::move(load_config_json),
std::move(py_plugin_man))) {}
ExecutionGatewayProvider::~ExecutionGatewayProvider() = default;
std::vector<OrderGatewayDefinition> ExecutionGatewayProvider::create() {
return me_->create();
}
} // namespace bts::orderman