Skip to main content

df_hyperliquid_core/client/evm/precompiles/
assets.rs

1//! Market info: a perp's asset info, a spot pair, a token and its EVM contract,
2//! and a token's supply.
3
4use crate::client::evm::abi::AbiError;
5use crate::client::evm::precompiles::fields::{Fields, tokens};
6use crate::client::evm::precompiles::read::{Arg, Read, read};
7use crate::utils::Address;
8
9/// A perp's asset info, as [`perp_asset_info`] answers it.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct PerpAssetInfo {
12    /// The perp's name, such as `BTC`.
13    pub coin: String,
14    /// `marginTableId`: the margin table that sets its leverage tiers.
15    pub margin_table_id: u32,
16    /// `szDecimals`: the decimals its sizes are counted in.
17    pub sz_decimals: u8,
18    /// `maxLeverage`: the most leverage it allows, as a whole multiple.
19    pub max_leverage: u8,
20    /// `onlyIsolated`: whether it trades only with isolated margin.
21    pub only_isolated: bool,
22}
23
24/// `0x80a`: a perp's name, size decimals and leverage cap.
25#[must_use]
26pub fn perp_asset_info(perp: u32) -> Read<PerpAssetInfo> {
27    read(0x80a, &[Arg::Uint(u64::from(perp))], |answer| {
28        let tokens = tokens(0x80a, answer)?;
29        let mut fields = Fields::of("perpAssetInfo", &tokens)?;
30        Ok(PerpAssetInfo {
31            coin: fields.string()?,
32            margin_table_id: fields.u32()?,
33            sz_decimals: fields.u8()?,
34            max_leverage: fields.u8()?,
35            only_isolated: fields.bool()?,
36        })
37    })
38}
39
40/// A spot pair, as [`spot_info`] answers it.
41#[derive(Debug, Clone, PartialEq, Eq)]
42pub struct SpotInfo {
43    /// The pair's name, such as `PURR/USDC` or `@107`.
44    pub name: String,
45    /// The base token's index, then the quote's.
46    pub tokens: [u64; 2],
47}
48
49/// `0x80b`: a spot pair and the two tokens it is between.
50#[must_use]
51pub fn spot_info(spot: u32) -> Read<SpotInfo> {
52    read(0x80b, &[Arg::Uint(u64::from(spot))], |answer| {
53        let tokens = tokens(0x80b, answer)?;
54        let mut fields = Fields::of("spotInfo", &tokens)?;
55        let name = fields.string()?;
56        let pair: [u64; 2] =
57            fields.u64s()?.try_into().map_err(|_| AbiError::Type("spotInfo: not two tokens".to_owned()))?;
58        Ok(SpotInfo { name, tokens: pair })
59    })
60}
61
62/// A token, as [`token_info`] answers it.
63#[derive(Debug, Clone, PartialEq, Eq)]
64pub struct TokenInfo {
65    /// The token's name, such as `HYPE`.
66    pub name: String,
67    /// The spot pairs it trades in.
68    pub spots: Vec<u64>,
69    /// `deployerTradingFeeShare`: the deployer's share of trading fees.
70    pub deployer_trading_fee_share: u64,
71    /// The address that deployed the token; zero for none.
72    pub deployer: Address,
73    /// The linked ERC-20 on HyperEVM; zero when it has none.
74    pub evm_contract: Address,
75    /// `szDecimals`: the decimals its sizes are counted in.
76    pub sz_decimals: u8,
77    /// `weiDecimals`: the decimals its balances are counted in on HyperCore.
78    pub wei_decimals: u8,
79    /// How many more decimals the ERC-20 counts in than HyperCore does.
80    pub evm_extra_wei_decimals: i8,
81}
82
83/// `0x80c`: everything about a token, its EVM contract included.
84#[must_use]
85pub fn token_info(token: u32) -> Read<TokenInfo> {
86    read(0x80c, &[Arg::Uint(u64::from(token))], |answer| {
87        let tokens = tokens(0x80c, answer)?;
88        let mut fields = Fields::of("tokenInfo", &tokens)?;
89        Ok(TokenInfo {
90            name: fields.string()?,
91            spots: fields.u64s()?,
92            deployer_trading_fee_share: fields.u64()?,
93            deployer: fields.address()?,
94            evm_contract: fields.address()?,
95            sz_decimals: fields.u8()?,
96            wei_decimals: fields.u8()?,
97            evm_extra_wei_decimals: fields.i8()?,
98        })
99    })
100}
101
102/// An account holding non-circulating supply.
103#[derive(Debug, Clone, Copy, PartialEq, Eq)]
104pub struct UserBalance {
105    /// The holding account.
106    pub user: Address,
107    /// What it holds, in the token's `weiDecimals`.
108    pub balance: u64,
109}
110
111/// A token's supply, as [`token_supply`] answers it, in the token's `weiDecimals`.
112#[derive(Debug, Clone, PartialEq, Eq)]
113pub struct TokenSupply {
114    /// `maxSupply`: the most there can ever be.
115    pub max_supply: u64,
116    /// `totalSupply`: what exists now.
117    pub total_supply: u64,
118    /// `circulatingSupply`: what exists now, less the non-circulating balances.
119    pub circulating_supply: u64,
120    /// `futureEmissions`: what is still to be emitted.
121    pub future_emissions: u64,
122    /// `nonCirculatingUserBalances`: the accounts whose balances do not count as circulating.
123    pub non_circulating_user_balances: Vec<UserBalance>,
124}
125
126/// `0x80d`: a token's supply, and who holds the part that does not circulate.
127#[must_use]
128pub fn token_supply(token: u32) -> Read<TokenSupply> {
129    read(0x80d, &[Arg::Uint(u64::from(token))], |answer| {
130        let tokens = tokens(0x80d, answer)?;
131        let mut fields = Fields::of("tokenSupply", &tokens)?;
132        let (max_supply, total_supply, circulating_supply, future_emissions) =
133            (fields.u64()?, fields.u64()?, fields.u64()?, fields.u64()?);
134        let holders = fields
135            .items()?
136            .iter()
137            .map(|item| {
138                let mut holder = Fields::of("tokenSupply", std::slice::from_ref(item))?;
139                Ok(UserBalance { user: holder.address()?, balance: holder.u64()? })
140            })
141            .collect::<Result<_, AbiError>>()?;
142        Ok(TokenSupply {
143            max_supply,
144            total_supply,
145            circulating_supply,
146            future_emissions,
147            non_circulating_user_balances: holders,
148        })
149    })
150}
151
152#[cfg(test)]
153#[path = "tests/assets.rs"]
154mod tests;