initialize_mint.rs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #[tokio::test]
  17. async fn initialize_mint() {
  18. let context = ProgramTest::new("pinocchio_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 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. // When a new mint account is created and initialized.
  36. let instructions = vec![
  37. system_instruction::create_account(
  38. &context.payer.pubkey(),
  39. &account.pubkey(),
  40. rent.minimum_balance(account_size),
  41. account_size as u64,
  42. &TOKEN_PROGRAM_ID,
  43. ),
  44. initialize_ix,
  45. ];
  46. let tx = Transaction::new_signed_with_payer(
  47. &instructions,
  48. Some(&context.payer.pubkey()),
  49. &[&context.payer, &account],
  50. context.last_blockhash,
  51. );
  52. context.banks_client.process_transaction(tx).await.unwrap();
  53. // Then an account has the correct data.
  54. let account = context
  55. .banks_client
  56. .get_account(account.pubkey())
  57. .await
  58. .unwrap();
  59. assert!(account.is_some());
  60. let account = account.unwrap();
  61. let mint = spl_token::state::Mint::unpack(&account.data).unwrap();
  62. assert!(mint.is_initialized);
  63. assert!(mint.mint_authority == COption::Some(mint_authority));
  64. assert!(mint.freeze_authority == COption::Some(freeze_authority));
  65. assert!(mint.decimals == 0)
  66. }