nautilus_model/defi/
token.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Posei Systems Pty Ltd. All rights reserved.
3//  https://poseitrader.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use std::{
17    fmt::{Display, Formatter},
18    sync::Arc,
19};
20
21use alloy_primitives::Address;
22use serde::{Deserialize, Serialize};
23
24use crate::defi::chain::SharedChain;
25
26/// Represents a cryptocurrency token on a blockchain network.
27#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
28pub struct Token {
29    /// The blockchain network where this token exists.
30    pub chain: SharedChain,
31    /// The blockchain address of the token contract.
32    pub address: Address,
33    /// The full name of the token.
34    pub name: String,
35    /// The token's ticker symbol.
36    pub symbol: String,
37    /// The number of decimal places used to represent fractional token amounts.
38    pub decimals: u8,
39}
40
41/// A thread-safe shared pointer to a `Token`, enabling efficient reuse across multiple components.
42pub type SharedToken = Arc<Token>;
43
44impl Token {
45    /// Creates a new [`Token`] instance with the specified properties.
46    #[must_use]
47    pub fn new(
48        chain: SharedChain,
49        address: Address,
50        name: String,
51        symbol: String,
52        decimals: u8,
53    ) -> Self {
54        Self {
55            chain,
56            address,
57            name,
58            symbol,
59            decimals,
60        }
61    }
62}
63
64impl Display for Token {
65    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
66        write!(f, "Token(symbol={}, name={})", self.symbol, self.name)
67    }
68}
69
70#[cfg(test)]
71mod tests {
72    use std::sync::Arc;
73
74    use rstest::rstest;
75
76    use super::*;
77    use crate::defi::chain::chains;
78
79    #[rstest]
80    fn test_token_constructor() {
81        let chain = Arc::new(chains::ETHEREUM.clone());
82        let address = "0xA0b86a33E6441b936662bb6B5d1F8Fb0E2b57A5D"
83            .parse()
84            .unwrap();
85
86        let token = Token::new(
87            chain.clone(),
88            address,
89            "Wrapped Ether".to_string(),
90            "WETH".to_string(),
91            18,
92        );
93
94        assert_eq!(token.chain.chain_id, chain.chain_id);
95        assert_eq!(token.address, address);
96        assert_eq!(token.name, "Wrapped Ether");
97        assert_eq!(token.symbol, "WETH");
98        assert_eq!(token.decimals, 18);
99    }
100
101    #[rstest]
102    fn test_token_display_with_special_characters() {
103        // Test edge case where token names/symbols contain formatting characters
104        let chain = Arc::new(chains::ETHEREUM.clone());
105        let token = Token::new(
106            chain,
107            "0xA0b86a33E6441b936662bb6B5d1F8Fb0E2b57A5D"
108                .parse()
109                .unwrap(),
110            "Test Token (with parentheses)".to_string(),
111            "TEST-1".to_string(),
112            18,
113        );
114
115        let display = token.to_string();
116        assert_eq!(
117            display,
118            "Token(symbol=TEST-1, name=Test Token (with parentheses))"
119        );
120    }
121}