initialize_account2.rs 2.4 KB

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