pragma_common/web3/token/
mod.rs

1pub mod constants;
2
3pub use constants::*;
4
5use std::collections::BTreeMap;
6
7use super::Chain;
8
9#[derive(Debug, PartialEq, Eq, Hash, Clone)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize,))]
11#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
12pub struct Token {
13    pub name: String,
14    pub ticker: String,
15    pub decimals: u32,
16    pub addresses: Option<BTreeMap<Chain, String>>,
17}
18
19impl Token {
20    pub fn new(
21        name: &str,
22        ticker: &str,
23        decimals: u32,
24        addresses: Option<BTreeMap<Chain, String>>,
25    ) -> Self {
26        Self {
27            name: name.to_string(),
28            ticker: ticker.to_string(),
29            decimals,
30            addresses,
31        }
32    }
33
34    pub fn new_without_addresses(name: &str, ticker: &str, decimals: u32) -> Self {
35        Self {
36            name: name.to_string(),
37            ticker: ticker.to_string(),
38            decimals,
39            addresses: None,
40        }
41    }
42
43    #[must_use]
44    pub fn with_addresses(mut self, addresses: BTreeMap<Chain, String>) -> Self {
45        self.addresses = Some(addresses);
46        self
47    }
48
49    /// Returns the address of the token for the provided `Chain`
50    pub fn address(&self, chain: Chain) -> Option<String> {
51        self.addresses.as_ref().and_then(|e| e.get(&chain).cloned())
52    }
53}