Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
hyperliquid_l2_diff.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <common/types.hpp>
5#include <nlohmann/json.hpp>
6#include <string>
7#include <utility>
8#include <vector>
9
11
12// One side of an order book, ordered best price first — the same order the
13// exchange/publisher already builds "bids"/"asks" arrays in.
14using L2Levels = std::vector<std::pair<price_t, qty_t>>;
15
16// Price levels Hyperliquid publishes per side on `l2Book` and `l2`. The
17// simulator's own book is not depth-limited — it holds every level the venue
18// ever told it about, plus anything a trade print left resting — so the publish
19// path truncates to this. See split_book_snapshot().
20inline constexpr uint32_t L2_PUBLISH_DEPTH = 20;
21
22// Splits a BookSnapshot frame's flat level array into the venue's
23// `[bids, asks]` JSON and the diff baseline those same levels form, each
24// truncated to L2_PUBLISH_DEPTH.
25//
26// One function producing both, deliberately: a diff's "r" entries address
27// positions in the array the subscriber last received, so a baseline truncated
28// differently from the JSON would number levels the client never saw.
29void split_book_snapshot(const MDLevel* levels, uint32_t num_bid, uint32_t num_ask,
30 nlohmann::json::array_t& bids, nlohmann::json::array_t& asks,
31 L2Levels& curr_bid, L2Levels& curr_ask);
32
33struct L2Diff {
34 nlohmann::json l; // [bid_changed_or_added[], ask_changed_or_added[]] — {"p","s"} entries, book order
35 nlohmann::json r; // [bid_removed_indices[], ask_removed_indices[]] — indices into the *previous* array
36};
37
38// Computes the incremental diff between a previous and current order book
39// state, matching Hyperliquid's undocumented `l2` channel diff shape:
40// changed/added levels are reported by price+size, removed levels are
41// reported by their index in the previous per-side array.
42//
43// Ordering is part of that shape, not an implementation detail. The venue emits
44// "l" in book order and "r" in ascending price order — so bid removal indices
45// descend and ask removal indices ascend — and a client is free to apply the
46// removals in the order it receives them. See diff_side() in the .cpp.
47L2Diff compute_l2_diff(const L2Levels& prev_bid, const L2Levels& prev_ask,
48 const L2Levels& curr_bid, const L2Levels& curr_ask);
49
50// Inverse of hyperliquid::decode_l2_diff() (SlickQuant/hyperliquid-cpp):
51// JSON-dump, raw-deflate compress (no zlib/gzip header), base64-encode.
52std::string encode_l2_diff(const nlohmann::json& payload);
53
54} // end namespace slick::sim::md_publisher
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
L2Diff compute_l2_diff(const L2Levels &prev_bid, const L2Levels &prev_ask, const L2Levels &curr_bid, const L2Levels &curr_ask)