So it can be used in the implementation for all binance apis (usds futs, cross margin, etc.)
105 lines
2.4 KiB
C++
105 lines
2.4 KiB
C++
#include "order_transaction.h"
|
|
#include "lib/core/TimeStamp.h"
|
|
#include "lib/utils/Uuid.h"
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
namespace binance_execution {
|
|
|
|
using std::int64_t;
|
|
using std::string;
|
|
|
|
const string &OrderTransaction::Type_Name(Type type) {
|
|
switch (type) {
|
|
case NONE: {
|
|
static const string &name = "NONE";
|
|
return name;
|
|
}
|
|
case CREATE: {
|
|
static const string &name = "CREATE";
|
|
return name;
|
|
}
|
|
case CANCEL: {
|
|
static const string &name = "CANCEL";
|
|
return name;
|
|
}
|
|
case CANCEL_REPLACE: {
|
|
static const string &name = "CANCEL_REPLACE";
|
|
return name;
|
|
}
|
|
default: {
|
|
static const string &name = "UNKNOWN";
|
|
return name;
|
|
}
|
|
}
|
|
}
|
|
|
|
bool OrderTransaction::Parse_Type(const string &str, Type *type) {
|
|
if (str == "CREATE") {
|
|
*type = CREATE;
|
|
} else if (str == "CANCEL") {
|
|
*type = CANCEL;
|
|
} else if (str == "CANCEL_REPLACE") {
|
|
*type = CANCEL_REPLACE;
|
|
} else if (str == "NONE") {
|
|
*type = NONE;
|
|
} else {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
OrderTransaction OrderTransaction::create_cancel(string orig_id) {
|
|
OrderTransaction tx;
|
|
tx.orig_id = std::move(orig_id);
|
|
tx.cxl_id = bts::utils::uuid();
|
|
tx.type = OrderTransaction::CANCEL;
|
|
return tx;
|
|
}
|
|
|
|
OrderTransaction OrderTransaction::create_cancel_replace(string orig_id) {
|
|
OrderTransaction tx;
|
|
tx.orig_id = std::move(orig_id);
|
|
tx.cxl_id = bts::utils::uuid();
|
|
tx.new_id = bts::utils::uuid();
|
|
tx.type = OrderTransaction::CANCEL_REPLACE;
|
|
return tx;
|
|
}
|
|
|
|
void to_json(nlohmann::json &j, const OrderTransaction &tx) {
|
|
j["oid"] = tx.orig_id;
|
|
j["cid"] = tx.cxl_id;
|
|
j["nid"] = tx.new_id;
|
|
j["typ"] = OrderTransaction::Type_Name(tx.type);
|
|
j["ts"] = tx.timestamp.nanos();
|
|
}
|
|
|
|
void from_json(const nlohmann::json &j, OrderTransaction &tx) {
|
|
auto it = j.find("oid");
|
|
if (it != j.end()) {
|
|
it->get_to(tx.orig_id);
|
|
}
|
|
it = j.find("cid");
|
|
if (it != j.end()) {
|
|
it->get_to(tx.cxl_id);
|
|
}
|
|
it = j.find("nid");
|
|
if (it != j.end()) {
|
|
it->get_to(tx.new_id);
|
|
}
|
|
it = j.find("typ");
|
|
if (it != j.end()) {
|
|
string typ_str;
|
|
it->get_to(typ_str);
|
|
OrderTransaction::Parse_Type(typ_str, &tx.type);
|
|
}
|
|
it = j.find("ts");
|
|
if (it != j.end()) {
|
|
int64_t ts_nanos;
|
|
it->get_to(ts_nanos);
|
|
tx.timestamp = bts::core::TimeStamp::from_nanos(ts_nanos);
|
|
}
|
|
}
|
|
|
|
} // namespace binance_execution
|