lib.rs 3.5 KB

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