1
0

initialize_mint2.rs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. mod setup;
  2. use {
  3. pinocchio_token_interface::state::mint::Mint,
  4. setup::TOKEN_PROGRAM_ID,
  5. solana_keypair::Keypair,
  6. solana_program_option::COption,
  7. solana_program_pack::Pack,
  8. solana_program_test::{tokio, ProgramTest},
  9. solana_pubkey::Pubkey,
  10. solana_signer::Signer,
  11. solana_system_interface::instruction::create_account,
  12. solana_transaction::Transaction,
  13. std::mem::size_of,
  14. };
  15. #[tokio::test]
  16. async fn initialize_mint2() {
  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 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. // When a new mint account is created and initialized.
  35. let instructions = vec![
  36. create_account(
  37. &context.payer.pubkey(),
  38. &account.pubkey(),
  39. rent.minimum_balance(account_size),
  40. account_size as u64,
  41. &TOKEN_PROGRAM_ID,
  42. ),
  43. initialize_ix,
  44. ];
  45. let tx = Transaction::new_signed_with_payer(
  46. &instructions,
  47. Some(&context.payer.pubkey()),
  48. &[&context.payer, &account],
  49. context.last_blockhash,
  50. );
  51. context.banks_client.process_transaction(tx).await.unwrap();
  52. // Then an account has the correct data.
  53. let account = context
  54. .banks_client
  55. .get_account(account.pubkey())
  56. .await
  57. .unwrap();
  58. assert!(account.is_some());
  59. let account = account.unwrap();
  60. let mint = spl_token::state::Mint::unpack(&account.data).unwrap();
  61. assert!(mint.is_initialized);
  62. assert!(mint.mint_authority == COption::Some(mint_authority));
  63. assert!(mint.freeze_authority == COption::Some(freeze_authority));
  64. assert!(mint.decimals == 0)
  65. }