Files
spark/src/app_sparkd/sparkd/md/coinbase/CoinbaseSecurityRequestor.cpp
T

237 lines
8.4 KiB
C++

#include "CoinbaseSecurityRequestor.h"
#include "domain/protobufs/security_type.pb.h"
#include "domain/protobufs/venue.pb.h"
#include "domain/security.h"
#include "domain/security_criteria.h"
#include "spark/logging/logging.h"
#include "spark/persistence/IDGenerator.h"
#include "sparkd/md/ccapi/CCApiCorrelationID.h"
#include "sparkd/md/ccapi/CCApiMessageHandler.h"
#include "sparkd/md/ccapi/CCApiSession.h"
#include "sparkd/md/ccapi/ICCApiMessageHandler.h"
#include "sparkd/md/coinbase/CoinbaseSpot.h"
#include "system/collections/contains.h"
#include "system/collections/remove.h"
#include <algorithm>
#include <ccapi_cpp/ccapi_event_dispatcher.h>
#include <ccapi_cpp/ccapi_macro.h>
#include <ccapi_cpp/ccapi_message.h>
#include <ccapi_cpp/ccapi_request.h>
#include <ccapi_cpp/ccapi_session.h>
#include <string>
#include <unordered_set>
namespace bts::md::coinbase {
using bts::domain::Security;
using bts::domain::SecurityCriteria;
using bts::domain::SecurityID;
using bts::domain::protobufs::SecurityType;
using bts::domain::protobufs::Venue;
using bts::domain::protobufs::Venue_Name;
using bts::persistence::IDGenerator;
using std::string;
using std::unordered_set;
using std::vector;
#define VENUE Venue::VENUE_COINBASE
#define VENUE_NAME (Venue_Name(Venue::VENUE_COINBASE))
#define CCAPI_VENUE_NAME CCAPI_EXCHANGE_NAME_COINBASE
#define COINBASE_MSG_TYPE_NAME(x) \
std::string { CoinbaseMessageType{x}.name() }
#define SEC_TYPE(x) bts::domain::protobufs::SECURITY_TYPE_##x
static ccapi::Request exchange_info_req() {
auto corr_id = CCApiCorrelationID::from_parts(VENUE_NAME);
ccapi::Request req{
ccapi::Request::Operation::GET_INSTRUMENTS, CCAPI_VENUE_NAME};
LOG(md, Info, "{}", req.toString());
req.setCorrelationId(corr_id.to_string());
return req;
}
static vector<string> get_sec_descs(const vector<Security> &sec_ptrs) {
vector<string> descs{sec_ptrs.size()};
std::transform(
sec_ptrs.begin(), sec_ptrs.end(), descs.begin(),
[](const auto &s) { return s.desc(); });
return descs;
}
static void validate_response(const ccapi::Event &event, bool is_expected) {
bts_assert_msgf(
event.getType() == ccapi::Event::Type::RESPONSE,
"Received event w/ unexpected type:\n{}", event.toStringPretty());
bts_assert_msgf(
is_expected, "Received unexpected response from Coinbase:\n{}",
event.toStringPretty());
}
struct CoinbaseSecurityRequestor::PImpl {
// The session automatically starts/stops in its ctr/dtr
CCApiSession *const session;
// Maps security descs (strings) to numeric ids
IDGenerator *const sec_id_store;
// 'criteria' and 'excluded_securities' collectively form the security
// filter predicate. Securities are forwarded to the callback if and only if
// they are included by 'criteria', and absent from 'excluded_securities'.
const SecurityCriteria criteria;
const unordered_set<string> excluded_securities;
const unordered_set<string> securities;
// These proxy handlers don't actually process new securities. Member methods
// in this class process the securities; we simply bind them to proxy handler
// callbacks. This is easier than defining separate ICCApiMessageHandler
// impls for 'markets', and 'futures', esp since they would need access to
// state contained in this class.
CCApiMessageHandler exch_info_handler_proxy;
Callback callback;
// True when an 'exchange info' request is in flight
bool exch_info_req_pending = false;
// True when someone called BinanceUsSecurityRequestor::request while another
// request was already in flight. In this case, the 'external_req_pending'
// flag is used to signal that another request should be sent immediately
// after the current one finishes.
bool external_req_pending = false;
explicit PImpl(
CCApiSession *const session,
domain::SecurityCriteria criteria,
persistence::IDGenerator *const sec_id_store,
unordered_set<string> excluded_securities)
: session(session), sec_id_store(sec_id_store),
criteria(std::move(criteria)),
excluded_securities(std::move(excluded_securities)) {
exch_info_handler_proxy.callback().bind<&PImpl::on_exchange_info>(*this);
}
bool is_busy() const { return exch_info_req_pending; }
void request() {
external_req_pending = false;
exch_info_req_pending = true;
session->request(exchange_info_req(), &exch_info_handler_proxy);
}
/**
* @brief Parses securities from a ccapi::Message
*
* @details Only new securities that satisfy the 'SecurityCriteria' are added
* to \p added_secs
*
* @tparam TParser
* @param msg The ccapi::Message to parse
* @param types The types of securities to parse
* @param parser Ther parser to use
*/
template <class T>
void parse_secs(
const ccapi::Message &msg,
const vector<SecurityType> &types,
vector<Security> &added_secs) {
T::parse(msg, added_secs, VENUE);
// Exclude secs that don't satisfy the SecurityCriteria
auto is_sec_excluded = [this, &types](const Security &sec) {
return !CONTAINS(types, sec.type()) ||
!criteria.includes_or_undefined(sec) ||
CONTAINS(excluded_securities, sec.desc()) ||
securities.count(sec.desc()) > 0;
};
REMOVE_IF(added_secs, is_sec_excluded);
// Assign security ids
std::cout << types.size();
for (auto &sec : added_secs) {
// Since the ids haven't been set yet, we should expect them to be 0
bts_assert(sec.id().value() == 0);
SecurityID sec_id{sec_id_store->get_or_add(VENUE_NAME, sec.desc())};
sec.security_common.id = sec_id;
}
}
/**
* @brief Parses securities from ccapi messages, updates the securities set,
* and subscribes depth for newly arrived securities
*
* @details Securities that don't pass the criteria filter are ignored
*
* @tparam TParser The type of parser
* @param msgs Vector of ccapi messages containing securities
* @param types The types of securities to update (types must be explicitly
* specified to be included)
*/
template <class T>
void update_securities(
const vector<const ccapi::Message *> &msgs,
const vector<SecurityType> &types) {
vector<Security> added_secs;
for (const auto *msg : msgs) {
bts_assert_msgf(
msg->getType() == ccapi::Message::Type::GET_INSTRUMENTS,
"Received msg w/ unexpected type:\n{}", msg->toStringPretty());
parse_secs<T>(*msg, types, added_secs);
}
auto added_sec_descs = get_sec_descs(added_secs);
LOG(md, Info, "Received Coinbase securities:\n{}",
fmt::to_string(fmt::join(added_sec_descs, "\n")));
callback(added_secs);
}
/**
* Handles 'exchange info' update from CCApiSession
*/
bool on_exchange_info(
const ccapi::Event &event,
ccapi::Session * /*session*/,
const vector<const ccapi::Message *> &msgs,
const CCApiCorrelationID & /*corr_id*/) {
validate_response(event, exch_info_req_pending);
LOG(md, Diag, "Received Coinbase exchange info (spots):\n{}",
event.toStringPretty());
std::cout << msgs.size();
update_securities<CoinbaseSpot>(msgs, {SEC_TYPE(SPOT)});
exch_info_req_pending = false;
if (external_req_pending && !is_busy()) {
request();
}
return true;
}
};
CoinbaseSecurityRequestor::CoinbaseSecurityRequestor(
CCApiSession *const session,
SecurityCriteria criteria,
IDGenerator *const sec_id_store,
unordered_set<string> excluded_securities)
: me_(std::make_unique<PImpl>(
session,
std::move(criteria),
sec_id_store,
std::move(excluded_securities))) {}
CoinbaseSecurityRequestor::CoinbaseSecurityRequestor(
CoinbaseSecurityRequestor &&) noexcept = default;
CoinbaseSecurityRequestor &CoinbaseSecurityRequestor::operator=(
CoinbaseSecurityRequestor &&) noexcept = default;
CoinbaseSecurityRequestor::~CoinbaseSecurityRequestor() = default;
void CoinbaseSecurityRequestor::request() {
if (is_busy()) {
me_->external_req_pending = true;
return;
}
me_->request();
}
bool CoinbaseSecurityRequestor::is_busy() const { return me_->is_busy(); }
CoinbaseSecurityRequestor::Callback &CoinbaseSecurityRequestor::callback() {
return me_->callback;
}
} // namespace bts::md::coinbase