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