mint_to.rs 1.8 KB

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