mint_to_checked.rs 1.8 KB

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