This is a prerequisite to synchronizing security ids stored via IDGenerator (the "new" way to store ids, added when we implemented Deribit), and SecurityDB (the original way from spark days). The problem was that these 2 implementations were using separate databases. We need to unify the interfaces between these storage services to avoid breaking existing code.
239 lines
8.3 KiB
C++
239 lines
8.3 KiB
C++
#ifndef _WIN32
|
|
#include "SyntheticSecurityService.h"
|
|
|
|
#include "lib/api/domain/SecurityType.pb.h"
|
|
#include "lib/api/domain/Venue.pb.h"
|
|
#include "lib/api/public/ServiceContext.h"
|
|
#include "lib/api/services/PersistenceService.pb.h"
|
|
#include "lib/config/config.h"
|
|
#include "lib/domain/SecurityCriteria.h"
|
|
#include "lib/md/SecurityDB.h"
|
|
#include "lib/md/SecurityIDRecorder.h"
|
|
#include "lib/md/SymbolLoader.h"
|
|
#include "lib/md/common.h"
|
|
#include "lib/utils/Dispatcher.h"
|
|
#include "lib/utils/Event.h"
|
|
#include <set>
|
|
|
|
namespace bts {
|
|
namespace api {
|
|
namespace synthetic {
|
|
|
|
using bts::domain::SecurityCriteria;
|
|
using bts::md::SecurityIDRecorder;
|
|
using bts::md::SymbolLoader;
|
|
|
|
#define SEC_TYPE(x) bts::api::domain::SECURITY_TYPE_##x
|
|
|
|
struct SecurityLess {
|
|
bool operator()(const api::Security &left, const api::Security &right) {
|
|
return left.id() < right.id();
|
|
}
|
|
};
|
|
typedef SyntheticSecurityServiceBase::MetadataStatefulPublisher
|
|
MetadataPublisher;
|
|
|
|
struct SyntheticSecurityService::Pimpl {
|
|
Pimpl(const MetadataStatefulPublisher &fn)
|
|
: recorder(nullptr, std::nullopt, &utils::Dispatcher::background()),
|
|
synthetics(), on_security(), meta(), on_metadata(),
|
|
metadata_publisher(fn) {}
|
|
SecurityIDRecorder recorder;
|
|
std::unordered_map<SecurityKey, Security> synthetics;
|
|
utils::Event<Security> on_security;
|
|
std::unordered_map<SecurityKey, Metadata> meta;
|
|
utils::Event<Metadata> on_metadata;
|
|
MetadataStatefulPublisher metadata_publisher;
|
|
};
|
|
|
|
SyntheticSecurityService::SyntheticSecurityService(api::ServiceContext &context)
|
|
: SyntheticSecurityServiceBase(context),
|
|
_me(new Pimpl(make_metadata_stateful_publisher())) {
|
|
LOG(md, Info, "Initializing SyntheticSecurityService");
|
|
|
|
// load securities
|
|
const auto symbols =
|
|
SymbolLoader::load(SecurityCriteria::create({}, {SEC_TYPE(SYNTHETIC)}));
|
|
bts::md::db::load_securities(
|
|
symbols, [=, this](const api::Security &security) {
|
|
if (security.Contract_case() == api::Security::kSynthetic) {
|
|
auto key = SecurityKey(security.id());
|
|
_me->synthetics.insert(std::make_pair(key, security));
|
|
}
|
|
});
|
|
|
|
// Load metadata
|
|
auto metadata = bts::md::db::load_metadata();
|
|
for (const auto &entry : metadata) {
|
|
auto iter = _me->synthetics.find(SecurityKey(entry.security_key()));
|
|
if (iter != _me->synthetics.end()) {
|
|
if (entry.strategy().deleted()) {
|
|
_me->synthetics.erase(iter);
|
|
} else {
|
|
_me->meta.insert(std::make_pair(entry.security_key(), entry));
|
|
_me->metadata_publisher.add_or_update(entry);
|
|
}
|
|
}
|
|
}
|
|
if (_me->meta.size() > 0) {
|
|
_me->metadata_publisher.flush();
|
|
}
|
|
}
|
|
SyntheticSecurityService::~SyntheticSecurityService() {}
|
|
|
|
const std::unordered_map<SecurityKey, Security>
|
|
SyntheticSecurityService::securities() {
|
|
return _me->synthetics;
|
|
}
|
|
utils::EventSubscriber<Security> &SyntheticSecurityService::on_securities() {
|
|
return _me->on_security;
|
|
}
|
|
const Metadata *SyntheticSecurityService::get_metadata(SecurityKey key) {
|
|
auto iter = _me->meta.find(key);
|
|
if (iter != _me->meta.end()) {
|
|
return &iter->second;
|
|
}
|
|
return nullptr;
|
|
}
|
|
utils::EventSubscriber<Metadata> &SyntheticSecurityService::on_metadata() {
|
|
return _me->on_metadata;
|
|
}
|
|
|
|
void send_reply(
|
|
const api::Response<CreateResult> &fn,
|
|
CreateResult::Error err,
|
|
uint32_t key = bts::md::invalid_security_key) {
|
|
CreateResult r;
|
|
r.set_key(key);
|
|
r.set_error(err);
|
|
fn(r);
|
|
LOG(md, Info, "Create new synthetic security key={} response={}", key,
|
|
CreateResult::Error_Name(err));
|
|
}
|
|
|
|
void SyntheticSecurityService::create_synthetic(
|
|
const Security &security, const api::Response<CreateResult> &reply) {
|
|
|
|
CreateResult result;
|
|
// Validate security is synthetic
|
|
if (security.Contract_case() != Security::kSynthetic) {
|
|
send_reply(reply, CreateResult::ERROR_NOT_SYNTHETIC);
|
|
return;
|
|
}
|
|
if (security.id() != bts::md::invalid_security_key) {
|
|
send_reply(reply, CreateResult::ERROR_ID_ALREADY_ASSIGNED);
|
|
return;
|
|
}
|
|
if (security.exchange() != api::VENUE_BTS) {
|
|
send_reply(reply, CreateResult::ERROR_BAD_VENUE);
|
|
return;
|
|
}
|
|
if (security.symbol().empty()) {
|
|
send_reply(reply, CreateResult::ERROR_BAD_SYMBOL);
|
|
return;
|
|
}
|
|
if (security.synthetic().name().empty()) {
|
|
send_reply(reply, CreateResult::ERROR_BAD_NAME);
|
|
return;
|
|
}
|
|
|
|
// Assign security key
|
|
_me->recorder.assign_key(
|
|
security, [this, reply](const api::Security &security) {
|
|
// Notify listeners that a new security is added
|
|
auto iter = _me->synthetics.find(SecurityKey(security.id()));
|
|
if (iter != _me->synthetics.end()) {
|
|
auto metadata = get_metadata(iter->first);
|
|
if (metadata && metadata->strategy().deleted()) {
|
|
// If this security is deleted we can just undelete it
|
|
Metadata newmeta;
|
|
// Rather than copy from the existing metatdata create a
|
|
// default. The existing metadata is from a deleted spread so
|
|
// may not be valid UI will call update_security_metadata if
|
|
// non-defaults are needed
|
|
newmeta.set_security_key(security.id());
|
|
newmeta.mutable_strategy()->set_deleted(false);
|
|
LOG(Api, Info, "Un-deleting security {}", security.id());
|
|
auto id = security.id();
|
|
update_security_metadata(
|
|
newmeta, [id, reply](const UpdateResult &result) {
|
|
if (result.error() == UpdateResult::ERROR_OK) {
|
|
// undelete was successful. Metadata update will make
|
|
// this visible again
|
|
send_reply(reply, CreateResult::ERROR_OK, id);
|
|
} else {
|
|
LOG(Api, Info,
|
|
"Failed to un-delete security {}. Error {}", id,
|
|
UpdateResult_Error_Name(result.error()));
|
|
send_reply(
|
|
reply,
|
|
CreateResult::ERROR_SECURITY_ALREADY_EXISTS, id);
|
|
}
|
|
});
|
|
} else {
|
|
send_reply(
|
|
reply, CreateResult::ERROR_SECURITY_ALREADY_EXISTS,
|
|
security.id());
|
|
}
|
|
} else {
|
|
_me->synthetics.insert(std::make_pair(security.id(), security));
|
|
_me->on_security.publish(security);
|
|
// signal success
|
|
send_reply(reply, CreateResult::ERROR_OK, security.id());
|
|
}
|
|
});
|
|
}
|
|
|
|
void SyntheticSecurityService::update_security_metadata(
|
|
const Metadata &request, const api::Response<UpdateResult> &reply) {
|
|
UpdateResult result;
|
|
if (request.security_key() == bts::md::invalid_security_key) {
|
|
result.set_error(UpdateResult::ERROR_MISSING_KEY);
|
|
reply(result);
|
|
return;
|
|
}
|
|
auto key = SecurityKey(request.security_key());
|
|
if (_me->synthetics.find(key) == _me->synthetics.end()) {
|
|
result.set_error(UpdateResult::ERROR_UNKNOWN_SECURITY);
|
|
reply(result);
|
|
return;
|
|
}
|
|
// update the on disk database
|
|
if (!bts::md::db::store_metadata(request)) {
|
|
result.set_error(UpdateResult::ERROR_DATABASE);
|
|
reply(result);
|
|
return;
|
|
}
|
|
// update the local database
|
|
_me->meta[key] = request;
|
|
// notify subscribers
|
|
_me->on_metadata.publish(request);
|
|
_me->metadata_publisher.add_or_update(request);
|
|
_me->metadata_publisher.flush();
|
|
LOG(md, Info, "Update synthetic security key={} response={}", key,
|
|
UpdateResult::Error_Name(result.error()));
|
|
|
|
reply(result);
|
|
}
|
|
|
|
void SyntheticSecurityService::query_security_metadata(
|
|
const MetadataQuery &request,
|
|
const api::Response<MetadataCollection> &reply) {
|
|
|
|
MetadataCollection result;
|
|
for (const auto k : request.security_key()) {
|
|
SecurityKey key(k);
|
|
auto iter = _me->meta.find(key);
|
|
if (iter != _me->meta.end()) {
|
|
result.mutable_data()->Add()->CopyFrom(iter->second);
|
|
}
|
|
}
|
|
reply(result);
|
|
}
|
|
|
|
} // namespace synthetic
|
|
} // namespace api
|
|
} // namespace bts
|
|
|
|
#endif
|