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