Skip to main content

df_hyperliquid_core/transfer/send/
token.rs

1//! A spot token as a transfer names it, read from the caller's `spotMeta`: its `NAME:0x..` wire
2//! form, whether it is the network's HYPE (whose index and id differ between the networks), and
3//! where and at how many places it crosses to HyperEVM.
4
5use crate::client::{Network, Token};
6use crate::transfer::evm::{HYPE_SYSTEM, system_address};
7use crate::utils::Address;
8use serde::Deserialize;
9
10/// HYPE's decimals on HyperEVM, where it is the chain's own coin.
11pub const HYPE_EVM_DECIMALS: u32 = 18;
12
13/// One token of `spotMeta`'s `tokens`, the fields a transfer reads.
14#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
15#[serde(rename_all = "camelCase")]
16#[non_exhaustive]
17pub struct SpotToken {
18    /// The token's name, `HYPE` or `PURR`.
19    pub name: String,
20    /// What a balance's `token` and `borrowLend` name it by.
21    pub index: u32,
22    /// `0x` and 32 hex digits.
23    pub token_id: String,
24    /// The decimals Core counts it in.
25    pub wei_decimals: u32,
26    /// Its ERC-20 on HyperEVM; `null` for a token with none, and for HYPE on
27    /// mainnet, which is HyperEVM's own coin.
28    #[serde(default)]
29    pub evm_contract: Option<EvmContract>,
30}
31
32/// A token's ERC-20 on HyperEVM. The venue spells this key in snake case.
33#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
34#[non_exhaustive]
35pub struct EvmContract {
36    /// The ERC-20's address on HyperEVM.
37    pub address: Address,
38    /// Its decimals on HyperEVM less `weiDecimals`.
39    #[serde(rename = "evm_extra_wei_decimals")]
40    pub extra_wei_decimals: i32,
41}
42
43impl SpotToken {
44    /// `NAME:0x<tokenId>`, the one way a transfer names a token.
45    #[must_use]
46    pub fn wire(&self) -> String {
47        format!("{}:{}", self.name, self.token_id)
48    }
49
50    /// Whether this is the network's HYPE, by its index there.
51    #[must_use]
52    pub fn is_hype(&self, network: Network) -> bool {
53        self.index == network.hype().index
54    }
55
56    /// Where it crosses from Core to HyperEVM: `0x2222...2222` for HYPE, `0x20` and the index for
57    /// any other token.
58    #[must_use]
59    pub fn system_address(&self, network: Network) -> Address {
60        match self.is_hype(network) {
61            true => HYPE_SYSTEM,
62            false => system_address(u64::from(self.index)),
63        }
64    }
65
66    /// Its decimals on HyperEVM: eighteen for HYPE, `weiDecimals` plus the
67    /// ERC-20's extra for a linked token, `None` for a token with no ERC-20.
68    #[must_use]
69    pub fn evm_decimals(&self, network: Network) -> Option<u32> {
70        if self.is_hype(network) {
71            return Some(HYPE_EVM_DECIMALS);
72        }
73        let link = self.evm_contract?;
74        u32::try_from(i64::from(self.wei_decimals) + i64::from(link.extra_wei_decimals)).ok()
75    }
76
77    /// The most places an amount crossing to HyperEVM may have: the fewer of its Core and HyperEVM
78    /// decimals. `None` when it cannot cross.
79    #[must_use]
80    pub fn crossing_places(&self, network: Network) -> Option<u32> {
81        self.evm_decimals(network).map(|evm| evm.min(self.wei_decimals))
82    }
83}
84
85/// USDC or HYPE from the network itself, with no link to HyperEVM read: HYPE
86/// needs none, and USDC does not cross here.
87impl From<Token> for SpotToken {
88    fn from(token: Token) -> SpotToken {
89        SpotToken {
90            name: token.name.to_owned(),
91            index: token.index,
92            token_id: token.token_id.to_owned(),
93            wei_decimals: token.wei_decimals,
94            evm_contract: None,
95        }
96    }
97}
98
99#[cfg(test)]
100#[path = "tests/token.rs"]
101mod tests;