ui_amount_to_amount.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. mod setup;
  2. use {
  3. crate::setup::mollusk::{create_mint_account, mollusk},
  4. core::str::from_utf8,
  5. mollusk_svm::result::Check,
  6. setup::{mint, TOKEN_PROGRAM_ID},
  7. solana_program_error::ProgramError,
  8. solana_program_test::{tokio, ProgramTest},
  9. solana_pubkey::Pubkey,
  10. solana_signer::Signer,
  11. solana_transaction::Transaction,
  12. };
  13. #[tokio::test]
  14. async fn ui_amount_to_amount() {
  15. let mut context = ProgramTest::new("pinocchio_token_program", TOKEN_PROGRAM_ID, None)
  16. .start_with_context()
  17. .await;
  18. // Given a mint account.
  19. let mint_authority = Pubkey::new_unique();
  20. let freeze_authority = Pubkey::new_unique();
  21. let mint = mint::initialize(
  22. &mut context,
  23. mint_authority,
  24. Some(freeze_authority),
  25. &TOKEN_PROGRAM_ID,
  26. )
  27. .await
  28. .unwrap();
  29. let ui_amount_to_amount_ix =
  30. spl_token::instruction::ui_amount_to_amount(&spl_token::ID, &mint, "1000.00").unwrap();
  31. let tx = Transaction::new_signed_with_payer(
  32. &[ui_amount_to_amount_ix],
  33. Some(&context.payer.pubkey()),
  34. &[&context.payer],
  35. context.last_blockhash,
  36. );
  37. context.banks_client.process_transaction(tx).await.unwrap();
  38. // Then the transaction should succeed.
  39. let account = context.banks_client.get_account(mint).await.unwrap();
  40. assert!(account.is_some());
  41. }
  42. #[test]
  43. fn ui_amount_to_amount_with_maximum_decimals() {
  44. // Given a mint account with `u8::MAX` as decimals.
  45. let mint = Pubkey::new_unique();
  46. let mint_authority = Pubkey::new_unique();
  47. let freeze_authority = Pubkey::new_unique();
  48. let mint_account = create_mint_account(
  49. mint_authority,
  50. Some(freeze_authority),
  51. u8::MAX,
  52. &TOKEN_PROGRAM_ID,
  53. );
  54. // String representing the ui value `0.000....002`
  55. let mut ui_amount = [b'0'; u8::MAX as usize + 1];
  56. ui_amount[1] = b'.';
  57. ui_amount[ui_amount.len() - 1] = b'2';
  58. let input = from_utf8(&ui_amount).unwrap();
  59. // When we convert the ui amount using the mint, the transaction should
  60. // succeed and return 20 as the amount.
  61. let instruction =
  62. spl_token::instruction::ui_amount_to_amount(&spl_token::ID, &mint, input).unwrap();
  63. mollusk().process_and_validate_instruction(
  64. &instruction,
  65. &[(mint, mint_account)],
  66. &[Check::success(), Check::return_data(&20u64.to_le_bytes())],
  67. );
  68. }
  69. #[test]
  70. fn fail_ui_amount_to_amount_with_invalid_ui_amount() {
  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. // String representing the ui value `2.0`
  82. let ui_amount = [b'2', b'.', b'0'];
  83. let input = from_utf8(&ui_amount).unwrap();
  84. // When we try to convert the ui amount using the mint, the transaction should
  85. // fail with an error since the resulting value does not fit in an `u64`.
  86. let instruction =
  87. spl_token::instruction::ui_amount_to_amount(&spl_token::ID, &mint, input).unwrap();
  88. mollusk().process_and_validate_instruction(
  89. &instruction,
  90. &[(mint, mint_account)],
  91. &[Check::err(ProgramError::InvalidArgument)],
  92. );
  93. }