atlas_token_2022/
lib.rs

1#![allow(clippy::arithmetic_side_effects)]
2#![deny(missing_docs)]
3#![cfg_attr(not(test), warn(unsafe_code))]
4
5//! An ERC20-like Token program for the Atlas blockchain
6
7pub 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
22// Export current sdk types for downstream users building with a different sdk
23// version
24pub 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
34/// Convert the UI representation of a token amount (using the decimals field
35/// defined in its mint) to the raw amount
36pub 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
40/// Convert a raw amount to its UI representation (using the decimals field
41/// defined in its mint)
42pub fn amount_to_ui_amount(amount: u64, decimals: u8) -> f64 {
43    amount as f64 / 10_usize.pow(decimals as u32) as f64
44}
45
46/// Convert a raw amount to its UI representation (using the decimals field
47/// defined in its mint)
48pub fn amount_to_ui_amount_string(amount: u64, decimals: u8) -> String {
49    let decimals = decimals as usize;
50    if decimals > 0 {
51        // Left-pad zeros to decimals + 1, so we at least have an integer zero
52        let mut s = format!("{:01$}", amount, decimals + 1);
53        // Add the decimal point (Sorry, "," locales!)
54        s.insert(s.len() - decimals, '.');
55        s
56    } else {
57        amount.to_string()
58    }
59}
60
61/// Convert a raw amount to its UI representation using the given decimals field
62/// Excess zeroes or unneeded decimal point are trimmed.
63pub 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
68/// Trims a string number by removing excess zeroes or unneeded decimal point
69fn 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
77/// Try to convert a UI representation of a token amount to its raw amount using
78/// the given decimals field
79pub 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    // splitting a string, even an empty one, will always yield an iterator of at
83    // least length == 1
84    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
103/// Checks that the supplied program ID is correct for the ZK ElGamal proof
104/// program
105pub 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
114/// Checks if the supplied program ID is that of the system program
115pub 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
122/// Checks if the supplied program ID is that of the ElGamal registry program
123pub(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/// Check instruction data and proof data auditor ciphertext consistency
133#[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}