build: Move okx execution to its own lib
This commit is contained in:
1 parent
66718ed77e
commit
c03f0fcf0e
8 files changed
+298
-3
No files matched your search
@@ -115,6 +115,7 @@ else()
|
||||
# subdirs
|
||||
if (BUILD_SPARKD)
|
||||
add_subdirectory(src/lib_binance_execution)
|
||||
add_subdirectory(src/lib_okx_execution)
|
||||
add_subdirectory(src/app_sparkd)
|
||||
add_subdirectory(src/data_files)
|
||||
endif()
|
||||
|
||||
@@ -115,6 +115,7 @@ target_link_libraries(
|
||||
sparkd
|
||||
spark
|
||||
binance_execution
|
||||
okx_execution
|
||||
databento::databento
|
||||
${emumba-deribit-connector-library_LIBRARIES}
|
||||
tbb
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "binance_execution/gateway_config.h"
|
||||
#include "binance_execution/spots/spots_gateway.h"
|
||||
#include "binance_execution/usds_futures/usds_futures_gateway.h"
|
||||
#include "okx_execution/okx_execution_gateway.h"
|
||||
#include "spark/domain_model/Security.pb.h"
|
||||
#include "spark/io/JsonReader.h"
|
||||
#include "spark/orderman/gateways/CCAPIExecutionGateway.h"
|
||||
@@ -12,7 +13,6 @@
|
||||
#include "spark/orderman/gateways/GeneralExecutionGateway.h"
|
||||
#include "spark/orderman/gateways/ICEExecutionGateway.h"
|
||||
#include "spark/orderman/gateways/IExecutionGenericAdapter.h"
|
||||
#include "spark/orderman/gateways/OkxExecutionGateway.h"
|
||||
#include "spark/orderman/gateways/OnixFIXAdapter.h"
|
||||
#include "spark/orderman/gateways/sim/SimExecutionGatewayFactory.h"
|
||||
#include "spark/orderman/gateways/sim/SimExecutionGatewayUniqueWrapper.h"
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
cmake_minimum_required(VERSION 3.23)
|
||||
project(okx_execution)
|
||||
include(${CMAKE_SOURCE_DIR}/cmake/InitLibrary.cmake)
|
||||
|
||||
init_library(STATIC)
|
||||
target_link_libraries(
|
||||
${PROJECT_NAME}
|
||||
PUBLIC
|
||||
spark
|
||||
onixs_fix
|
||||
)
|
||||
@@ -0,0 +1,5 @@
|
||||
@PACKAGE_INIT@
|
||||
|
||||
include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")
|
||||
|
||||
check_required_components(@PROJECT_NAME@)
|
||||
+2
-2
@@ -1,4 +1,5 @@
|
||||
#include "OkxExecutionGateway.h"
|
||||
#include "okx_execution_gateway.h"
|
||||
#include "okx_execution/okx_utils.h"
|
||||
#include "spark/api/service_helpers/AdminServiceUtils.h"
|
||||
#include "spark/api/services/OrderService.pb.h"
|
||||
#include "spark/config/protos/okx_exe_cfg.pb.h"
|
||||
@@ -17,7 +18,6 @@
|
||||
#include "spark/orderman/gateways/IExecutionGateway.h"
|
||||
#include "spark/orderman/orderman_utils/ExchangePosition.h"
|
||||
#include "spark/orderman/orderman_utils/ExecutionReport.h"
|
||||
#include "spark/orderman/orderman_utils/OkxUtils.h"
|
||||
#include "spark/orderman/orderman_utils/orderman_common.h"
|
||||
#include "spark/utils/Uuid.h"
|
||||
#include "spark/utils/bts_assert.h"
|
||||
File renamed without changes.
@@ -0,0 +1,277 @@
|
||||
#include "spark/api/services/OrderService.pb.h"
|
||||
#include "spark/domain_model/Security.pb.h"
|
||||
#include "spark/logging/logging.h"
|
||||
#include "spark/md/SecurityOracle.h"
|
||||
#include "spark/orderman/CreateOrderMsg.h"
|
||||
#include "spark/orderman/gateways/IExecutionGateway.h"
|
||||
#include "spark/orderman/orderman_utils/ExecutionReport.h"
|
||||
#include "spark/utils/bts_assert.h"
|
||||
|
||||
#include <OnixS/FIXEngine/FIX/Exception.h>
|
||||
#include <OnixS/FIXEngine/FIX/FIX42.h>
|
||||
#include <OnixS/FIXEngine/FIX/Message.h>
|
||||
#include <OnixS/FIXEngine/FIX/Numeric.h>
|
||||
#include <OnixS/FIXEngine/FIX/ProtocolVersion.h>
|
||||
#include <boost/pool/object_pool.hpp>
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <ctime>
|
||||
#include <exception>
|
||||
#include <fmt/core.h>
|
||||
#include <map>
|
||||
#include <nlohmann/json_fwd.hpp>
|
||||
#include <openssl/buffer.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/hmac.h>
|
||||
#include <quickfix/FixFields.h>
|
||||
#include <quickfix/Utility.h>
|
||||
#include <regex>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <unistd.h>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace bts::orderman {
|
||||
using json = nlohmann::json;
|
||||
class OkxUtils {
|
||||
public:
|
||||
// Function template to get a value from a JSON object by key
|
||||
template <typename T>
|
||||
static inline T getJsonValue(
|
||||
const json &jsonObject,
|
||||
const std::string &key,
|
||||
const T &defaultValue = T()) {
|
||||
if (jsonObject.contains(key) && jsonObject[key].is_primitive()) {
|
||||
return jsonObject.value(key, defaultValue);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
// Function to parse JSON safely
|
||||
static inline std::optional<json> parseJsonSafely(
|
||||
const std::string &jsonString) {
|
||||
try {
|
||||
return json::parse(jsonString);
|
||||
} catch (const std::exception &e) {
|
||||
std::string s = e.what();
|
||||
LOG(OrderMan, bts::Error, "Failed to parse JSON: {}", s);
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
struct OrderResponse {
|
||||
bool accepted;
|
||||
std::string message;
|
||||
};
|
||||
|
||||
static inline OrderResponse orderAccepted(const std::string &jsonString) {
|
||||
auto body = parseJsonSafely(jsonString);
|
||||
if (!body)
|
||||
return {false, "NO Body"};
|
||||
auto &jsonResponse = *body;
|
||||
|
||||
return {
|
||||
jsonResponse.value("code", "1") == "0",
|
||||
jsonResponse.value("msg", "NO code or message")};
|
||||
}
|
||||
|
||||
// Function to extract order information from JSON
|
||||
static inline std::tuple<std::string, std::string, std::string> extractOrderInfo(
|
||||
const std::string &jsonString) {
|
||||
auto body = parseJsonSafely(jsonString);
|
||||
if (!body.has_value()) {
|
||||
return {"", "", "json parsing error no body"};
|
||||
}
|
||||
auto jsonResponse = body.value();
|
||||
if (jsonResponse.contains("data") && jsonResponse["data"].is_array()) {
|
||||
const auto &dataArray = jsonResponse["data"];
|
||||
if (!dataArray.empty() && dataArray[0].is_object()) {
|
||||
const auto &firstItem = dataArray[0];
|
||||
|
||||
// Extract values using the templated function
|
||||
std::string ordId =
|
||||
OkxUtils::getJsonValue(firstItem, "ordId", std::string());
|
||||
std::string timestamp =
|
||||
OkxUtils::getJsonValue(firstItem, "ts", std::string());
|
||||
std::string sMsg =
|
||||
OkxUtils::getJsonValue(firstItem, "sMsg", std::string());
|
||||
|
||||
return {ordId, timestamp, sMsg};
|
||||
}
|
||||
}
|
||||
return {"", "", "json parsing error"};
|
||||
}
|
||||
// Function to encode a byte array to Base64
|
||||
static std::string base64Encode(const unsigned char *buffer, size_t length) {
|
||||
BIO *bmem = BIO_new(BIO_s_mem());
|
||||
BIO *b64 = BIO_new(BIO_f_base64());
|
||||
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); // No newlines
|
||||
BIO_push(b64, bmem);
|
||||
BIO_write(b64, buffer, static_cast<int>(length));
|
||||
BIO_flush(b64);
|
||||
|
||||
BUF_MEM *bptr;
|
||||
BIO_get_mem_ptr(b64, &bptr);
|
||||
std::string result(bptr->data, bptr->length);
|
||||
|
||||
BIO_free_all(b64);
|
||||
return result;
|
||||
}
|
||||
|
||||
static inline std::vector<unsigned char> generateSignature(
|
||||
const std::string &prehash, const std::string &secretKey) {
|
||||
unsigned int len = EVP_MAX_MD_SIZE;
|
||||
std::vector<unsigned char> result(len);
|
||||
HMAC(
|
||||
EVP_sha256(), secretKey.c_str(), static_cast<int>(secretKey.length()),
|
||||
reinterpret_cast<const unsigned char *>(prehash.c_str()),
|
||||
prehash.length(), result.data(), &len);
|
||||
result.resize(len);
|
||||
return result;
|
||||
}
|
||||
static inline std::string getSign(
|
||||
std::string message, const std::string &secret_key = "your_secret_key") {
|
||||
auto signature = generateSignature(message, secret_key);
|
||||
std::string sign = base64Encode(signature.data(), signature.size());
|
||||
|
||||
return sign;
|
||||
}
|
||||
static std::tuple<long, std::string> getLoginSignTime(
|
||||
const std::string &secret_key = "your_secret_key",
|
||||
const std::string &method = "GET",
|
||||
const std::string &path = "/users/self/verify") {
|
||||
auto seconds = bts::core::TimeStamp::now().seconds();
|
||||
std::string message = std::to_string(seconds) + method + path;
|
||||
return {seconds, getSign(message, secret_key)};
|
||||
}
|
||||
static std::string getUTCTimestamp(bts::core::TimeStamp t) {
|
||||
auto rawTimestamp =
|
||||
t.to_iso_string() +
|
||||
"Z"; //gives 20241213T175033.840123Z should be 2024-12-13T17:50:33.840Z
|
||||
// Use regex to parse and reformat
|
||||
std::regex re(
|
||||
R"((\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})\.(\d{3})\d*Z)");
|
||||
std::string formattedTimestamp =
|
||||
std::regex_replace(rawTimestamp, re, "$1-$2-$3T$4:$5:$6.$7Z");
|
||||
|
||||
return formattedTimestamp;
|
||||
}
|
||||
static std::array<char, 32> stringToFixedArray(const std::string &s) {
|
||||
std::array<char, 32> idAsArray{};
|
||||
std::copy_n(
|
||||
s.begin(), std::min(s.size(), idAsArray.size() - 1),
|
||||
idAsArray.begin());
|
||||
return idAsArray;
|
||||
}
|
||||
static std::unordered_map<std::string, std::string> createHeaders(
|
||||
const std::string &apiKey,
|
||||
const std::string &passphrase,
|
||||
const std::string &secretKey,
|
||||
const std::string &method,
|
||||
const std::string &requestPath,
|
||||
const bool simulated_trading,
|
||||
const std::string &body = "") {
|
||||
auto ts = bts::core::TimeStamp::now();
|
||||
std::string timestamp = getUTCTimestamp(ts);
|
||||
std::string prehash = timestamp + method + requestPath + body;
|
||||
auto signature = getSign(prehash, secretKey);
|
||||
LOG(OrderMan, bts::Info, "prehash '{}' , key '{}'", prehash, signature);
|
||||
// Prepare headers
|
||||
std::unordered_map<std::string, std::string> headers = {
|
||||
{"accept", "application/json"},
|
||||
{"Content-Type", "application/json"},
|
||||
{"OK-ACCESS-KEY", apiKey},
|
||||
{"OK-ACCESS-SIGN", signature},
|
||||
{"OK-ACCESS-TIMESTAMP", timestamp},
|
||||
{"OK-ACCESS-PASSPHRASE", passphrase}};
|
||||
if (simulated_trading){
|
||||
headers.insert({"x-simulated-trading", "1"});
|
||||
}
|
||||
|
||||
return headers;
|
||||
}
|
||||
static std::string generateCurlCommand(
|
||||
const std::string &url,
|
||||
const std::unordered_map<std::string, std::string> &headers,
|
||||
const std::string &body) {
|
||||
std::ostringstream oss;
|
||||
|
||||
// Start with the curl command
|
||||
std::string curlCommand = "curl -X POST " + url + " \\\n";
|
||||
|
||||
// Add headers to the curl command
|
||||
for (const auto &header : headers) {
|
||||
curlCommand +=
|
||||
" -H \"" + header.first + ": " + header.second + "\" \\\n";
|
||||
}
|
||||
|
||||
// Add bodyl
|
||||
curlCommand += " -d '" + body + "'";
|
||||
|
||||
return curlCommand;
|
||||
}
|
||||
static inline api::SecurityType getInstType(const std::string &type) {
|
||||
static const std::unordered_map<std::string, api::SecurityType> type_map =
|
||||
{{"SPOT", api::SECURITYTYPE_SPOT},
|
||||
{"OPTION", api::SECURITYTYPE_OPTION},
|
||||
{"FUTURES", api::SECURITYTYPE_FUTURE},
|
||||
{"SWAP", api::SECURITYTYPE_FUTURE}};
|
||||
|
||||
auto it = type_map.find(type);
|
||||
return it != type_map.end() ? it->second : api::SECURITYTYPE_UNSET;
|
||||
}
|
||||
static inline api::orderman::NativeOrderType getOrdType(const std::string &type) {
|
||||
static const std::unordered_map<std::string, api::orderman::NativeOrderType> type_map =
|
||||
{{"limit", api::orderman::NATIVE_ORDER_TYPE_LIMIT},
|
||||
{"market", api::orderman::NATIVE_ORDER_TYPE_MARKET}};
|
||||
|
||||
auto it = type_map.find(type);
|
||||
return it != type_map.end() ? it->second : api::orderman::NativeOrderType::NATIVE_ORDER_TYPE_UNDEFINED;
|
||||
}
|
||||
static void populateCreateOrderFields(
|
||||
ExecutionReport &er, const CreateOrderMsg &create_order_msg) {
|
||||
er.security = create_order_msg.security;
|
||||
er.order_type = create_order_msg.order_type;
|
||||
er.time_in_force = create_order_msg.time_in_force;
|
||||
er.manual_order_indicator = create_order_msg.manual_order_indicator;
|
||||
er.side = create_order_msg.side;
|
||||
er.order_key = create_order_msg.key;
|
||||
to_array(
|
||||
create_order_msg.routing_record->account(), &er.account_name.value());
|
||||
to_array(
|
||||
create_order_msg.routing_record->operator_id(),
|
||||
&er.operator_id.value());
|
||||
er.exchange = create_order_msg.security->exchange();
|
||||
}
|
||||
static void populateExecTypeAndOrderStatus(
|
||||
ExecutionReport &er, const std::string &state, const std::string &cncl_src) {
|
||||
if (state == "canceled") {
|
||||
if(cncl_src=="14" ){
|
||||
er.exec_type = api::orderman::ExecType::EXEC_TYPE_CANCELED;
|
||||
er.order_status = api::orderman::NATIVE_ORDER_STATUS_CANCELED;
|
||||
}else{
|
||||
er.exec_type = api::orderman::ExecType::EXEC_TYPE_REJECTED;
|
||||
er.order_status = api::orderman::NATIVE_ORDER_STATUS_REJECTED;
|
||||
}
|
||||
} else if (state == "live") {
|
||||
er.exec_type = api::orderman::ExecType::EXEC_TYPE_NEW;
|
||||
er.order_status = api::orderman::NATIVE_ORDER_STATUS_NEW;
|
||||
} else if (state == "partially_filled") {
|
||||
er.exec_type = api::orderman::ExecType::EXEC_TYPE_PARTIAL_FILL;
|
||||
er.order_status = api::orderman::NATIVE_ORDER_STATUS_PARTIALLY_FILLED;
|
||||
} else if (state == "filled") {
|
||||
er.exec_type = api::orderman::ExecType::EXEC_TYPE_FILL;
|
||||
er.order_status = api::orderman::NATIVE_ORDER_STATUS_FILLED;
|
||||
} else {
|
||||
er.exec_type = api::orderman::ExecType::EXEC_TYPE_UNDEFINED;
|
||||
er.order_status = api::orderman::NATIVE_ORDER_STATUS_UNDEFINED;
|
||||
}
|
||||
}
|
||||
static inline void handleOkException(
|
||||
const std::string &context, const std::string &error) {
|
||||
bts_assert_msgf(false, "{} Exception: {}", context, error);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace bts::orderman::okxexe
|
||||
Reference in new issue
Block a user