initialize_mint2.rs 2.4 KB

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