amount_to_ui_amount.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. mod setup;
  2. use {
  3. mollusk_svm::result::Check,
  4. setup::{
  5. mint,
  6. mollusk::{create_mint_account, mollusk},
  7. TOKEN_PROGRAM_ID,
  8. },
  9. solana_program_test::{tokio, ProgramTest},
  10. solana_pubkey::Pubkey,
  11. solana_signer::Signer,
  12. solana_transaction::Transaction,
  13. };
  14. #[tokio::test]
  15. async fn amount_to_ui_amount() {
  16. let mut context = ProgramTest::new("pinocchio_token_program", TOKEN_PROGRAM_ID, None)
  17. .start_with_context()
  18. .await;
  19. // Given a mint account.
  20. let mint_authority = Pubkey::new_unique();
  21. let freeze_authority = Pubkey::new_unique();
  22. let mint = mint::initialize(
  23. &mut context,
  24. mint_authority,
  25. Some(freeze_authority),
  26. &TOKEN_PROGRAM_ID,
  27. )
  28. .await
  29. .unwrap();
  30. let amount_to_ui_amount_ix =
  31. spl_token::instruction::amount_to_ui_amount(&spl_token::ID, &mint, 1000).unwrap();
  32. let tx = Transaction::new_signed_with_payer(
  33. &[amount_to_ui_amount_ix],
  34. Some(&context.payer.pubkey()),
  35. &[&context.payer],
  36. context.last_blockhash,
  37. );
  38. context.banks_client.process_transaction(tx).await.unwrap();
  39. // Then the transaction should succeed.
  40. let account = context.banks_client.get_account(mint).await.unwrap();
  41. assert!(account.is_some());
  42. }
  43. #[test]
  44. fn amount_to_ui_amount_with_maximum_decimals() {
  45. // Given a mint account with `u8::MAX` as decimals.
  46. let mint = Pubkey::new_unique();
  47. let mint_authority = Pubkey::new_unique();
  48. let freeze_authority = Pubkey::new_unique();
  49. let mint_account = create_mint_account(
  50. mint_authority,
  51. Some(freeze_authority),
  52. u8::MAX,
  53. &TOKEN_PROGRAM_ID,
  54. );
  55. // When we convert a 20 amount using the mint, the transaction should
  56. // succeed and return the correct UI amount.
  57. let instruction =
  58. spl_token::instruction::amount_to_ui_amount(&spl_token::ID, &mint, 20).unwrap();
  59. // The expected UI amount is "0.000....002" without the trailing zeros.
  60. let mut ui_amount = [b'0'; u8::MAX as usize + 1];
  61. ui_amount[1] = b'.';
  62. ui_amount[ui_amount.len() - 1] = b'2';
  63. mollusk().process_and_validate_instruction(
  64. &instruction,
  65. &[(mint, mint_account)],
  66. &[Check::success(), Check::return_data(&ui_amount)],
  67. );
  68. }
  69. #[test]
  70. fn amount_to_ui_amount_with_u64_max() {
  71. // Given a mint account with `u8::MAX` as decimals.
  72. let mint = Pubkey::new_unique();
  73. let mint_authority = Pubkey::new_unique();
  74. let freeze_authority = Pubkey::new_unique();
  75. let mint_account = create_mint_account(
  76. mint_authority,
  77. Some(freeze_authority),
  78. u8::MAX,
  79. &TOKEN_PROGRAM_ID,
  80. );
  81. // When we convert an u64::MAX amount using the mint, the transaction should
  82. // succeed and return the correct UI amount.
  83. let instruction =
  84. spl_token::instruction::amount_to_ui_amount(&spl_token::ID, &mint, u64::MAX).unwrap();
  85. // The expected UI amount is a `u64::MAX` with 255 decimal places.
  86. // - 2 digits for `0.`
  87. // - 255 digits for the maximum decimals.
  88. let mut ui_amount = [b'0'; u8::MAX as usize + 2];
  89. ui_amount[1] = b'.';
  90. let mut offset = ui_amount.len();
  91. let mut value = u64::MAX;
  92. while value > 0 {
  93. let remainder = value % 10;
  94. value /= 10;
  95. offset -= 1;
  96. ui_amount[offset] = b'0' + (remainder as u8);
  97. }
  98. mollusk().process_and_validate_instruction(
  99. &instruction,
  100. &[(mint, mint_account)],
  101. &[Check::success(), Check::return_data(&ui_amount)],
  102. );
  103. }