initialize_account3.rs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. mod setup;
  2. use {
  3. setup::{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. system_instruction,
  10. transaction::Transaction,
  11. },
  12. };
  13. #[test_case::test_case(TOKEN_PROGRAM_ID ; "p-token")]
  14. #[tokio::test]
  15. async fn initialize_account3(token_program: Pubkey) {
  16. let mut context = ProgramTest::new("pinocchio_token_program", TOKEN_PROGRAM_ID, None)
  17. .start_with_context()
  18. .await;
  19. // Given a mint account.
  20. let mint_authority = Pubkey::new_unique();
  21. let freeze_authority = Pubkey::new_unique();
  22. let mint = mint::initialize(
  23. &mut context,
  24. mint_authority,
  25. Some(freeze_authority),
  26. &token_program,
  27. )
  28. .await
  29. .unwrap();
  30. // Given a mint authority, freeze authority and an account keypair.
  31. let owner = Pubkey::new_unique();
  32. let account = Keypair::new();
  33. let account_size = 165;
  34. let rent = context.banks_client.get_rent().await.unwrap();
  35. let mut initialize_ix = spl_token::instruction::initialize_account3(
  36. &spl_token::ID,
  37. &account.pubkey(),
  38. &mint,
  39. &owner,
  40. )
  41. .unwrap();
  42. // Switches the program id to the token program.
  43. initialize_ix.program_id = token_program;
  44. // When a new mint account is created and initialized.
  45. let instructions = vec![
  46. system_instruction::create_account(
  47. &context.payer.pubkey(),
  48. &account.pubkey(),
  49. rent.minimum_balance(account_size),
  50. account_size as u64,
  51. &token_program,
  52. ),
  53. initialize_ix,
  54. ];
  55. let tx = Transaction::new_signed_with_payer(
  56. &instructions,
  57. Some(&context.payer.pubkey()),
  58. &[&context.payer, &account],
  59. context.last_blockhash,
  60. );
  61. context.banks_client.process_transaction(tx).await.unwrap();
  62. // Then an account has the correct data.
  63. let account = context
  64. .banks_client
  65. .get_account(account.pubkey())
  66. .await
  67. .unwrap();
  68. assert!(account.is_some());
  69. let account = account.unwrap();
  70. let account = spl_token::state::Account::unpack(&account.data).unwrap();
  71. assert!(!account.is_frozen());
  72. assert!(account.owner == owner);
  73. assert!(account.mint == mint);
  74. }