df_hyperliquid_core/utils/
asset.rs1use std::fmt;
7
8use crate::trade::builder::fee::{BuilderFee, OUTCOME_BUILDER_CAP, PERP_BUILDER_CAP, SPOT_BUILDER_CAP};
9use crate::utils::sign::msgpack::Value;
10
11pub const SPOT_ASSET_BASE: u32 = 10_000;
13
14pub const HIP3_ASSET_BASE: u32 = 100_000;
17
18pub const HIP3_DEX_SPAN: u32 = 10_000;
20
21pub const OUTCOME_ASSET_BASE: u32 = 100_000_000;
23
24#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
26pub enum OutcomeSide {
27 Yes,
29 No,
31}
32
33impl OutcomeSide {
34 #[must_use]
36 pub const fn digit(self) -> u32 {
37 match self {
38 OutcomeSide::Yes => 0,
39 OutcomeSide::No => 1,
40 }
41 }
42
43 #[must_use]
45 pub const fn other(self) -> OutcomeSide {
46 match self {
47 OutcomeSide::Yes => OutcomeSide::No,
48 OutcomeSide::No => OutcomeSide::Yes,
49 }
50 }
51
52 #[must_use]
54 pub const fn as_str(self) -> &'static str {
55 match self {
56 OutcomeSide::Yes => "yes",
57 OutcomeSide::No => "no",
58 }
59 }
60}
61
62impl serde::Serialize for OutcomeSide {
63 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
64 serializer.serialize_str(self.as_str())
65 }
66}
67
68impl<'de> serde::Deserialize<'de> for OutcomeSide {
69 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
70 match String::deserialize(deserializer)?.as_str() {
71 "yes" => Ok(OutcomeSide::Yes),
72 "no" => Ok(OutcomeSide::No),
73 other => Err(serde::de::Error::custom(format!("`{other}` is not an outcome side; expected yes or no"))),
74 }
75 }
76}
77
78#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
80#[non_exhaustive]
81pub enum AssetKind {
82 Perp,
84 Spot,
86 Hip3Perp,
88 Outcome,
90}
91
92impl AssetKind {
93 pub const ALL: [AssetKind; 4] = [AssetKind::Perp, AssetKind::Spot, AssetKind::Hip3Perp, AssetKind::Outcome];
95
96 #[must_use]
98 pub const fn of(id: u32) -> AssetKind {
99 if id >= OUTCOME_ASSET_BASE {
100 AssetKind::Outcome
101 } else if id >= HIP3_ASSET_BASE {
102 AssetKind::Hip3Perp
103 } else if id >= SPOT_ASSET_BASE {
104 AssetKind::Spot
105 } else {
106 AssetKind::Perp
107 }
108 }
109
110 #[must_use]
114 pub const fn builder_cap(self) -> BuilderFee {
115 match self {
116 AssetKind::Perp | AssetKind::Hip3Perp => PERP_BUILDER_CAP,
117 AssetKind::Spot => SPOT_BUILDER_CAP,
118 AssetKind::Outcome => OUTCOME_BUILDER_CAP,
119 }
120 }
121
122 #[must_use]
126 pub fn strictest(assets: impl IntoIterator<Item = Asset>) -> Option<AssetKind> {
127 assets.into_iter().map(Asset::kind).reduce(|kept, next| match next.builder_cap() < kept.builder_cap() {
128 true => next,
129 false => kept,
130 })
131 }
132
133 #[must_use]
135 pub const fn one(self) -> &'static str {
136 match self {
137 AssetKind::Perp => "a perp",
138 AssetKind::Spot => "a spot",
139 AssetKind::Hip3Perp => "a HIP-3 perp",
140 AssetKind::Outcome => "an outcome",
141 }
142 }
143}
144
145#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
149pub struct Asset(u32);
150
151impl Asset {
152 #[must_use]
154 pub const fn from_id(id: u32) -> Asset {
155 Asset(id)
156 }
157
158 #[must_use]
160 pub const fn id(self) -> u32 {
161 self.0
162 }
163
164 #[must_use]
166 pub const fn kind(self) -> AssetKind {
167 AssetKind::of(self.0)
168 }
169
170 #[must_use]
172 pub const fn perp(index: u32) -> Option<Asset> {
173 match index < SPOT_ASSET_BASE {
174 true => Some(Asset(index)),
175 false => None,
176 }
177 }
178
179 #[must_use]
182 pub const fn spot(pair: u32) -> Option<Asset> {
183 match pair < HIP3_ASSET_BASE - SPOT_ASSET_BASE {
184 true => Some(Asset(SPOT_ASSET_BASE + pair)),
185 false => None,
186 }
187 }
188
189 #[must_use]
193 pub const fn hip3(dex: u32, index: u32) -> Option<Asset> {
194 if dex == 0 || index >= HIP3_DEX_SPAN {
195 return None;
196 }
197 let Some(offset) = dex.checked_mul(HIP3_DEX_SPAN) else {
198 return None;
199 };
200 let Some(start) = HIP3_ASSET_BASE.checked_add(offset) else {
201 return None;
202 };
203 match start.checked_add(index) {
204 Some(id) if id < OUTCOME_ASSET_BASE => Some(Asset(id)),
205 _ => None,
206 }
207 }
208
209 #[must_use]
212 pub const fn hip3_parts(self) -> Option<(u32, u32)> {
213 if !matches!(self.kind(), AssetKind::Hip3Perp) {
214 return None;
215 }
216 let offset = self.0 - HIP3_ASSET_BASE;
217 match offset / HIP3_DEX_SPAN {
218 0 => None,
219 place => Some((place, offset % HIP3_DEX_SPAN)),
220 }
221 }
222
223 #[must_use]
225 pub const fn perp_on(dex: u32, index: u32) -> Option<Asset> {
226 match dex {
227 0 => Asset::perp(index),
228 dex => Asset::hip3(dex, index),
229 }
230 }
231
232 #[must_use]
234 pub const fn outcome(outcome: u32, side: OutcomeSide) -> Option<Asset> {
235 let Some(tens) = outcome.checked_mul(10) else {
236 return None;
237 };
238 match OUTCOME_ASSET_BASE.checked_add(tens + side.digit()) {
239 Some(id) => Some(Asset(id)),
240 None => None,
241 }
242 }
243
244 #[must_use]
246 pub const fn outcome_side(self) -> Option<(u32, OutcomeSide)> {
247 if self.0 < OUTCOME_ASSET_BASE {
248 return None;
249 }
250 let encoding = self.0 - OUTCOME_ASSET_BASE;
251 let side = match encoding % 10 {
252 0 => OutcomeSide::Yes,
253 1 => OutcomeSide::No,
254 _ => return None,
255 };
256 Some((encoding / 10, side))
257 }
258}
259
260impl fmt::Display for Asset {
261 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
262 write!(f, "{}", self.0)
263 }
264}
265
266impl From<Asset> for Value {
268 fn from(asset: Asset) -> Value {
269 Value::Uint(u64::from(asset.0))
270 }
271}
272
273#[cfg(test)]
274#[path = "tests/asset.rs"]
275mod tests;