1#![allow(clippy::arithmetic_side_effects)]
2#![deny(missing_docs)]
3#![cfg_attr(not(test), warn(unsafe_code))]
4
5pub 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 state;
18
19#[cfg(not(feature = "no-entrypoint"))]
20mod entrypoint;
21
22pub use atlas_zk_sdk;
25pub use atlas_token_2022_interface::{check_id, check_program_account, id, ID};
26use {
27 error::TokenError,
28 atlas_program_error::{ProgramError, ProgramResult},
29 atlas_pubkey::Pubkey,
30 atlas_sdk_ids::system_program,
31 atlas_zk_sdk::encryption::pod::elgamal::PodElGamalCiphertext,
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: &Pubkey,
107) -> ProgramResult {
108 if zk_elgamal_proof_program_id != &atlas_zk_sdk::zk_elgamal_proof_program::id() {
109 return Err(ProgramError::IncorrectProgramId);
110 }
111 Ok(())
112}
113
114pub fn check_system_program_account(system_program_id: &Pubkey) -> 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: &Pubkey,
125) -> ProgramResult {
126 if elgamal_registry_account_program_id != &atlas_elgamal_registry::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}