diff --git a/src/lib/utils/crypto/base64_chars.h b/src/lib/utils/crypto/base64_chars.h new file mode 100644 index 000000000..c261bbab6 --- /dev/null +++ b/src/lib/utils/crypto/base64_chars.h @@ -0,0 +1,8 @@ +#pragma once + +namespace bts::system::crypto { + +static const char base64_chars[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +} // namespace bts::system::crypto diff --git a/src/lib/utils/crypto/ed25519.cpp b/src/lib/utils/crypto/ed25519.cpp new file mode 100644 index 000000000..a065d12e7 --- /dev/null +++ b/src/lib/utils/crypto/ed25519.cpp @@ -0,0 +1,107 @@ +#include "ed25519.h" +#include "lib/logging/logging.h" +#include "lib/utils/crypto/base64_chars.h" +#include "lib/utils/crypto/evp_md_ctx.h" +#include "lib/utils/crypto/evp_pkey.h" +#include "lib/utils/crypto/file.h" + +#include +#include +#include +#include + +namespace bts::system::crypto { + +static void binary_to_hex( + const std::vector &binary, std::string &result) { + std::ostringstream oss; + for (unsigned char c : binary) { + oss << std::hex << std::setfill('0') << std::setw(2) + << static_cast(c); + } + result = oss.str(); +} + +static void binary_to_b64( + const std::vector &input, std::string &result) { + int val = 0, valb = -6; + for (unsigned char c : input) { + val = (val << 8) + c; + valb += 8; + while (valb >= 0) { + result.push_back(base64_chars[(val >> valb) & 0x3F]); + valb -= 6; + } + } + if (valb > -6) + result.push_back(base64_chars[((val << 8) >> (valb + 8)) & 0x3F]); + while (result.size() % 4) + result.push_back('='); +} + +bool ed25519_digest( + const std::string &payload, + const std::string &pem_file_path, + std::vector &result) { + // Load the private key + File pkey_file{pem_file_path.c_str(), "r"}; + EvpPkey pkey = EvpPkey::read_pem(pem_file_path); + if (!pkey) { + LOG(Gen, Error, "Failed to initialize pem key."); + return false; + } + // Create a signing context + EvpMdCtx mdctx = EvpMdCtx::get_new(); + if (!mdctx) { + LOG(Gen, Error, "Failed to create EVP_MD_CTX."); + return false; + } + int res = EVP_DigestSignInit(mdctx.value, NULL, NULL, NULL, pkey.value); + // Initialize signing operation + if (res <= 0) { + LOG(Gen, Error, "Failed to initialize DigestSign."); + } + // Sign the payload + size_t siglen = 0; + const unsigned char *tbs = + reinterpret_cast(payload.data()); + const size_t tbslen = payload.size(); + res = EVP_DigestSign(mdctx.value, NULL, &siglen, tbs, tbslen); + if (res <= 0) { + LOG(Gen, Error, "Failed to determine signature length."); + } + result.resize(siglen); + res = EVP_DigestSign(mdctx.value, result.data(), &siglen, tbs, tbslen); + if (res <= 0) { + LOG(Gen, Error, "Failed to compute signature"); + } + // Resize vector in case signature is shorter (very unlikely for Ed25519) + result.resize(siglen); + return true; +} + +bool ed25519_digest_b64( + const std::string &payload, + const std::string &pem_file_path, + std::string &result) { + std::vector result_bytes; + if (!ed25519_digest(payload, pem_file_path, result_bytes)) { + return false; + } + binary_to_b64(result_bytes, result); + return true; +} + +bool ed25519_digest_hex( + const std::string &payload, + const std::string &pem_file_path, + std::string &result) { + std::vector result_bytes; + if (!ed25519_digest(payload, pem_file_path, result_bytes)) { + return false; + } + binary_to_hex(result_bytes, result); + return true; +} + +} // namespace bts::system::crypto diff --git a/src/lib/utils/crypto/ed25519.h b/src/lib/utils/crypto/ed25519.h new file mode 100644 index 000000000..52042cdbf --- /dev/null +++ b/src/lib/utils/crypto/ed25519.h @@ -0,0 +1,24 @@ +#pragma once + +#include +#include +#include + +namespace bts::system::crypto { + +bool ed25519_digest( + const std::string &payload, + const std::string &pem_file_path, + std::vector &result); + +bool ed25519_digest_b64( + const std::string &payload, + const std::string &pem_file_path, + std::string &result); + +bool ed25519_digest_hex( + const std::string &payload, + const std::string &pem_file_path, + std::string &result); + +} // namespace bts::system::crypto diff --git a/src/lib/utils/crypto/evp_md_ctx.h b/src/lib/utils/crypto/evp_md_ctx.h new file mode 100644 index 000000000..fef252017 --- /dev/null +++ b/src/lib/utils/crypto/evp_md_ctx.h @@ -0,0 +1,20 @@ +#pragma once + +#include +#include + +namespace bts::system::crypto { + +struct EvpMdCtx { + EVP_MD_CTX *value; + ~EvpMdCtx() { EVP_MD_CTX_free(value); } + + static EvpMdCtx get_new() { return {EVP_MD_CTX_new()}; } + + constexpr explicit operator bool() const { return value; } + + private: + EvpMdCtx(EVP_MD_CTX *value) : value(value) {} +}; + +} // namespace bts::system::crypto diff --git a/src/lib/utils/crypto/evp_pkey.cpp b/src/lib/utils/crypto/evp_pkey.cpp new file mode 100644 index 000000000..8769772bb --- /dev/null +++ b/src/lib/utils/crypto/evp_pkey.cpp @@ -0,0 +1,17 @@ +#include "evp_pkey.h" +#include "lib/utils/crypto/file.h" + +#include +#include + +namespace bts::system::crypto { + +EvpPkey EvpPkey::read_pem(const std::string &file_path) { + File f = File::read(file_path.c_str()); + if (f.value) { + return EvpPkey{PEM_read_PrivateKey(f.value, NULL, NULL, NULL)}; + } + return {}; +} + +} // namespace bts::system::crypto diff --git a/src/lib/utils/crypto/evp_pkey.h b/src/lib/utils/crypto/evp_pkey.h new file mode 100644 index 000000000..3eb04370e --- /dev/null +++ b/src/lib/utils/crypto/evp_pkey.h @@ -0,0 +1,30 @@ +#pragma once + +#include +#include +#include + +namespace bts::system::crypto { + +struct EvpPkey { + EVP_PKEY *value = NULL; + + EvpPkey() = default; + + ~EvpPkey() { + if (value) { + EVP_PKEY_free(value); + } + } + + static EvpPkey get_new() { return {EVP_PKEY_new()}; } + + static EvpPkey read_pem(const std::string &file_path); + + constexpr explicit operator bool() const { return value; } + + private: + EvpPkey(EVP_PKEY *pkey) : value(pkey) {} +}; + +} // namespace bts::system::crypto diff --git a/src/lib/utils/crypto/file.h b/src/lib/utils/crypto/file.h new file mode 100644 index 000000000..f185627fd --- /dev/null +++ b/src/lib/utils/crypto/file.h @@ -0,0 +1,21 @@ +#pragma once + +#include +namespace bts::system::crypto { + +struct File { + FILE *value = NULL; + + File(const char *file_path, const char *modes) + : value(fopen(file_path, modes)) {} + + ~File() { + if (value) { + fclose(value); + } + } + + static File read(const char *file_path) { return {file_path, "r"}; } +}; + +} // namespace bts::system::crypto diff --git a/src/lib_binance_execution/binance_execution/spots/messages/logon_request.cpp b/src/lib_binance_execution/binance_execution/spots/messages/logon_request.cpp new file mode 100644 index 000000000..a958b1f47 --- /dev/null +++ b/src/lib_binance_execution/binance_execution/spots/messages/logon_request.cpp @@ -0,0 +1,77 @@ +#include "logon_request.h" +#include "binance_execution/type_converters.h" +#include "lib/core/TimeStamp.h" +#include "lib/utils/Uuid.h" +#include "lib/utils/crypto/ed25519.h" + +#include +#include + +namespace binance_execution::spots { + +LogonRequest LogonRequest::get_new(std::string api_key) { + LogonRequest r; + r.id = bts::utils::uuid(); + r.params.apiKey = api_key; + r.params.timestamp = convert_time_stamp(bts::core::TimeStamp::now()); + return r; +} + +LogonRequest LogonRequest::get_new( + std::string api_key, const std::string &pem_key_path) { + LogonRequest r; + r.id = bts::utils::uuid(); + r.params.apiKey = api_key; + r.params.timestamp = convert_time_stamp(bts::core::TimeStamp::now()); + if (!pem_key_path.empty()) { + r.sign(pem_key_path); + } + return r; +} + +void LogonRequest::sign(const std::string &pem_key_path) { + params.signature.clear(); + bts::system::crypto::ed25519_digest_b64( + fmt::format("apiKey={}×tamp={}", params.apiKey, params.timestamp), + pem_key_path, params.signature); +} + +void to_json(nlohmann::json &j, const LogonRequest::Params &p) { + j["apiKey"] = p.apiKey; + j["signature"] = p.signature; + j["timestamp"] = p.timestamp; +} + +void from_json(const nlohmann::json &j, LogonRequest::Params &p) { + auto it = j.find("apiKey"); + if (it != j.end()) { + it->get_to(p.apiKey); + } + it = j.find("signature"); + if (it != j.end()) { + it->get_to(p.signature); + } + it = j.find("timestamp"); + if (it != j.end()) { + it->get_to(p.timestamp); + } +} + +void to_json(nlohmann::json &j, const LogonRequest &r) { + j["id"] = r.id; + j["method"] = "session.logon"; + j["params"] = r.params; +} + +void from_json(const nlohmann::json &j, LogonRequest &r) { + auto it = j.find("id"); + if (it != j.end()) { + it->get_to(r.id); + } + it = j.find("params"); + if (it != j.end()) { + it->get_to(r.params); + } +} + +} // namespace binance_execution::spots diff --git a/src/lib_binance_execution/binance_execution/spots/messages/logon_request.h b/src/lib_binance_execution/binance_execution/spots/messages/logon_request.h new file mode 100644 index 000000000..3b159b29c --- /dev/null +++ b/src/lib_binance_execution/binance_execution/spots/messages/logon_request.h @@ -0,0 +1,43 @@ +#pragma once + +#include "lib/core/FMT_IMPL.h" + +#include +#include + +namespace binance_execution::spots { + +struct LogonRequest { + std::string id; + + struct Params { + std::string apiKey; + std::string signature; + std::int64_t timestamp; + }; + + Params params; + + static LogonRequest get_new(std::string api_key); + static LogonRequest get_new( + std::string api_key, const std::string &pem_key_path); + std::string signature_params() const; + void sign(const std::string &pem_key_path); +}; + +void to_json(nlohmann::json &j, const LogonRequest &r); +void from_json(const nlohmann::json &j, LogonRequest &r); + +void to_json(nlohmann::json &j, const LogonRequest::Params &r); +void from_json(const nlohmann::json &j, LogonRequest::Params &r); + +} // namespace binance_execution::spots + +FMT_IMPL( + binance_execution::spots::LogonRequest, + r, + R"({{ "id": "{}", "method": "session.logon", "params": {{ "apiKey": "{}", "signature": "{}", "timestamp": {} }} }})", + r.id, + r.params.apiKey, + r.params.signature, + r.params.timestamp); 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 6154dcf80..738fa847b 100644 --- a/src/lib_binance_execution/binance_execution/spots/spots_gateway.cpp +++ b/src/lib_binance_execution/binance_execution/spots/spots_gateway.cpp @@ -3,20 +3,28 @@ #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/cancel_order_request.h" +#include "binance_execution/spots/messages/create_order_request.h" +#include "binance_execution/spots/messages/logon_request.h" #include "binance_execution/spots/messages/modify_order_request.h" -#include "binance_execution/spots/rest_api_client.h" +#include "binance_execution/spots/native_order.h" #include "binance_execution/spots/spots_order_store.h" +#include "binance_execution/spots/user_data_stream/balance_update.h" +#include "binance_execution/spots/user_data_stream/order_update.h" #include "binance_execution/spots/user_data_stream/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/api/services/OrderService.pb.h" +#include "lib/collections/concurrent_queue.h" #include "lib/config/protos/jetfix.pb.h" #include "lib/domain_model/Security.h" #include "lib/domain_model/Security.pb.h" #include "lib/domain_model/Side.h" #include "lib/io/json_formatter.h" +#include "lib/io/websocket/websocket_client.h" #include "lib/logging/logging.h" #include "lib/md/SecurityOracle.h" #include "lib/mercury/events/Update.h" @@ -29,17 +37,22 @@ #include "lib/orderman/orderman_utils/ExchangePosition.h" #include "lib/orderman/orderman_utils/ExecutionReport.h" #include "lib/orderman/orderman_utils/orderman_common.h" +#include "lib/utils/Delegate.h" +#include "lib/utils/Uuid.h" +#include "lib/utils/bts_lock_guard.h" +#include "lib/utils/crypto/ed25519.h" #include "lib/utils/overloaded.h" +#include "lib/utils/thread_manager.h" -#include +#include #include +#include #include #include +#include #include #include -using nlohmann::json; - namespace binance_execution::spots { #define ASSERT_FN(test, fn, ...) \ @@ -51,10 +64,19 @@ namespace binance_execution::spots { #define WARN_FN(fn, ...) LOG_FN(bts::Warn, fn, __VA_ARGS__) #define ERROR_FN(fn, ...) LOG_FN(bts::Error, fn, __VA_ARGS__) +#define INFO(...) LOG(OrderMan, bts::Info, __VA_ARGS__) +#define ERROR(...) LOG(OrderMan, bts::Error, __VA_ARGS__) + #define VENUE bts::api::VENUE_BINANCE -using boost::lockfree::spsc_queue; +using binance_execution::spots::user_data_stream::AccountUpdate; +using binance_execution::spots::user_data_stream::BalanceUpdate; +using binance_execution::spots::user_data_stream::EventType; +using binance_execution::spots::user_data_stream::OrderUpdate; +using bts::collections::ConcurrentQueue; using bts::core::Delegate; +using bts::core::TimeSpan; +using bts::core::TimeStamp; using bts::io::json_formatter; using bts::orderman::CancelOrderMsg; using bts::orderman::CancelReplaceOrderMsg; @@ -64,12 +86,21 @@ using bts::orderman::NewAdminDataEvent; using bts::orderman::NewSecuritiesEvent; using bts::orderman::OrderMsg; using bts::orderman::RFQMsg; +using bts::system::crypto::ed25519_digest_b64; +using bts::websocket::WebsocketClient; +using bts::websocket::WebsocketClientConfig; +using nlohmann::json; +using std::error_code; +using std::exception; using std::int64_t; using std::optional; using std::shared_ptr; using std::size_t; using std::string; +using std::thread; +using std::tuple; using std::uint32_t; +using std::unordered_map; using std::vector; struct ExecutionGatewayCallbacks { @@ -88,17 +119,6 @@ struct SpotsGateway::PImpl { : sec_oracle(std::move(sec_oracle)), config(std::move(config)), jetfix_connection(std::make_shared( std::move(jetfix_connection))), - rest_api_client( - this->config.rest_endpoint, - this->config.api_key, - this->config.secret_key), - 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->config.account, this->jetfix_connection) { bts_assert(this->jetfix_connection->venue() == VENUE); @@ -106,23 +126,21 @@ struct SpotsGateway::PImpl { pos.exchange = VENUE; } - static constexpr const char *CANCEL_REPLACE_CANCEL_SUFFIX = "crc"; - bts::md::SecurityOracle sec_oracle; GatewayConfig config; std::shared_ptr jetfix_connection; + optional ws_client; + optional ws_run_thread; - RestApiClient rest_api_client; - optional user_data_sub; - Delegate account_update_callback; - Delegate order_update_callback; - ListenKeyRequestor listen_key_requestor; + unordered_map> response_handlers; + unordered_map inflight_orders; SpotsOrderStore store; - spsc_queue incoming_account_updates{1024}; - spsc_queue incoming_order_updates{1024}; - spsc_queue> internal_order_rejects{256}; - vector account_updates_vec; + std::mutex instance_mx; + ConcurrentQueue incoming_account_updates; + ConcurrentQueue incoming_order_updates; + ConcurrentQueue> internal_order_rejects; + vector account_updates_vec; ExecutionGatewayCallbacks callbacks; bts::orderman::ExchangePosition pos; @@ -130,42 +148,218 @@ struct SpotsGateway::PImpl { std::shared_ptr key_map; ExecutionReportStore orders; + tuple &> build_request( + const string &method) { + BTS_LOCK_GUARD(instance_mx); + string id = bts::utils::uuid(); + string req = + fmt::format(R"({{ "id": "{}", "method": "{}" }})", id, method); + return {req, id, response_handlers[id]}; + } + + template + tuple &> build_request( + const string &method, T params) { + BTS_LOCK_GUARD(instance_mx); + string id = bts::utils::uuid(); + string req = fmt::format( + R"({{ "id": "{}", "method": "{}", "params": {} }})", id, method, + params); + return {req, id, response_handlers[id]}; + } + void start() { INFO_FN("start"); - listen_key_requestor.callback().bind<&PImpl::on_listen_key_>(*this); - listen_key_requestor.start(); + bts_assert(!ws_run_thread); + LOG(OrderMan, bts::Info, "Creating websocket client. url: {}", + config.ws_endpoint); + // Init websocket + WebsocketClientConfig ws_stream_client_cfg; + const std::string &url = config.ws_endpoint; + ws_stream_client_cfg.url = url; + ws_stream_client_cfg.name = "Binance_UserData"; + auto &ws_cli = ws_client.emplace(ws_stream_client_cfg); + ws_cli.open_handler().bind<&PImpl::on_open>(*this); + ws_cli.message_handler().bind<&PImpl::on_message>(*this); + ws_cli.close_handler().bind<&PImpl::on_close>(*this); + INFO_FN("start", "Creating websocket client. url: {}", url); + ws_cli.connect(); + ws_run_thread.emplace([&]() { + bts::threading::configure_current_thread("binance_spots_gw_ws"); + DIAG_FN("binance_spots_exe_ws", "Begin. url: {}", url); + ws_cli.run(); + DIAG_FN("binance_spots_exe_ws", "End. url: {}", url); + }); + } + + void logon() { + INFO_FN("logon"); + // Prepare request + LogonRequest::Params req; + req.apiKey = config.api_key; + req.timestamp = convert_time_stamp(TimeStamp::now()); + ed25519_digest_b64( + fmt::format("apiKey={}×tamp={}", req.apiKey, req.timestamp), + config.secret_key, req.signature); + auto [req_str, req_id, hnd] = + build_request("session.logon", json_formatter(req)); + hnd.bind<&PImpl::on_logon>(*this); + INFO_FN("on_open", "Sending logon request. {}", req_str); + const auto logon_ec = ws_client->send_text(req_str); + bts_assert_msgf( + !logon_ec, "Logon request failed. {}", logon_ec.message()); + } + + void subscribe() { + INFO_FN("subscribe"); + auto [req_str, req_id, hnd] = build_request("userDataStream.subscribe"); + hnd.bind<&PImpl::on_subscribed>(*this); + const error_code err = ws_client->send_text(req_str); + bts_assert_msgf(!err, "Subscribe failed. {}", err.message()); + } + + void on_open(const std::error_code &ec) { + INFO_FN("on_open"); + bts_assert_msgf(!ec, "Websocket opened with error. {}", ec.message()); + ws_client->ping_continuously(25); + logon(); + } + + void on_logon(const json &msg) { + INFO(R"([binance_spots_gw_logon.json]{})", msg.dump()); + subscribe(); + } + + void on_subscribed(const json &msg) { + INFO(R"([binance_spots_gw_subscribed.json]{})", msg.dump()); + } + + void on_close(const std::error_code &ec) { + ERROR_FN("on_close", "Websocket closed. err: {}", ec.message()); + } + + void on_order_ack(const json &msg) { + BTS_LOCK_GUARD(instance_mx); + INFO(R"([binance_spots_gw_order_ack.json]{})", msg.dump()); + auto id_it = msg.find("id"); + bts_assert_msgf( + id_it != msg.end(), "Received order ack without id. {}", msg.dump()); + string id; + id_it->get_to(id); + auto error_it = msg.find("error"); + if (error_it != msg.end() && !error_it->empty()) { + ERROR_FN("on_order_ack", "{}", msg.dump()); + store.on_reject(id); + auto req_it = inflight_orders.find(id); + if (req_it != inflight_orders.end()) { + internal_order_rejects.push( + orders.reject(req_it->second, error_it->dump())); + } + } + inflight_orders.erase(id); + } + + void on_message(const string &msg_str) { + // This function handles 2 general types of messages: + // * Response - ack for a particular request (place order, cancel order, etc.) + // * Notification - push notification for a particular event type (fill, position change, + // etc.) + // If this is a Response, find the response handler with the same id, invoke it, then destroy + // it. If it's a Notification, demux the message by event type (not id), and invoke the + // appropriate callback. + DIAG_FN("on_message", "{}", msg_str); + // Convert to nlohmann::json + json msg; + try { + msg = json::parse(msg_str); + } catch (const exception &ex) { + ERROR_FN( + "on_message", "Failed to parse response json: {}.\n{}", + string{ex.what()}, msg_str); + return; + } + auto id_it = msg.find("id"); + if (id_it != msg.end()) { + string id; + id_it->get_to(id); + auto hnd_it = response_handlers.find(id); + if (hnd_it == response_handlers.end()) { + WARN_FN("on_message", "Received response with unknown id '{}'", id); + return; + } + try { + hnd_it->second(msg); + } catch (const exception &ex) { + ERROR_FN( + "on_message", "message handler threw. msg: {}; ex: {}", msg_str, + string{ex.what()}); + } + response_handlers.erase(hnd_it); + return; + } + // Extract event data. Unfortunately, Binance testnet doesn't adhere to its own documentation, + // so we must support 2 notification structures - the correct structure (according to their + // documentation), and the **SBE (Simple Binary Encoding) structure. For the former, we read + // the root object. For the latter, we read the object in the "event" field. + // + // ** The actual data is not binary encoded. I use the name "SBE" because the problematic data + // has the same shape, in terms of field names, as SBE notifications. + json *event_msg_p = &msg; + EventType event_type = EventType::NONE; + { + auto event_type_it = msg.find("e"); + if (event_type_it == msg.end()) { + // Apply the workaround described above. Make "event_msg_p" point to the "event" field + // (not the root). + WARN_FN("on_message", "Adapting SBE notification. {}", msg_str); + auto event_it = msg.find("event"); + if (event_it != msg.end()) { + event_type_it = event_it->find("e"); + } + if (event_it == msg.end() || event_type_it == event_it->end()) { + // This is an unexpected structure. It doesn't have "e" or "event" fields. + ERROR_FN( + "on_message", "Failed to process response json: {}", + msg_str); + return; + } + event_type_it->get_to(event_type); + event_msg_p = &*event_it; + } + } + switch (event_type.value) { + case EventType::ORDER_UPDATE: { + OrderUpdate upd; + event_msg_p->get_to(upd); + INFO(R"([binance_spots_exch_update.json]{})", json_formatter(upd)); + incoming_order_updates.push(std::move(upd)); + break; + } + case EventType::OUTBOUND_ACCOUNT_POSITION: { + AccountUpdate upd; + event_msg_p->get_to(upd); + INFO(R"([binance_spots_account_update.json]{})", json_formatter(upd)); + incoming_account_updates.push(std::move(upd)); + break; + } + case EventType::BALANCE_UPDATE: { + BalanceUpdate upd; + event_msg_p->get_to(upd); + INFO(R"([binance_spots_balance_update.json]{})", json_formatter(upd)); + break; + } + default: + ERROR_FN( + "on_message", "Response has unexpected event type '{}'", + event_type); + break; + } } void stop() { - INFO_FN("stop"); - user_data_sub.reset(); - } - - void on_account_update(user_data_stream::AccountUpdate u) { - INFO_FN("on_account_update", "\n{:2}", json_formatter(u)); - incoming_account_updates.push(std::move(u)); - } - - void on_order_update(user_data_stream::OrderUpdate u) { - LOG(OrderMan, bts::Info, R"([binance_spots_exch_update.json]{})", - json_formatter(u)); - incoming_order_updates.push(std::move(u)); - } - - void on_listen_key_(const string &listen_key) { - INFO_FN("on_listen_key", "value: {}", listen_key); - user_data_sub.reset(); - if (listen_key.empty()) { - LOG(OrderMan, bts::Error, - "Unable to subscribe to user data because 'listen_key' " - "is empty."); - return; - } - auto &uds = user_data_sub.emplace( - config.ws_stream_endpoint + "/ws/" + listen_key); - uds.account_update_callback().bind<&PImpl::on_account_update>(*this); - uds.order_update_callback().bind<&PImpl::on_order_update>(*this); - uds.start(); + INFO_FN("stop", "Disconnecting websocket..."); + ws_client->disconnect(); + ws_client.reset(); } void create_order(const CreateOrderMsg &m) { @@ -186,17 +380,27 @@ struct SpotsGateway::PImpl { "{} {} {} @ {} (id={}, type={}, tif={}). Sending request", m.side, m.quantity, req.symbol, m.price, m.key, m.order_type, m.time_in_force); - const auto res = rest_api_client.create_order(req); - if (!res.ok()) { - ERROR_FN("create_order", "id={}. {}", m.key, json_formatter(res)); - internal_order_rejects.push( - orders.reject(m, json_formatter(*res.error).to_string())); + auto [req_str, req_id, hnd] = + build_request("order.place", json_formatter(req)); + { + BTS_LOCK_GUARD(instance_mx); + inflight_orders[req_id] = m; + } + hnd.bind<&PImpl::on_order_ack>(*this); + const auto ec = ws_client->send_text(req_str); + if (ec) { + ERROR_FN("create_order", "id={}. {}", m.key, ec.message()); + { + BTS_LOCK_GUARD(instance_mx); + response_handlers.erase(req_id); + inflight_orders.erase(req_id); + } + internal_order_rejects.push(orders.reject(m, ec.message())); } } void update_order(const CancelReplaceOrderMsg &m) { const auto &fields = m.create_fields; - RestApiResult res; ModifyOrderRequest req; std::string err; if (!store.cancel_replace(m, req, err)) { @@ -216,15 +420,23 @@ struct SpotsGateway::PImpl { "{} {} {} @ {} (id={}, type={}, tif={}). Sending request", fields.side, fields.quantity, req.symbol, fields.price, fields.key, m.exchange_order_id, fields.order_type, fields.time_in_force); - res = rest_api_client.modify_order(req); - if (!res.ok()) { + auto [req_str, req_id, hnd] = + build_request("order.cancelReplace", json_formatter(req)); + { + BTS_LOCK_GUARD(instance_mx); + inflight_orders[req_id] = fields; + } + hnd.bind<&PImpl::on_order_ack>(*this); + const auto ec = ws_client->send_text(req_str); + if (ec) { ERROR_FN( "update_order", "id={}, exch_id={}. {}", fields.key, - m.exchange_order_id, json_formatter(res)); + m.exchange_order_id, ec.message()); + BTS_LOCK_GUARD(instance_mx); + response_handlers.erase(req_id); internal_order_rejects.push(orders.set_error( fields.key, - fmt::format( - "Update request failed. {}", json_formatter(*res.error)))); + fmt::format("Update request failed. {}", ec.message()))); } } @@ -242,15 +454,18 @@ struct SpotsGateway::PImpl { INFO_FN( "cancel_order", "{} {} (id={}, exch_id={})", m.side, req.symbol, m.key, m.exchange_order_id); - const auto res = rest_api_client.cancel_order(req); - if (!res.ok()) { + auto [req_str, req_id, hnd] = + build_request("order.cancel", json_formatter(req)); + hnd.bind<&PImpl::on_order_ack>(*this); + const auto ec = ws_client->send_text(req_str); + if (ec) { ERROR_FN( "cancel_order", "{} {} (id={}, exch_id={}). {}", m.side, - req.symbol, m.key, m.exchange_order_id, json_formatter(res)); + req.symbol, m.key, m.exchange_order_id, ec.message()); + BTS_LOCK_GUARD(instance_mx); + response_handlers.erase(req_id); internal_order_rejects.push(orders.set_error( - m.key, - fmt::format( - "Cancel request failed. {}", json_formatter(*res.error)))); + m.key, fmt::format("Cancel request failed. {}", ec.message()))); } } @@ -262,9 +477,6 @@ struct SpotsGateway::PImpl { } void dispatch_order_updates() { - if (incoming_order_updates.empty()) { - return; - } user_data_stream::OrderUpdate curr; while (incoming_order_updates.pop(curr)) { std::shared_ptr ord_p = store.on_exch_update(curr); diff --git a/src/lib_binance_execution/binance_execution/spots/spots_order_store.cpp b/src/lib_binance_execution/binance_execution/spots/spots_order_store.cpp index 505bfcbb5..1ff761107 100644 --- a/src/lib_binance_execution/binance_execution/spots/spots_order_store.cpp +++ b/src/lib_binance_execution/binance_execution/spots/spots_order_store.cpp @@ -148,6 +148,11 @@ struct SpotsOrderStore::PImpl { orders.size(), order_timestamps.size()); return ord_p; } + + void on_reject(const string &txid) { + LOG(OrderMan, bts::Warn, "Binance::on_reject. txid: {}, n_orders: {}", + txid, orders.size(), order_timestamps.size()); + } }; SpotsOrderStore::SpotsOrderStore() : me(std::make_unique()) {} @@ -183,4 +188,6 @@ shared_ptr SpotsOrderStore::on_exch_update( return me->on_exch_update(upd); } +void SpotsOrderStore::on_reject(const string &txid) { me->on_reject(txid); } + } // namespace binance_execution::spots diff --git a/src/lib_binance_execution/binance_execution/spots/spots_order_store.h b/src/lib_binance_execution/binance_execution/spots/spots_order_store.h index c939280b6..10eee29a1 100644 --- a/src/lib_binance_execution/binance_execution/spots/spots_order_store.h +++ b/src/lib_binance_execution/binance_execution/spots/spots_order_store.h @@ -35,6 +35,8 @@ struct SpotsOrderStore { std::shared_ptr on_exch_update( const user_data_stream::OrderUpdate &upd); + void on_reject(const std::string &txid); + std::shared_ptr order_from_bts_id(std::uint32_t bts_id); private: