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(
12    feature = "borsh",
13    derive(borsh::BorshSerialize, borsh::BorshDeserialize)
14)]
15#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
16pub struct Token {
17    pub name: String,
18    pub ticker: String,
19    pub decimals: u32,
20    pub addresses: Option<BTreeMap<Chain, String>>,
21}
22
23impl Token {
24    pub fn new(
25        name: &str,
26        ticker: &str,
27        decimals: u32,
28        addresses: Option<BTreeMap<Chain, String>>,
29    ) -> Self {
30        Self {
31            name: name.to_string(),
32            ticker: ticker.to_string(),
33            decimals,
34            addresses,
35        }
36    }
37
38    pub fn new_without_addresses(name: &str, ticker: &str, decimals: u32) -> Self {
39        Self {
40            name: name.to_string(),
41            ticker: ticker.to_string(),
42            decimals,
43            addresses: None,
44        }
45    }
46
47    #[must_use]
48    pub fn with_addresses(mut self, addresses: BTreeMap<Chain, String>) -> Self {
49        self.addresses = Some(addresses);
50        self
51    }
52
53    /// Returns the address of the token for the provided `Chain`
54    pub fn address(&self, chain: Chain) -> Option<String> {
55        self.addresses.as_ref().and_then(|e| e.get(&chain).cloned())
56    }
57}