create_token_mint.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. use {
  2. solana_program::{
  3. account_info::{next_account_info, AccountInfo},
  4. entrypoint::ProgramResult,
  5. msg,
  6. program::invoke_signed,
  7. program_pack::Pack,
  8. pubkey::Pubkey,
  9. rent::Rent,
  10. system_instruction,
  11. sysvar::Sysvar,
  12. },
  13. spl_token::{
  14. instruction as token_instruction,
  15. state::Mint,
  16. },
  17. mpl_token_metadata::{
  18. instruction as mpl_instruction,
  19. },
  20. };
  21. pub fn create_token_mint(
  22. program_id: &Pubkey,
  23. accounts: &[AccountInfo],
  24. token_title: String,
  25. token_symbol: String,
  26. token_uri: String,
  27. mint_authority_pda_bump: u8,
  28. ) -> ProgramResult {
  29. let accounts_iter = &mut accounts.iter();
  30. let mint_account = next_account_info(accounts_iter)?;
  31. let mint_authority = next_account_info(accounts_iter)?;
  32. let metadata_account = next_account_info(accounts_iter)?;
  33. let payer = next_account_info(accounts_iter)?;
  34. let rent = next_account_info(accounts_iter)?;
  35. let system_program = next_account_info(accounts_iter)?;
  36. let token_program = next_account_info(accounts_iter)?;
  37. let token_metadata_program = next_account_info(accounts_iter)?;
  38. msg!("Creating mint authority...");
  39. msg!("Mint Authority: {}", mint_authority.key);
  40. invoke_signed(
  41. &system_instruction::create_account(
  42. &payer.key,
  43. &mint_authority.key,
  44. (Rent::get()?).minimum_balance(8) as u64,
  45. 8,
  46. &program_id,
  47. ),
  48. &[
  49. payer.clone(),
  50. mint_authority.clone(),
  51. system_program.clone(),
  52. ],
  53. &[&[
  54. b"mint_authority_",
  55. mint_account.key.as_ref(),
  56. &[mint_authority_pda_bump],
  57. ]]
  58. )?;
  59. msg!("Creating mint account...");
  60. msg!("Mint: {}", mint_account.key);
  61. invoke_signed(
  62. &system_instruction::create_account(
  63. &payer.key,
  64. &mint_account.key,
  65. (Rent::get()?).minimum_balance(Mint::LEN),
  66. Mint::LEN as u64,
  67. &token_program.key,
  68. ),
  69. &[
  70. mint_account.clone(),
  71. payer.clone(),
  72. token_program.clone(),
  73. ],
  74. &[&[
  75. b"mint_authority_",
  76. mint_account.key.as_ref(),
  77. &[mint_authority_pda_bump],
  78. ]]
  79. )?;
  80. msg!("Initializing mint account...");
  81. msg!("Mint: {}", mint_account.key);
  82. invoke_signed(
  83. &token_instruction::initialize_mint(
  84. &token_program.key,
  85. &mint_account.key,
  86. &mint_authority.key,
  87. Some(&mint_authority.key),
  88. 9,
  89. )?,
  90. &[
  91. mint_account.clone(),
  92. mint_authority.clone(),
  93. token_program.clone(),
  94. rent.clone(),
  95. ],
  96. &[&[
  97. b"mint_authority_",
  98. mint_account.key.as_ref(),
  99. &[mint_authority_pda_bump],
  100. ]]
  101. )?;
  102. msg!("Creating metadata account...");
  103. msg!("Metadata account address: {}", metadata_account.key);
  104. invoke_signed(
  105. &mpl_instruction::create_metadata_accounts_v2(
  106. *token_metadata_program.key,
  107. *metadata_account.key,
  108. *mint_account.key,
  109. *mint_authority.key,
  110. *payer.key,
  111. *mint_authority.key,
  112. token_title,
  113. token_symbol,
  114. token_uri,
  115. None,
  116. 0,
  117. true,
  118. false,
  119. None,
  120. None,
  121. ),
  122. &[
  123. metadata_account.clone(),
  124. mint_account.clone(),
  125. mint_authority.clone(),
  126. payer.clone(),
  127. token_metadata_program.clone(),
  128. rent.clone(),
  129. ],
  130. &[&[
  131. b"mint_authority_",
  132. mint_account.key.as_ref(),
  133. &[mint_authority_pda_bump],
  134. ]]
  135. )?;
  136. msg!("Token mint created successfully.");
  137. Ok(())
  138. }
  139. pub struct MintAuthority {}