safe_token_2022/
lib.rs

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