ui_amount_to_amount.rs 1.5 KB

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