lib.rs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #![allow(clippy::result_large_err)]
  2. use anchor_lang::prelude::*;
  3. use anchor_lang::system_program::{create_account, CreateAccount};
  4. declare_id!("ARVNCsYKDQsCLHbwUTJLpFXVrJdjhWZStyzvxmKe2xHi");
  5. #[program]
  6. pub mod create_system_account {
  7. use super::*;
  8. pub fn create_system_account(ctx: Context<CreateSystemAccount>) -> Result<()> {
  9. msg!("Program invoked. Creating a system account...");
  10. msg!(
  11. " New public key will be: {}",
  12. &ctx.accounts.new_account.key().to_string()
  13. );
  14. // The minimum lamports for rent exemption
  15. let lamports = (Rent::get()?).minimum_balance(0);
  16. create_account(
  17. CpiContext::new(
  18. ctx.accounts.system_program.to_account_info(),
  19. CreateAccount {
  20. from: ctx.accounts.payer.to_account_info(), // From pubkey
  21. to: ctx.accounts.new_account.to_account_info(), // To pubkey
  22. },
  23. ),
  24. lamports, // Lamports
  25. 0, // Space
  26. &ctx.accounts.system_program.key(), // Owner Program
  27. )?;
  28. msg!("Account created succesfully.");
  29. Ok(())
  30. }
  31. }
  32. #[derive(Accounts)]
  33. pub struct CreateSystemAccount<'info> {
  34. #[account(mut)]
  35. pub payer: Signer<'info>,
  36. #[account(mut)]
  37. pub new_account: Signer<'info>,
  38. pub system_program: Program<'info, System>,
  39. }