#![allow(clippy::integer_arithmetic)]
#![deny(missing_docs)]
#![cfg_attr(not(test), forbid(unsafe_code))]
pub mod error;
pub mod instruction;
pub mod native_mint;
pub mod processor;
pub mod state;
#[cfg(not(feature = "no-entrypoint"))]
mod entrypoint;
pub use solana_program;
use solana_program::{entrypoint::ProgramResult, program_error::ProgramError, pubkey::Pubkey};
pub fn ui_amount_to_amount(ui_amount: f64, decimals: u8) -> u64 {
(ui_amount * 10_usize.pow(decimals as u32) as f64) as u64
}
pub fn amount_to_ui_amount(amount: u64, decimals: u8) -> f64 {
amount as f64 / 10_usize.pow(decimals as u32) as f64
}
pub fn amount_to_ui_amount_string(amount: u64, decimals: u8) -> String {
let decimals = decimals as usize;
if decimals > 0 {
let mut s = format!("{:01$}", amount, decimals + 1);
s.insert(s.len() - decimals, '.');
s
} else {
amount.to_string()
}
}
pub fn amount_to_ui_amount_string_trimmed(amount: u64, decimals: u8) -> String {
let mut s = amount_to_ui_amount_string(amount, decimals);
if decimals > 0 {
let zeros_trimmed = s.trim_end_matches('0');
s = zeros_trimmed.trim_end_matches('.').to_string();
}
s
}
pub fn try_ui_amount_into_amount(ui_amount: String, decimals: u8) -> Result<u64, ProgramError> {
let decimals = decimals as usize;
let mut parts = ui_amount.split('.');
let mut amount_str = parts.next().unwrap().to_string(); let after_decimal = parts.next().unwrap_or("");
let after_decimal = after_decimal.trim_end_matches('0');
if (amount_str.is_empty() && after_decimal.is_empty())
|| parts.next().is_some()
|| after_decimal.len() > decimals
{
return Err(ProgramError::InvalidArgument);
}
amount_str.push_str(after_decimal);
for _ in 0..decimals.saturating_sub(after_decimal.len()) {
amount_str.push('0');
}
amount_str
.parse::<u64>()
.map_err(|_| ProgramError::InvalidArgument)
}
solana_program::declare_id!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");
pub fn check_program_account(spl_token_program_id: &Pubkey) -> ProgramResult {
if spl_token_program_id != &id() {
return Err(ProgramError::IncorrectProgramId);
}
Ok(())
}