1
0

amount_to_ui_amount.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 = spl_token_interface::instruction::amount_to_ui_amount(
  31. &spl_token_interface::ID,
  32. &mint,
  33. 1000,
  34. )
  35. .unwrap();
  36. let tx = Transaction::new_signed_with_payer(
  37. &[amount_to_ui_amount_ix],
  38. Some(&context.payer.pubkey()),
  39. &[&context.payer],
  40. context.last_blockhash,
  41. );
  42. context.banks_client.process_transaction(tx).await.unwrap();
  43. // Then the transaction should succeed.
  44. let account = context.banks_client.get_account(mint).await.unwrap();
  45. assert!(account.is_some());
  46. }
  47. #[test]
  48. fn amount_to_ui_amount_with_maximum_decimals() {
  49. // Given a mint account with `u8::MAX` as decimals.
  50. let mint = Pubkey::new_unique();
  51. let mint_authority = Pubkey::new_unique();
  52. let freeze_authority = Pubkey::new_unique();
  53. let mint_account = create_mint_account(
  54. mint_authority,
  55. Some(freeze_authority),
  56. u8::MAX,
  57. &TOKEN_PROGRAM_ID,
  58. );
  59. // When we convert a 20 amount using the mint, the transaction should
  60. // succeed and return the correct UI amount.
  61. let instruction =
  62. spl_token_interface::instruction::amount_to_ui_amount(&spl_token_interface::ID, &mint, 20)
  63. .unwrap();
  64. // The expected UI amount is "0.000....002" without the trailing zeros.
  65. let mut ui_amount = [b'0'; u8::MAX as usize + 1];
  66. ui_amount[1] = b'.';
  67. ui_amount[ui_amount.len() - 1] = b'2';
  68. mollusk().process_and_validate_instruction(
  69. &instruction,
  70. &[(mint, mint_account)],
  71. &[Check::success(), Check::return_data(&ui_amount)],
  72. );
  73. }
  74. #[test]
  75. fn amount_to_ui_amount_with_u64_max() {
  76. // Given a mint account with `u8::MAX` as decimals.
  77. let mint = Pubkey::new_unique();
  78. let mint_authority = Pubkey::new_unique();
  79. let freeze_authority = Pubkey::new_unique();
  80. let mint_account = create_mint_account(
  81. mint_authority,
  82. Some(freeze_authority),
  83. u8::MAX,
  84. &TOKEN_PROGRAM_ID,
  85. );
  86. // When we convert an u64::MAX amount using the mint, the transaction should
  87. // succeed and return the correct UI amount.
  88. let instruction = spl_token_interface::instruction::amount_to_ui_amount(
  89. &spl_token_interface::ID,
  90. &mint,
  91. u64::MAX,
  92. )
  93. .unwrap();
  94. // The expected UI amount is a `u64::MAX` with 255 decimal places.
  95. // - 2 digits for `0.`
  96. // - 255 digits for the maximum decimals.
  97. let mut ui_amount = [b'0'; u8::MAX as usize + 2];
  98. ui_amount[1] = b'.';
  99. let mut offset = ui_amount.len();
  100. let mut value = u64::MAX;
  101. while value > 0 {
  102. let remainder = value % 10;
  103. value /= 10;
  104. offset -= 1;
  105. ui_amount[offset] = b'0' + (remainder as u8);
  106. }
  107. mollusk().process_and_validate_instruction(
  108. &instruction,
  109. &[(mint, mint_account)],
  110. &[Check::success(), Check::return_data(&ui_amount)],
  111. );
  112. }