diff --git a/src/app_sparkd/configurations/dex_dev/orca_1.json b/src/app_sparkd/configurations/dex_dev/orca_1.json new file mode 100644 index 000000000..608d1c53a --- /dev/null +++ b/src/app_sparkd/configurations/dex_dev/orca_1.json @@ -0,0 +1,6 @@ +{ + "account": "DEX_DEV", + "operator_id": "DEX_DEV", + "json_conf_file_name": "config.json", + "default_priority_fee": 1000 +} diff --git a/src/app_sparkd/configurations/dex_dev/raydium_1.json b/src/app_sparkd/configurations/dex_dev/raydium_1.json new file mode 100644 index 000000000..608d1c53a --- /dev/null +++ b/src/app_sparkd/configurations/dex_dev/raydium_1.json @@ -0,0 +1,6 @@ +{ + "account": "DEX_DEV", + "operator_id": "DEX_DEV", + "json_conf_file_name": "config.json", + "default_priority_fee": 1000 +} diff --git a/src/app_sparkd/sparkd/execution/execution_gateway_provider.cpp b/src/app_sparkd/sparkd/execution/execution_gateway_provider.cpp index 759f45909..2a3302463 100644 --- a/src/app_sparkd/sparkd/execution/execution_gateway_provider.cpp +++ b/src/app_sparkd/sparkd/execution/execution_gateway_provider.cpp @@ -4,6 +4,7 @@ #include "binance/spots/spots_gateway.h" #include "binance/usds_futures/usds_futures_gateway.h" #include "okx_execution/okx_execution_gateway.h" +#include "sparkd/execution/rakurai/rakurai_execution_gateway.h" #include "onixs_fix/OnixFIXAdapter.h" #include "python_interop/plugin_manager.h" #include "spark/api/domain/Venue.h" @@ -119,6 +120,18 @@ struct ExecutionGatewayProvider::PImpl { conn, conn.venue(), std::make_unique(o), o); case api::VENUE_OKX: return make_gw(conn, o); + case api::VENUE_ORCA: + case api::VENUE_RAYDIUM: { + auto cfg_j = load_config_json(conn.name()); + bts_assert_msgf( + cfg_j, + "Failed to load execution gateway config. name: {}", + conn.name()); + auto cfg = cfg_j->get(); + cfg.expand_env(); + return make_gw( + conn, o, std::move(cfg), conn); + } case api::VENUE_BINANCE: { auto cfg = parse_config_json(conn.name()); diff --git a/src/app_sparkd/sparkd/execution/rakurai/rakurai_execution_gateway.cpp b/src/app_sparkd/sparkd/execution/rakurai/rakurai_execution_gateway.cpp new file mode 100644 index 000000000..6384104a9 --- /dev/null +++ b/src/app_sparkd/sparkd/execution/rakurai/rakurai_execution_gateway.cpp @@ -0,0 +1,742 @@ +#include "sparkd/execution/rakurai/rakurai_execution_gateway.h" + +#include "spark/config/config.h" +#include "spark/config/protos/system_config.pb.h" +#include "spark/domain/DomainSecurityUtils.h" +#include "spark/domain/Price.h" +#include "spark/domain/Quantity.h" +#include "spark/domain_model/Security.h" +#include "spark/logging/logging.h" +#include "spark/md/SecurityOracle.h" +#include "spark/orderman/CancelOrderMsg.h" +#include "spark/orderman/CancelReplaceOrderMsg.h" +#include "spark/orderman/CreateOrderMsg.h" +#include "spark/orderman/RFQMsg.h" +#include "spark/orderman/events/NewAdminDataEvent.h" +#include "spark/orderman/events/NewSecuritiesEvent.h" +#include "spark/orderman/orderman_utils/CancelReject.h" +#include "spark/orderman/orderman_utils/ExecutionReport.h" +#include "spark/orderman/orderman_utils/orderman_common.h" +#include "spark/utils/bts_assert.h" +#include "sparkd/rakurai/RakuraiSharedRuntime.h" + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace bts::orderman::rakurai { + +namespace { + +#define LOG_FN(level, fn, ...) \ + LOG(OrderMan, level, "RakuraiExecutionGateway::" fn ". " __VA_ARGS__) +#define WARN_FN(fn, ...) LOG_FN(Warn, fn, __VA_ARGS__) + +using ConnectorNewOrder = ::rakurai::dex_connector::order_entry::new_order; +using ConnectorOrderState = ::rakurai::dex_connector::order_entry::order_state; +using ConnectorSide = ::rakurai::dex_connector::order_entry::side; + +std::optional extract_short_pool_id(const api::Security& security) { + std::string desc; + if (!domain::DomainSecurityUtils::get_security_desc(security, desc)) { + return std::nullopt; + } + const auto pos = desc.rfind('-'); + if (pos == std::string::npos || pos + 1 >= desc.size()) { + return std::nullopt; + } + return desc.substr(pos + 1); +} + +api::orderman::NativeOrderStatus to_order_status(ConnectorOrderState state) { + using Status = ::rakurai::dex_connector::data_types::txn_status_t; + switch (state.tx_status) { + case Status::accepted: + return api::orderman::NATIVE_ORDER_STATUS_NEW; + case Status::confirmed: + return state.is_pass ? api::orderman::NATIVE_ORDER_STATUS_FILLED + : api::orderman::NATIVE_ORDER_STATUS_REJECTED; + case Status::failed: + return api::orderman::NATIVE_ORDER_STATUS_REJECTED; + case Status::pending: + return api::orderman::NATIVE_ORDER_STATUS_PENDING_NEW; + } + return api::orderman::NATIVE_ORDER_STATUS_UNDEFINED; +} + +api::orderman::ExecType to_exec_type(ConnectorOrderState state) { + using Status = ::rakurai::dex_connector::data_types::txn_status_t; + switch (state.tx_status) { + case Status::accepted: + return api::orderman::EXEC_TYPE_NEW; + case Status::confirmed: + return state.is_pass ? api::orderman::EXEC_TYPE_FILL + : api::orderman::EXEC_TYPE_REJECTED; + case Status::failed: + return api::orderman::EXEC_TYPE_REJECTED; + case Status::pending: + return api::orderman::EXEC_TYPE_PENDING_NEW; + } + return api::orderman::EXEC_TYPE_UNDEFINED; +} + +std::string txn_status_name( + ::rakurai::dex_connector::data_types::txn_status_t status) { + using Status = ::rakurai::dex_connector::data_types::txn_status_t; + switch (status) { + case Status::accepted: + return "accepted"; + case Status::confirmed: + return "confirmed"; + case Status::failed: + return "failed"; + case Status::pending: + return "pending"; + } + return "unknown"; +} + +std::string json_for_log(const nlohmann::json& j) { + try { + return j.dump(); + } catch (const std::exception& ex) { + return fmt::format("", ex.what()); + } +} + +} // namespace + +struct RakuraiExecutionGateway::PImpl { + struct ActiveOrder { + CreateOrderMsg create_msg; + trace::CorID cor_id; + std::array pool_id{}; + double quantity_scale = 1.0; + std::uint64_t raw_quantity = 0; + std::uint64_t priority_fee = 0; + std::uint64_t client_id = 0; + std::uint64_t txn_id = 0; + }; + + struct BufferedReport { + std::shared_ptr report; + trace::CorID cor_id; + }; + + explicit PImpl( + md::SecurityOracle, + GatewayConfig cfg, + config::JetFIX_Connection jetfix_connection) + : config(std::move(cfg)), + jetfix_connection(std::make_shared( + std::move(jetfix_connection))) { + config.expand_env(); + venue = this->jetfix_connection->venue(); + bts_assert(venue == api::VENUE_ORCA || venue == api::VENUE_RAYDIUM); + LOG_FN( + Info, + "ctor", + "session:{} venue:{} account:{} operator_id:{} json_conf_file_name:{} default_priority_fee:{} connector_config:{}", + this->jetfix_connection->name(), + api::Venue_Name(venue), + config.account, + config.operator_id, + config.json_conf_file_name, + config.default_priority_fee, + json_for_log(config.connector_config)); + } + + GatewayConfig config; + std::shared_ptr jetfix_connection; + api::Venue venue = api::VENUE_UNSET; + std::mutex mutex; + std::unordered_map active_orders; + std::unordered_map client_id_to_order_key; + std::deque pending_client_bindings; + std::deque buffered_reports; + std::size_t order_update_subscription_id = 0; + std::size_t status_subscription_id = 0; + bool started = false; + bool connected = false; + OnExeReport exe_report; + OnGwStatusChange gw_status_change; + OnCancelReject cancel_reject; + OnExchangePositionState exchange_position_state; + OnExchangeOpenOrdersState exchange_open_orders; + + void start() { + { + std::lock_guard lock(mutex); + if (started) { + LOG_FN( + Info, "start", "already started. venue:{}", + api::Venue_Name(venue)); + return; + } + started = true; + } + LOG_FN( + Info, + "start", + "starting. session:{} venue:{} account:{} operator_id:{} json_conf_file_name:{} default_priority_fee:{}", + jetfix_connection->name(), + api::Venue_Name(venue), + config.account, + config.operator_id, + config.json_conf_file_name, + config.default_priority_fee); + + auto& runtime = bts::rakurai::RakuraiSharedRuntime::instance(); + order_update_subscription_id = + runtime.add_order_update_subscriber([this](ConnectorOrderState state) { + this->on_order_update(state); + }); + status_subscription_id = + runtime.add_status_subscriber([this](bool is_connected_now) { + this->on_status_change(is_connected_now); + }); + + try { + if (!config.json_conf_file_name.empty()) { + const auto config_path = + boost::filesystem::path{ + config::get().config_path()} / + config.json_conf_file_name; + LOG_FN( + Info, + "start", + "ensuring shared runtime from file. path:{}", + config_path.string()); + runtime.ensure_started_from_file(config_path.string()); + } else { + LOG_FN( + Info, + "start", + "ensuring shared runtime from inline config. config:{}", + json_for_log(config.connector_config)); + runtime.ensure_started_from_json( + config.connector_config, jetfix_connection->name()); + } + LOG_FN( + Info, + "start", + "shared runtime start request submitted. order_update_sub_id:{} status_sub_id:{} connected_now:{}", + order_update_subscription_id, + status_subscription_id, + runtime.is_connected()); + } catch (...) { + runtime.remove_order_update_subscriber(order_update_subscription_id); + runtime.remove_status_subscriber(status_subscription_id); + order_update_subscription_id = 0; + status_subscription_id = 0; + { + std::lock_guard lock(mutex); + started = false; + } + throw; + } + } + + void stop() { + LOG_FN( + Info, + "stop", + "stopping. order_update_sub_id:{} status_sub_id:{}", + order_update_subscription_id, + status_subscription_id); + auto& runtime = bts::rakurai::RakuraiSharedRuntime::instance(); + if (order_update_subscription_id != 0) { + runtime.remove_order_update_subscriber(order_update_subscription_id); + order_update_subscription_id = 0; + } + if (status_subscription_id != 0) { + runtime.remove_status_subscriber(status_subscription_id); + status_subscription_id = 0; + } + } + + void on_status_change(bool is_connected_now) { + LOG_FN( + Info, + "on_status_change", + "status update. venue:{} old_connected:{} new_connected:{} active_orders:{} pending_bindings:{} buffered_reports:{}", + api::Venue_Name(venue), + connected, + is_connected_now, + active_orders.size(), + pending_client_bindings.size(), + buffered_reports.size()); + { + std::lock_guard lock(mutex); + connected = is_connected_now; + } + if (gw_status_change) { + gw_status_change(is_connected_now); + } + } + + void on_order_update(ConnectorOrderState order_state) { + std::lock_guard lock(mutex); + LOG_FN( + Info, + "on_order_update", + "incoming order update. client_id:{} txn_id:{} txn_status:{} is_pass:{} active_orders:{} pending_bindings:{} mapped_clients:{}", + order_state.client_id, + order_state.txn_id, + txn_status_name(order_state.tx_status), + order_state.is_pass, + active_orders.size(), + pending_client_bindings.size(), + client_id_to_order_key.size()); + auto key_it = client_id_to_order_key.find(order_state.client_id); + if (key_it == client_id_to_order_key.end()) { + if (pending_client_bindings.empty()) { + LOG_FN( + Warn, + "on_order_update", + "dropping order update with unknown client_id:{} because there are no pending client bindings", + order_state.client_id); + return; + } + const auto key = pending_client_bindings.front(); + pending_client_bindings.pop_front(); + client_id_to_order_key.emplace(order_state.client_id, key); + LOG_FN( + Info, + "on_order_update", + "bound client_id:{} to order_key:{}", + order_state.client_id, + key.value()); + key_it = client_id_to_order_key.find(order_state.client_id); + } + + auto order_it = active_orders.find(key_it->second); + if (order_it == active_orders.end()) { + LOG_FN( + Warn, + "on_order_update", + "dropping order update for client_id:{} mapped_order_key:{} because active order was not found", + order_state.client_id, + key_it->second.value()); + return; + } + + auto& active = order_it->second; + active.client_id = order_state.client_id; + active.txn_id = order_state.txn_id; + auto report = build_report_locked(active, order_state); + buffered_reports.push_back({std::move(report), active.cor_id}); + LOG_FN( + Info, + "on_order_update", + "buffered execution report. order_key:{} client_id:{} txn_id:{} buffered_reports:{} order_status:{} exec_type:{}", + active.create_msg.key.value(), + active.client_id, + active.txn_id, + buffered_reports.size(), + api::orderman::NativeOrderStatus_Name( + buffered_reports.back().report->order_status), + api::orderman::ExecType_Name(buffered_reports.back().report->exec_type)); + + const auto order_status = buffered_reports.back().report->order_status; + if (order_status == api::orderman::NATIVE_ORDER_STATUS_FILLED || + order_status == api::orderman::NATIVE_ORDER_STATUS_REJECTED) { + LOG_FN( + Info, + "on_order_update", + "removing terminal active order. order_key:{} client_id:{} status:{}", + active.create_msg.key.value(), + order_state.client_id, + api::orderman::NativeOrderStatus_Name(order_status)); + client_id_to_order_key.erase(order_state.client_id); + active_orders.erase(order_it); + } + } + + void send_create(const CreateOrderMsg& msg, trace::CorID cor_id) { + std::lock_guard lock(mutex); + std::string security_desc = ""; + if (msg.security != nullptr) { + std::string desc; + if (domain::DomainSecurityUtils::get_security_desc(*msg.security, desc)) { + security_desc = std::move(desc); + } else { + security_desc = ""; + } + } + LOG_FN( + Info, + "send_create", + "create request. order_key:{} venue:{} side:{} qty:{} price:{} tif:{} order_type:{} connected:{} security_present:{} desc:{}", + msg.key.value(), + api::Venue_Name(msg.venue()), + api::Side_Name(msg.side), + msg.quantity.full_value(), + msg.price, + api::orderman::TimeInForce_Name(msg.time_in_force), + api::orderman::NativeOrderType_Name(msg.order_type), + connected, + msg.security != nullptr, + security_desc); + if (msg.security == nullptr) { + enqueue_reject_locked(msg, cor_id, "Rakurai order missing security"); + return; + } + if (msg.venue() != venue) { + enqueue_reject_locked( + msg, + cor_id, + fmt::format( + "Rakurai gateway venue mismatch. expected:{} actual:{}", + api::Venue_Name(venue), + api::Venue_Name(msg.venue()))); + return; + } + if (msg.order_type != api::orderman::NATIVE_ORDER_TYPE_MARKET) { + enqueue_reject_locked( + msg, + cor_id, + "Rakurai DEX gateway currently supports market orders only"); + return; + } + if (!connected) { + enqueue_reject_locked( + msg, cor_id, "Rakurai execution session is not connected"); + return; + } + + const auto short_pool_id = extract_short_pool_id(*msg.security); + if (!short_pool_id.has_value()) { + enqueue_reject_locked( + msg, cor_id, "Unable to derive Rakurai pool alias from security"); + return; + } + + const auto metadata = + bts::rakurai::RakuraiSharedRuntime::instance().find_pool_metadata( + venue, *short_pool_id); + if (!metadata.has_value()) { + LOG_FN( + Warn, + "send_create", + "pool metadata missing. venue:{} short_pool_id:{}", + api::Venue_Name(venue), + *short_pool_id); + enqueue_reject_locked( + msg, + cor_id, + "Rakurai pool metadata not available yet; wait for pool definitions"); + return; + } + + const auto raw_qty_f = + msg.quantity.full_value() * metadata->quantity_scale; + if (!std::isfinite(raw_qty_f) || raw_qty_f <= 0.0) { + enqueue_reject_locked( + msg, cor_id, "Rakurai order quantity must be positive"); + return; + } + + ConnectorNewOrder order; + order.order_def.pool_id = metadata->security.pool_id; + order.order_def.dex = metadata->security.venue; + order.order_def.order_side = + msg.side == api::SIDE_BUY ? ConnectorSide::buy : ConnectorSide::sell; + order.qty = static_cast(std::llround(raw_qty_f)); + order.priority_fee = config.default_priority_fee; + LOG_FN( + Info, + "send_create", + "launching new order. order_key:{} short_pool_id:{} raw_qty:{} priority_fee:{} dex:{} quantity_scale:{}", + msg.key.value(), + *short_pool_id, + order.qty, + order.priority_fee, + static_cast(metadata->security.venue), + metadata->quantity_scale); + + try { + bts::rakurai::RakuraiSharedRuntime::instance().launch_new_order(order); + } catch (const std::exception& ex) { + WARN_FN( + "send_create", + "launch_new_order threw. order_key:{} reason:{}", + msg.key.value(), + std::string(ex.what())); + enqueue_reject_locked(msg, cor_id, ex.what()); + return; + } + + active_orders.emplace( + msg.key, + ActiveOrder{ + msg, + cor_id, + metadata->security.pool_id, + metadata->quantity_scale, + order.qty, + order.priority_fee}); + pending_client_bindings.push_back(msg.key); + LOG_FN( + Info, + "send_create", + "create request accepted locally. order_key:{} active_orders:{} pending_bindings:{}", + msg.key.value(), + active_orders.size(), + pending_client_bindings.size()); + } + + void send_cancel_reject(OrderKey key, const std::string& reason) { + WARN_FN( + "send_cancel_reject", + "cancel reject. order_key:{} reason:{}", + key.value(), + reason); + if (cancel_reject) { + cancel_reject({key, CancelRejectReason::Other, reason}); + } + } + + std::shared_ptr build_report_locked( + const ActiveOrder& active, + ConnectorOrderState state) const { + auto report = std::make_shared(); + report->account_name = account_name_for(active.create_msg); + report->operator_id = operator_id_for(active.create_msg); + report->order_key = active.create_msg.key; + report->msg_seq_num = 0; + report->security = active.create_msg.security; + report->leg_security = nullptr; + report->side = active.create_msg.side; + report->time_in_force = active.create_msg.time_in_force; + report->order_type = active.create_msg.order_type; + report->manual_order_indicator = active.create_msg.manual_order_indicator; + report->exchange = venue; + report->cfg = jetfix_connection.get(); + report->exchange_order_id = ExchangeOrderID64( + active.client_id != 0 ? active.client_id : state.txn_id); + report->quantity = active.create_msg.quantity; + report->price = active.create_msg.price; + report->stop_price = active.create_msg.stop_px; + report->max_show = active.create_msg.display_quantity; + report->transact_time = + ExchangeTimeStamp64(static_cast(datetime::unix_nano_now())); + report->exec_type = to_exec_type(state); + report->order_status = to_order_status(state); + report->filled_quantity = + report->order_status == api::orderman::NATIVE_ORDER_STATUS_FILLED + ? active.create_msg.quantity + : Quantity(0.0f); + report->last_shares = report->filled_quantity; + report->last_price = active.create_msg.price; + report->avg_price = active.create_msg.price; + report->msg = fmt::format( + "Rakurai txn_status={} txn_id={} pass={}", + txn_status_name(state.tx_status), + state.txn_id, + state.is_pass); + LOG_FN( + Info, + "build_report_locked", + "built report. order_key:{} client_id:{} txn_id:{} exec_type:{} order_status:{} message:{}", + active.create_msg.key.value(), + active.client_id, + state.txn_id, + api::orderman::ExecType_Name(report->exec_type), + api::orderman::NativeOrderStatus_Name(report->order_status), + report->msg); + return report; + } + + void enqueue_reject_locked( + const CreateOrderMsg& msg, + trace::CorID cor_id, + const std::string& reason) { + auto report = std::make_shared(); + report->account_name = account_name_for(msg); + report->operator_id = operator_id_for(msg); + report->order_key = msg.key; + report->msg_seq_num = 0; + report->security = msg.security; + report->leg_security = nullptr; + report->side = msg.side; + report->time_in_force = msg.time_in_force; + report->order_type = msg.order_type; + report->manual_order_indicator = msg.manual_order_indicator; + report->exchange = venue; + report->cfg = jetfix_connection.get(); + report->exchange_order_id = ExchangeOrderID64(0); + report->quantity = msg.quantity; + report->filled_quantity = Quantity(0.0f); + report->last_shares = Quantity(0.0f); + report->price = msg.price; + report->stop_price = msg.stop_px; + report->max_show = msg.display_quantity; + report->last_price = Price(std::numeric_limits::quiet_NaN()); + report->avg_price = Price(std::numeric_limits::quiet_NaN()); + report->transact_time = + ExchangeTimeStamp64(static_cast(datetime::unix_nano_now())); + report->exec_type = api::orderman::EXEC_TYPE_REJECTED; + report->order_status = api::orderman::NATIVE_ORDER_STATUS_REJECTED; + report->msg = reason; + buffered_reports.push_back({std::move(report), cor_id}); + WARN_FN( + "enqueue_reject_locked", + "buffered reject. order_key:{} reason:{} buffered_reports:{}", + msg.key.value(), + reason, + buffered_reports.size()); + } + + AccountName account_name_for(const CreateOrderMsg& msg) const { + const auto account = msg.routing_record != nullptr && + !msg.routing_record->account().empty() + ? msg.routing_record->account() + : config.account; + AccountName result; + result->copy_from(account); + return result; + } + + OperatorID operator_id_for(const CreateOrderMsg& msg) const { + const auto operator_id = + msg.routing_record != nullptr && + !msg.routing_record->operator_id().empty() + ? msg.routing_record->operator_id() + : config.operator_id; + OperatorID result; + orderman::to_array(operator_id, &result.value()); + return result; + } +}; + +RakuraiExecutionGateway::RakuraiExecutionGateway( + md::SecurityOracle sec_oracle, + GatewayConfig config, + config::JetFIX_Connection jetfix_connection) + : me_(std::make_unique( + std::move(sec_oracle), + std::move(config), + std::move(jetfix_connection))) {} + +RakuraiExecutionGateway::~RakuraiExecutionGateway() { me_->stop(); } + +void RakuraiExecutionGateway::start() { me_->start(); } + +void RakuraiExecutionGateway::dispatch_incoming_fix_messages() { + std::deque reports; + { + std::lock_guard lock(me_->mutex); + reports.swap(me_->buffered_reports); + } + + if (!reports.empty()) { + LOG_FN( + Info, + "dispatch_incoming_fix_messages", + "dispatching buffered reports. count:{} has_callback:{}", + reports.size(), + me_->exe_report != nullptr); + } + if (!me_->exe_report) { + return; + } + + for (auto& buffered : reports) { + LOG_FN( + Info, + "dispatch_incoming_fix_messages", + "publishing execution report. order_key:{} exec_type:{} order_status:{} msg:{}", + buffered.report->order_key.value(), + api::orderman::ExecType_Name(buffered.report->exec_type), + api::orderman::NativeOrderStatus_Name(buffered.report->order_status), + buffered.report->msg); + me_->exe_report(std::move(buffered.report), buffered.cor_id); + } +} + +void RakuraiExecutionGateway::on_securities( + const mercury::Update& update) { + LOG_FN( + Info, + "on_securities", + "received securities update. type:{} has_events:{} count:{}", + static_cast(update.event_type), + update.events != nullptr, + update.events != nullptr ? update.events->size() : 0); +} + +void RakuraiExecutionGateway::on_admin_data( + const mercury::Update& update) { + LOG_FN( + Info, + "on_admin_data", + "received admin data update. type:{} has_events:{} count:{}", + static_cast(update.event_type), + update.events != nullptr, + update.events != nullptr ? update.events->size() : 0); +} + +void RakuraiExecutionGateway::send_fix(const jetfix::OutFIXPtr&, trace::CorID) { + WARN_FN("send_fix", "FIX transport is not supported for Rakurai"); +} + +void RakuraiExecutionGateway::send_msgs( + std::vector const& msgs, + trace::CorID cor_id) { + LOG_FN( + Info, + "send_msgs", + "received outbound message batch. count:{}", + msgs.size()); + for (const auto& msg : msgs) { + std::visit( + [&](const auto& order_msg) { + using T = std::decay_t; + if constexpr (std::is_same_v) { + me_->send_create(order_msg, cor_id); + } else if constexpr (std::is_same_v) { + me_->send_cancel_reject( + order_msg.key, + "Rakurai DEX gateway does not support order cancel"); + } else if constexpr (std::is_same_v) { + me_->send_cancel_reject( + order_msg.create_fields.key, + "Rakurai DEX gateway does not support order replace"); + } else if constexpr (std::is_same_v) { + WARN_FN("send_msgs", "RFQ is not supported for Rakurai"); + } + }, + msg); + } +} + +OnExeReport& RakuraiExecutionGateway::on_exe_report() { return me_->exe_report; } + +OnGwStatusChange& RakuraiExecutionGateway::on_gw_status_change() { + return me_->gw_status_change; +} + +OnCancelReject& RakuraiExecutionGateway::on_cancel_reject() { + return me_->cancel_reject; +} + +OnExchangePositionState& RakuraiExecutionGateway::on_exchange_position_state() { + return me_->exchange_position_state; +} + +OnExchangeOpenOrdersState& RakuraiExecutionGateway::on_exchange_open_orders() { + return me_->exchange_open_orders; +} + +} // namespace bts::orderman::rakurai diff --git a/src/app_sparkd/sparkd/execution/rakurai/rakurai_execution_gateway.h b/src/app_sparkd/sparkd/execution/rakurai/rakurai_execution_gateway.h new file mode 100644 index 000000000..52165106f --- /dev/null +++ b/src/app_sparkd/sparkd/execution/rakurai/rakurai_execution_gateway.h @@ -0,0 +1,76 @@ +#pragma once + +#include "spark/io/JsonReader.h" +#include "spark/orderman/gateways/IExecutionGateway.h" +#include "spark/os/os.h" + +#include +#include + +namespace bts::config { +class JetFIX_Connection; +} + +namespace bts::md { +class SecurityOracle; +} + +namespace bts::orderman::rakurai { + +struct GatewayConfig { + std::string account; + std::string operator_id; + std::string json_conf_file_name; + std::uint64_t default_priority_fee = 1000; + nlohmann::json connector_config; + + void expand_env() { + bts::os::expand_env_in_place(account); + bts::os::expand_env_in_place(operator_id); + bts::os::expand_env_in_place(json_conf_file_name); + } +}; + +inline void from_json(const nlohmann::json& j, GatewayConfig& cfg) { + cfg.connector_config = j; + + const bts::io::JsonReader jr{&j}; + jr.read_or("account", cfg.account); + jr.read_or("operator_id", cfg.operator_id); + jr.read_or("json_conf_file_name", cfg.json_conf_file_name); + jr.read_or("default_priority_fee", cfg.default_priority_fee); +} + +class RakuraiExecutionGateway : public IExecutionGateway { + public: + explicit RakuraiExecutionGateway( + bts::md::SecurityOracle sec_oracle, + GatewayConfig config, + bts::config::JetFIX_Connection jetfix_connection); + ~RakuraiExecutionGateway() override; + + void start() override; + void dispatch_incoming_fix_messages() override; + void on_securities( + const bts::mercury::Update&) + override; + void on_admin_data( + const bts::mercury::Update&) override; + void send_fix(const bts::jetfix::OutFIXPtr&, bts::trace::CorID) override; + void send_msgs( + std::vector const&, + bts::trace::CorID) override; + + bts::orderman::OnExeReport& on_exe_report() override; + bts::orderman::OnGwStatusChange& on_gw_status_change() override; + bts::orderman::OnCancelReject& on_cancel_reject() override; + bts::orderman::OnExchangePositionState& + on_exchange_position_state() override; + bts::orderman::OnExchangeOpenOrdersState& on_exchange_open_orders() override; + + private: + struct PImpl; + std::unique_ptr me_; +}; + +} // namespace bts::orderman::rakurai diff --git a/src/app_sparkd/sparkd/md/rakurai/RakuraiDexSubscription.cpp b/src/app_sparkd/sparkd/md/rakurai/RakuraiDexSubscription.cpp index dae519f75..1d759f746 100644 --- a/src/app_sparkd/sparkd/md/rakurai/RakuraiDexSubscription.cpp +++ b/src/app_sparkd/sparkd/md/rakurai/RakuraiDexSubscription.cpp @@ -3,8 +3,7 @@ #include "spark/config/config.h" #include "spark/config/protos/system_config.pb.h" #include "spark/logging/logging.h" -#include "spark/utils/thread_manager.h" -#include "sparkd/md/rakurai/RakuraiDexDataHandler.h" +#include "sparkd/rakurai/RakuraiSharedRuntime.h" #include @@ -24,6 +23,7 @@ struct RakuraiDexSubscription::PImpl { std::uint32_t num_depth_levels = 0; std::vector excluded_securities; std::shared_ptr sec_id_store; + std::size_t subscription_id = 0; }; RakuraiDexSubscription::RakuraiDexSubscription( @@ -46,33 +46,29 @@ void RakuraiDexSubscription::start( std::shared_ptr listener) { const auto config_path = Path{config::get().config_path()} / me_->json_conf_file_name; - const auto thread_name = - me_->venue == api::domain::VENUE_ORCA ? "rakurai_orca" : "rakurai_raydium"; LOG(md, Info, "Starting Rakurai DEX market data. venue:{} config:{}", int(me_->venue), config_path.string()); - threading::make_thread( - thread_name, - [venue = me_->venue, - config_file = config_path.string(), - num_depth_levels = me_->num_depth_levels, - excluded_securities = me_->excluded_securities, - sec_id_store = me_->sec_id_store, - listener = std::move(listener)]() mutable { - rakurai::RakuraiDexDataHandler::run( - std::move(listener), - std::move(sec_id_store), - venue, - config_file, - num_depth_levels, - excluded_securities); - }) - .detach(); + me_->subscription_id = + rakurai::RakuraiSharedRuntime::instance().add_market_data_subscriber( + std::move(listener), + me_->sec_id_store, + me_->venue, + me_->num_depth_levels, + me_->excluded_securities); + rakurai::RakuraiSharedRuntime::instance().ensure_started_from_file( + config_path.string()); } -void RakuraiDexSubscription::stop() {} +void RakuraiDexSubscription::stop() { + if (me_->subscription_id != 0) { + rakurai::RakuraiSharedRuntime::instance().remove_market_data_subscriber( + me_->subscription_id); + me_->subscription_id = 0; + } +} } // namespace bts::md diff --git a/src/app_sparkd/sparkd/rakurai/RakuraiSharedRuntime.cpp b/src/app_sparkd/sparkd/rakurai/RakuraiSharedRuntime.cpp new file mode 100644 index 000000000..0a8b97b8f --- /dev/null +++ b/src/app_sparkd/sparkd/rakurai/RakuraiSharedRuntime.cpp @@ -0,0 +1,867 @@ +#include "sparkd/rakurai/RakuraiSharedRuntime.h" + +#include "spark/api/domain/SecurityType.pb.h" +#include "spark/domain/Book.h" +#include "spark/domain/Price.h" +#include "spark/domain/Quantity.h" +#include "spark/domain/Security.h" +#include "spark/logging/logging.h" +#include "spark/md/IMarketDataListener.h" +#include "spark/persistence/IDGenerator.h" +#include "spark/persistence/SqliteStore.h" +#include "sparkd/md/MarketDataError.h" + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace bts::rakurai { + +namespace { + +using ConnectorVenue = ::rakurai::dex_connector::data_types::venue; +using ConnectorSecurity = ::rakurai::dex_connector::data_types::security; +using ConnectorBookLevel = ::rakurai::dex_connector::data_types::book_level; +using ConnectorDexConfig = ::rakurai::dex_connector::configuration::dex_config; + +const char* connector_venue_name(ConnectorVenue venue) { + switch (venue) { + case ConnectorVenue::orca_clmm: + return "orca_clmm"; + case ConnectorVenue::orca_amm: + return "orca_amm"; + case ConnectorVenue::raydium_amm: + return "raydium_amm"; + case ConnectorVenue::raydium_cpmm: + return "raydium_cpmm"; + case ConnectorVenue::raydium_clmm: + return "raydium_clmm"; + default: + return "invalid"; + } +} + +bool venue_matches(api::domain::Venue spark_venue, ConnectorVenue connector_venue) { + switch (spark_venue) { + case api::domain::VENUE_ORCA: + return connector_venue == ConnectorVenue::orca_clmm || + connector_venue == ConnectorVenue::orca_amm; + case api::domain::VENUE_RAYDIUM: + return connector_venue == ConnectorVenue::raydium_amm || + connector_venue == ConnectorVenue::raydium_cpmm || + connector_venue == ConnectorVenue::raydium_clmm; + default: + return false; + } +} + +bool venue_matches(api::Venue spark_venue, ConnectorVenue connector_venue) { + switch (spark_venue) { + case api::VENUE_ORCA: + return connector_venue == ConnectorVenue::orca_clmm || + connector_venue == ConnectorVenue::orca_amm; + case api::VENUE_RAYDIUM: + return connector_venue == ConnectorVenue::raydium_amm || + connector_venue == ConnectorVenue::raydium_cpmm || + connector_venue == ConnectorVenue::raydium_clmm; + default: + return false; + } +} + +std::string bytes_to_hex(const std::array& bytes) { + static constexpr char kHex[] = "0123456789abcdef"; + std::string out; + out.reserve(bytes.size() * 2); + for (std::uint8_t byte : bytes) { + out.push_back(kHex[(byte >> 4) & 0x0f]); + out.push_back(kHex[byte & 0x0f]); + } + return out; +} + +std::string short_hex(const std::array& bytes) { + const auto hex = bytes_to_hex(bytes); + return hex.substr(0, std::min(hex.size(), 12)); +} + +std::string token_display_name( + const ::rakurai::dex_connector::data_types::token_info& token) { + return token.name.empty() ? short_hex(token.mint_address) : token.name; +} + +std::string build_security_desc( + const ConnectorSecurity& security, + std::string_view short_pool_id) { + return fmt::format( + "{}-{}-{}", + token_display_name(security.token_a), + token_display_name(security.token_b), + short_pool_id); +} + +double quantity_scale_from_decimals(std::uint8_t decimals) { + double scale = 1.0; + for (std::uint8_t i = 0; i < decimals; ++i) { + scale *= 10.0; + } + return scale; +} + +domain::BookLevel to_book_level( + const ConnectorBookLevel& source, + double quantity_scale) { + domain::BookLevel level; + level.price = domain::Price::from_double(source.price); + const auto normalized_quantity = + quantity_scale > 0.0 + ? static_cast(source.quantity) / quantity_scale + : static_cast(source.quantity); + level.total_quantity = + domain::Quantity(static_cast(normalized_quantity)); + level.direct_quantity = level.total_quantity; + level.n_orders = 0; + return level; +} + +std::list parse_dex_configs(const nlohmann::json& config) { + std::list dex_config_list; + const auto dex_it = config.find("dex"); + if (dex_it == config.end() || !dex_it->is_array()) { + return dex_config_list; + } + + for (const auto& dex_value : *dex_it) { + if (!dex_value.is_object()) { + continue; + } + + ConnectorDexConfig dex_config; + const std::string dex = dex_value.value("id", ""); + + if (dex == "orca_clmm") { + dex_config.dex_name = ConnectorVenue::orca_clmm; + dex_config.pool_typ = + ::rakurai::dex_connector::data_types::pool_type::clmm; + } else if (dex == "orca_amm") { + dex_config.dex_name = ConnectorVenue::orca_amm; + dex_config.pool_typ = + ::rakurai::dex_connector::data_types::pool_type::amm; + } else if (dex == "raydium_amm") { + dex_config.dex_name = ConnectorVenue::raydium_amm; + dex_config.pool_typ = + ::rakurai::dex_connector::data_types::pool_type::amm; + } else if (dex == "raydium_cpmm") { + dex_config.dex_name = ConnectorVenue::raydium_cpmm; + dex_config.pool_typ = + ::rakurai::dex_connector::data_types::pool_type::cpmm; + } else if (dex == "raydium_clmm") { + dex_config.dex_name = ConnectorVenue::raydium_clmm; + dex_config.pool_typ = + ::rakurai::dex_connector::data_types::pool_type::clmm; + } else { + LOG(md, Warn, "Skipping unsupported Rakurai dex config '{}'", dex); + continue; + } + + const auto pools_it = dex_value.find("pool_ids"); + if (pools_it != dex_value.end() && pools_it->is_array()) { + for (const auto& pool_value : *pools_it) { + dex_config.interested_pools.emplace_back( + ::rakurai::dex_connector::solanaAddressToBytes( + pool_value.get())); + } + } + + dex_config_list.push_back(std::move(dex_config)); + } + + return dex_config_list; +} + +nlohmann::json load_json_file(const std::string& path) { + LOG(md, Info, "Rakurai shared runtime loading config file: {}", path); + std::ifstream config_file(path); + if (!config_file.is_open()) { + throw std::runtime_error("Unable to open Rakurai config: " + path); + } + + nlohmann::json config; + config_file >> config; + + const auto nested_config_it = config.find("json_conf_file_name"); + if (nested_config_it != config.end() && nested_config_it->is_string() && + !config.contains("dex")) { + const auto nested_name = nested_config_it->get(); + auto nested_path = boost::filesystem::path(nested_name); + if (nested_path.is_relative()) { + nested_path = boost::filesystem::path(path).parent_path() / nested_path; + } + LOG(md, Info, + "Rakurai shared runtime resolved wrapper config '{}' -> '{}'. wrapper:{}", + path, + nested_path.string(), + config.dump()); + return load_json_file(nested_path.string()); + } + + LOG(md, Info, + "Rakurai shared runtime loaded connector config from '{}': {}", + path, + config.dump()); + return config; +} + +} // namespace + +struct RakuraiSharedRuntime::PImpl { + struct MarketDataSubscriber { + std::size_t id = 0; + std::shared_ptr listener; + std::shared_ptr sec_id_store; + api::domain::Venue venue = api::domain::VENUE_UNSPECIFIED; + std::size_t depth_limit = domain::Book::max_depth(); + std::unordered_set excluded_securities; + std::unordered_map> + securities; + std::unordered_map pool_to_security_id; + std::unordered_map pool_quantity_scales; + std::mutex mutex; + }; + + struct ListenerAdapter final : public ::rakurai::dex_connector::dex_listener { + explicit ListenerAdapter(PImpl& owner) : owner(owner) {} + + void on_pool_definition( + ConnectorVenue venue, + const std::vector& securities) override { + owner.on_pool_definition(venue, securities); + } + + void on_book( + ConnectorVenue venue, + const std::array& pool_id, + const std::vector& bids, + const std::vector& asks) override { + owner.on_book(venue, pool_id, bids, asks); + } + + void on_order_update( + ::rakurai::dex_connector::order_entry::order_state order_state) + override { + owner.on_order_update(order_state); + } + + void on_ata_verification_status( + const std::array& token_id, + const std::string& ata_address, + ::rakurai::dex_connector::ata_status status) override { + LOG(md, Info, + "Rakurai ATA verification. token:{} ata:{} status:{}", + short_hex(token_id), + ata_address, + static_cast(status)); + } + + PImpl& owner; + }; + + mutable std::mutex mutex; + std::optional config; + std::string config_label; + std::optional run_thread; + std::unique_ptr<::rakurai::dex_connector::dex_handler> handler; + std::unique_ptr<::rakurai::dex_connector::trading_session_interface> + trading_session; + persistence::SqliteStore pool_alias_store{"rakurai_pool_alias"}; + ListenerAdapter listener{*this}; + std::unordered_map> + market_data_subscribers; + std::unordered_map order_update_subscribers; + std::unordered_map status_subscribers; + std::unordered_map pool_metadata; + std::unordered_map short_pool_to_full_pool; + std::unordered_set expected_pool_ids; + std::unordered_set resolved_pool_ids; + std::size_t next_subscriber_id = 1; + bool started = false; + bool connected = false; + + ~PImpl() { + if (run_thread.has_value() && run_thread->joinable()) { + run_thread->detach(); + } + } + + std::size_t next_id_locked() { return next_subscriber_id++; } + + void ensure_started(const nlohmann::json& cfg, const std::string& label) { + std::lock_guard lock(mutex); + LOG(md, Info, + "Rakurai shared runtime ensure_started requested. label:{} config:{}", + label, + cfg.dump()); + if (!config.has_value()) { + config = cfg; + config_label = label; + } else if (*config != cfg) { + throw std::runtime_error( + "Rakurai shared runtime already configured with a different config source: " + + config_label + " vs " + label); + } + + if (started) { + LOG(md, Info, + "Rakurai shared runtime already started. label:{} connected:{}", + config_label, + connected); + return; + } + started = true; + LOG(md, Info, + "Rakurai shared runtime launching thread. label:{}", + config_label); + run_thread.emplace([this]() { run(); }); + } + + std::size_t add_market_data_subscriber( + std::shared_ptr listener_in, + std::shared_ptr sec_id_store, + api::domain::Venue venue, + std::uint32_t num_depth_levels, + const std::vector& excluded_securities) { + auto sub = std::make_shared(); + sub->listener = std::move(listener_in); + sub->sec_id_store = std::move(sec_id_store); + sub->venue = venue; + sub->depth_limit = + num_depth_levels == 0 + ? domain::Book::max_depth() + : std::min( + num_depth_levels, domain::Book::max_depth()); + sub->excluded_securities.insert( + excluded_securities.begin(), excluded_securities.end()); + + std::vector snapshot; + { + std::lock_guard lock(mutex); + sub->id = next_id_locked(); + market_data_subscribers.emplace(sub->id, sub); + snapshot.reserve(pool_metadata.size()); + for (const auto& [_, metadata] : pool_metadata) { + if (venue_matches(venue, metadata.security.venue)) { + snapshot.push_back(metadata); + } + } + } + + replay_pool_definitions(*sub, snapshot); + return sub->id; + } + + void remove_market_data_subscriber(std::size_t id) { + std::lock_guard lock(mutex); + market_data_subscribers.erase(id); + } + + std::size_t add_order_update_subscriber(OrderUpdateCallback callback) { + std::lock_guard lock(mutex); + const auto id = next_id_locked(); + order_update_subscribers.emplace(id, std::move(callback)); + return id; + } + + void remove_order_update_subscriber(std::size_t id) { + std::lock_guard lock(mutex); + order_update_subscribers.erase(id); + } + + std::size_t add_status_subscriber(StatusCallback callback) { + bool current_connected = false; + std::size_t id = 0; + { + std::lock_guard lock(mutex); + id = next_id_locked(); + status_subscribers.emplace(id, callback); + current_connected = connected; + } + callback(current_connected); + return id; + } + + void remove_status_subscriber(std::size_t id) { + std::lock_guard lock(mutex); + status_subscribers.erase(id); + } + + bool is_connected() const { + std::lock_guard lock(mutex); + return connected; + } + + void launch_new_order(const RakuraiSharedRuntime::NewOrder& order) { + std::lock_guard lock(mutex); + if (!connected || trading_session == nullptr) { + throw std::runtime_error("Rakurai execution session is not connected"); + } + trading_session->launch_new_order(order); + } + + std::optional find_pool_metadata( + api::Venue venue, + const std::string& short_pool_id) const { + std::lock_guard lock(mutex); + const auto pool_it = short_pool_to_full_pool.find(short_pool_id); + if (pool_it == short_pool_to_full_pool.end()) { + return std::nullopt; + } + const auto metadata_it = pool_metadata.find(pool_it->second); + if (metadata_it == pool_metadata.end() || + !venue_matches(venue, metadata_it->second.security.venue)) { + return std::nullopt; + } + return metadata_it->second; + } + + void run() { + try { + nlohmann::json connector_config; + { + std::lock_guard lock(mutex); + connector_config = *config; + } + + auto dex_configs = parse_dex_configs(connector_config); + if (dex_configs.empty()) { + throw std::runtime_error("No Rakurai dex pools configured"); + } + LOG(md, Info, + "Rakurai shared runtime parsed config. dex_configs:{} config:{}", + dex_configs.size(), + connector_config.dump()); + { + std::lock_guard lock(mutex); + expected_pool_ids.clear(); + resolved_pool_ids.clear(); + for (const auto& dex_config : dex_configs) { + for (const auto& pool_id : dex_config.interested_pools) { + expected_pool_ids.insert(bytes_to_hex(pool_id)); + } + } + } + + ::rakurai::dex_connector::configuration::logging_config logging_config; + if (connector_config.contains("app_logging")) { + const auto& app_logging = connector_config.at("app_logging"); + logging_config.severity = + app_logging.value("severity", logging_config.severity); + logging_config.is_console_enabled = + app_logging.value("console", logging_config.is_console_enabled); + logging_config.is_file_enabled = + app_logging.value("file", logging_config.is_file_enabled); + logging_config.queue_size = + app_logging.value("queuesize", logging_config.queue_size); + } + + ::rakurai::dex_connector::configuration::wallet wallet_config; + wallet_config.wallet_pub_key = + ::rakurai::dex_connector::solanaAddressToBytes( + connector_config.at("wallet_pub").get()); + const auto& wallet_pvt = connector_config.at("wallet_pvt"); + if (!wallet_pvt.is_array() || + wallet_pvt.size() != wallet_config.wallet_pvt_key.size()) { + throw std::runtime_error( + "Rakurai wallet_pvt must contain 64 integer entries"); + } + for (std::size_t i = 0; i < wallet_config.wallet_pvt_key.size(); ++i) { + wallet_config.wallet_pvt_key[i] = + wallet_pvt.at(i).get(); + } + + std::list<::rakurai::dex_connector::configuration::uri_config> uris; + auto add_uri = [&](const char* md_key, const char* oms_key) { + ::rakurai::dex_connector::configuration::uri_config uri; + uri.md_server_uri = + connector_config.at(md_key).get(); + uri.node_oms_uri = + connector_config.at(oms_key).get(); + uris.push_back(std::move(uri)); + }; + add_uri("md_server_uri_1", "node_oms_uri_1"); + const bool cluster_mode = + connector_config.value("cluster_mode", false); + if (cluster_mode) { + add_uri("md_server_uri_2", "node_oms_uri_2"); + } + + const auto core = connector_config.value("core", 0U); + + { + std::lock_guard lock(mutex); + handler = std::make_unique<::rakurai::dex_connector::dex_handler>( + dex_configs, + wallet_config, + logging_config, + uris, + core, + cluster_mode, + &listener); + trading_session = std::make_unique< + ::rakurai::dex_connector::trading_session_interface>( + handler->create_new_trading_session()); + connected = false; + } + + handler->run(); + } catch (const std::exception& ex) { + LOG(md, Error, "Rakurai shared runtime failed: {}", std::string(ex.what())); + notify_md_error(ex.what()); + } + + { + std::lock_guard lock(mutex); + connected = false; + } + notify_status(false); + } + + void notify_status(bool is_connected_now) { + std::vector callbacks; + { + std::lock_guard lock(mutex); + for (const auto& [_, callback] : status_subscribers) { + callbacks.push_back(callback); + } + } + for (auto& callback : callbacks) { + callback(is_connected_now); + } + } + + void notify_md_error(const std::string& message) { + std::vector> listeners; + { + std::lock_guard lock(mutex); + for (const auto& [_, sub] : market_data_subscribers) { + listeners.push_back(sub->listener); + } + } + for (const auto& listener_ptr : listeners) { + if (!listener_ptr) { + continue; + } + md::MarketDataError error; + error.type = md::MarketDataError::DISCONNECTION; + error.message = message; + listener_ptr->on_error(error); + } + } + + void load_aliases_locked() { + if (!short_pool_to_full_pool.empty()) { + return; + } + + constexpr std::string_view kPoolAliasNamespace = "rakurai_pool_alias"; + for (const auto& [ns, rows] : pool_alias_store.retrieve_all()) { + if (ns != kPoolAliasNamespace) { + continue; + } + for (const auto& [full_pool_id, alias] : rows) { + if (!alias.empty()) { + short_pool_to_full_pool.emplace(alias, full_pool_id); + } + } + } + } + + std::string get_or_add_short_pool_id_locked(const std::string& pool_id) { + constexpr std::string_view kPoolAliasNamespace = "rakurai_pool_alias"; + const auto [exists, rowid, _] = + pool_alias_store.retrieve_one(std::string(kPoolAliasNamespace), pool_id); + if (exists) { + const auto alias = std::to_string(rowid); + short_pool_to_full_pool.emplace(alias, pool_id); + return alias; + } + + const auto new_id = + pool_alias_store.insert(std::string(kPoolAliasNamespace), pool_id, ""); + const auto alias = std::to_string(new_id); + short_pool_to_full_pool.emplace(alias, pool_id); + return alias; + } + + void on_pool_definition( + ConnectorVenue connector_venue, + const std::vector& securities) { + std::vector> subscribers; + std::vector touched; + bool should_notify_ready = false; + std::size_t resolved_pool_count = 0; + std::size_t expected_pool_count = 0; + { + std::lock_guard lock(mutex); + load_aliases_locked(); + for (const auto* security : securities) { + if (security == nullptr) { + continue; + } + const auto pool_id = bytes_to_hex(security->pool_id); + pool_metadata[pool_id] = PoolMetadata{ + *security, + pool_id, + get_or_add_short_pool_id_locked(pool_id), + quantity_scale_from_decimals(security->token_a.decimals)}; + resolved_pool_ids.insert(pool_id); + touched.push_back(pool_metadata.at(pool_id)); + } + + const bool has_trading_session = trading_session != nullptr; + const bool has_pool_definitions = !resolved_pool_ids.empty(); + const bool all_expected_pools_resolved = + expected_pool_ids.empty() + ? has_pool_definitions + : std::all_of( + expected_pool_ids.begin(), + expected_pool_ids.end(), + [this](const std::string& pool_id) { + return resolved_pool_ids.contains(pool_id); + }); + if (!connected && has_trading_session && all_expected_pools_resolved) { + connected = true; + should_notify_ready = true; + resolved_pool_count = resolved_pool_ids.size(); + expected_pool_count = expected_pool_ids.size(); + } + + for (const auto& [_, sub] : market_data_subscribers) { + if (venue_matches(sub->venue, connector_venue)) { + subscribers.push_back(sub); + } + } + } + + if (should_notify_ready) { + LOG(md, Info, + "Rakurai shared runtime ready. resolved_pools:{} expected_pools:{}", + resolved_pool_count, + expected_pool_count); + notify_status(true); + } + + for (const auto& sub : subscribers) { + replay_pool_definitions(*sub, touched); + } + } + + void replay_pool_definitions( + MarketDataSubscriber& sub, + const std::vector& metadata_list) { + std::lock_guard lock(sub.mutex); + std::vector emitted; + + for (const auto& metadata : metadata_list) { + if (!venue_matches(sub.venue, metadata.security.venue)) { + continue; + } + + const auto& security = metadata.security; + const auto& pool_id = metadata.full_pool_id; + const auto desc = build_security_desc(security, metadata.short_pool_id); + const auto symbol = token_display_name(security.token_a); + const auto currency = token_display_name(security.token_b); + if (sub.excluded_securities.contains(pool_id) || + sub.excluded_securities.contains(metadata.short_pool_id) || + sub.excluded_securities.contains(desc)) { + continue; + } + + domain::Security emitted_security; + emitted_security.security = domain::SpotSecurity{}; + emitted_security.security_common.venue = sub.venue; + emitted_security.security_common.exchange_sec_id = metadata.short_pool_id; + emitted_security.security_common.symbol = symbol; + emitted_security.security_common.currency = currency; + emitted_security.security_common.asset = symbol; + emitted_security.security_common.group = + connector_venue_name(security.venue); + emitted_security.security_common.desc = desc; + emitted_security.security_common.min_price_increment = + security.min_tick > 0.0 ? security.min_tick : 0.0; + emitted_security.security_common.min_quantity_increment = + static_cast(security.min_trade_amount); + emitted_security.security_common.min_quantity = + static_cast(security.min_trade_amount); + *emitted_security.security_common.id = sub.sec_id_store->get_or_add( + sub.venue, api::domain::SECURITY_TYPE_SPOT, pool_id); + + auto [it, is_new] = sub.securities.try_emplace( + pool_id, std::make_unique(std::move(emitted_security))); + if (!is_new) { + *it->second = std::move(emitted_security); + } + + sub.pool_to_security_id[pool_id] = it->second->id(); + sub.pool_quantity_scales[pool_id] = metadata.quantity_scale; + emitted.push_back(it->second.get()); + } + + if (!emitted.empty() && sub.listener) { + sub.listener->on_securities(emitted); + } + } + + void on_book( + ConnectorVenue connector_venue, + const std::array& pool_id, + const std::vector& bids, + const std::vector& asks) { + std::vector> subscribers; + { + std::lock_guard lock(mutex); + for (const auto& [_, sub] : market_data_subscribers) { + if (venue_matches(sub->venue, connector_venue)) { + subscribers.push_back(sub); + } + } + } + + const auto pool_key = bytes_to_hex(pool_id); + for (const auto& sub : subscribers) { + std::lock_guard lock(sub->mutex); + const auto it = sub->pool_to_security_id.find(pool_key); + if (it == sub->pool_to_security_id.end()) { + continue; + } + + const auto quantity_scale_it = sub->pool_quantity_scales.find(pool_key); + const auto quantity_scale = + quantity_scale_it != sub->pool_quantity_scales.end() + ? quantity_scale_it->second + : 1.0; + + auto book = domain::Book::empty(it->second); + const auto bid_depth = + std::min(sub->depth_limit, bids.size()); + const auto ask_depth = + std::min(sub->depth_limit, asks.size()); + + for (std::size_t i = 0; i < bid_depth; ++i) { + book.bids()[i] = to_book_level(bids[i], quantity_scale); + } + for (std::size_t i = 0; i < ask_depth; ++i) { + book.asks()[i] = to_book_level(asks[i], quantity_scale); + } + + if (sub->listener) { + sub->listener->on_book(book); + } + } + } + + void on_order_update( + ::rakurai::dex_connector::order_entry::order_state order_state) { + std::vector callbacks; + { + std::lock_guard lock(mutex); + for (const auto& [_, callback] : order_update_subscribers) { + callbacks.push_back(callback); + } + } + + for (auto& callback : callbacks) { + callback(order_state); + } + } +}; + +RakuraiSharedRuntime& RakuraiSharedRuntime::instance() { + static RakuraiSharedRuntime runtime; + return runtime; +} + +RakuraiSharedRuntime::RakuraiSharedRuntime() + : me_(std::make_unique()) {} + +RakuraiSharedRuntime::~RakuraiSharedRuntime() = default; + +std::size_t RakuraiSharedRuntime::add_market_data_subscriber( + std::shared_ptr listener, + std::shared_ptr sec_id_store, + api::domain::Venue venue, + std::uint32_t num_depth_levels, + const std::vector& excluded_securities) { + return me_->add_market_data_subscriber( + std::move(listener), + std::move(sec_id_store), + venue, + num_depth_levels, + excluded_securities); +} + +void RakuraiSharedRuntime::remove_market_data_subscriber(std::size_t id) { + me_->remove_market_data_subscriber(id); +} + +std::size_t RakuraiSharedRuntime::add_order_update_subscriber( + OrderUpdateCallback callback) { + return me_->add_order_update_subscriber(std::move(callback)); +} + +void RakuraiSharedRuntime::remove_order_update_subscriber(std::size_t id) { + me_->remove_order_update_subscriber(id); +} + +std::size_t RakuraiSharedRuntime::add_status_subscriber(StatusCallback callback) { + return me_->add_status_subscriber(std::move(callback)); +} + +void RakuraiSharedRuntime::remove_status_subscriber(std::size_t id) { + me_->remove_status_subscriber(id); +} + +void RakuraiSharedRuntime::ensure_started_from_file( + const std::string& config_path) { + me_->ensure_started(load_json_file(config_path), config_path); +} + +void RakuraiSharedRuntime::ensure_started_from_json( + const nlohmann::json& config, + const std::string& config_label) { + me_->ensure_started(config, config_label); +} + +bool RakuraiSharedRuntime::is_connected() const { return me_->is_connected(); } + +void RakuraiSharedRuntime::launch_new_order(const NewOrder& order) { + me_->launch_new_order(order); +} + +std::optional +RakuraiSharedRuntime::find_pool_metadata( + api::Venue venue, + const std::string& short_pool_id) const { + return me_->find_pool_metadata(venue, short_pool_id); +} + +} // namespace bts::rakurai diff --git a/src/app_sparkd/sparkd/rakurai/RakuraiSharedRuntime.h b/src/app_sparkd/sparkd/rakurai/RakuraiSharedRuntime.h new file mode 100644 index 000000000..87311c9ce --- /dev/null +++ b/src/app_sparkd/sparkd/rakurai/RakuraiSharedRuntime.h @@ -0,0 +1,78 @@ +#pragma once + +#include "spark/api/domain/Venue.pb.h" +#include "spark/domain_model/Security.pb.h" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace bts::md { +class IMarketDataListener; +} + +namespace bts::persistence { +class IDGenerator; +} + +namespace bts::rakurai { + +class RakuraiSharedRuntime { + public: + using OrderUpdate = ::rakurai::dex_connector::order_entry::order_state; + using NewOrder = ::rakurai::dex_connector::order_entry::new_order; + using OrderUpdateCallback = std::function; + using StatusCallback = std::function; + + struct PoolMetadata { + ::rakurai::dex_connector::data_types::security security; + std::string full_pool_id; + std::string short_pool_id; + double quantity_scale = 1.0; + }; + + static RakuraiSharedRuntime& instance(); + + std::size_t add_market_data_subscriber( + std::shared_ptr listener, + std::shared_ptr sec_id_store, + api::domain::Venue venue, + std::uint32_t num_depth_levels, + const std::vector& excluded_securities); + void remove_market_data_subscriber(std::size_t id); + + std::size_t add_order_update_subscriber(OrderUpdateCallback callback); + void remove_order_update_subscriber(std::size_t id); + + std::size_t add_status_subscriber(StatusCallback callback); + void remove_status_subscriber(std::size_t id); + + void ensure_started_from_file(const std::string& config_path); + void ensure_started_from_json( + const nlohmann::json& config, + const std::string& config_label); + + bool is_connected() const; + void launch_new_order(const NewOrder& order); + std::optional find_pool_metadata( + api::Venue venue, + const std::string& short_pool_id) const; + + private: + RakuraiSharedRuntime(); + ~RakuraiSharedRuntime(); + RakuraiSharedRuntime(const RakuraiSharedRuntime&) = delete; + RakuraiSharedRuntime& operator=(const RakuraiSharedRuntime&) = delete; + + struct PImpl; + std::unique_ptr me_; +}; + +} // namespace bts::rakurai