lib.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. use {
  2. borsh::{
  3. BorshSerialize, BorshDeserialize,
  4. },
  5. solana_program::{
  6. account_info::{next_account_info, AccountInfo},
  7. entrypoint,
  8. entrypoint::ProgramResult,
  9. msg,
  10. program::invoke,
  11. pubkey::Pubkey,
  12. rent::Rent,
  13. system_instruction,
  14. sysvar::Sysvar,
  15. },
  16. spl_token::{
  17. instruction as token_instruction,
  18. },
  19. mpl_token_metadata::{
  20. instruction as mpl_instruction,
  21. },
  22. };
  23. entrypoint!(process_instruction);
  24. fn process_instruction(
  25. _program_id: &Pubkey,
  26. accounts: &[AccountInfo],
  27. instruction_data: &[u8],
  28. ) -> ProgramResult {
  29. const MINT_SIZE: u64 = 82;
  30. let accounts_iter = &mut accounts.iter();
  31. let mint_account = next_account_info(accounts_iter)?;
  32. let metadata_account = next_account_info(accounts_iter)?;
  33. let mint_authority = 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. let token_metadata = TokenMetadata::try_from_slice(instruction_data)?;
  39. msg!("Creating mint account...");
  40. msg!("Mint: {}", mint_account.key);
  41. invoke(
  42. &system_instruction::create_account(
  43. &mint_authority.key,
  44. &mint_account.key,
  45. (Rent::get()?).minimum_balance(MINT_SIZE as usize),
  46. MINT_SIZE,
  47. &token_program.key,
  48. ),
  49. &[
  50. mint_account.clone(),
  51. mint_authority.clone(),
  52. token_program.clone(),
  53. ]
  54. )?;
  55. msg!("Initializing mint account...");
  56. msg!("Mint: {}", mint_account.key);
  57. invoke(
  58. &token_instruction::initialize_mint(
  59. &token_program.key,
  60. &mint_account.key,
  61. &mint_authority.key,
  62. Some(&mint_authority.key),
  63. 0, // 0 Decimals
  64. )?,
  65. &[
  66. mint_account.clone(),
  67. mint_authority.clone(),
  68. token_program.clone(),
  69. rent.clone(),
  70. ]
  71. )?;
  72. msg!("Creating metadata account...");
  73. msg!("Metadata account address: {}", metadata_account.key);
  74. invoke(
  75. &mpl_instruction::create_metadata_accounts_v2(
  76. *token_metadata_program.key, // Program ID (the Token Metadata Program)
  77. *metadata_account.key, // Metadata Account
  78. *mint_account.key, // Mint Account
  79. *mint_authority.key, // Mint Authority
  80. *mint_authority.key, // Payer
  81. *mint_authority.key, // Update Authority
  82. token_metadata.title, // Name
  83. token_metadata.symbol, // Symbol
  84. token_metadata.uri, // URI
  85. None, // Creators
  86. 0, // Seller fee basis points
  87. true, // Update authority is signer
  88. false, // Is mutable
  89. None, // Collection
  90. None, // Uses
  91. ),
  92. &[
  93. metadata_account.clone(),
  94. mint_account.clone(),
  95. mint_authority.clone(),
  96. token_metadata_program.clone(),
  97. rent.clone(),
  98. ],
  99. )?;
  100. msg!("Token mint process completed successfully.");
  101. Ok(())
  102. }
  103. #[derive(BorshSerialize, BorshDeserialize, Debug)]
  104. pub struct TokenMetadata {
  105. title: String,
  106. symbol: String,
  107. uri: String,
  108. }