spl_token/
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 instruction;
8pub mod native_mint;
9pub mod processor;
10pub mod state;
11
12#[cfg(not(feature = "no-entrypoint"))]
13mod entrypoint;
14
15/// Export current sdk types for downstream users building with a different sdk
16/// version
17pub mod solana_program {
18    #![allow(missing_docs)]
19    pub mod entrypoint {
20        pub use solana_program_error::ProgramResult;
21    }
22    pub mod instruction {
23        pub use solana_instruction::{AccountMeta, Instruction};
24    }
25    pub mod program_error {
26        pub use solana_program_error::ProgramError;
27    }
28    pub mod program_option {
29        pub use solana_program_option::COption;
30    }
31    pub mod program_pack {
32        pub use solana_program_pack::{IsInitialized, Pack, Sealed};
33    }
34    pub mod pubkey {
35        pub use solana_pubkey::{Pubkey, PUBKEY_BYTES};
36    }
37}
38use solana_program_error::ProgramError;
39// Re-export spl_token_interface items
40pub use spl_token_interface::{check_id, check_program_account, id, ID};
41
42/// Convert the UI representation of a token amount (using the decimals field
43/// defined in its mint) to the raw amount
44pub fn ui_amount_to_amount(ui_amount: f64, decimals: u8) -> u64 {
45    (ui_amount * 10_usize.pow(decimals as u32) as f64) as u64
46}
47
48/// Convert a raw amount to its UI representation (using the decimals field
49/// defined in its mint)
50pub fn amount_to_ui_amount(amount: u64, decimals: u8) -> f64 {
51    amount as f64 / 10_usize.pow(decimals as u32) as f64
52}
53
54/// Convert a raw amount to its UI representation (using the decimals field
55/// defined in its mint)
56pub fn amount_to_ui_amount_string(amount: u64, decimals: u8) -> String {
57    let decimals = decimals as usize;
58    if decimals > 0 {
59        // Left-pad zeros to decimals + 1, so we at least have an integer zero
60        let mut s = format!("{:01$}", amount, decimals + 1);
61        // Add the decimal point (Sorry, "," locales!)
62        s.insert(s.len() - decimals, '.');
63        s
64    } else {
65        amount.to_string()
66    }
67}
68
69/// Convert a raw amount to its UI representation using the given decimals field
70/// Excess zeroes or unneeded decimal point are trimmed.
71pub fn amount_to_ui_amount_string_trimmed(amount: u64, decimals: u8) -> String {
72    let mut s = amount_to_ui_amount_string(amount, decimals);
73    if decimals > 0 {
74        let zeros_trimmed = s.trim_end_matches('0');
75        s = zeros_trimmed.trim_end_matches('.').to_string();
76    }
77    s
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
86    // at 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}