lib.rs 1.4 KB

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