mint_to.rs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. mod setup;
  2. use {
  3. setup::{account, mint, TOKEN_PROGRAM_ID},
  4. solana_keypair::Keypair,
  5. solana_program_pack::Pack,
  6. solana_program_test::{tokio, ProgramTest},
  7. solana_pubkey::Pubkey,
  8. solana_signer::Signer,
  9. solana_transaction::Transaction,
  10. };
  11. #[tokio::test]
  12. async fn mint_to() {
  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_ID,
  24. )
  25. .await
  26. .unwrap();
  27. // And a token account.
  28. let owner = Keypair::new();
  29. let account =
  30. account::initialize(&mut context, &mint, &owner.pubkey(), &TOKEN_PROGRAM_ID).await;
  31. // When we mint tokens to it.
  32. let mint_ix = spl_token::instruction::mint_to(
  33. &spl_token::ID,
  34. &mint,
  35. &account,
  36. &mint_authority.pubkey(),
  37. &[],
  38. 100,
  39. )
  40. .unwrap();
  41. let tx = Transaction::new_signed_with_payer(
  42. &[mint_ix],
  43. Some(&context.payer.pubkey()),
  44. &[&context.payer, &mint_authority],
  45. context.last_blockhash,
  46. );
  47. context.banks_client.process_transaction(tx).await.unwrap();
  48. // Then an account has the correct data.
  49. let account = context.banks_client.get_account(account).await.unwrap();
  50. assert!(account.is_some());
  51. let account = account.unwrap();
  52. let account = spl_token::state::Account::unpack(&account.data).unwrap();
  53. assert!(account.amount == 100);
  54. }