initialize_mint.rs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. mod setup;
  2. use {
  3. setup::TOKEN_PROGRAM_ID,
  4. solana_program_test::{tokio, ProgramTest},
  5. 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. spl_token_interface::state::mint::Mint,
  14. std::mem::size_of,
  15. };
  16. #[test_case::test_case(TOKEN_PROGRAM_ID ; "p-token")]
  17. #[tokio::test]
  18. async fn initialize_mint(token_program: Pubkey) {
  19. let context = ProgramTest::new("pinocchio_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_mint(
  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. }