mint_to.rs 1.9 KB

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