create.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. use {
  2. borsh::{BorshDeserialize, BorshSerialize},
  3. mpl_token_metadata::instruction as mpl_instruction,
  4. solana_program::{
  5. account_info::{next_account_info, AccountInfo},
  6. entrypoint::ProgramResult,
  7. msg,
  8. program::invoke,
  9. program_pack::Pack,
  10. rent::Rent,
  11. system_instruction,
  12. sysvar::Sysvar,
  13. },
  14. spl_token::{instruction as token_instruction, state::Mint},
  15. };
  16. #[derive(BorshSerialize, BorshDeserialize, Debug)]
  17. pub struct CreateTokenArgs {
  18. pub token_title: String,
  19. pub token_symbol: String,
  20. pub token_uri: String,
  21. pub decimals: u8,
  22. }
  23. pub fn create_token(accounts: &[AccountInfo], args: CreateTokenArgs) -> ProgramResult {
  24. let accounts_iter = &mut accounts.iter();
  25. let mint_account = next_account_info(accounts_iter)?;
  26. let mint_authority = next_account_info(accounts_iter)?;
  27. let metadata_account = next_account_info(accounts_iter)?;
  28. let payer = next_account_info(accounts_iter)?;
  29. let rent = next_account_info(accounts_iter)?;
  30. let system_program = next_account_info(accounts_iter)?;
  31. let token_program = next_account_info(accounts_iter)?;
  32. let token_metadata_program = next_account_info(accounts_iter)?;
  33. // First create the account for the Mint
  34. //
  35. msg!("Creating mint account...");
  36. msg!("Mint: {}", mint_account.key);
  37. invoke(
  38. &system_instruction::create_account(
  39. payer.key,
  40. mint_account.key,
  41. (Rent::get()?).minimum_balance(Mint::LEN),
  42. Mint::LEN as u64,
  43. token_program.key,
  44. ),
  45. &[
  46. mint_account.clone(),
  47. payer.clone(),
  48. system_program.clone(),
  49. token_program.clone(),
  50. ],
  51. )?;
  52. // Now initialize that account as a Mint (standard Mint)
  53. //
  54. msg!("Initializing mint account...");
  55. msg!("Mint: {}", mint_account.key);
  56. invoke(
  57. &token_instruction::initialize_mint(
  58. token_program.key,
  59. mint_account.key,
  60. mint_authority.key,
  61. Some(mint_authority.key),
  62. args.decimals,
  63. )?,
  64. &[
  65. mint_account.clone(),
  66. mint_authority.clone(),
  67. token_program.clone(),
  68. rent.clone(),
  69. ],
  70. )?;
  71. // Now create the account for that Mint's metadata
  72. //
  73. msg!("Creating metadata account...");
  74. msg!("Metadata account address: {}", metadata_account.key);
  75. invoke(
  76. &mpl_instruction::create_metadata_accounts_v3(
  77. *token_metadata_program.key,
  78. *metadata_account.key,
  79. *mint_account.key,
  80. *mint_authority.key,
  81. *payer.key,
  82. *mint_authority.key,
  83. args.token_title,
  84. args.token_symbol,
  85. args.token_uri,
  86. None,
  87. 0,
  88. true,
  89. false,
  90. None,
  91. None,
  92. None,
  93. ),
  94. &[
  95. metadata_account.clone(),
  96. mint_account.clone(),
  97. mint_authority.clone(),
  98. payer.clone(),
  99. token_metadata_program.clone(),
  100. rent.clone(),
  101. ],
  102. )?;
  103. msg!("Token mint created successfully.");
  104. Ok(())
  105. }