lib.rs 3.2 KB

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