spl_token_2022_interface/
lib.rs

1#![allow(clippy::arithmetic_side_effects)]
2#![deny(missing_docs)]
3#![cfg_attr(not(test), warn(unsafe_code))]
4
5//! An ERC20-like Token program for the Solana blockchain
6
7pub mod error;
8pub mod extension;
9pub mod generic_token_account;
10pub mod instruction;
11pub mod native_mint;
12pub mod pod;
13#[cfg(feature = "serde")]
14pub mod serialization;
15pub mod state;
16
17// Export current sdk types for downstream users building with a different sdk
18// version
19pub use solana_zk_sdk;
20use {
21    solana_program_error::{ProgramError, ProgramResult},
22    solana_pubkey::Pubkey,
23};
24
25solana_pubkey::declare_id!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb");
26
27/// Checks that the supplied program ID is correct for spl-token-2022
28pub fn check_program_account(spl_token_program_id: &Pubkey) -> ProgramResult {
29    if spl_token_program_id != &id() {
30        return Err(ProgramError::IncorrectProgramId);
31    }
32    Ok(())
33}
34
35/// In-lined spl token program id to avoid a dependency
36pub mod inline_spl_token {
37    solana_pubkey::declare_id!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");
38}
39
40/// Checks that the supplied program ID is correct for spl-token or
41/// spl-token-2022
42pub fn check_spl_token_program_account(spl_token_program_id: &Pubkey) -> ProgramResult {
43    if spl_token_program_id != &id() && spl_token_program_id != &inline_spl_token::id() {
44        return Err(ProgramError::IncorrectProgramId);
45    }
46    Ok(())
47}
48
49/// Trims a string number by removing excess zeroes or unneeded decimal point
50fn trim_ui_amount_string(mut ui_amount: String, decimals: u8) -> String {
51    if decimals > 0 {
52        let zeros_trimmed = ui_amount.trim_end_matches('0');
53        ui_amount = zeros_trimmed.trim_end_matches('.').to_string();
54    }
55    ui_amount
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn test_inline_spl_token_program_id() {
64        assert_eq!(inline_spl_token::id(), spl_token_interface::id());
65    }
66}