hyperliquid_rust_sdk_abrkn/
meta.rs

1use std::collections::HashMap;
2
3use ethers::abi::ethereum_types::H128;
4use serde::Deserialize;
5
6#[derive(Deserialize, Debug, Clone)]
7pub struct Meta {
8    pub universe: Vec<AssetMeta>,
9}
10
11pub type MetaAndAssetCtxs = (Meta, Vec<AssetContext>);
12#[derive(Deserialize, Debug, Clone)]
13#[serde(rename_all = "camelCase")]
14pub struct AssetContext {
15    pub day_ntl_vlm: String,
16    pub funding: String,
17    pub impact_pxs: Option<(String, String)>,
18    pub mark_px: String,
19    pub mid_px: Option<String>,
20    pub open_interest: Option<String>,
21    pub oracle_px: Option<String>,
22    pub premium: Option<String>,
23    pub prev_day_px: Option<String>,
24}
25
26#[derive(Deserialize, Debug, Clone)]
27pub struct SpotMeta {
28    pub universe: Vec<SpotAssetMeta>,
29    pub tokens: Vec<TokenInfo>,
30}
31
32impl SpotMeta {
33    pub fn add_pair_and_name_to_index_map(
34        &self,
35        mut coin_to_asset: HashMap<String, u32>,
36    ) -> HashMap<String, u32> {
37        let index_to_name: HashMap<usize, &str> = self
38            .tokens
39            .iter()
40            .map(|info| (info.index, info.name.as_str()))
41            .collect();
42
43        for asset in self.universe.iter() {
44            let spot_ind: u32 = 10000 + asset.index as u32;
45            let name_to_ind = (asset.name.clone(), spot_ind);
46
47            let Some(token_1_name) = index_to_name.get(&asset.tokens[0]) else {
48                continue;
49            };
50
51            let Some(token_2_name) = index_to_name.get(&asset.tokens[1]) else {
52                continue;
53            };
54
55            coin_to_asset.insert(format!("{}/{}", token_1_name, token_2_name), spot_ind);
56            coin_to_asset.insert(name_to_ind.0, name_to_ind.1);
57        }
58
59        coin_to_asset
60    }
61}
62
63#[derive(Deserialize, Debug, Clone)]
64#[serde(untagged)]
65pub enum SpotMetaAndAssetCtxs {
66    SpotMeta(SpotMeta),
67    Context(Vec<SpotAssetContext>),
68}
69
70#[derive(Deserialize, Debug, Clone)]
71#[serde(rename_all = "camelCase")]
72pub struct SpotAssetContext {
73    pub day_ntl_vlm: String,
74    pub mark_px: String,
75    pub mid_px: Option<String>,
76    pub prev_day_px: String,
77    pub circulating_supply: String,
78    pub coin: String,
79}
80
81#[derive(Deserialize, Debug, Clone)]
82#[serde(rename_all = "camelCase")]
83pub struct AssetMeta {
84    pub name: String,
85    pub sz_decimals: u32,
86}
87
88#[derive(Deserialize, Debug, Clone)]
89#[serde(rename_all = "camelCase")]
90pub struct SpotAssetMeta {
91    pub tokens: [usize; 2],
92    pub name: String,
93    pub index: usize,
94    pub is_canonical: bool,
95}
96
97#[derive(Debug, Deserialize, Clone)]
98#[serde(rename_all = "camelCase")]
99pub struct TokenInfo {
100    pub name: String,
101    pub sz_decimals: u8,
102    pub wei_decimals: u8,
103    pub index: usize,
104    pub token_id: H128,
105    pub is_canonical: bool,
106}