Files
spark/src/app_sparkd/sparkd/md/binance/BinanceMiniTickerUpdate.cpp
T

109 lines
3.2 KiB
C++

#ifdef SPARK_ENABLE_CCAPI
#include "BinanceMiniTickerUpdate.h"
#include "domain/price.h"
#include "domain/quantity.h"
#include "domain/security_market_data_statistics.h"
#include "sparkd/md/ccapi/CCApiReader.h"
#include "system/json/json_dumper.h"
#include "system/json/json_reader.h"
#include "system/time_stamp.h"
#include <ccapi_cpp/ccapi_message.h>
#include <type_traits>
#include <utility>
namespace bts::md::binance {
using bts::domain::Price;
using bts::domain::Quantity;
using bts::domain::SecurityID;
using bts::domain::SecurityMarketDataStatistics;
using bts::io::JsonReader;
using std::map;
using std::string;
using std::vector;
static bool parse(
const map<string, string> &name_values,
vector<BinanceMiniTickerUpdate> &ticker_upds) {
auto it = name_values.find(CCAPI_HTTP_BODY);
if (it == name_values.end()) {
return false;
}
CCApiReader ccapi_reader{&name_values};
nlohmann::json js;
if (!ccapi_reader.read(CCAPI_HTTP_BODY, js)) {
return false;
}
JsonReader jreader{&js};
if (!jreader.read<bool>("success")) {
LOG(md, Error,
"BinanceMiniTickerUpdate json field 'success' is false:\n{}", js);
return false;
}
if (!jreader.read("result", ticker_upds)) {
LOG(md, Error,
"BinanceMiniTickerUpdate json field 'result' is invalid:\n{}", js);
}
return !ticker_upds.empty();
}
bool BinanceMiniTickerUpdate::parse(
const ccapi::Message &msg, vector<BinanceMiniTickerUpdate> &ticker_upds) {
auto orig_sz = ticker_upds.size();
for (const auto &sec_el : msg.getElementList()) {
const auto &hp_props = sec_el.getNameValueMap();
auto it = hp_props.find(CCAPI_HTTP_BODY);
if (it == hp_props.end()) {
continue;
}
bts::md::binance::parse(hp_props, ticker_upds);
}
return ticker_upds.size() > orig_sz;
}
void from_json(const nlohmann::json &j, BinanceMiniTickerUpdate &upd) {
JsonReader jr{&j};
jr.read("e", upd.event_type);
jr.read("E", upd.event_time);
jr.read("s", upd.symbol);
jr.read("c", upd.close_price);
jr.read("o", upd.open_price);
jr.read("h", upd.high_price);
jr.read("l", upd.low_price);
jr.read("v", upd.base_asset_volume);
jr.read("q", upd.quote_asset_volume);
}
void to_json(nlohmann::json &j, const BinanceMiniTickerUpdate &upd) {
j = nlohmann::json{
{"e", upd.event_type},
{"E", upd.event_time.to_iso_string()},
{"s", upd.symbol},
{"c", upd.close_price},
{"o", upd.open_price},
{"h", upd.high_price},
{"l", upd.low_price},
{"v", upd.base_asset_volume},
{"q", upd.quote_asset_volume}};
}
void BinanceMiniTickerUpdate::to_security_statistics(
SecurityMarketDataStatistics &sec_stats) {
sec_stats.open = Price::from_double(open_price);
sec_stats.settle = Price::from_double(close_price);
sec_stats.high = Price::from_double(high_price);
sec_stats.low = Price::from_double(low_price);
sec_stats.volume.value() = static_cast<Quantity::Value>(quote_asset_volume);
}
std::ostream &operator<<(
std::ostream &out, const BinanceMiniTickerUpdate &upd) noexcept {
out << io::JsonDumper::dump(upd);
return out;
}
} // namespace bts::md::binance
#endif