initialize.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. use create_account_api::prelude::*;
  2. use solana_program::msg;
  3. use steel::sysvar::rent::Rent;
  4. use steel::*;
  5. pub fn process_initialize(accounts: &[AccountInfo<'_>]) -> ProgramResult {
  6. // Load accounts
  7. let [payer, new_account, system_program] = accounts else {
  8. msg!("Not enough accounts provided");
  9. return Err(ProgramError::NotEnoughAccountKeys);
  10. };
  11. // Validate accounts
  12. payer
  13. .is_signer()
  14. .map_err(|_| ProgramError::MissingRequiredSignature)?;
  15. new_account.is_signer()?.is_empty()?.is_writable()?;
  16. system_program.is_program(&system_program::ID)?;
  17. // The helper "create_account" will create an account
  18. // owned by our program and not the system program
  19. // Calculate the minimum balance needed for the account
  20. // Space required is the size of our NewAccount struct
  21. let space_required = std::mem::size_of::<NewAccount>() as u64;
  22. let lamports_required = (Rent::get()?).minimum_balance(space_required as usize);
  23. // Create the account by invoking a create_account system instruction
  24. solana_program::program::invoke(
  25. &solana_program::system_instruction::create_account(
  26. payer.key,
  27. new_account.key,
  28. lamports_required,
  29. space_required,
  30. &system_program::ID,
  31. ),
  32. &[payer.clone(), new_account.clone(), system_program.clone()],
  33. )?;
  34. msg!("A new account has been created and initialized!");
  35. Ok(())
  36. }