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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use std::{str::FromStr, sync::OnceLock};
use solana_sdk::pubkey::Pubkey;
static DEVNET_TOKEN_MAP: OnceLock<TokenMap> = OnceLock::new();
static MAINNET_TOKEN_MAP: OnceLock<TokenMap> = OnceLock::new();
pub fn devnet_token_map() -> &'static TokenMap {
DEVNET_TOKEN_MAP.get_or_init(|| TokenMap::new(true))
}
pub fn mainnet_token_map() -> &'static TokenMap {
MAINNET_TOKEN_MAP.get_or_init(|| TokenMap::new(false))
}
pub fn token_map(devnet_tokens: bool) -> &'static TokenMap {
if devnet_tokens {
devnet_token_map()
} else {
mainnet_token_map()
}
}
pub type TokenType = &'static Token;
#[derive(Debug, Clone)]
pub struct Token {
pub mint: Pubkey,
pub symbol: &'static str,
pub decimals: u32,
}
impl Token {
/// Human-readable amount in f64
pub fn ui_amount(&self, amount: u64) -> f64 {
amount as f64 / 10f64.powf(self.decimals as f64)
}
}
#[derive(Debug, Clone)]
pub struct TokenMap {
/// "USDC" token, use a custom token on devnet
pub usdc: Token,
/// Custom "USDC" token used internally with 9 decimals
pub usdc_9: Token,
}
impl TokenMap {
pub fn new(devnet_tokens: bool) -> Self {
if devnet_tokens {
// Devnet token metadata
Self {
usdc: Token {
mint: solana_sdk::pubkey!("DGFhnr4oofkJ3K1vtNWhwfLGTDz6vJWoUcXkpwXe1X78"),
symbol: "USDC",
decimals: 6,
},
usdc_9: Token {
mint: solana_sdk::pubkey!("BR5T9y8RwtNjMSsz9SzzJsSveVc476ujSJzrzQfcWFz3"),
symbol: "USDC_9",
decimals: 9,
},
}
} else {
// Mainnet token metadata
Self {
usdc: Token {
mint: solana_sdk::pubkey!("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"),
symbol: "USDC",
decimals: 6,
},
usdc_9: Token {
mint: solana_sdk::pubkey!("BR5T9y8RwtNjMSsz9SzzJsSveVc476ujSJzrzQfcWFz3"),
symbol: "USDC_9",
decimals: 9,
},
}
}
}
/// Find a supported token either by name or by pubkey string.
///
/// Returns `None` if the token is not supported.
///
/// # Examples
/// ```
/// use aimo_core::token_map::token_map;
///
/// 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),
"USDC_9" => Some(&self.usdc_9),
// 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,
&self.usdc_9,
]
.iter()
.find(|t| t.mint == mint)
.map(|v| &**v)
})
}
}
}
}