lib.rs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. use borsh::{BorshDeserialize, BorshSerialize};
  2. use solana_program::{
  3. account_info::{AccountInfo, next_account_info},
  4. entrypoint,
  5. entrypoint::ProgramResult,
  6. msg,
  7. program::invoke,
  8. pubkey::Pubkey,
  9. rent::Rent,
  10. system_instruction,
  11. system_program,
  12. sysvar::Sysvar,
  13. };
  14. entrypoint!(process_instruction);
  15. fn process_instruction(
  16. _program_id: &Pubkey,
  17. accounts: &[AccountInfo],
  18. instruction_data: &[u8],
  19. ) -> ProgramResult {
  20. let accounts_iter = &mut accounts.iter();
  21. let payer = next_account_info(accounts_iter)?;
  22. let new_account = next_account_info(accounts_iter)?;
  23. let system_program = next_account_info(accounts_iter)?;
  24. msg!("Program invoked. Creating a system account...");
  25. msg!(" New public key will be: {}", &new_account.key.to_string());
  26. // Determine the necessary minimum rent by calculating the account's size
  27. //
  28. let account_span = instruction_data.len();
  29. let lamports_required = (Rent::get()?).minimum_balance(account_span);
  30. msg!("Account span: {}", &account_span);
  31. msg!("Lamports required: {}", &lamports_required);
  32. invoke(
  33. &system_instruction::create_account(
  34. &payer.key,
  35. &new_account.key,
  36. lamports_required,
  37. account_span as u64,
  38. &system_program::ID,
  39. ),
  40. &[
  41. payer.clone(), new_account.clone(), system_program.clone()
  42. ]
  43. )?;
  44. msg!("Account created succesfully.");
  45. Ok(())
  46. }