mint.rs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. use pda_mint_authority_api::prelude::*;
  2. use solana_program::msg;
  3. use steel::*;
  4. pub fn process_mint(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
  5. // parse args.
  6. let args = Mint::try_from_bytes(data)?;
  7. let amount = u64::from_le_bytes(args.amount);
  8. // Load accounts.
  9. let [payer_info, mint_info, ata_info, mint_authority_info, token_program, associated_token_program, system_program] =
  10. accounts
  11. else {
  12. return Err(ProgramError::NotEnoughAccountKeys);
  13. };
  14. msg!("Minting tokens to associated token account...");
  15. msg!("Mint: {:?}", mint_info);
  16. msg!("Token Address: {:?}", &ata_info);
  17. // validation
  18. payer_info.is_signer()?;
  19. mint_info.as_mint()?;
  20. token_program.is_program(&spl_token::ID)?;
  21. if ata_info.lamports() == 0 {
  22. msg!("Creating associated token account...");
  23. create_associated_token_account(
  24. payer_info,
  25. payer_info,
  26. ata_info,
  27. mint_info,
  28. system_program,
  29. token_program,
  30. associated_token_program,
  31. )?;
  32. msg!("Associated Token Address: {}", ata_info.key);
  33. } else {
  34. msg!("Associated token account exists.");
  35. }
  36. mint_authority_info
  37. .is_writable()?
  38. .has_seeds(&[MINT_AUTHORITY], &pda_mint_authority_api::ID)?;
  39. ata_info
  40. .is_writable()?
  41. .as_associated_token_account(payer_info.key, mint_info.key)?;
  42. msg!("Minting token to associated token account...");
  43. msg!("Mint: {}", mint_info.key);
  44. msg!("Token Address: {}", ata_info.key);
  45. solana_program::program::invoke_signed(
  46. &spl_token::instruction::mint_to(
  47. &spl_token::id(),
  48. mint_info.key,
  49. ata_info.key,
  50. mint_authority_info.key,
  51. &[mint_authority_info.key],
  52. amount,
  53. )?,
  54. &[
  55. token_program.clone(),
  56. mint_info.clone(),
  57. ata_info.clone(),
  58. mint_authority_info.clone(),
  59. ],
  60. &[&[MINT_AUTHORITY, &[mint_authority_pda().1]]],
  61. )?;
  62. msg!("Token minted successfully.");
  63. Ok(())
  64. }