nautilus_model/defi/
token.rs1use 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#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
28pub struct Token {
29 pub chain: SharedChain,
31 pub address: Address,
33 pub name: String,
35 pub symbol: String,
37 pub decimals: u8,
39}
40
41pub type SharedToken = Arc<Token>;
43
44impl Token {
45 #[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 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}