1#![allow(clippy::arithmetic_side_effects)]
2#![deny(missing_docs)]
3
4pub mod error;
7pub mod extension;
8pub mod generic_token_account;
9pub mod instruction;
10pub mod native_mint;
11pub mod offchain;
12pub mod onchain;
13pub mod pod;
14pub mod pod_instruction;
15pub mod processor;
16pub mod state;
17
18#[cfg(not(feature = "no-entrypoint"))]
19mod entrypoint;
20
21#[deprecated(
22 since = "9.1.0",
23 note = "Use spl_token_2022_interface instead and remove spl_token_2022 as a dependency"
24)]
25pub use spl_token_2022_interface::{check_id, check_program_account, id, ID};
26use {
27 solana_address::Address,
28 solana_program_error::{ProgramError, ProgramResult},
29 solana_sdk_ids::system_program,
30 solana_zk_sdk_pod::encryption::elgamal::PodElGamalCiphertext,
31 spl_token_2022_interface::error::TokenError,
32};
33
34pub fn ui_amount_to_amount(ui_amount: f64, decimals: u8) -> u64 {
37 (ui_amount * 10_usize.pow(decimals as u32) as f64) as u64
38}
39
40pub fn amount_to_ui_amount(amount: u64, decimals: u8) -> f64 {
43 amount as f64 / 10_usize.pow(decimals as u32) as f64
44}
45
46pub fn amount_to_ui_amount_string(amount: u64, decimals: u8) -> String {
49 let decimals = decimals as usize;
50 if decimals > 0 {
51 let mut s = format!("{:01$}", amount, decimals + 1);
53 s.insert(s.len() - decimals, '.');
55 s
56 } else {
57 amount.to_string()
58 }
59}
60
61pub fn amount_to_ui_amount_string_trimmed(amount: u64, decimals: u8) -> String {
64 let s = amount_to_ui_amount_string(amount, decimals);
65 trim_ui_amount_string(s, decimals)
66}
67
68fn trim_ui_amount_string(mut ui_amount: String, decimals: u8) -> String {
70 if decimals > 0 {
71 let zeros_trimmed = ui_amount.trim_end_matches('0');
72 ui_amount = zeros_trimmed.trim_end_matches('.').to_string();
73 }
74 ui_amount
75}
76
77pub fn try_ui_amount_into_amount(ui_amount: String, decimals: u8) -> Result<u64, ProgramError> {
80 let decimals = decimals as usize;
81 let mut parts = ui_amount.split('.');
82 let mut amount_str = parts.next().unwrap().to_string();
85 let after_decimal = parts.next().unwrap_or("");
86 let after_decimal = after_decimal.trim_end_matches('0');
87 if (amount_str.is_empty() && after_decimal.is_empty())
88 || parts.next().is_some()
89 || after_decimal.len() > decimals
90 {
91 return Err(ProgramError::InvalidArgument);
92 }
93
94 amount_str.push_str(after_decimal);
95 for _ in 0..decimals.saturating_sub(after_decimal.len()) {
96 amount_str.push('0');
97 }
98 amount_str
99 .parse::<u64>()
100 .map_err(|_| ProgramError::InvalidArgument)
101}
102
103pub fn check_zk_elgamal_proof_program_account(
106 zk_elgamal_proof_program_id: &Address,
107) -> ProgramResult {
108 if zk_elgamal_proof_program_id != &solana_zk_elgamal_proof_interface::id() {
109 return Err(ProgramError::IncorrectProgramId);
110 }
111 Ok(())
112}
113
114pub fn check_system_program_account(system_program_id: &Address) -> ProgramResult {
116 if system_program_id != &system_program::id() {
117 return Err(ProgramError::IncorrectProgramId);
118 }
119 Ok(())
120}
121
122pub(crate) fn check_elgamal_registry_program_account(
124 elgamal_registry_account_program_id: &Address,
125) -> ProgramResult {
126 if elgamal_registry_account_program_id != &spl_elgamal_registry_interface::id() {
127 return Err(ProgramError::IncorrectProgramId);
128 }
129 Ok(())
130}
131
132#[cfg(feature = "zk-ops")]
134pub(crate) fn check_auditor_ciphertext(
135 instruction_data_auditor_ciphertext_lo: &PodElGamalCiphertext,
136 instruction_data_auditor_ciphertext_hi: &PodElGamalCiphertext,
137 proof_context_auditor_ciphertext_lo: &PodElGamalCiphertext,
138 proof_context_auditor_ciphertext_hi: &PodElGamalCiphertext,
139) -> ProgramResult {
140 if instruction_data_auditor_ciphertext_lo != proof_context_auditor_ciphertext_lo {
141 return Err(TokenError::ConfidentialTransferBalanceMismatch.into());
142 }
143 if instruction_data_auditor_ciphertext_hi != proof_context_auditor_ciphertext_hi {
144 return Err(TokenError::ConfidentialTransferBalanceMismatch.into());
145 }
146 Ok(())
147}