forked from crypto-crawler/crypto-msg-parser-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto_msg_parser.cpp
More file actions
282 lines (251 loc) · 9.44 KB
/
crypto_msg_parser.cpp
File metadata and controls
282 lines (251 loc) · 9.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include "crypto_msg_parser.h"
#include <cstring>
#include <iostream>
#include <nlohmann/json.hpp>
#include "crypto_msg_parser_ffi.h"
// map TradeSide values to JSON as strings
NLOHMANN_JSON_SERIALIZE_ENUM(TradeSide, {
{kBuy, "buy"},
{kSell, "sell"},
})
NLOHMANN_JSON_SERIALIZE_ENUM(MarketType,
{
{Unknown, "unknown"},
{Spot, "spot"},
{LinearFuture, "linear_future"},
{InverseFuture, "inverse_future"},
{LinearSwap, "linear_swap"},
{InverseSwap, "inverse_swap"},
{AmericanOption, "american_option"},
{EuropeanOption, "european_option"},
{Move, "move"},
{BVOL, "bvol"},
})
NLOHMANN_JSON_SERIALIZE_ENUM(MessageType, {
{Other, "other"},
{Trade, "trade"},
{L2Event, "l2_event"},
{L2Snapshot, "l2_snapshot"},
{L2TopK, "l2_topk"},
{L3Event, "l3_event"},
{L3Snapshot, "l3_snapshot"},
{BBO, "bbo"},
{Ticker, "ticker"},
{Candlestick, "candlestick"},
{FundingRate, "funding_rate"},
{OpenInterest, "open_interest"},
})
static void to_json_shared(nlohmann::json& j, const ParsedMessage& msg) {
j["exchange"] = msg.exchange;
j["market_type"] = msg.market_type;
j["msg_type"] = msg.msg_type;
j["symbol"] = msg.symbol;
j["pair"] = msg.pair;
j["timestamp"] = msg.timestamp;
j["json"] = msg.json;
}
static void to_json(nlohmann::json& j, const TradeMsg& msg) {
to_json_shared(j, msg);
j["price"] = msg.price;
j["quantity_base"] = msg.quantity_base;
j["quantity_quote"] = msg.quantity_quote;
if (msg.quantity_contract != std::nullopt) {
j["quantity_contract"] = msg.quantity_contract.value();
}
j["side"] = msg.side;
j["trade_id"] = msg.trade_id;
}
static void to_json(nlohmann::json& j, const Order& order) {
j["price"] = order.price;
j["quantity_base"] = order.quantity_base;
j["quantity_quote"] = order.quantity_quote;
if (order.quantity_contract != std::nullopt) {
j["quantity_contract"] = order.quantity_contract.value();
}
}
static void to_json(nlohmann::json& j, const OrderBookMsg& msg) {
to_json_shared(j, msg);
j["asks"] = msg.asks;
j["bids"] = msg.bids;
j["snapshot"] = msg.snapshot;
if (msg.seq_id != std::nullopt) {
j["seq_id"] = msg.seq_id.value();
}
if (msg.prev_seq_id != std::nullopt) {
j["prev_seq_id"] = msg.prev_seq_id.value();
}
}
static void to_json(nlohmann::json& j, const FundingRateMsg& msg) {
to_json_shared(j, msg);
j["funding_rate"] = msg.funding_rate;
j["funding_time"] = msg.funding_time;
if (msg.estimated_rate != std::nullopt) {
j["estimated_rate"] = msg.estimated_rate.value();
}
}
static void from_json_shared(const nlohmann::json& j, ParsedMessage& msg) {
j.at("exchange").get_to(msg.exchange);
j.at("market_type").get_to(msg.market_type);
j.at("msg_type").get_to(msg.msg_type);
j.at("symbol").get_to(msg.symbol);
j.at("pair").get_to(msg.pair);
j.at("timestamp").get_to(msg.timestamp);
j.at("json").get_to(msg.json);
}
static void from_json(const nlohmann::json& j, TradeMsg& msg) {
from_json_shared(j, msg);
j.at("price").get_to(msg.price);
j.at("quantity_base").get_to(msg.quantity_base);
j.at("quantity_quote").get_to(msg.quantity_quote);
if (j.contains("quantity_contract")) {
msg.quantity_contract = j.at("quantity_contract").get<double>();
}
j.at("side").get_to(msg.side);
j.at("trade_id").get_to(msg.trade_id);
}
static void from_json(const nlohmann::json& j, Order& order) {
j.at("price").get_to(order.price);
j.at("quantity_base").get_to(order.quantity_base);
j.at("quantity_quote").get_to(order.quantity_quote);
if (j.contains("quantity_contract")) {
order.quantity_contract = j.at("quantity_contract").get<double>();
}
}
static void from_json(const nlohmann::json& j, OrderBookMsg& msg) {
from_json_shared(j, msg);
j.at("asks").get_to(msg.asks);
j.at("bids").get_to(msg.bids);
j.at("snapshot").get_to(msg.snapshot);
if (j.contains("seq_id")) {
msg.seq_id = j.at("seq_id").get<uint64_t>();
}
if (j.contains("prev_seq_id")) {
msg.prev_seq_id = j.at("prev_seq_id").get<uint64_t>();
}
}
static void from_json(const nlohmann::json& j, FundingRateMsg& msg) {
from_json_shared(j, msg);
j.at("funding_rate").get_to(msg.funding_rate);
j.at("funding_time").get_to(msg.funding_time);
if (j.contains("estimated_rate")) {
msg.estimated_rate = j.at("estimated_rate").get<double>();
}
}
static inline bool is_null_terminated_view(std::string_view sv) noexcept {
return !sv.empty() && sv.data()[sv.length()] == '\0';
}
std::string extract_symbol(std::string_view exchange, MarketType market_type,
std::string_view msg) {
const char* exchange_c_str = nullptr;
std::string exchange_string;
if (is_null_terminated_view(exchange)) {
exchange_c_str = exchange.data();
} else {
exchange_string = std::move(std::string(exchange));
exchange_c_str = exchange_string.c_str();
}
const char* msg_c_str = nullptr;
std::string msg_string;
if (is_null_terminated_view(msg)) {
msg_c_str = msg.data();
} else {
msg_string = std::move(std::string(msg));
msg_c_str = exchange_string.c_str();
}
const char* symbol = extract_symbol(exchange_c_str, market_type, msg_c_str);
return std::string(symbol);
}
// NOLINTNEXTLINE
MessageType get_msg_type(std::string_view exchange, std::string_view msg) {
const char* exchange_c_str = nullptr;
std::string exchange_string;
if (is_null_terminated_view(exchange)) {
exchange_c_str = exchange.data();
} else {
exchange_string = std::move(std::string(exchange));
exchange_c_str = exchange_string.c_str();
}
const char* msg_c_str = nullptr;
std::string msg_string;
if (is_null_terminated_view(msg)) {
msg_c_str = msg.data();
} else {
msg_string = std::move(std::string(msg));
msg_c_str = msg_string.c_str();
}
return get_msg_type(exchange_c_str, msg_c_str);
}
std::vector<TradeMsg> parse_trade(std::string_view exchange,
MarketType market_type,
std::string_view msg) {
const char* exchange_c_str = nullptr;
std::string exchange_string;
if (is_null_terminated_view(exchange)) {
exchange_c_str = exchange.data();
} else {
exchange_string = std::move(std::string(exchange));
exchange_c_str = exchange_string.c_str();
}
const char* msg_c_str = nullptr;
std::string msg_string;
if (is_null_terminated_view(msg)) {
msg_c_str = msg.data();
} else {
msg_string = std::move(std::string(msg));
msg_c_str = msg_string.c_str();
}
const char* parsed_msg = parse_trade(exchange_c_str, market_type, msg_c_str);
nlohmann::json j = nlohmann::json::parse(parsed_msg);
deallocate_string(parsed_msg);
return j.get<std::vector<TradeMsg>>();
}
std::vector<OrderBookMsg> parse_l2(std::string_view exchange,
MarketType market_type, std::string_view msg,
int64_t timestamp) {
const char* exchange_c_str = nullptr;
std::string exchange_string;
if (is_null_terminated_view(exchange)) {
exchange_c_str = exchange.data();
} else {
exchange_string = std::move(std::string(exchange));
exchange_c_str = exchange_string.c_str();
}
const char* msg_c_str = nullptr;
std::string msg_string;
if (is_null_terminated_view(msg)) {
msg_c_str = msg.data();
} else {
msg_string = std::move(std::string(msg));
msg_c_str = msg_string.c_str();
}
const char* parsed_msg =
parse_l2(exchange_c_str, market_type, msg_c_str, timestamp);
nlohmann::json j = nlohmann::json::parse(parsed_msg);
deallocate_string(parsed_msg);
return j.get<std::vector<OrderBookMsg>>();
}
std::vector<FundingRateMsg> parse_funding_rate(std::string_view exchange,
MarketType market_type,
std::string_view msg) {
const char* exchange_c_str = nullptr;
std::string exchange_string;
if (is_null_terminated_view(exchange)) {
exchange_c_str = exchange.data();
} else {
exchange_string = std::move(std::string(exchange));
exchange_c_str = exchange_string.c_str();
}
const char* msg_c_str = nullptr;
std::string msg_string;
if (is_null_terminated_view(msg)) {
msg_c_str = msg.data();
} else {
msg_string = std::move(std::string(msg));
msg_c_str = msg_string.c_str();
}
const char* parsed_msg =
parse_funding_rate(exchange_c_str, market_type, msg_c_str);
nlohmann::json j = nlohmann::json::parse(parsed_msg);
deallocate_string(parsed_msg);
return j.get<std::vector<FundingRateMsg>>();
}