1
1

mint_to_checked.rs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #![cfg(feature = "test-sbf")]
  2. mod setup;
  3. use setup::{account, mint, TOKEN_PROGRAM_ID};
  4. use solana_program_test::{tokio, ProgramTest};
  5. use solana_sdk::{
  6. program_pack::Pack,
  7. pubkey::Pubkey,
  8. signature::{Keypair, Signer},
  9. transaction::Transaction,
  10. };
  11. #[test_case::test_case(TOKEN_PROGRAM_ID ; "p-token")]
  12. #[tokio::test]
  13. async fn mint_to_checked(token_program: Pubkey) {
  14. let mut context = ProgramTest::new("token_program", TOKEN_PROGRAM_ID, None)
  15. .start_with_context()
  16. .await;
  17. // Given a mint account.
  18. let mint_authority = Keypair::new();
  19. let freeze_authority = Pubkey::new_unique();
  20. let mint = mint::initialize(
  21. &mut context,
  22. mint_authority.pubkey(),
  23. Some(freeze_authority),
  24. &token_program,
  25. )
  26. .await
  27. .unwrap();
  28. // And a token account.
  29. let owner = Keypair::new();
  30. let account = account::initialize(&mut context, &mint, &owner.pubkey(), &token_program).await;
  31. // When we mint tokens to it.
  32. let mut mint_ix = spl_token::instruction::mint_to_checked(
  33. &spl_token::ID,
  34. &mint,
  35. &account,
  36. &mint_authority.pubkey(),
  37. &[],
  38. 100,
  39. 4,
  40. )
  41. .unwrap();
  42. // Switches the program id to the token program.
  43. mint_ix.program_id = token_program;
  44. let tx = Transaction::new_signed_with_payer(
  45. &[mint_ix],
  46. Some(&context.payer.pubkey()),
  47. &[&context.payer, &mint_authority],
  48. context.last_blockhash,
  49. );
  50. context.banks_client.process_transaction(tx).await.unwrap();
  51. // Then an account has the correct data.
  52. let account = context.banks_client.get_account(account).await.unwrap();
  53. assert!(account.is_some());
  54. let account = account.unwrap();
  55. let account = spl_token::state::Account::unpack(&account.data).unwrap();
  56. assert!(account.amount == 100);
  57. }