Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
hyperliquid_info_proxy.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <slick/logger.hpp>
4#include <slick/net/http.hpp>
5#include <uwebsockets/App.h>
6#include <nlohmann/json.hpp>
7#include <memory>
8#include <string>
9#include <string_view>
10
11namespace slick::sim {
12
14 std::string status_line;
15 std::string body;
16};
17
18// Decides what to relay to the client given the raw response from the
19// upstream Hyperliquid REST API. Pure/synchronous (no I/O) so it can be unit
20// tested directly against synthetic slick::net::Http::Response values,
21// without depending on real network timing.
22//
23// slick::net::Http reports both a genuine upstream error and a local
24// transport failure (connection refused, timeout, DNS failure, ...) via
25// result_code=500 — in the transport-failure case, result_text holds the raw
26// exception message, not a JSON body. Distinguish the two by whether the
27// body actually parses as JSON.
28inline ProxiedHttpResponse build_proxied_response(const slick::net::Http::Response& rsp) {
29 bool upstream_reached = false;
30 try {
31 [[maybe_unused]] auto result = nlohmann::json::parse(rsp.result_text);
32 upstream_reached = true;
33 } catch (...) {}
34
35 if (!upstream_reached) {
36 return {"502 Bad Gateway", R"({"status":"err","response":"failed to reach upstream Hyperliquid API"})"};
37 }
38
39 std::string status_line = std::to_string(rsp.result_code) + " " +
40 (rsp.reason.empty() ? "OK" : rsp.reason);
41 return {std::move(status_line), rsp.result_text};
42}
43
44// Proxies a client's POST /info request to the real Hyperliquid REST API and
45// relays the response back verbatim. Shared by HyperliquidPublisher (md
46// publisher, port 5001) and HyperliquidRestOrderGateway (order gateway, port
47// 4003) so whichever port a hyperliquid-cpp `Info` client points its
48// base_url at, `load_meta()`/`canonical_coin()` (and any other /info query)
49// get real production data instead of a local stub.
51 uWS::HttpResponse<false>* res,
52 const std::string& upstream_base_url)
53{
54 auto body_buffer = std::make_shared<std::string>();
55
56 res->onAborted([]() {
57 LOG_WARN("HyperliquidInfoProxy: POST /info aborted");
58 });
59
60 res->onData([res, body_buffer, upstream_base_url](std::string_view chunk, bool is_last) {
61 body_buffer->append(chunk);
62 if (!is_last) return;
63
64 auto rsp = slick::net::Http::post(
65 upstream_base_url + "/info", *body_buffer,
66 {{"Content-Type", "application/json"}});
67
68 auto proxied = build_proxied_response(rsp);
69 if (proxied.status_line == "502 Bad Gateway") {
70 LOG_WARN("HyperliquidInfoProxy: failed to reach upstream {}/info: {}",
71 upstream_base_url, rsp.result_text);
72 }
73
74 res->writeStatus(proxied.status_line)
75 ->writeHeader("Content-Type", "application/json")
76 ->end(proxied.body);
77 });
78}
79
80} // end namespace slick::sim
void proxy_hyperliquid_info_request(uWS::HttpResponse< false > *res, const std::string &upstream_base_url)
ProxiedHttpResponse build_proxied_response(const slick::net::Http::Response &rsp)