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#[cfg_attr(
28 feature = "python",
29 pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model")
30)]
31#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
32pub struct Token {
33 pub chain: SharedChain,
35 pub address: Address,
37 pub name: String,
39 pub symbol: String,
41 pub decimals: u8,
43}
44
45pub type SharedToken = Arc<Token>;
47
48impl Token {
49 #[must_use]
51 pub fn new(
52 chain: SharedChain,
53 address: Address,
54 name: String,
55 symbol: String,
56 decimals: u8,
57 ) -> Self {
58 Self {
59 chain,
60 address,
61 name,
62 symbol,
63 decimals,
64 }
65 }
66}
67
68impl Display for Token {
69 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
70 write!(f, "Token(symbol={}, name={})", self.symbol, self.name)
71 }
72}
73
74#[cfg(test)]
75mod tests {
76 use std::sync::Arc;
77
78 use rstest::rstest;
79
80 use super::*;
81 use crate::defi::chain::chains;
82
83 #[rstest]
84 fn test_token_constructor() {
85 let chain = Arc::new(chains::ETHEREUM.clone());
86 let address = "0xA0b86a33E6441b936662bb6B5d1F8Fb0E2b57A5D"
87 .parse()
88 .unwrap();
89
90 let token = Token::new(
91 chain.clone(),
92 address,
93 "Wrapped Ether".to_string(),
94 "WETH".to_string(),
95 18,
96 );
97
98 assert_eq!(token.chain.chain_id, chain.chain_id);
99 assert_eq!(token.address, address);
100 assert_eq!(token.name, "Wrapped Ether");
101 assert_eq!(token.symbol, "WETH");
102 assert_eq!(token.decimals, 18);
103 }
104
105 #[rstest]
106 fn test_token_display_with_special_characters() {
107 let chain = Arc::new(chains::ETHEREUM.clone());
109 let token = Token::new(
110 chain,
111 "0xA0b86a33E6441b936662bb6B5d1F8Fb0E2b57A5D"
112 .parse()
113 .unwrap(),
114 "Test Token (with parentheses)".to_string(),
115 "TEST-1".to_string(),
116 18,
117 );
118
119 let display = token.to_string();
120 assert_eq!(
121 display,
122 "Token(symbol=TEST-1, name=Test Token (with parentheses))"
123 );
124 }
125}