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}
24
25impl TokenMap {
26 pub fn new(devnet_tokens: bool) -> Self {
27 if devnet_tokens {
28 // Devnet token metadata
29 Self {
30 usdc: Token {
31 mint: Pubkey::from_str_const("4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU"),
32 symbol: "USDC",
33 decimals: 6,
34 },
35 }
36 } else {
37 // Mainnet token metadata
38 Self {
39 usdc: Token {
40 mint: Pubkey::from_str_const("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"),
41 symbol: "USDC",
42 decimals: 6,
43 },
44 }
45 }
46 }
47
48 /// Find a supported token either by name or by pubkey string.
49 ///
50 /// Returns `None` if the token is not supported.
51 ///
52 /// # Examples
53 /// ```
54 /// use aimo_core::token_map::token_map;
55 ///
56 /// let token_map = token_map(false);
57 ///
58 /// // Find by symbol
59 /// let usdc_by_symbol = token_map.find("USDC");
60 ///
61 /// // Find by mint address
62 /// let usdc_by_mint = token_map.find("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v");
63 /// ```
64 pub fn find(&'static self, token: &str) -> Option<TokenType> {
65 match token.to_ascii_uppercase().as_str() {
66 "USDC" => Some(&self.usdc),
67
68 // match other token names here:
69 // "USDT": => xxxx
70
71 // Fallback to token mint pubkey
72 _ => {
73 Pubkey::from_str(token).ok().and_then(|mint| {
74 [
75 // List other tokens here
76 &self.usdc,
77 ]
78 .iter()
79 .find(|t| t.mint == mint)
80 .map(|v| &**v)
81 })
82 }
83 }
84 }
85}