Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
hyperliquid_publisher.hpp
Go to the documentation of this file.
1#pragma once
2
6#include <uwebsockets/App.h>
7#include <nlohmann/json.hpp>
9#include <unordered_set>
10#include <unordered_map>
11#include <vector>
12#include <thread>
13#include <atomic>
14#include <chrono>
15#include <array>
16#include <utility>
17
18struct us_listen_socket_t;
19
21
22using json = nlohmann::json;
24
26 uint64_t seq_num = 0;
27 std::chrono::steady_clock::time_point last_pong;
29 static uint_fast64_t heartbeat_count;
30
31 struct SubInfo {
32 std::unordered_set<std::string> active;
33 std::unordered_set<std::string> pending;
34 std::vector<std::string> received;
35 };
36 std::array<SubInfo, static_cast<uint8_t>(HyperliquidChannel::__COUNT__)> subs;
37};
38
40 using wsT = uWS::WebSocket<false, true, HyperliquidPerSocketData>;
41
42public:
43 HyperliquidPublisher(const json& config,
44 slick::queue<Request>& request_queue,
45 slick::queue<uint8_t>& market_data_queue);
46
47private:
48 void setup_routes(uWS::App &app) override;
49 void publish_market_data_update(MarketDataUpdate* update) override;
50 void handle_message(wsT* ws, std::string_view message);
51 void handle_info(uWS::HttpResponse<false>* res, uWS::HttpRequest* req);
52 void publish_subscription_response(MarketDataUpdate* update);
53 void publish_book_snapshot(MarketDataUpdate* update);
54 void publish_trade_summary(MarketDataUpdate* update);
55 void check_heartbeats() override;
56 void handle_close(wsT* ws, int code, std::string_view message);
57 void send_error(wsT* ws, const std::string& error_msg);
58 void unsubscribe_md(wsT* ws);
59 void subscribe_channel(wsT* ws, HyperliquidChannel channel, const std::string& coin);
60
61private:
62 // ws_thread_, listen_socket_, loop_, heartbeat_timer_, port_, running_,
63 // data_cursor_, ping_interval_ms_ and pong_timeout_ belong to
64 // WebsocketMarketDataPublisher. Use those members so heartbeat checks and
65 // inherited start()/stop() operate on the same state.
66 std::string upstream_base_url_;
67
68 std::unordered_set<wsT*> clients_;
69 // symbol -> multimap<channel, ws>
70 std::unordered_map<std::string, std::unordered_multimap<uint8_t, wsT*>> symbol_channel_client_;
71 // channel -> symbol -> set<ws>
72 std::array<std::unordered_map<std::string, std::unordered_set<wsT*>>,
73 static_cast<uint8_t>(HyperliquidChannel::__COUNT__)> subscription_info_;
74 // coin -> (bid, ask) — last book state broadcast to "l2" subscribers, used to compute the next diff
75 std::unordered_map<std::string, std::pair<L2Levels, L2Levels>> l2_diff_baseline_;
76};
77
78} // end namespace slick::sim::md_publisher
HyperliquidPublisher(const json &config, slick::queue< Request > &request_queue, slick::queue< uint8_t > &market_data_queue)
WebsocketMarketDataPublisher(Venue venue, slick::queue< Request > &request_queue, slick::queue< uint8_t > &market_data_queue, uint32_t port)
HyperliquidChannel
The market-data channels the Hyperliquid adapter serves.
slick::sim::exch::HyperliquidChannel HyperliquidChannel
std::array< SubInfo, static_cast< uint8_t >(HyperliquidChannel::__COUNT__)> subs