lib.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 mod solana_program {
  15. #![allow(missing_docs)]
  16. pub mod entrypoint {
  17. pub use solana_program_error::ProgramResult;
  18. }
  19. pub mod instruction {
  20. pub use solana_instruction::{AccountMeta, Instruction};
  21. }
  22. pub mod program_error {
  23. pub use solana_program_error::ProgramError;
  24. }
  25. pub mod program_option {
  26. pub use solana_program_option::COption;
  27. }
  28. pub mod program_pack {
  29. pub use solana_program_pack::{IsInitialized, Pack, Sealed};
  30. }
  31. pub mod pubkey {
  32. pub use solana_pubkey::{Pubkey, PUBKEY_BYTES};
  33. }
  34. }
  35. use {
  36. solana_program_error::{ProgramError, ProgramResult},
  37. solana_pubkey::Pubkey,
  38. };
  39. /// Convert the UI representation of a token amount (using the decimals field
  40. /// defined in its mint) to the raw amount
  41. pub fn ui_amount_to_amount(ui_amount: f64, decimals: u8) -> u64 {
  42. (ui_amount * 10_usize.pow(decimals as u32) as f64) as u64
  43. }
  44. /// Convert a raw amount to its UI representation (using the decimals field
  45. /// defined in its mint)
  46. pub fn amount_to_ui_amount(amount: u64, decimals: u8) -> f64 {
  47. amount as f64 / 10_usize.pow(decimals as u32) as f64
  48. }
  49. /// Convert a raw amount to its UI representation (using the decimals field
  50. /// defined in its mint)
  51. pub 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. /// Convert a raw amount to its UI representation using the given decimals field
  64. /// Excess zeroes or unneeded decimal point are trimmed.
  65. pub fn amount_to_ui_amount_string_trimmed(amount: u64, decimals: u8) -> String {
  66. let mut s = amount_to_ui_amount_string(amount, decimals);
  67. if decimals > 0 {
  68. let zeros_trimmed = s.trim_end_matches('0');
  69. s = zeros_trimmed.trim_end_matches('.').to_string();
  70. }
  71. s
  72. }
  73. /// Try to convert a UI representation of a token amount to its raw amount using
  74. /// the given decimals field
  75. pub fn try_ui_amount_into_amount(ui_amount: String, decimals: u8) -> Result<u64, ProgramError> {
  76. let decimals = decimals as usize;
  77. let mut parts = ui_amount.split('.');
  78. // splitting a string, even an empty one, will always yield an iterator of
  79. // at least length == 1
  80. let mut amount_str = parts.next().unwrap().to_string();
  81. let after_decimal = parts.next().unwrap_or("");
  82. let after_decimal = after_decimal.trim_end_matches('0');
  83. if (amount_str.is_empty() && after_decimal.is_empty())
  84. || parts.next().is_some()
  85. || after_decimal.len() > decimals
  86. {
  87. return Err(ProgramError::InvalidArgument);
  88. }
  89. amount_str.push_str(after_decimal);
  90. for _ in 0..decimals.saturating_sub(after_decimal.len()) {
  91. amount_str.push('0');
  92. }
  93. amount_str
  94. .parse::<u64>()
  95. .map_err(|_| ProgramError::InvalidArgument)
  96. }
  97. solana_pubkey::declare_id!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");
  98. /// Checks that the supplied program ID is the correct one for SPL-token
  99. pub fn check_program_account(spl_token_program_id: &Pubkey) -> ProgramResult {
  100. if spl_token_program_id != &id() {
  101. return Err(ProgramError::IncorrectProgramId);
  102. }
  103. Ok(())
  104. }