initialize_mint.rs 2.3 KB

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