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