mint.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. use {
  2. solana_program::{
  3. account_info::{next_account_info, AccountInfo},
  4. entrypoint::ProgramResult,
  5. msg,
  6. program::invoke,
  7. },
  8. spl_token::{
  9. instruction as token_instruction,
  10. },
  11. spl_associated_token_account::{
  12. instruction as associated_token_account_instruction,
  13. },
  14. mpl_token_metadata::{
  15. instruction as mpl_instruction,
  16. },
  17. };
  18. pub fn mint_to(
  19. accounts: &[AccountInfo],
  20. ) -> ProgramResult {
  21. let accounts_iter = &mut accounts.iter();
  22. let mint_account = next_account_info(accounts_iter)?;
  23. let metadata_account = next_account_info(accounts_iter)?;
  24. let edition_account = next_account_info(accounts_iter)?;
  25. let mint_authority = next_account_info(accounts_iter)?;
  26. let associated_token_account = next_account_info(accounts_iter)?;
  27. let payer = next_account_info(accounts_iter)?;
  28. let rent = next_account_info(accounts_iter)?;
  29. let _system_program = next_account_info(accounts_iter)?;
  30. let token_program = next_account_info(accounts_iter)?;
  31. let associated_token_program = next_account_info(accounts_iter)?;
  32. let token_metadata_program = next_account_info(accounts_iter)?;
  33. if associated_token_account.lamports() == 0 {
  34. msg!("Creating associated token account...");
  35. invoke(
  36. &associated_token_account_instruction::create_associated_token_account(
  37. &payer.key,
  38. &payer.key,
  39. &mint_account.key,
  40. ),
  41. &[
  42. mint_account.clone(),
  43. associated_token_account.clone(),
  44. payer.clone(),
  45. token_program.clone(),
  46. associated_token_program.clone(),
  47. ]
  48. )?;
  49. } else {
  50. msg!("Associated token account exists.");
  51. }
  52. msg!("Associated Token Address: {}", associated_token_account.key);
  53. // Mint the NFT to the user's wallet
  54. //
  55. msg!("Minting NFT to associated token account...");
  56. invoke(
  57. &token_instruction::mint_to(
  58. &token_program.key,
  59. &mint_account.key,
  60. &associated_token_account.key,
  61. &mint_authority.key,
  62. &[&mint_authority.key],
  63. 1,
  64. )?,
  65. &[
  66. mint_account.clone(),
  67. mint_authority.clone(),
  68. associated_token_account.clone(),
  69. token_program.clone(),
  70. ],
  71. )?;
  72. // We can make this a Limited Edition NFT through Metaplex,
  73. // which will disable minting by setting the Mint & Freeze Authorities to the
  74. // Edition Account.
  75. //
  76. msg!("Creating edition account...");
  77. msg!("Edition account address: {}", edition_account.key);
  78. invoke(
  79. &mpl_instruction::create_master_edition_v3(
  80. *token_metadata_program.key, // Program ID
  81. *edition_account.key, // Edition
  82. *mint_account.key, // Mint
  83. *mint_authority.key, // Update Authority
  84. *mint_authority.key, // Mint Authority
  85. *metadata_account.key, // Metadata
  86. *payer.key, // Payer
  87. Some(1), // Max Supply
  88. ),
  89. &[
  90. edition_account.clone(),
  91. metadata_account.clone(),
  92. mint_account.clone(),
  93. mint_authority.clone(),
  94. payer.clone(),
  95. token_metadata_program.clone(),
  96. rent.clone(),
  97. ]
  98. )?;
  99. // If we don't use Metaplex Editions, we must disable minting manually
  100. //
  101. // -------------------------------------------------------------------
  102. // msg!("Disabling future minting of this NFT...");
  103. // invoke(
  104. // &token_instruction::set_authority(
  105. // &token_program.key,
  106. // &mint_account.key,
  107. // None,
  108. // token_instruction::AuthorityType::MintTokens,
  109. // &mint_authority.key,
  110. // &[&mint_authority.key],
  111. // )?,
  112. // &[
  113. // mint_account.clone(),
  114. // mint_authority.clone(),
  115. // token_program.clone(),
  116. // ],
  117. // )?;
  118. // invoke(
  119. // &token_instruction::set_authority(
  120. // &token_program.key,
  121. // &mint_account.key,
  122. // None,
  123. // token_instruction::AuthorityType::FreezeAccount,
  124. // &mint_authority.key,
  125. // &[&mint_authority.key],
  126. // )?,
  127. // &[
  128. // mint_account.clone(),
  129. // mint_authority.clone(),
  130. // token_program.clone(),
  131. // ],
  132. // )?;
  133. msg!("NFT minted successfully.");
  134. Ok(())
  135. }