initialize_account3.rs 2.2 KB

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