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)]
80pub enum AssetKind {
81 Perp,
83 Spot,
85 Hip3Perp,
87 Outcome,
89}
90
91impl AssetKind {
92 pub const ALL: [AssetKind; 4] = [AssetKind::Perp, AssetKind::Spot, AssetKind::Hip3Perp, AssetKind::Outcome];
94
95 #[must_use]
97 pub const fn of(id: u32) -> AssetKind {
98 if id >= OUTCOME_ASSET_BASE {
99 AssetKind::Outcome
100 } else if id >= HIP3_ASSET_BASE {
101 AssetKind::Hip3Perp
102 } else if id >= SPOT_ASSET_BASE {
103 AssetKind::Spot
104 } else {
105 AssetKind::Perp
106 }
107 }
108
109 #[must_use]
113 pub const fn builder_cap(self) -> BuilderFee {
114 match self {
115 AssetKind::Perp | AssetKind::Hip3Perp => PERP_BUILDER_CAP,
116 AssetKind::Spot => SPOT_BUILDER_CAP,
117 AssetKind::Outcome => OUTCOME_BUILDER_CAP,
118 }
119 }
120
121 #[must_use]
125 pub fn strictest(assets: impl IntoIterator<Item = Asset>) -> Option<AssetKind> {
126 assets.into_iter().map(Asset::kind).reduce(|kept, next| match next.builder_cap() < kept.builder_cap() {
127 true => next,
128 false => kept,
129 })
130 }
131
132 #[must_use]
134 pub const fn one(self) -> &'static str {
135 match self {
136 AssetKind::Perp => "a perp",
137 AssetKind::Spot => "a spot",
138 AssetKind::Hip3Perp => "a HIP-3 perp",
139 AssetKind::Outcome => "an outcome",
140 }
141 }
142}
143
144#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
148pub struct Asset(u32);
149
150impl Asset {
151 #[must_use]
153 pub const fn from_id(id: u32) -> Asset {
154 Asset(id)
155 }
156
157 #[must_use]
159 pub const fn id(self) -> u32 {
160 self.0
161 }
162
163 #[must_use]
165 pub const fn kind(self) -> AssetKind {
166 AssetKind::of(self.0)
167 }
168
169 #[must_use]
171 pub const fn perp(index: u32) -> Option<Asset> {
172 match index < SPOT_ASSET_BASE {
173 true => Some(Asset(index)),
174 false => None,
175 }
176 }
177
178 #[must_use]
181 pub const fn spot(pair: u32) -> Option<Asset> {
182 match pair < HIP3_ASSET_BASE - SPOT_ASSET_BASE {
183 true => Some(Asset(SPOT_ASSET_BASE + pair)),
184 false => None,
185 }
186 }
187
188 #[must_use]
192 pub const fn hip3(dex: u32, index: u32) -> Option<Asset> {
193 if dex == 0 || index >= HIP3_DEX_SPAN {
194 return None;
195 }
196 let Some(offset) = dex.checked_mul(HIP3_DEX_SPAN) else {
197 return None;
198 };
199 let Some(start) = HIP3_ASSET_BASE.checked_add(offset) else {
200 return None;
201 };
202 match start.checked_add(index) {
203 Some(id) if id < OUTCOME_ASSET_BASE => Some(Asset(id)),
204 _ => None,
205 }
206 }
207
208 #[must_use]
211 pub const fn hip3_parts(self) -> Option<(u32, u32)> {
212 if !matches!(self.kind(), AssetKind::Hip3Perp) {
213 return None;
214 }
215 let offset = self.0 - HIP3_ASSET_BASE;
216 match offset / HIP3_DEX_SPAN {
217 0 => None,
218 place => Some((place, offset % HIP3_DEX_SPAN)),
219 }
220 }
221
222 #[must_use]
224 pub const fn perp_on(dex: u32, index: u32) -> Option<Asset> {
225 match dex {
226 0 => Asset::perp(index),
227 dex => Asset::hip3(dex, index),
228 }
229 }
230
231 #[must_use]
233 pub const fn outcome(outcome: u32, side: OutcomeSide) -> Option<Asset> {
234 let Some(tens) = outcome.checked_mul(10) else {
235 return None;
236 };
237 match OUTCOME_ASSET_BASE.checked_add(tens + side.digit()) {
238 Some(id) => Some(Asset(id)),
239 None => None,
240 }
241 }
242
243 #[must_use]
245 pub const fn outcome_side(self) -> Option<(u32, OutcomeSide)> {
246 if self.0 < OUTCOME_ASSET_BASE {
247 return None;
248 }
249 let encoding = self.0 - OUTCOME_ASSET_BASE;
250 let side = match encoding % 10 {
251 0 => OutcomeSide::Yes,
252 1 => OutcomeSide::No,
253 _ => return None,
254 };
255 Some((encoding / 10, side))
256 }
257}
258
259impl fmt::Display for Asset {
260 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
261 write!(f, "{}", self.0)
262 }
263}
264
265impl From<Asset> for Value {
267 fn from(asset: Asset) -> Value {
268 Value::Uint(u64::from(asset.0))
269 }
270}
271
272#[cfg(test)]
273#[path = "tests/asset.rs"]
274mod tests;