aimo-core 0.1.1

AiMo Network core protocol Rust specs
Documentation
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)
                })
            }
        }
    }
}