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
21pub use solana_zk_sdk;
24#[deprecated(
25 since = "9.1.0",
26 note = "Use spl_token_2022_interface instead and remove spl_token_2022 as a dependency"
27)]
28pub use spl_token_2022_interface::{check_id, check_program_account, id, ID};
29use {
30 solana_program_error::{ProgramError, ProgramResult},
31 solana_pubkey::Pubkey,
32 solana_sdk_ids::system_program,
33 solana_zk_sdk::encryption::pod::elgamal::PodElGamalCiphertext,
34 spl_token_2022_interface::error::TokenError,
35};
36
37pub fn ui_amount_to_amount(ui_amount: f64, decimals: u8) -> u64 {
40 (ui_amount * 10_usize.pow(decimals as u32) as f64) as u64
41}
42
43pub fn amount_to_ui_amount(amount: u64, decimals: u8) -> f64 {
46 amount as f64 / 10_usize.pow(decimals as u32) as f64
47}
48
49pub fn amount_to_ui_amount_string(amount: u64, decimals: u8) -> String {
52 let decimals = decimals as usize;
53 if decimals > 0 {
54 let mut s = format!("{:01$}", amount, decimals + 1);
56 s.insert(s.len() - decimals, '.');
58 s
59 } else {
60 amount.to_string()
61 }
62}
63
64pub fn amount_to_ui_amount_string_trimmed(amount: u64, decimals: u8) -> String {
67 let s = amount_to_ui_amount_string(amount, decimals);
68 trim_ui_amount_string(s, decimals)
69}
70
71fn trim_ui_amount_string(mut ui_amount: String, decimals: u8) -> String {
73 if decimals > 0 {
74 let zeros_trimmed = ui_amount.trim_end_matches('0');
75 ui_amount = zeros_trimmed.trim_end_matches('.').to_string();
76 }
77 ui_amount
78}
79
80pub fn try_ui_amount_into_amount(ui_amount: String, decimals: u8) -> Result<u64, ProgramError> {
83 let decimals = decimals as usize;
84 let mut parts = ui_amount.split('.');
85 let mut amount_str = parts.next().unwrap().to_string();
88 let after_decimal = parts.next().unwrap_or("");
89 let after_decimal = after_decimal.trim_end_matches('0');
90 if (amount_str.is_empty() && after_decimal.is_empty())
91 || parts.next().is_some()
92 || after_decimal.len() > decimals
93 {
94 return Err(ProgramError::InvalidArgument);
95 }
96
97 amount_str.push_str(after_decimal);
98 for _ in 0..decimals.saturating_sub(after_decimal.len()) {
99 amount_str.push('0');
100 }
101 amount_str
102 .parse::<u64>()
103 .map_err(|_| ProgramError::InvalidArgument)
104}
105
106pub fn check_zk_elgamal_proof_program_account(
109 zk_elgamal_proof_program_id: &Pubkey,
110) -> ProgramResult {
111 if zk_elgamal_proof_program_id != &solana_zk_sdk::zk_elgamal_proof_program::id() {
112 return Err(ProgramError::IncorrectProgramId);
113 }
114 Ok(())
115}
116
117pub fn check_system_program_account(system_program_id: &Pubkey) -> ProgramResult {
119 if system_program_id != &system_program::id() {
120 return Err(ProgramError::IncorrectProgramId);
121 }
122 Ok(())
123}
124
125pub(crate) fn check_elgamal_registry_program_account(
127 elgamal_registry_account_program_id: &Pubkey,
128) -> ProgramResult {
129 if elgamal_registry_account_program_id != &spl_elgamal_registry_interface::id() {
130 return Err(ProgramError::IncorrectProgramId);
131 }
132 Ok(())
133}
134
135#[cfg(feature = "zk-ops")]
137pub(crate) fn check_auditor_ciphertext(
138 instruction_data_auditor_ciphertext_lo: &PodElGamalCiphertext,
139 instruction_data_auditor_ciphertext_hi: &PodElGamalCiphertext,
140 proof_context_auditor_ciphertext_lo: &PodElGamalCiphertext,
141 proof_context_auditor_ciphertext_hi: &PodElGamalCiphertext,
142) -> ProgramResult {
143 if instruction_data_auditor_ciphertext_lo != proof_context_auditor_ciphertext_lo {
144 return Err(TokenError::ConfidentialTransferBalanceMismatch.into());
145 }
146 if instruction_data_auditor_ciphertext_hi != proof_context_auditor_ciphertext_hi {
147 return Err(TokenError::ConfidentialTransferBalanceMismatch.into());
148 }
149 Ok(())
150}