1#![allow(clippy::integer_arithmetic)]
2#![deny(missing_docs)]
3#![cfg_attr(not(test), forbid(unsafe_code))]
4
5pub 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
21pub 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
31pub 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
37pub fn amount_to_ui_amount(amount: u64, decimals: u8) -> f64 {
39 amount as f64 / 10_usize.pow(decimals as u32) as f64
40}
41
42pub fn amount_to_ui_amount_string(amount: u64, decimals: u8) -> String {
44 let decimals = decimals as usize;
45 if decimals > 0 {
46 let mut s = format!("{:01$}", amount, decimals + 1);
48 s.insert(s.len() - decimals, '.');
50 s
51 } else {
52 amount.to_string()
53 }
54}
55
56pub 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
67pub 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(); 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
93pub 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
101pub 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
109pub fn cmp_pubkeys(a: &Pubkey, b: &Pubkey) -> bool {
112 sol_memcmp(a.as_ref(), b.as_ref(), PUBKEY_BYTES) == 0
113}