Repository navigation
Expand file tree
/
Copy pathinfo.cpp
More file actions
276 lines (233 loc) · 7.62 KB
/
Copy pathinfo.cpp
File metadata and controls
276 lines (233 loc) · 7.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <hyperliquid/info.hpp>
#include <stdexcept>
#include <utility>
namespace hyperliquid {
Info::Info(
std::string_view base_url,
bool skip_ws,
bool user_thread_dispatch,
uint32_t mux_record_size,
const char* mux_shm_name,
uint32_t read_buffer_size,
uint32_t read_control_size,
const char* read_buffer_shm_name,
uint32_t write_buffer_size
)
: Api(base_url)
{
if (!skip_ws) {
ws_ = std::make_unique<WebsocketManager>(
base_url,
mux_record_size,
mux_shm_name,
read_buffer_size,
read_control_size,
read_buffer_shm_name,
write_buffer_size,
user_thread_dispatch
);
}
}
Info::Info(
std::string_view base_url,
slick::stream_buffer_multiplexer &mux,
bool skip_ws,
bool user_thread_dispatch,
uint32_t read_buffer_size,
uint32_t read_control_size,
const char* read_buffer_shm_name,
uint32_t write_buffer_size
)
: Api(base_url)
{
if (!skip_ws) {
ws_ = std::make_unique<WebsocketManager>(
base_url,
mux,
read_buffer_size,
read_control_size,
read_buffer_shm_name,
write_buffer_size,
user_thread_dispatch
);
}
}
// Internal helper
nlohmann::json Info::info_post(const nlohmann::json& payload) {
return post("/info", payload);
}
nlohmann::json Info::remap_subscription(const nlohmann::json& subscription) {
auto remapped = subscription;
const auto type = remapped.value("type", "");
if (type == "l2Book" || type == "trades" || type == "candle" ||
type == "bbo" || type == "activeAssetCtx" || type == "activeAssetData")
{
remapped["coin"] = canonical_coin(remapped.at("coin").get<std::string>());
}
return remapped;
}
void Info::populate_perp_meta(const nlohmann::json& meta) {
int idx = 0;
for (const auto& asset : meta.at("universe")) {
const std::string coin = asset.at("name").get<std::string>();
coin_to_asset[coin] = idx;
name_to_coin[coin] = coin;
asset_to_sz_decimals[idx] = asset.at("szDecimals").get<int>();
++idx;
}
perp_meta_loaded_ = true;
}
void Info::populate_spot_meta(const nlohmann::json& spot_meta) {
std::unordered_map<int, nlohmann::json> token_by_index;
for (const auto& token : spot_meta.at("tokens"))
token_by_index[token.at("index").get<int>()] = token;
for (const auto& spot_info : spot_meta.at("universe")) {
const int index = spot_info.at("index").get<int>();
const int asset = 10000 + index;
const std::string coin = spot_info.at("name").get<std::string>();
coin_to_asset[coin] = asset;
name_to_coin[coin] = coin;
name_to_coin["@" + std::to_string(index)] = coin;
const int base = spot_info.at("tokens").at(0).get<int>();
const int quote = spot_info.at("tokens").at(1).get<int>();
const auto& base_info = token_by_index.at(base);
const auto& quote_info = token_by_index.at(quote);
asset_to_sz_decimals[asset] = base_info.at("szDecimals").get<int>();
const std::string pair_name =
base_info.at("name").get<std::string>() + "/" +
quote_info.at("name").get<std::string>();
name_to_coin.emplace(pair_name, coin);
}
spot_meta_loaded_ = true;
}
// Meta
void Info::load_meta() {
if (!spot_meta_loaded_)
spot_meta();
if (!perp_meta_loaded_)
meta();
}
nlohmann::json Info::meta() {
auto result = info_post({{"type", "meta"}});
if (!perp_meta_loaded_)
populate_perp_meta(result);
return result;
}
nlohmann::json Info::spot_meta() {
auto result = info_post({{"type", "spotMeta"}});
if (!spot_meta_loaded_)
populate_spot_meta(result);
return result;
}
std::string Info::canonical_coin(std::string_view coin_or_name) {
if (!perp_meta_loaded_ || !spot_meta_loaded_)
load_meta();
const auto it = name_to_coin.find(std::string(coin_or_name));
if (it != name_to_coin.end())
return it->second;
return std::string(coin_or_name);
}
int Info::name_to_asset(std::string_view coin_or_name) {
const std::string coin = canonical_coin(coin_or_name);
const auto it = coin_to_asset.find(coin);
if (it == coin_to_asset.end())
throw std::invalid_argument("Unknown coin: " + std::string(coin_or_name));
return it->second;
}
// Market data
nlohmann::json Info::all_mids() {
return info_post({{"type", "allMids"}});
}
nlohmann::json Info::l2_snapshot(std::string_view coin) {
return info_post({{"type", "l2Book"}, {"coin", canonical_coin(coin)}});
}
nlohmann::json Info::bbo(std::string_view coin) {
return info_post({{"type", "bbo"}, {"coin", canonical_coin(coin)}});
}
nlohmann::json Info::candle_snapshot(
std::string_view coin, std::string_view interval,
int64_t start_ms, std::optional<int64_t> end_ms)
{
nlohmann::json req{
{"type", "candleSnapshot"},
{"req",
{
{"coin", canonical_coin(coin)},
{"interval", interval},
{"startTime", start_ms},
}},
};
if (end_ms)
req["req"]["endTime"] = *end_ms;
return info_post(req);
}
nlohmann::json Info::perp_asset_ctxs() {
return info_post({{"type", "metaAndAssetCtxs"}});
}
nlohmann::json Info::spot_asset_ctxs() {
return info_post({{"type", "spotMetaAndAssetCtxs"}});
}
nlohmann::json Info::funding_history(
std::string_view coin, int64_t start_ms, std::optional<int64_t> end_ms)
{
nlohmann::json req{
{"type", "fundingHistory"},
{"coin", canonical_coin(coin)},
{"startTime", start_ms},
};
if (end_ms)
req["endTime"] = *end_ms;
return info_post(req);
}
// User data
nlohmann::json Info::user_state(std::string_view address) {
return info_post({{"type", "clearinghouseState"}, {"user", address}});
}
nlohmann::json Info::open_orders(std::string_view address) {
return info_post({{"type", "openOrders"}, {"user", address}});
}
nlohmann::json Info::user_fills(std::string_view address) {
return info_post({{"type", "userFills"}, {"user", address}});
}
nlohmann::json Info::user_fills_by_time(
std::string_view address, int64_t start_ms, std::optional<int64_t> end_ms)
{
nlohmann::json req{
{"type", "userFillsByTime"},
{"user", address},
{"startTime", start_ms},
};
if (end_ms)
req["endTime"] = *end_ms;
return info_post(req);
}
nlohmann::json Info::query_order_by_oid(std::string_view address, int64_t oid) {
return info_post({{"type", "orderStatus"}, {"user", address}, {"oid", oid}});
}
nlohmann::json Info::query_order_by_cloid(
std::string_view address, std::string_view cloid)
{
return info_post({{"type", "orderStatus"}, {"user", address}, {"oid", cloid}});
}
nlohmann::json Info::sub_accounts(std::string_view address) {
return info_post({{"type", "subAccounts"}, {"user", address}});
}
// WebSocket
int Info::subscribe(const nlohmann::json& subscription,
std::function<void(const nlohmann::json&)> callback)
{
if (!ws_)
throw std::runtime_error("WebSocket not started (use skip_ws=false)");
return ws_->subscribe(remap_subscription(subscription), std::move(callback));
}
void Info::unsubscribe(const nlohmann::json& subscription, int subscription_id) {
if (ws_)
ws_->unsubscribe(remap_subscription(subscription), subscription_id);
}
bool Info::dispatch(uint32_t producer_id, const char* data, size_t length) {
if (ws_) {
return ws_->dispatch(producer_id, data, length);
}
return false;
}
} // namespace hyperliquid