Repository navigation
Expand file tree
/
Copy pathtypes.hpp
More file actions
136 lines (106 loc) · 4.13 KB
/
Copy pathtypes.hpp
File metadata and controls
136 lines (106 loc) · 4.13 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
#pragma once
#include <cstdint>
#include <optional>
#include <stdexcept>
#include <string>
#include <string_view>
#include <variant>
namespace hyperliquid {
namespace detail {
// Value of a hex digit (either case), or -1 if `c` is not one.
constexpr int hex_nibble(char c) noexcept {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return -1;
}
} // namespace detail
// ── Time in force ────────────────────────────────────────────────────────────
enum class Tif {
Alo, // Add liquidity only
Ioc, // Immediate or cancel
Gtc, // Good till cancel
};
inline std::string tif_to_string(Tif t) {
switch (t) {
case Tif::Alo: return "Alo";
case Tif::Ioc: return "Ioc";
case Tif::Gtc: return "Gtc";
}
return "Gtc";
}
// ── Order types ──────────────────────────────────────────────────────────────
struct LimitOrderType {
Tif tif = Tif::Gtc;
};
struct TriggerOrderType {
double trigger_px;
bool is_market;
std::string tpsl; // "tp" or "sl"
};
using OrderType = std::variant<LimitOrderType, TriggerOrderType>;
// ── Client order ID (16-byte hex: "0x" + 32 hex chars) ──────────────────────
class Cloid {
public:
// Throws std::invalid_argument if raw is not "0x" followed by exactly 32 hex chars.
explicit Cloid(std::string raw) : raw_(std::move(raw)) { validate(); }
static Cloid from_int(uint64_t v) {
char buf[35];
std::snprintf(buf, sizeof(buf), "0x%032llx",
static_cast<unsigned long long>(v));
return Cloid{buf};
}
// Throws std::invalid_argument if s is not "0x" followed by exactly 32 hex chars.
static Cloid from_str(std::string_view s) {
return Cloid{std::string(s)};
}
std::string_view to_raw() const noexcept { return raw_; }
bool operator==(const Cloid& o) const noexcept { return raw_ == o.raw_; }
private:
std::string raw_;
void validate() const {
if (raw_.size() < 2 || raw_[0] != '0' || (raw_[1] != 'x' && raw_[1] != 'X'))
throw std::invalid_argument("Cloid must be a hex string starting with 0x");
if (raw_.size() - 2 != 32)
throw std::invalid_argument("Cloid must be exactly 16 bytes (32 hex chars after 0x)");
for (size_t i = 2; i < raw_.size(); ++i) {
if (detail::hex_nibble(raw_[i]) < 0)
throw std::invalid_argument("Cloid contains a non-hex character");
}
}
};
// ── Order / cancel / modify request types ───────────────────────────────────
struct OrderRequest {
std::string coin;
bool is_buy = true;
double sz = 0.0;
double limit_px = 0.0;
OrderType order_type = LimitOrderType{Tif::Gtc};
bool reduce_only = false;
std::optional<Cloid> cloid;
};
struct CancelRequest {
std::string coin;
int64_t oid;
};
struct CancelByCloidRequest {
std::string coin;
Cloid cloid;
};
struct ModifyRequest {
int64_t oid; // order ID to modify
OrderRequest order;
};
// ── Builder fee ──────────────────────────────────────────────────────────────
// b = builder address, f = fee in tenths of basis points (e.g. 10 = 1 bp)
struct BuilderInfo {
std::string b;
int f = 0;
};
// ── EIP-712 signature ────────────────────────────────────────────────────────
struct Signature {
std::string r; // "0x" + 64 hex chars
std::string s; // "0x" + 64 hex chars
int v; // 27 or 28
};
} // namespace hyperliquid