1
0

mint.rs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. use {
  2. pinocchio_token_interface::state::mint::Mint,
  3. solana_keypair::Keypair,
  4. solana_program_error::ProgramError,
  5. solana_program_test::{BanksClientError, ProgramTestContext},
  6. solana_pubkey::Pubkey,
  7. solana_signer::Signer,
  8. solana_system_interface::instruction::create_account,
  9. solana_transaction::Transaction,
  10. std::mem::size_of,
  11. };
  12. pub async fn initialize(
  13. context: &mut ProgramTestContext,
  14. mint_authority: Pubkey,
  15. freeze_authority: Option<Pubkey>,
  16. program_id: &Pubkey,
  17. ) -> Result<Pubkey, ProgramError> {
  18. initialize_with_decimals(context, mint_authority, freeze_authority, 4, program_id).await
  19. }
  20. pub async fn initialize_with_decimals(
  21. context: &mut ProgramTestContext,
  22. mint_authority: Pubkey,
  23. freeze_authority: Option<Pubkey>,
  24. decimals: u8,
  25. program_id: &Pubkey,
  26. ) -> Result<Pubkey, ProgramError> {
  27. // Mint account keypair.
  28. let account = Keypair::new();
  29. let account_size = size_of::<Mint>();
  30. let rent = context.banks_client.get_rent().await.unwrap();
  31. let mut initialize_ix = spl_token_interface::instruction::initialize_mint(
  32. &spl_token_interface::ID,
  33. &account.pubkey(),
  34. &mint_authority,
  35. freeze_authority.as_ref(),
  36. decimals,
  37. )
  38. .unwrap();
  39. // Switches the program id in case we are using a "custom" one.
  40. initialize_ix.program_id = *program_id;
  41. // Create a new account and initialize as a mint.
  42. let instructions = vec![
  43. create_account(
  44. &context.payer.pubkey(),
  45. &account.pubkey(),
  46. rent.minimum_balance(account_size),
  47. account_size as u64,
  48. program_id,
  49. ),
  50. initialize_ix,
  51. ];
  52. let tx = Transaction::new_signed_with_payer(
  53. &instructions,
  54. Some(&context.payer.pubkey()),
  55. &[&context.payer, &account],
  56. context.last_blockhash,
  57. );
  58. context.banks_client.process_transaction(tx).await.unwrap();
  59. Ok(account.pubkey())
  60. }
  61. pub async fn mint(
  62. context: &mut ProgramTestContext,
  63. mint: &Pubkey,
  64. account: &Pubkey,
  65. mint_authority: &Keypair,
  66. amount: u64,
  67. program_id: &Pubkey,
  68. ) -> Result<(), BanksClientError> {
  69. let mut mint_ix = spl_token_interface::instruction::mint_to(
  70. &spl_token_interface::ID,
  71. mint,
  72. account,
  73. &mint_authority.pubkey(),
  74. &[],
  75. amount,
  76. )
  77. .unwrap();
  78. // Switches the program id to the token program.
  79. mint_ix.program_id = *program_id;
  80. let tx = Transaction::new_signed_with_payer(
  81. &[mint_ix],
  82. Some(&context.payer.pubkey()),
  83. &[&context.payer, mint_authority],
  84. context.last_blockhash,
  85. );
  86. context.banks_client.process_transaction(tx).await
  87. }