lib.rs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. use anchor_lang::prelude::*;
  2. use anchor_lang::system_program;
  3. declare_id!("6gUwvaZPvC8ZxKuC1h5aKz4mRd7pFyEfUZckiEsBZSbk");
  4. const LAMPORTS_PER_SOL: u64 = 1000000000;
  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!(" New public key will be: {}", &ctx.accounts.new_account.key().to_string());
  11. system_program::create_account(
  12. CpiContext::new(
  13. ctx.accounts.system_program.to_account_info(),
  14. system_program::CreateAccount {
  15. from: ctx.accounts.payer.to_account_info(), // From pubkey
  16. to: ctx.accounts.new_account.to_account_info(), // To pubkey
  17. },
  18. ),
  19. 1 * LAMPORTS_PER_SOL, // Lamports (1 SOL)
  20. 0, // Space
  21. &ctx.accounts.system_program.key(), // Owner
  22. )?;
  23. msg!("Account created succesfully.");
  24. Ok(())
  25. }
  26. }
  27. #[derive(Accounts)]
  28. pub struct CreateSystemAccount<'info> {
  29. #[account(mut)]
  30. pub payer: Signer<'info>,
  31. #[account(mut)]
  32. pub new_account: Signer<'info>,
  33. pub system_program: Program<'info, System>,
  34. }