Skip to main content

spl_token_2022_interface/
lib.rs

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