initialize_account2.rs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #![cfg(feature = "test-sbf")]
  2. mod setup;
  3. use setup::{mint, TOKEN_PROGRAM_ID};
  4. use solana_program_test::{tokio, ProgramTest};
  5. use solana_sdk::{
  6. program_pack::Pack,
  7. pubkey::Pubkey,
  8. signature::{Keypair, Signer},
  9. system_instruction,
  10. transaction::Transaction,
  11. };
  12. #[test_case::test_case(TOKEN_PROGRAM_ID ; "p-token")]
  13. #[tokio::test]
  14. async fn initialize_account2(token_program: Pubkey) {
  15. let mut context = ProgramTest::new("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,
  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 mut initialize_ix = spl_token::instruction::initialize_account2(
  35. &spl_token::ID,
  36. &account.pubkey(),
  37. &mint,
  38. &owner,
  39. )
  40. .unwrap();
  41. // Switches the program id to the token program.
  42. initialize_ix.program_id = token_program;
  43. // When a new mint account is created and initialized.
  44. let instructions = vec![
  45. system_instruction::create_account(
  46. &context.payer.pubkey(),
  47. &account.pubkey(),
  48. rent.minimum_balance(account_size),
  49. account_size as u64,
  50. &token_program,
  51. ),
  52. initialize_ix,
  53. ];
  54. let tx = Transaction::new_signed_with_payer(
  55. &instructions,
  56. Some(&context.payer.pubkey()),
  57. &[&context.payer, &account],
  58. context.last_blockhash,
  59. );
  60. context.banks_client.process_transaction(tx).await.unwrap();
  61. // Then an account has the correct data.
  62. let account = context
  63. .banks_client
  64. .get_account(account.pubkey())
  65. .await
  66. .unwrap();
  67. assert!(account.is_some());
  68. let account = account.unwrap();
  69. let account = spl_token::state::Account::unpack(&account.data).unwrap();
  70. assert!(!account.is_frozen());
  71. assert!(account.owner == owner);
  72. assert!(account.mint == mint);
  73. }