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