4#include <openssl/evp.h>
9#include <unordered_map>
10#include <unordered_set>
16std::string format_price(
price_t price) {
20std::string format_qty(
qty_t qty) {
32 bool descending_index,
33 nlohmann::json& l_out, nlohmann::json& r_out) {
34 std::unordered_map<price_t, qty_t> prev_by_price;
35 prev_by_price.reserve(prev.size());
36 for (
const auto& [price, qty] : prev) {
37 prev_by_price.emplace(price, qty);
40 std::unordered_set<price_t> curr_prices;
41 curr_prices.reserve(curr.size());
42 for (
const auto& [price, qty] : curr) {
43 curr_prices.insert(price);
46 l_out = nlohmann::json::array();
47 for (
const auto& [price, qty] : curr) {
48 auto it = prev_by_price.find(price);
49 if (it == prev_by_price.end() || it->second != qty) {
50 l_out.push_back({{
"p", format_price(price)}, {
"s", format_qty(qty)}});
54 r_out = nlohmann::json::array();
55 if (descending_index) {
56 for (
size_t i = prev.size(); i-- > 0; ) {
57 if (!curr_prices.contains(prev[i].first)) {
62 for (
size_t i = 0; i < prev.size(); ++i) {
63 if (!curr_prices.contains(prev[i].first)) {
70std::string base64_encode(
const std::vector<uint8_t>& data) {
71 if (data.empty())
return {};
72 std::string out(4 * ((data.size() + 2) / 3),
'\0');
73 int len = EVP_EncodeBlock(
74 reinterpret_cast<unsigned char*
>(out.data()),
75 data.data(),
static_cast<int>(data.size()));
76 out.resize(
static_cast<size_t>(len));
85 nlohmann::json::array_t& bids, nlohmann::json::array_t& asks,
94 bids.reserve(bid_count);
95 asks.reserve(ask_count);
96 curr_bid.reserve(bid_count);
97 curr_ask.reserve(ask_count);
104 auto emit = [](
const MDLevel& level, nlohmann::json::array_t& out,
L2Levels& baseline) {
109 {
"px", format_price(price)},
110 {
"sz", format_qty(qty)},
113 baseline.emplace_back(price, qty);
116 for (uint32_t i = 0; i < bid_count; ++i) {
117 emit(levels[i], bids, curr_bid);
121 for (uint32_t i = 0; i < ask_count; ++i) {
122 emit(levels[num_bid + i], asks, curr_ask);
128 nlohmann::json l_bid, r_bid, l_ask, r_ask;
129 diff_side(prev_bid, curr_bid,
true, l_bid, r_bid);
130 diff_side(prev_ask, curr_ask,
false, l_ask, r_ask);
133 diff.
l = nlohmann::json::array({std::move(l_bid), std::move(l_ask)});
134 diff.
r = nlohmann::json::array({std::move(r_bid), std::move(r_ask)});
139 const std::string plain = payload.dump();
142 if (deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -MAX_WBITS, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
143 throw std::runtime_error(
"Failed to initialize raw deflate encoder");
145 stream.next_in =
reinterpret_cast<Bytef*
>(
const_cast<char*
>(plain.data()));
146 stream.avail_in =
static_cast<uInt
>(plain.size());
148 std::vector<uint8_t> compressed(deflateBound(&stream,
static_cast<uLong
>(plain.size())));
149 stream.next_out = compressed.data();
150 stream.avail_out =
static_cast<uInt
>(compressed.size());
152 int result = deflate(&stream, Z_FINISH);
153 size_t produced = compressed.size() - stream.avail_out;
155 if (result != Z_STREAM_END) {
156 throw std::runtime_error(
"Failed to deflate l2 diff payload");
158 compressed.resize(produced);
160 return base64_encode(compressed);
std::vector< std::pair< price_t, qty_t > > L2Levels
std::string encode_l2_diff(const nlohmann::json &payload)
void split_book_snapshot(const MDLevel *levels, uint32_t num_bid, uint32_t num_ask, nlohmann::json::array_t &bids, nlohmann::json::array_t &asks, L2Levels &curr_bid, L2Levels &curr_ask)
constexpr uint32_t L2_PUBLISH_DEPTH
std::string to_hyperliquid_number(int_fast64_t value)
Formats a fixed-point price or size as Hyperliquid does on the wire.
L2Diff compute_l2_diff(const L2Levels &prev_bid, const L2Levels &prev_ask, const L2Levels &curr_bid, const L2Levels &curr_ask)