Skip to main content

simulator_api/
mints.rs

1//! The major-mint universe shared by the reroute reports: tables of stables and priced majors,
2//! plus the helpers that classify, price, and label mints against them.
3
4use solana_address::{Address, address};
5use spl_token_interface::native_mint;
6
7/// A mint: its address, display symbol, and token decimals.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub struct Mint {
10    #[allow(missing_docs)]
11    pub address: Address,
12    /// Display symbol, e.g. `USDC`.
13    pub symbol: &'static str,
14    /// Decimal places: one whole token is `10^decimals` raw base units.
15    pub decimals: u32,
16}
17
18const fn mint(address: Address, symbol: &'static str, decimals: u32) -> Mint {
19    Mint {
20        address,
21        symbol,
22        decimals,
23    }
24}
25
26/// USD stablecoins, priced at $1 per whole token. USDC is first and USDT second.
27pub const STABLES: [Mint; 8] = [
28    mint(
29        address!("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"),
30        "USDC",
31        6,
32    ),
33    mint(
34        address!("Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB"),
35        "USDT",
36        6,
37    ),
38    mint(
39        address!("2b1kV6DkPAnxd5ixfnxCpjxmKwqjjaYmCZfHsFu24GXo"),
40        "PYUSD",
41        6,
42    ),
43    mint(
44        address!("USDSwr9ApdHk5bvJKMjzff41FfuX8bSxdKcR81vTwcA"),
45        "USDS",
46        6,
47    ),
48    mint(
49        address!("DEkqHyPN7GMRJ5cArtQFAWefqbZb33Hyf6s5iCwjEonT"),
50        "USDe",
51        9,
52    ),
53    mint(
54        address!("9zNQRsGLjNKwCUU5Gq5LR8beUCPzQMVMqKAi3SSZh54u"),
55        "FDUSD",
56        6,
57    ),
58    mint(
59        address!("A1KLoBrKBde8Ty9qtNQUtq3C2ortoC3u7twggz7sEto6"),
60        "USDY",
61        6,
62    ),
63    mint(
64        address!("USD1ttGY1N17NEEHLmELoaybftRBUSErhqYiQzvEmuB"),
65        "USD1",
66        6,
67    ),
68];
69
70/// Non-stable majors priced from in-range quote medians (vs USDC/USDT/SOL).
71pub const PRICED_MAJORS: [Mint; 10] = [
72    mint(
73        address!("HzwqbKZw8HxMN6bF2yFZNrht3c2iXXzpKcFu7uBEDKtr"),
74        "EURC",
75        6,
76    ),
77    mint(
78        address!("cbbtcf3aa214zXHbiAZQwf4122FBYbraNdFqgw4iMij"),
79        "cbBTC",
80        8,
81    ),
82    mint(
83        address!("3NZ9JMVBmGAqocybic2c7LQCJScmgsAZ6vQqTDzcqmJh"),
84        "WBTC",
85        8,
86    ),
87    mint(
88        address!("7vfCXTUXx5WJV5JADk17DUJ4ksgau7utNKj4b963voxs"),
89        "WETH",
90        8,
91    ),
92    mint(
93        address!("J1toso1uCk3RLmjorhTtrVwY9HJ7X8V9yYac6Y7kGCPn"),
94        "jitoSOL",
95        9,
96    ),
97    mint(
98        address!("mSoLzYCxHdYgdzU16g5QSh3i5K3z3KZK7ytfqcJm7So"),
99        "mSOL",
100        9,
101    ),
102    mint(
103        address!("bSo13r4TkiE4KumL71LsHTPpL2euBYLFx6h9HP3piy1"),
104        "bSOL",
105        9,
106    ),
107    mint(
108        address!("jupSoLaHXQiZZTSfEWMTRRgpnyFm8f6sZdosWBjx93v"),
109        "jupSOL",
110        9,
111    ),
112    mint(
113        address!("5oVNBeEEQvYi1cX3ir8Dx5n1P7pdxydbGF2X4TxVusJm"),
114        "INF",
115        9,
116    ),
117    mint(
118        address!("BNso1VUJnh4zcfpZa6986Ea66P6TCp59hvtNJ8b1X85"),
119        "bnSOL",
120        9,
121    ),
122];
123
124/// Wrapped SOL, the mint every native-SOL leg trades as.
125pub const SOL: Address = native_mint::id();
126#[allow(missing_docs)]
127pub const USDC: Address = STABLES[0].address;
128#[allow(missing_docs)]
129pub const USDT: Address = STABLES[1].address;
130
131fn find_in(table: &'static [Mint], mint: &Address) -> Option<&'static Mint> {
132    table.iter().find(|entry| entry.address == *mint)
133}
134
135/// Decimals of a mint in [`STABLES`]; `None` for any other mint.
136pub fn stable_decimals(mint: &Address) -> Option<u32> {
137    find_in(&STABLES, mint).map(|entry| entry.decimals)
138}
139
140/// Decimals of a mint in [`PRICED_MAJORS`]; `None` for any other mint.
141pub fn priced_decimals(mint: &Address) -> Option<u32> {
142    find_in(&PRICED_MAJORS, mint).map(|entry| entry.decimals)
143}
144
145/// Whether the mint is one the tables name, and so can anchor another mint's price.
146pub fn is_major(mint: &Address) -> bool {
147    *mint == SOL || stable_decimals(mint).is_some() || priced_decimals(mint).is_some()
148}
149
150/// One major against a longtail mint is not a majors pair: the longtail side is what
151/// the router misprices.
152pub fn trades_only_majors(input_mint: &Address, output_mint: &Address) -> bool {
153    is_major(input_mint) && is_major(output_mint)
154}
155
156/// `None` outside the tables, which displays as a shortened address rather than a guess.
157pub fn symbol(mint: &Address) -> Option<&'static str> {
158    if *mint == SOL {
159        return Some("SOL");
160    }
161    find_in(&STABLES, mint)
162        .or_else(|| find_in(&PRICED_MAJORS, mint))
163        .map(|entry| entry.symbol)
164}
165
166/// Leg mints are resolved before a swap ever becomes reroutable (`resolve_swap` only emits a
167/// `Reroutable` swap once both mints parse), so a leg mint that still fails to parse here is
168/// corrupt wire data or an upstream bug, not a legitimate mint-less state. Panicking loudly beats
169/// defaulting: a silent default would bucket that bug's output as an ordinary exotic leg instead
170/// of surfacing it.
171///
172/// # Panics
173///
174/// When `raw` is not a base58 address.
175pub fn parse_mint(raw: &str) -> Address {
176    raw.parse()
177        .expect("leg mints are resolved before a swap becomes reroutable")
178}
179
180/// The token's symbol, or a shortened address
181pub fn short_mint(mint: &str) -> String {
182    if let Some(symbol) = mint.parse().ok().as_ref().and_then(symbol) {
183        return symbol.to_string();
184    }
185    match mint.len() > 12 {
186        true => format!("{}..{}", &mint[..6], &mint[mint.len() - 4..]),
187        false => mint.to_string(),
188    }
189}
190
191#[cfg(test)]
192mod tests {
193    use super::*;
194
195    /// A duplicate reads as a missing mint, quietly widening what counts as exotic.
196    #[test]
197    fn the_major_universe_is_nineteen_distinct_mints() {
198        let mut all: Vec<Address> = STABLES
199            .iter()
200            .chain(PRICED_MAJORS.iter())
201            .map(|entry| entry.address)
202            .chain([SOL])
203            .collect();
204        assert_eq!(all.len(), 19);
205        all.sort();
206        all.dedup();
207        assert_eq!(all.len(), 19, "a mint is listed twice");
208        assert!(all.iter().all(is_major));
209        // Every major displays as a symbol, so an address in the output means an uncommon token.
210        assert!(all.iter().all(|mint| symbol(mint).is_some()));
211
212        let mut symbols: Vec<&str> = all.iter().filter_map(symbol).collect();
213        symbols.sort_unstable();
214        symbols.dedup();
215        assert_eq!(symbols.len(), 19, "two mints share a symbol");
216    }
217
218    #[test]
219    fn short_mint_prefers_the_symbol_and_shortens_the_rest() {
220        assert_eq!(
221            short_mint("So11111111111111111111111111111111111111112"),
222            "SOL"
223        );
224        assert_eq!(
225            short_mint("DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263"),
226            "DezXAZ..B263"
227        );
228        assert_eq!(short_mint("BONK"), "BONK");
229    }
230}