205 lines
7.4 KiB
C++
205 lines
7.4 KiB
C++
#include "DeribitDemoConfiguration.h"
|
|
#include "lib/io/JsonReader.h"
|
|
#include "lib/logging/logging.h"
|
|
#include "lib/utils/FileUtils.h"
|
|
#include "rapidjson/istreamwrapper.h"
|
|
#include <emumba-deribit-connector/deribit_utils.h>
|
|
#include <exception>
|
|
#include <fstream>
|
|
#include <list>
|
|
#include <nlohmann/detail/macro_scope.hpp>
|
|
#include <nlohmann/json.hpp>
|
|
#include <optional>
|
|
|
|
namespace emumba::deribit_connector::configuration {
|
|
|
|
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
|
|
emumba::deribit_connector::configuration::subscription_config,
|
|
public_book,
|
|
public_index,
|
|
public_markprice,
|
|
public_quote,
|
|
public_ticker,
|
|
public_trade,
|
|
private_portfolio,
|
|
use_raw_interval,
|
|
book_depth,
|
|
requests_limit);
|
|
// NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
|
|
// emumba::deribit_connector::configuration::symbol_config,
|
|
// symbol_name,
|
|
// currency_type,
|
|
// cred_index,
|
|
// interested_instruments,
|
|
// interested_subscriptions,
|
|
// interested_in_future,
|
|
// interested_in_future_combo,
|
|
// interested_in_option,
|
|
// interested_in_option_combo);
|
|
|
|
inline void to_json(
|
|
nlohmann::json &j,
|
|
const emumba ::deribit_connector::configuration::symbol_config &s) {
|
|
j["symbol_name"] = s.symbol_name;
|
|
j["currency_type"] = s.currency_type;
|
|
j["cred_index"] = s.cred_index;
|
|
j["interested_instruments"] = s.interested_instruments;
|
|
j["interested_subscriptions"] = s.interested_subscriptions;
|
|
j["interested_in_future"] = s.interested_in_future;
|
|
j["interested_in_future_combo"] = s.interested_in_future_combo;
|
|
j["interested_in_option"] = s.interested_in_option;
|
|
j["interested_in_option_combo"] = s.interested_in_option_combo;
|
|
}
|
|
|
|
inline void from_json(
|
|
const nlohmann::json &j,
|
|
emumba::deribit_connector::configuration::symbol_config &s) {
|
|
j.at("symbol_name").get_to(s.symbol_name);
|
|
j.at("currency_type").get_to(s.currency_type);
|
|
j.at("cred_index").get_to(s.cred_index);
|
|
j.at("interested_instruments").get_to(s.interested_instruments);
|
|
j.at("interested_subscriptions").get_to(s.interested_subscriptions);
|
|
j.at("interested_in_future").get_to(s.interested_in_future);
|
|
j.at("interested_in_future_combo").get_to(s.interested_in_future_combo);
|
|
j.at("interested_in_option").get_to(s.interested_in_option);
|
|
j.at("interested_in_option_combo").get_to(s.interested_in_option_combo);
|
|
}
|
|
|
|
inline void to_json(nlohmann::json &j, const logging_config &l) {
|
|
j["severity"] = l.severity;
|
|
j["console"] = l.is_console_enabled;
|
|
j["file"] = l.is_file_enabled;
|
|
j["queuesize"] = l.queue_size;
|
|
}
|
|
|
|
inline void from_json(const nlohmann::json &j, logging_config &l) {
|
|
bts::io::JsonReader jr{&j};
|
|
jr.read("severity", l.severity);
|
|
jr.read("console", l.is_console_enabled);
|
|
jr.read("file", l.is_file_enabled);
|
|
jr.read("queuesize", l.queue_size);
|
|
}
|
|
|
|
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
|
|
reconnection_config, retry_count, retry_interval, reception_timeout);
|
|
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(order_tracker, uuid, start_id);
|
|
|
|
inline void to_json(nlohmann::json &j, const trading_session_config &t) {
|
|
j["orders_tracker"] = t.orders_tracker;
|
|
j["core"] = t.core;
|
|
j["cancel_on_disconnect"] = t.cancel_on_disconnect;
|
|
j["session"] = nlohmann::json{{"heartbeat_interval", t.heartbeat_interval}};
|
|
}
|
|
|
|
inline void from_json(const nlohmann::json &j, trading_session_config &ts) {
|
|
j.at("orders_tracker").get_to(ts.orders_tracker);
|
|
j.at("core").get_to(ts.core);
|
|
j.at("cancel_on_disconnect").get_to(ts.cancel_on_disconnect);
|
|
j.at("session").at("heartbeat_interval").get_to(ts.heartbeat_interval);
|
|
}
|
|
|
|
} // namespace emumba::deribit_connector::configuration
|
|
|
|
namespace bts::cmds {
|
|
|
|
static constexpr const char *test_site() {
|
|
return "wss://test.deribit.com/ws/api/v2";
|
|
}
|
|
|
|
static std::string read_url(nlohmann::json &doc) {
|
|
io::JsonReader jr{&doc};
|
|
std::string url;
|
|
jr.try_read("uri", url);
|
|
if (url.empty()) {
|
|
return test_site();
|
|
}
|
|
return url;
|
|
}
|
|
|
|
template <class Obj>
|
|
static void read_instruments(
|
|
Obj &obj, std::unordered_set<std::string> &instruments) {
|
|
const char *instruments_field = "instruments";
|
|
bts_assert(obj.HasMember(instruments_field));
|
|
for (auto &instrument : obj[instruments_field].GetArray()) {
|
|
instruments.insert(instrument.GetString());
|
|
}
|
|
}
|
|
|
|
static std::list<emumba::deribit_connector::configuration::symbol_config>
|
|
read_symbol_config(rapidjson::Document &doc) {
|
|
using RJR = RapidJsonReader;
|
|
|
|
std::list<emumba::deribit_connector::configuration::symbol_config> syms_out;
|
|
for (auto &sym_in : doc["symbol"].GetArray()) {
|
|
auto &sym = syms_out.emplace_back();
|
|
sym.symbol_name = RJR::get_string(sym_in, "id");
|
|
sym.interested_in_future = RJR::get_bool(sym_in, "interested_in_future");
|
|
sym.interested_in_option = RJR::get_bool(sym_in, "interested_in_option");
|
|
sym.currency_type = RJR::get_string(sym_in, "currency_type", "usd");
|
|
sym.cred_index = 1;
|
|
|
|
read_instruments(sym_in, sym.interested_instruments);
|
|
auto chan_in = sym_in["channel"].GetObject();
|
|
|
|
auto &subs = sym.interested_subscriptions;
|
|
subs.requests_limit = RJR::get_uint(sym_in, "requests_limit", 20u);
|
|
subs.book_depth = RJR::get_uint(sym_in, "book_depth", 5u);
|
|
subs.public_book = RJR::get_bool(chan_in, "PUBLIC_BOOK");
|
|
subs.public_quote = RJR::get_bool(chan_in, "PUBLIC_QUOTE");
|
|
subs.public_trade = RJR::get_bool(chan_in, "PUBLIC_TRADE");
|
|
subs.public_ticker = RJR::get_bool(chan_in, "PUBLIC_TICKER");
|
|
subs.public_markprice = RJR::get_bool(chan_in, "PUBLIC_MARKPRICE");
|
|
subs.public_index = RJR::get_bool(chan_in, "PUBLIC_INDEX");
|
|
// subs.private_orders = RJR::get_bool(chan_in, "PRIVATE_ORDERS");
|
|
// subs.private_trades = RJR::get_bool(chan_in, "PRIVATE_TRADES");
|
|
subs.private_portfolio = RJR::get_bool(chan_in, "PRIVATE_PORTFOLIO");
|
|
subs.use_raw_interval = RJR::get_bool(sym_in, "raw_enable", true);
|
|
}
|
|
return syms_out;
|
|
}
|
|
|
|
static std::
|
|
map<uint, emumba::deribit_connector::configuration::credential_config>
|
|
read_credential_config(rapidjson::Document &doc) {
|
|
using RJR = RapidJsonReader;
|
|
using namespace emumba::deribit_connector;
|
|
|
|
std::map<uint, configuration::credential_config> rv;
|
|
for (auto &cred_in : doc["credentials"].GetArray()) {
|
|
configuration::credential_config rv_i;
|
|
rv_i.client_name = RJR::get_string(cred_in, "client_name");
|
|
rv_i.client_id = RJR::get_string(cred_in, "id");
|
|
rv_i.index = RJR::get_uint(cred_in, "subaccount_index");
|
|
rv_i.secret_key = RJR::get_string(cred_in, "secret_key");
|
|
rv.insert(
|
|
std::pair<uint, configuration::credential_config>(rv_i.index, rv_i));
|
|
}
|
|
return rv;
|
|
}
|
|
|
|
std::optional<DeribitDemoConfiguration> DeribitDemoConfiguration::read_file(
|
|
const std::string &config_path) {
|
|
nlohmann::json conf_j;
|
|
try {
|
|
conf_j =
|
|
nlohmann::json::parse(bts::spartan::FileUtils::read_file(config_path));
|
|
} catch (const std::exception &ex) {
|
|
LOG(md, Error, "Error in parsing config from {}: {}", config_path,
|
|
static_cast<int>(conf_j.GetParseError()));
|
|
return std::nullopt;
|
|
}
|
|
|
|
DeribitDemoConfiguration conf;
|
|
io::JsonReader jr{&conf_j};
|
|
jr.try_read("app_logging", conf.logging_configuration);
|
|
conf.logging_configuration = read_app_logging(doc);
|
|
conf.deribit_url = read_url(conf_j);
|
|
conf.trading_config = read_trading_session_config(doc);
|
|
conf.symbol_config_list = read_symbol_config(doc);
|
|
conf.credential_map = read_credential_config(doc);
|
|
return conf;
|
|
}
|
|
|
|
} // namespace bts::cmds
|