1
1

mint.rs 2.3 KB

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