1
0

mint.rs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. // Mint account keypair.
  19. let account = Keypair::new();
  20. let account_size = size_of::<Mint>();
  21. let rent = context.banks_client.get_rent().await.unwrap();
  22. let mut initialize_ix = spl_token::instruction::initialize_mint(
  23. &spl_token::ID,
  24. &account.pubkey(),
  25. &mint_authority,
  26. freeze_authority.as_ref(),
  27. 4,
  28. )
  29. .unwrap();
  30. // Switches the program id in case we are using a "custom" one.
  31. initialize_ix.program_id = *program_id;
  32. // Create a new account and initialize as a mint.
  33. let instructions = vec![
  34. create_account(
  35. &context.payer.pubkey(),
  36. &account.pubkey(),
  37. rent.minimum_balance(account_size),
  38. account_size as u64,
  39. program_id,
  40. ),
  41. initialize_ix,
  42. ];
  43. let tx = Transaction::new_signed_with_payer(
  44. &instructions,
  45. Some(&context.payer.pubkey()),
  46. &[&context.payer, &account],
  47. context.last_blockhash,
  48. );
  49. context.banks_client.process_transaction(tx).await.unwrap();
  50. Ok(account.pubkey())
  51. }
  52. pub async fn mint(
  53. context: &mut ProgramTestContext,
  54. mint: &Pubkey,
  55. account: &Pubkey,
  56. mint_authority: &Keypair,
  57. amount: u64,
  58. program_id: &Pubkey,
  59. ) -> Result<(), BanksClientError> {
  60. let mut mint_ix = spl_token::instruction::mint_to(
  61. &spl_token::ID,
  62. mint,
  63. account,
  64. &mint_authority.pubkey(),
  65. &[],
  66. amount,
  67. )
  68. .unwrap();
  69. // Switches the program id to the token program.
  70. mint_ix.program_id = *program_id;
  71. let tx = Transaction::new_signed_with_payer(
  72. &[mint_ix],
  73. Some(&context.payer.pubkey()),
  74. &[&context.payer, mint_authority],
  75. context.last_blockhash,
  76. );
  77. context.banks_client.process_transaction(tx).await
  78. }