lib.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. use {
  2. borsh::{BorshDeserialize, BorshSerialize},
  3. solana_program::{
  4. account_info::{next_account_info, AccountInfo},
  5. entrypoint,
  6. entrypoint::ProgramResult,
  7. msg,
  8. program::invoke,
  9. pubkey::Pubkey,
  10. rent::Rent,
  11. system_instruction,
  12. sysvar::Sysvar,
  13. },
  14. spl_token_2022::{extension::ExtensionType, instruction as token_instruction, state::Mint},
  15. };
  16. #[derive(BorshSerialize, BorshDeserialize, Debug)]
  17. pub struct CreateTokenArgs {
  18. pub token_decimals: u8,
  19. }
  20. entrypoint!(process_instruction);
  21. fn process_instruction(
  22. _program_id: &Pubkey,
  23. accounts: &[AccountInfo],
  24. instruction_data: &[u8],
  25. ) -> ProgramResult {
  26. let args = CreateTokenArgs::try_from_slice(instruction_data)?;
  27. let accounts_iter = &mut accounts.iter();
  28. let mint_account = next_account_info(accounts_iter)?;
  29. let mint_authority = next_account_info(accounts_iter)?;
  30. let close_authority = next_account_info(accounts_iter)?;
  31. let payer = next_account_info(accounts_iter)?;
  32. let rent = next_account_info(accounts_iter)?;
  33. let system_program = next_account_info(accounts_iter)?;
  34. let token_program = next_account_info(accounts_iter)?;
  35. // Find the size for the account with the Extension
  36. let space = ExtensionType::get_account_len::<Mint>(&[ExtensionType::MintCloseAuthority]);
  37. // Get the required rent exemption amount for the account
  38. let rent_required = Rent::get()?.minimum_balance(space);
  39. // Create the account for the Mint and allocate space
  40. msg!("Mint account address : {}", mint_account.key);
  41. invoke(
  42. &system_instruction::create_account(
  43. &payer.key,
  44. mint_account.key,
  45. rent_required,
  46. space as u64,
  47. token_program.key,
  48. ),
  49. &[
  50. mint_account.clone(),
  51. payer.clone(),
  52. system_program.clone(),
  53. token_program.clone(),
  54. ],
  55. )?;
  56. // This needs to be done before the Mint is initialized
  57. // Initialize the Mint close authority Extension
  58. invoke(
  59. &token_instruction::initialize_mint_close_authority(
  60. token_program.key,
  61. mint_account.key,
  62. Some(close_authority.key),
  63. )
  64. .unwrap(),
  65. &[
  66. mint_account.clone(),
  67. close_authority.clone(),
  68. token_program.clone(),
  69. system_program.clone(),
  70. ],
  71. )?;
  72. // Initialize the Token Mint
  73. invoke(
  74. &token_instruction::initialize_mint(
  75. &token_program.key,
  76. &mint_account.key,
  77. &mint_authority.key,
  78. Some(&mint_authority.key),
  79. args.token_decimals,
  80. )?,
  81. &[
  82. mint_account.clone(),
  83. mint_authority.clone(),
  84. token_program.clone(),
  85. rent.clone(),
  86. ],
  87. )?;
  88. msg!("Mint created!");
  89. Ok(())
  90. }