initialize_mint2.rs 2.3 KB

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