Skip to content
23 changes: 22 additions & 1 deletion programs/system/src/instructions/create_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use pinocchio::{
instruction::{AccountMeta, Instruction, Signer},
program::invoke_signed,
pubkey::Pubkey,
sysvars::rent::Rent,
program_error::ProgramError,
ProgramResult,
};

Expand All @@ -28,7 +30,26 @@ pub struct CreateAccount<'a> {
pub owner: &'a Pubkey,
}

impl CreateAccount<'_> {
impl<'a> CreateAccount<'a> {
pub fn with_rent_check(
from: &'a AccountInfo,
to: &'a AccountInfo,
rent_sysvar: &'a AccountInfo,
space: u64,
owner: &'a Pubkey,
) -> Result<Self, ProgramError> {
let rent = Rent::from_account_info(rent_sysvar)?;
let lamports = rent.minimum_balance(space as usize);

Ok(Self {
from,
to,
lamports,
space,
owner,
})
}

#[inline(always)]
pub fn invoke(&self) -> ProgramResult {
self.invoke_signed(&[])
Expand Down
27 changes: 26 additions & 1 deletion programs/system/src/instructions/create_account_with_seed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use pinocchio::{
account_info::AccountInfo,
instruction::{AccountMeta, Instruction, Signer},
program::invoke_signed,
program_error::ProgramError,
pubkey::Pubkey,
sysvars::rent::Rent,
ProgramResult,
};

Expand Down Expand Up @@ -40,7 +42,30 @@ pub struct CreateAccountWithSeed<'a, 'b, 'c> {
pub owner: &'c Pubkey,
}

impl CreateAccountWithSeed<'_, '_, '_> {
impl<'a, 'b, 'c> CreateAccountWithSeed<'a, 'b, 'c> {
pub fn with_rent_check(
from: &'a AccountInfo,
to: &'a AccountInfo,
base: Option<&'a AccountInfo>,
seed: &'b str,
rent_sysvar: &'a AccountInfo,
space: u64,
owner: &'c Pubkey,
) -> Result<Self, ProgramError> {
let rent = Rent::from_account_info(rent_sysvar)?;
let lamports = rent.minimum_balance(space as usize);

Ok(Self {
from,
to,
base,
seed,
lamports,
space,
owner,
})
}

#[inline(always)]
pub fn invoke(&self) -> ProgramResult {
self.invoke_signed(&[])
Expand Down
Loading