|
| 1 | +// WebSocket market data example: one WebSocket connection per coin. |
| 2 | +// Subscribes to ETH and BTC l2Book updates on testnet. |
| 3 | +// Usage: market_data_websocket_per_coin [seconds] |
| 4 | +// Defaults: seconds=30 |
| 5 | + |
| 6 | +#include <hyperliquid/hyperliquid.hpp> |
| 7 | + |
| 8 | +#include <slick/net/logging.hpp> |
| 9 | + |
| 10 | +#include <atomic> |
| 11 | +#include <chrono> |
| 12 | +#include <format> |
| 13 | +#include <iostream> |
| 14 | +#include <memory> |
| 15 | +#include <stdexcept> |
| 16 | +#include <string> |
| 17 | +#include <thread> |
| 18 | +#include <utility> |
| 19 | +#include <vector> |
| 20 | + |
| 21 | +namespace { |
| 22 | + |
| 23 | +const char* log_level_name(slick::net::LogLevel level) { |
| 24 | + switch (level) { |
| 25 | + case slick::net::LogLevel::Trace: |
| 26 | + return "TRACE"; |
| 27 | + case slick::net::LogLevel::Debug: |
| 28 | + return "DEBUG"; |
| 29 | + case slick::net::LogLevel::Info: |
| 30 | + return "INFO"; |
| 31 | + case slick::net::LogLevel::Warn: |
| 32 | + return "WARN"; |
| 33 | + case slick::net::LogLevel::Error: |
| 34 | + return "ERROR"; |
| 35 | + } |
| 36 | + return "UNKNOWN"; |
| 37 | +} |
| 38 | + |
| 39 | +void configure_slick_net_logging() { |
| 40 | + slick::net::set_log_handler( |
| 41 | + [](slick::net::LogLevel level, const char* format_text, std::format_args args) { |
| 42 | + std::string message; |
| 43 | + try { |
| 44 | + message = std::vformat(format_text, args); |
| 45 | + } catch (...) { |
| 46 | + message = format_text ? format_text : ""; |
| 47 | + } |
| 48 | + |
| 49 | + std::cout << "[" << log_level_name(level) << "] " << message << "\n"; |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +struct SlickNetLogHandler { |
| 54 | + SlickNetLogHandler() { |
| 55 | + configure_slick_net_logging(); |
| 56 | + } |
| 57 | + |
| 58 | + ~SlickNetLogHandler() { |
| 59 | + slick::net::clear_log_handler(); |
| 60 | + } |
| 61 | +}; |
| 62 | + |
| 63 | +int parse_duration_seconds(int argc, char* argv[]) { |
| 64 | + if (argc < 2) |
| 65 | + return 30; |
| 66 | + |
| 67 | + std::size_t parsed = 0; |
| 68 | + const int seconds = std::stoi(argv[1], &parsed); |
| 69 | + if (parsed != std::string(argv[1]).size() || seconds <= 0) |
| 70 | + throw std::invalid_argument("expected a positive integer duration"); |
| 71 | + return seconds; |
| 72 | +} |
| 73 | + |
| 74 | +void print_top_of_book(const nlohmann::json& msg, const std::string& coin) { |
| 75 | + if (!msg.contains("data")) |
| 76 | + return; |
| 77 | + |
| 78 | + const auto& data = msg["data"]; |
| 79 | + if (!data.contains("levels") || !data["levels"].is_array() || data["levels"].size() < 2) |
| 80 | + return; |
| 81 | + |
| 82 | + const auto& bids = data["levels"][0]; |
| 83 | + const auto& asks = data["levels"][1]; |
| 84 | + const std::string bid = (!bids.empty() && bids[0].contains("px")) |
| 85 | + ? bids[0]["px"].get<std::string>() |
| 86 | + : "n/a"; |
| 87 | + const std::string ask = (!asks.empty() && asks[0].contains("px")) |
| 88 | + ? asks[0]["px"].get<std::string>() |
| 89 | + : "n/a"; |
| 90 | + |
| 91 | + LOG_INFO("[l2Book] {} best_bid={} best_ask={}", coin, bid, ask); |
| 92 | +} |
| 93 | + |
| 94 | +struct CoinFeed { |
| 95 | + std::string coin; |
| 96 | + hyperliquid::Info info; |
| 97 | + nlohmann::json subscription; |
| 98 | + int subscription_id = 0; |
| 99 | + std::atomic<int> updates{0}; |
| 100 | + |
| 101 | + explicit CoinFeed(std::string coin_) |
| 102 | + : coin(std::move(coin_)) |
| 103 | + , info(hyperliquid::TESTNET_API_URL, /*skip_ws=*/false) |
| 104 | + , subscription{{"type", "l2Book"}, {"coin", coin}} |
| 105 | + {} |
| 106 | +}; |
| 107 | + |
| 108 | +} // namespace |
| 109 | + |
| 110 | +int main(int argc, char* argv[]) { |
| 111 | + const SlickNetLogHandler log_handler; |
| 112 | + |
| 113 | + int seconds = 30; |
| 114 | + try { |
| 115 | + seconds = parse_duration_seconds(argc, argv); |
| 116 | + } catch (const std::exception& ex) { |
| 117 | + LOG_ERROR("Invalid arguments: {}", ex.what()); |
| 118 | + LOG_INFO("Usage: market_data_websocket_per_coin [seconds]"); |
| 119 | + return 1; |
| 120 | + } |
| 121 | + |
| 122 | + std::vector<std::unique_ptr<CoinFeed>> feeds; |
| 123 | + feeds.push_back(std::make_unique<CoinFeed>("ETH")); |
| 124 | + feeds.push_back(std::make_unique<CoinFeed>("BTC")); |
| 125 | + |
| 126 | + for (auto& feed : feeds) { |
| 127 | + feed->subscription_id = feed->info.subscribe( |
| 128 | + feed->subscription, |
| 129 | + [feed_ptr = feed.get()](const nlohmann::json& msg) { |
| 130 | + feed_ptr->updates.fetch_add(1, std::memory_order_relaxed); |
| 131 | + print_top_of_book(msg, feed_ptr->coin); |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + LOG_INFO("Listening for ETH and BTC market data on testnet for {} seconds using one WebSocket per coin...", |
| 136 | + seconds); |
| 137 | + |
| 138 | + std::this_thread::sleep_for(std::chrono::seconds(seconds)); |
| 139 | + |
| 140 | + for (auto& feed : feeds) |
| 141 | + feed->info.unsubscribe(feed->subscription, feed->subscription_id); |
| 142 | + |
| 143 | + std::string summary = "Done."; |
| 144 | + for (const auto& feed : feeds) { |
| 145 | + summary += " " + feed->coin + "=" + |
| 146 | + std::to_string(feed->updates.load(std::memory_order_relaxed)) + |
| 147 | + " l2Book updates."; |
| 148 | + } |
| 149 | + LOG_INFO("{}", summary); |
| 150 | + |
| 151 | + return 0; |
| 152 | +} |
0 commit comments