spl_token_2022/
lib.rs

1#![allow(clippy::arithmetic_side_effects)]
2#![deny(missing_docs)]
3
4//! An ERC20-like Token program for the Solana blockchain
5
6pub 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// Export current sdk types for downstream users building with a different sdk
22// version
23pub 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
37/// Convert the UI representation of a token amount (using the decimals field
38/// defined in its mint) to the raw amount
39pub 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
43/// Convert a raw amount to its UI representation (using the decimals field
44/// defined in its mint)
45pub fn amount_to_ui_amount(amount: u64, decimals: u8) -> f64 {
46    amount as f64 / 10_usize.pow(decimals as u32) as f64
47}
48
49/// Convert a raw amount to its UI representation (using the decimals field
50/// defined in its mint)
51pub fn amount_to_ui_amount_string(amount: u64, decimals: u8) -> String {
52    let decimals = decimals as usize;
53    if decimals > 0 {
54        // Left-pad zeros to decimals + 1, so we at least have an integer zero
55        let mut s = format!("{:01$}", amount, decimals + 1);
56        // Add the decimal point (Sorry, "," locales!)
57        s.insert(s.len() - decimals, '.');
58        s
59    } else {
60        amount.to_string()
61    }
62}
63
64/// Convert a raw amount to its UI representation using the given decimals field
65/// Excess zeroes or unneeded decimal point are trimmed.
66pub 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
71/// Trims a string number by removing excess zeroes or unneeded decimal point
72fn 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
80/// Try to convert a UI representation of a token amount to its raw amount using
81/// the given decimals field
82pub 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    // splitting a string, even an empty one, will always yield an iterator of at
86    // least length == 1
87    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
106/// Checks that the supplied program ID is correct for the ZK ElGamal proof
107/// program
108pub 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
117/// Checks if the supplied program ID is that of the system program
118pub 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
125/// Checks if the supplied program ID is that of the ElGamal registry program
126pub(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/// Check instruction data and proof data auditor ciphertext consistency
136#[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}