aimo_core/
token_map.rs

1use std::{str::FromStr, sync::OnceLock};
2
3use solana_sdk::pubkey::Pubkey;
4
5static TOKEN_MAP: OnceLock<TokenMap> = OnceLock::new();
6
7pub fn token_map(devnet_tokens: bool) -> &'static TokenMap {
8    TOKEN_MAP.get_or_init(|| TokenMap::new(devnet_tokens))
9}
10
11pub type TokenType = &'static Token;
12
13#[derive(Debug, Clone)]
14pub struct Token {
15    pub mint: Pubkey,
16    pub symbol: &'static str,
17    pub decimals: u32,
18}
19
20#[derive(Debug, Clone)]
21pub struct TokenMap {
22    pub usdc: Token,
23    pub credits: Token,
24}
25
26impl TokenMap {
27    pub fn new(devnet_tokens: bool) -> Self {
28        if devnet_tokens {
29            // Devnet token metadata
30            Self {
31                usdc: Token {
32                    mint: Pubkey::from_str_const("4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU"),
33                    symbol: "USDC",
34                    decimals: 6,
35                },
36                credits: Token {
37                    mint: Pubkey::from_str_const("765u8dLS3iMNYGydfX2YWu2HrtWhHBUvwXcvQEYopecd"),
38                    symbol: "CREDITS",
39                    decimals: 6,
40                },
41            }
42        } else {
43            // Mainnet token metadata
44            Self {
45                usdc: Token {
46                    mint: Pubkey::from_str_const("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"),
47                    symbol: "USDC",
48                    decimals: 6,
49                },
50                credits: Token {
51                    mint: Pubkey::from_str_const("765u8dLS3iMNYGydfX2YWu2HrtWhHBUvwXcvQEYopecd"),
52                    symbol: "CREDITS",
53                    decimals: 6,
54                },
55            }
56        }
57    }
58
59    /// Find a supported token either by name or by pubkey string.
60    ///
61    /// Returns `None` if the token is not supported.
62    ///
63    /// # Examples
64    /// ```
65    /// use aimo_core::token_map::token_map;
66    ///
67    /// let token_map = token_map(false);
68    ///
69    /// // Find by symbol
70    /// let usdc_by_symbol = token_map.find("USDC");
71    ///
72    /// // Find by mint address  
73    /// let usdc_by_mint = token_map.find("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v");
74    /// ```
75    pub fn find(&'static self, token: &str) -> Option<TokenType> {
76        match token.to_ascii_uppercase().as_str() {
77            "USDC" => Some(&self.usdc),
78
79            // match other token names here:
80            // "USDT": => xxxx
81
82            // Fallback to token mint pubkey
83            _ => {
84                Pubkey::from_str(token).ok().and_then(|mint| {
85                    [
86                        // List other tokens here
87                        &self.usdc,
88                    ]
89                    .iter()
90                    .find(|t| t.mint == mint)
91                    .map(|v| &**v)
92                })
93            }
94        }
95    }
96}