spl_token_2022/
lib.rs

1#![allow(clippy::arithmetic_side_effects)]
2#![deny(missing_docs)]
3#![cfg_attr(not(test), forbid(unsafe_code))]
4
5//! An ERC20-like Token program for the Miraland blockchain
6
7pub mod error;
8pub mod extension;
9pub mod generic_token_account;
10pub mod instruction;
11pub mod native_mint;
12pub mod offchain;
13pub mod onchain;
14pub mod pod;
15pub mod pod_instruction;
16pub mod processor;
17pub mod proof;
18#[cfg(feature = "serde-traits")]
19pub mod serialization;
20pub mod state;
21
22#[cfg(not(feature = "no-entrypoint"))]
23mod entrypoint;
24
25// Export current sdk types for downstream users building with a different sdk
26// version
27use miraland_program::{
28    entrypoint::ProgramResult, program_error::ProgramError, pubkey::Pubkey, system_program,
29};
30pub use {miraland_program, miraland_zk_token_sdk};
31
32/// Convert the UI representation of a token amount (using the decimals field
33/// defined in its mint) to the raw amount
34pub fn ui_amount_to_amount(ui_amount: f64, decimals: u8) -> u64 {
35    (ui_amount * 10_usize.pow(decimals as u32) as f64) as u64
36}
37
38/// Convert a raw amount to its UI representation (using the decimals field
39/// defined in its mint)
40pub fn amount_to_ui_amount(amount: u64, decimals: u8) -> f64 {
41    amount as f64 / 10_usize.pow(decimals as u32) as f64
42}
43
44/// Convert a raw amount to its UI representation (using the decimals field
45/// defined in its mint)
46pub fn amount_to_ui_amount_string(amount: u64, decimals: u8) -> String {
47    let decimals = decimals as usize;
48    if decimals > 0 {
49        // Left-pad zeros to decimals + 1, so we at least have an integer zero
50        let mut s = format!("{:01$}", amount, decimals + 1);
51        // Add the decimal point (Sorry, "," locales!)
52        s.insert(s.len() - decimals, '.');
53        s
54    } else {
55        amount.to_string()
56    }
57}
58
59/// Convert a raw amount to its UI representation using the given decimals field
60/// Excess zeroes or unneeded decimal point are trimmed.
61pub fn amount_to_ui_amount_string_trimmed(amount: u64, decimals: u8) -> String {
62    let mut s = amount_to_ui_amount_string(amount, decimals);
63    if decimals > 0 {
64        let zeros_trimmed = s.trim_end_matches('0');
65        s = zeros_trimmed.trim_end_matches('.').to_string();
66    }
67    s
68}
69
70/// Try to convert a UI representation of a token amount to its raw amount using
71/// the given decimals field
72pub fn try_ui_amount_into_amount(ui_amount: String, decimals: u8) -> Result<u64, ProgramError> {
73    let decimals = decimals as usize;
74    let mut parts = ui_amount.split('.');
75    // splitting a string, even an empty one, will always yield an iterator of at
76    // least length == 1
77    let mut amount_str = parts.next().unwrap().to_string();
78    let after_decimal = parts.next().unwrap_or("");
79    let after_decimal = after_decimal.trim_end_matches('0');
80    if (amount_str.is_empty() && after_decimal.is_empty())
81        || parts.next().is_some()
82        || after_decimal.len() > decimals
83    {
84        return Err(ProgramError::InvalidArgument);
85    }
86
87    amount_str.push_str(after_decimal);
88    for _ in 0..decimals.saturating_sub(after_decimal.len()) {
89        amount_str.push('0');
90    }
91    amount_str
92        .parse::<u64>()
93        .map_err(|_| ProgramError::InvalidArgument)
94}
95
96miraland_program::declare_id!("Token8N5ecJeFxL83iFa2h7AgJ8AtufM7bbg63LrW89");
97
98/// Checks that the supplied program ID is correct for solarti-token-2022
99pub fn check_program_account(spl_token_program_id: &Pubkey) -> ProgramResult {
100    if spl_token_program_id != &id() {
101        return Err(ProgramError::IncorrectProgramId);
102    }
103    Ok(())
104}
105
106/// Checks that the supplied program ID is corect for solarti-token or
107/// solarti-token-2022
108pub fn check_spl_token_program_account(spl_token_program_id: &Pubkey) -> ProgramResult {
109    if spl_token_program_id != &id() && spl_token_program_id != &spl_token::id() {
110        return Err(ProgramError::IncorrectProgramId);
111    }
112    Ok(())
113}
114
115/// Checks that the supplied program ID is correct for the ZK Token proof
116/// program
117pub fn check_zk_token_proof_program_account(zk_token_proof_program_id: &Pubkey) -> ProgramResult {
118    if zk_token_proof_program_id != &miraland_zk_token_sdk::zk_token_proof_program::id() {
119        return Err(ProgramError::IncorrectProgramId);
120    }
121    Ok(())
122}
123
124/// Checks if the spplied program ID is that of the system program
125pub fn check_system_program_account(system_program_id: &Pubkey) -> ProgramResult {
126    if system_program_id != &system_program::id() {
127        return Err(ProgramError::IncorrectProgramId);
128    }
129    Ok(())
130}