initialize_account.rs 2.3 KB

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