apl_token/
lib.rs

1#![allow(clippy::arithmetic_side_effects)]
2#![deny(missing_docs)]
3#![cfg_attr(not(test), forbid(unsafe_code))]
4
5//! An ERC20-like Token program for the Arch blockchain
6
7pub mod error;
8pub mod instruction;
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 use arch_program;
18use arch_program::{entrypoint::ProgramResult, program_error::ProgramError, pubkey::Pubkey};
19
20/// Convert the UI representation of a token amount (using the decimals field
21/// defined in its mint) to the raw amount
22pub fn ui_amount_to_amount(ui_amount: f64, decimals: u8) -> u64 {
23    (ui_amount * 10_usize.pow(decimals as u32) as f64) as u64
24}
25
26/// Convert a raw amount to its UI representation (using the decimals field
27/// defined in its mint)
28pub fn amount_to_ui_amount(amount: u64, decimals: u8) -> f64 {
29    amount as f64 / 10_usize.pow(decimals as u32) as f64
30}
31
32/// Convert a raw amount to its UI representation (using the decimals field
33/// defined in its mint)
34pub fn amount_to_ui_amount_string(amount: u64, decimals: u8) -> String {
35    let decimals = decimals as usize;
36    if decimals > 0 {
37        // Left-pad zeros to decimals + 1, so we at least have an integer zero
38        let mut s = format!("{:01$}", amount, decimals + 1);
39        // Add the decimal point (Sorry, "," locales!)
40        s.insert(s.len() - decimals, '.');
41        s
42    } else {
43        amount.to_string()
44    }
45}
46
47/// Convert a raw amount to its UI representation using the given decimals field
48/// Excess zeroes or unneeded decimal point are trimmed.
49pub fn amount_to_ui_amount_string_trimmed(amount: u64, decimals: u8) -> String {
50    let mut s = amount_to_ui_amount_string(amount, decimals);
51    if decimals > 0 {
52        let zeros_trimmed = s.trim_end_matches('0');
53        s = zeros_trimmed.trim_end_matches('.').to_string();
54    }
55    s
56}
57
58/// Try to convert a UI representation of a token amount to its raw amount using
59/// the given decimals field
60pub fn try_ui_amount_into_amount(ui_amount: String, decimals: u8) -> Result<u64, ProgramError> {
61    let decimals = decimals as usize;
62    let mut parts = ui_amount.split('.');
63    // splitting a string, even an empty one, will always yield an iterator of
64    // at least length == 1
65    let mut amount_str = parts.next().unwrap().to_string();
66    let after_decimal = parts.next().unwrap_or("");
67    let after_decimal = after_decimal.trim_end_matches('0');
68    if (amount_str.is_empty() && after_decimal.is_empty())
69        || parts.next().is_some()
70        || after_decimal.len() > decimals
71    {
72        return Err(ProgramError::InvalidArgument);
73    }
74
75    amount_str.push_str(after_decimal);
76    for _ in 0..decimals.saturating_sub(after_decimal.len()) {
77        amount_str.push('0');
78    }
79    amount_str
80        .parse::<u64>()
81        .map_err(|_| ProgramError::InvalidArgument)
82}
83
84/// The program ID for the APL-token program
85pub fn id() -> Pubkey {
86    Pubkey::from_slice(b"apl-token00000000000000000000000")
87}
88
89/// Checks that the supplied program ID is the correct one for APL-token
90pub fn check_program_account(spl_token_program_id: &Pubkey) -> ProgramResult {
91    if spl_token_program_id != &id() {
92        return Err(ProgramError::IncorrectProgramId);
93    }
94    Ok(())
95}