Skip to main content

tycho_common/models/
token.rs

1use std::{
2    collections::HashMap,
3    hash::{Hash, Hasher},
4    sync::Arc,
5};
6
7use deepsize::DeepSizeOf;
8use num_bigint::BigUint;
9use serde::{Deserialize, Serialize};
10
11use super::{Address, Balance};
12use crate::{dto::ResponseToken, models::Chain, traits::TokenOwnerFinding, Bytes};
13
14/// Cost related to a token transfer, for example amount of gas in evm chains.
15pub type TransferCost = u64;
16
17/// Tax related to a token transfer. Should be given in Basis Points (1/100th of a percent)
18pub type TransferTax = u64;
19
20#[derive(Debug, Clone, Deserialize, Serialize, Eq, DeepSizeOf)]
21pub struct Token {
22    pub address: Bytes,
23    pub symbol: String,
24    pub decimals: u32,
25    pub tax: TransferTax,
26    pub gas: Vec<Option<TransferCost>>,
27    pub chain: Chain,
28    /// Quality is between 0-100, where:
29    ///  - 100: Normal token
30    ///  - 75: Rebase token
31    ///  - 50: Fee token
32    ///  - 10: Token analysis failed at creation
33    ///  - 9-5: Token analysis failed on cronjob (after creation).
34    ///  - 0: Failed to extract decimals onchain
35    pub quality: u32,
36}
37
38impl Token {
39    pub fn new(
40        address: &Bytes,
41        symbol: &str,
42        decimals: u32,
43        tax: u64,
44        gas: &[Option<u64>],
45        chain: Chain,
46        quality: u32,
47    ) -> Self {
48        Self {
49            address: address.clone(),
50            symbol: symbol.to_string(),
51            decimals,
52            tax,
53            gas: gas.to_owned(),
54            chain,
55            quality,
56        }
57    }
58
59    /// One
60    /// Get one token in BigUint format
61    ///
62    /// ## Return
63    /// Returns one token as BigUint
64    pub fn one(&self) -> BigUint {
65        BigUint::from((1.0 * 10f64.powi(self.decimals as i32)) as u128)
66    }
67
68    pub fn gas_usage(&self) -> BigUint {
69        BigUint::from(
70            self.gas
71                .clone()
72                .into_iter()
73                .flatten()
74                .collect::<Vec<u64>>()
75                .iter()
76                .min()
77                .copied()
78                .unwrap_or(0u64),
79        )
80    }
81}
82
83impl PartialOrd for Token {
84    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
85        self.address.partial_cmp(&other.address)
86    }
87}
88
89impl PartialEq for Token {
90    fn eq(&self, other: &Self) -> bool {
91        self.address == other.address
92    }
93}
94
95impl Hash for Token {
96    fn hash<H: Hasher>(&self, state: &mut H) {
97        self.address.hash(state);
98    }
99}
100
101impl From<Arc<Token>> for Address {
102    fn from(val: Arc<Token>) -> Self {
103        val.address.clone()
104    }
105}
106
107impl From<ResponseToken> for Token {
108    fn from(value: ResponseToken) -> Self {
109        Self {
110            chain: value.chain.into(),
111            address: value.address,
112            symbol: value.symbol,
113            decimals: value.decimals,
114            tax: value.tax,
115            gas: value.gas,
116            quality: value.quality,
117        }
118    }
119}
120
121/// Represents the quality of a token.
122///
123/// * `Good`: Indicates that the token has successfully passed the analysis process.
124/// * `Bad`: Indicates that the token has failed the analysis process. In this case, a detailed
125///   reason for the failure is provided.
126///
127/// Note: Transfer taxes do not impact the token's quality.
128/// Even if a token has transfer taxes, as long as it successfully passes the analysis,
129/// it will still be marked as `Good`.
130#[derive(Debug, Clone, Eq, PartialEq)]
131pub enum TokenQuality {
132    Good,
133    Bad { reason: String },
134}
135
136impl TokenQuality {
137    pub fn is_good(&self) -> bool {
138        matches!(self, Self::Good { .. })
139    }
140
141    pub fn bad(reason: impl ToString) -> Self {
142        Self::Bad { reason: reason.to_string() }
143    }
144}
145
146/// A store for tracking token owners and their balances.
147///
148/// The `TokenOwnerStore` maintains a mapping between token addresses and their respective
149/// owner's address and balance. It can be used to quickly retrieve token owner information
150/// without needing to query external sources.
151#[derive(Debug)]
152pub struct TokenOwnerStore {
153    /// A `HashMap` where the key is the token address and the value is a tuple containing
154    /// the owner address and the balance of the owner for the token.
155    values: HashMap<Address, (Address, Balance)>,
156}
157
158impl TokenOwnerStore {
159    pub fn new(values: HashMap<Address, (Address, Balance)>) -> Self {
160        TokenOwnerStore { values }
161    }
162}
163
164#[async_trait::async_trait]
165impl TokenOwnerFinding for TokenOwnerStore {
166    async fn find_owner(
167        &self,
168        token: Address,
169        _min_balance: Balance,
170    ) -> Result<Option<(Address, Balance)>, String> {
171        Ok(self.values.get(&token).cloned())
172    }
173}
174
175#[cfg(test)]
176mod tests {
177    use std::str::FromStr;
178
179    use super::*;
180
181    #[test]
182    fn test_constructor() {
183        let token = Token::new(
184            &Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap(),
185            "USDC",
186            6,
187            1000,
188            &[Some(1000u64)],
189            Chain::Ethereum,
190            100,
191        );
192
193        assert_eq!(token.symbol, "USDC");
194        assert_eq!(token.decimals, 6);
195        assert_eq!(
196            format!("{token_address:#x}", token_address = token.address),
197            "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"
198        );
199    }
200
201    #[test]
202    fn test_cmp() {
203        let usdc = Token::new(
204            &Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap(),
205            "USDC",
206            6,
207            1000,
208            &[Some(1000u64)],
209            Chain::Ethereum,
210            100,
211        );
212        let usdc2 = Token::new(
213            &Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap(),
214            "USDC2",
215            6,
216            1000,
217            &[Some(1000u64)],
218            Chain::Ethereum,
219            100,
220        );
221        let weth = Token::new(
222            &Bytes::from_str("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2").unwrap(),
223            "WETH",
224            18,
225            1000,
226            &[Some(1000u64)],
227            Chain::Ethereum,
228            100,
229        );
230
231        assert!(usdc < weth);
232        assert_eq!(usdc, usdc2);
233    }
234
235    #[test]
236    fn test_one() {
237        let usdc = Token::new(
238            &Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap(),
239            "USDC",
240            6,
241            1000,
242            &[Some(1000u64)],
243            Chain::Ethereum,
244            100,
245        );
246
247        assert_eq!(usdc.one(), BigUint::from(1000000u64));
248    }
249}