Skip to main content

df_hyperliquid_core/client/evm/abi/
token.rs

1//! A [`Token`], a value of some [`Kind`](crate::client::evm::abi::Kind): built
2//! from Rust values and read back into them, signed integers in two's complement.
3
4use crate::client::evm::u256::U256;
5use crate::utils::Address;
6
7/// A value of some [`Kind`](crate::client::evm::abi::Kind).
8#[derive(Debug, Clone, PartialEq, Eq)]
9#[non_exhaustive]
10pub enum Token {
11    /// A value of a `uintN`.
12    Uint(U256),
13    /// Two's complement in 256 bits: -1 is all ones, whatever the width.
14    Int(U256),
15    /// An `address`.
16    Address(Address),
17    /// A `bool`.
18    Bool(bool),
19    /// A `string`.
20    String(String),
21    /// The bytes of a `bytes`.
22    Bytes(Vec<u8>),
23    /// The `n` bytes of a `bytesN`.
24    Fixed(Vec<u8>),
25    /// `T[]` and `T[n]`.
26    Array(Vec<Token>),
27    /// The fields of a tuple, in order.
28    Tuple(Vec<Token>),
29}
30
31impl Token {
32    /// A `uintN` value from anything that converts into a [`U256`].
33    #[must_use]
34    pub fn uint(value: impl Into<U256>) -> Token {
35        Token::Uint(value.into())
36    }
37
38    /// An `intN` value, stored in two's complement.
39    #[must_use]
40    pub fn int(value: i128) -> Token {
41        let magnitude = U256::from_u128(value.unsigned_abs());
42        Token::Int(match value < 0 {
43            true => negated(magnitude),
44            false => magnitude,
45        })
46    }
47
48    /// An `address` value.
49    #[must_use]
50    pub fn address(address: Address) -> Token {
51        Token::Address(address)
52    }
53
54    /// The value of a `Uint`, or `None` for any other token.
55    #[must_use]
56    pub fn as_u256(&self) -> Option<U256> {
57        match self {
58            Token::Uint(value) => Some(*value),
59            _ => None,
60        }
61    }
62
63    /// The value of a `Uint`, while it fits a `u128`.
64    #[must_use]
65    pub fn as_u128(&self) -> Option<u128> {
66        self.as_u256().and_then(U256::to_u128)
67    }
68
69    /// The value of a `Uint`, while it fits a `u64`.
70    #[must_use]
71    pub fn as_u64(&self) -> Option<u64> {
72        self.as_u256().and_then(U256::to_u64)
73    }
74
75    /// The value of a `Uint`, while it fits a `u32`.
76    #[must_use]
77    pub fn as_u32(&self) -> Option<u32> {
78        self.as_u64().and_then(|value| u32::try_from(value).ok())
79    }
80
81    /// The value of a `Uint`, while it fits a `u16`.
82    #[must_use]
83    pub fn as_u16(&self) -> Option<u16> {
84        self.as_u64().and_then(|value| u16::try_from(value).ok())
85    }
86
87    /// The value of a `Uint`, while it fits a `u8`.
88    #[must_use]
89    pub fn as_u8(&self) -> Option<u8> {
90        self.as_u64().and_then(|value| u8::try_from(value).ok())
91    }
92
93    /// A signed integer, while it fits an `i128`.
94    #[must_use]
95    pub fn as_i128(&self) -> Option<i128> {
96        let Token::Int(value) = self else { return None };
97        match value.bits() == 256 {
98            false => value.to_u128().and_then(|value| i128::try_from(value).ok()),
99            true => {
100                let magnitude = negated(*value).to_u128()?;
101                match magnitude == i128::MIN.unsigned_abs() {
102                    true => Some(i128::MIN),
103                    false => i128::try_from(magnitude).ok().map(|magnitude| -magnitude),
104                }
105            }
106        }
107    }
108
109    /// The value of an `Int`, while it fits an `i64`.
110    #[must_use]
111    pub fn as_i64(&self) -> Option<i64> {
112        self.as_i128().and_then(|value| i64::try_from(value).ok())
113    }
114
115    /// The value of an `Int`, while it fits an `i8`.
116    #[must_use]
117    pub fn as_i8(&self) -> Option<i8> {
118        self.as_i128().and_then(|value| i8::try_from(value).ok())
119    }
120
121    /// The value of an `Address`, or `None` for any other token.
122    #[must_use]
123    pub fn as_address(&self) -> Option<Address> {
124        match self {
125            Token::Address(address) => Some(*address),
126            _ => None,
127        }
128    }
129
130    /// The value of a `Bool`, or `None` for any other token.
131    #[must_use]
132    pub fn as_bool(&self) -> Option<bool> {
133        match self {
134            Token::Bool(value) => Some(*value),
135            _ => None,
136        }
137    }
138
139    /// The text of a `String`, or `None` for any other token.
140    #[must_use]
141    pub fn as_str(&self) -> Option<&str> {
142        match self {
143            Token::String(text) => Some(text),
144            _ => None,
145        }
146    }
147
148    /// The bytes of a `bytes` or a `bytesN`.
149    #[must_use]
150    pub fn as_bytes(&self) -> Option<&[u8]> {
151        match self {
152            Token::Bytes(bytes) | Token::Fixed(bytes) => Some(bytes),
153            _ => None,
154        }
155    }
156
157    /// The items of an array or the fields of a tuple.
158    #[must_use]
159    pub fn as_items(&self) -> Option<&[Token]> {
160        match self {
161            Token::Array(items) | Token::Tuple(items) => Some(items),
162            _ => None,
163        }
164    }
165}
166
167/// `-value` in two's complement: `2^256 - value`, and zero for zero.
168pub(super) fn negated(value: U256) -> U256 {
169    match value.checked_sub(U256::ONE) {
170        Some(less) => U256::MAX.checked_sub(less).unwrap_or(U256::ZERO),
171        None => U256::ZERO,
172    }
173}