amount_to_ui_amount.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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(TOKEN_PROGRAM_ID ; "p-token")]
  7. #[tokio::test]
  8. async fn amount_to_ui_amount(token_program: Pubkey) {
  9. let mut context = ProgramTest::new("token_program", TOKEN_PROGRAM_ID, None)
  10. .start_with_context()
  11. .await;
  12. // Given a mint account.
  13. let mint_authority = Pubkey::new_unique();
  14. let freeze_authority = Pubkey::new_unique();
  15. let mint = mint::initialize(
  16. &mut context,
  17. mint_authority,
  18. Some(freeze_authority),
  19. &token_program,
  20. )
  21. .await
  22. .unwrap();
  23. let mut amount_to_ui_amount_ix =
  24. spl_token::instruction::amount_to_ui_amount(&spl_token::ID, &mint, 1000).unwrap();
  25. // Switches the program id to the token program.
  26. amount_to_ui_amount_ix.program_id = token_program;
  27. let tx = Transaction::new_signed_with_payer(
  28. &[amount_to_ui_amount_ix],
  29. Some(&context.payer.pubkey()),
  30. &[&context.payer],
  31. context.last_blockhash,
  32. );
  33. context.banks_client.process_transaction(tx).await.unwrap();
  34. // Then the transaction should succeed.
  35. let account = context.banks_client.get_account(mint).await.unwrap();
  36. assert!(account.is_some());
  37. }