amount_to_ui_amount.rs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #![cfg(feature = "test-sbf")]
  2. mod setup;
  3. use setup::{mint, TOKEN_PROGRAM_ID};
  4. use solana_program_test::{tokio, ProgramTest};
  5. use solana_sdk::{pubkey::Pubkey, signature::Signer, transaction::Transaction};
  6. #[test_case::test_case(spl_token::ID ; "spl-token")]
  7. #[test_case::test_case(TOKEN_PROGRAM_ID ; "p-token")]
  8. #[tokio::test]
  9. async fn amount_to_ui_amount(token_program: Pubkey) {
  10. let mut context = ProgramTest::new("token_program", TOKEN_PROGRAM_ID, None)
  11. .start_with_context()
  12. .await;
  13. // Given a mint account.
  14. let mint_authority = Pubkey::new_unique();
  15. let freeze_authority = Pubkey::new_unique();
  16. let mint = mint::initialize(
  17. &mut context,
  18. mint_authority,
  19. Some(freeze_authority),
  20. &token_program,
  21. )
  22. .await
  23. .unwrap();
  24. let mut amount_to_ui_amount_ix =
  25. spl_token::instruction::amount_to_ui_amount(&spl_token::ID, &mint, 1000).unwrap();
  26. // Switches the program id to the token program.
  27. amount_to_ui_amount_ix.program_id = token_program;
  28. let tx = Transaction::new_signed_with_payer(
  29. &[amount_to_ui_amount_ix],
  30. Some(&context.payer.pubkey()),
  31. &[&context.payer],
  32. context.last_blockhash,
  33. );
  34. context.banks_client.process_transaction(tx).await.unwrap();
  35. // Then the transaction should succeed.
  36. let account = context.banks_client.get_account(mint).await.unwrap();
  37. assert!(account.is_some());
  38. }