Skip to content

Commit 0f3cc2f

Browse files
zubayr1febo
andauthored
Use Create Account Checked & Create Account with Seed Checked (#204)
* use Sysvar rent account * implement invoke_signed_checked * removed the checked files * implement with_rent_check constructor * fix imports * remove checks * Apply suggestions from code review Co-authored-by: Fernando Otero <[email protected]> * fix formatting * Apply suggestions from code review Co-authored-by: Fernando Otero <[email protected]> --------- Co-authored-by: Fernando Otero <[email protected]>
1 parent 096a066 commit 0f3cc2f

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

programs/system/src/instructions/create_account.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use pinocchio::{
22
account_info::AccountInfo,
33
instruction::{AccountMeta, Instruction, Signer},
44
program::invoke_signed,
5+
program_error::ProgramError,
56
pubkey::Pubkey,
7+
sysvars::rent::Rent,
68
ProgramResult,
79
};
810

@@ -28,7 +30,27 @@ pub struct CreateAccount<'a> {
2830
pub owner: &'a Pubkey,
2931
}
3032

31-
impl CreateAccount<'_> {
33+
impl<'a> CreateAccount<'a> {
34+
#[inline(always)]
35+
pub fn with_minimal_balance(
36+
from: &'a AccountInfo,
37+
to: &'a AccountInfo,
38+
rent_sysvar: &'a AccountInfo,
39+
space: u64,
40+
owner: &'a Pubkey,
41+
) -> Result<Self, ProgramError> {
42+
let rent = Rent::from_account_info(rent_sysvar)?;
43+
let lamports = rent.minimum_balance(space as usize);
44+
45+
Ok(Self {
46+
from,
47+
to,
48+
lamports,
49+
space,
50+
owner,
51+
})
52+
}
53+
3254
#[inline(always)]
3355
pub fn invoke(&self) -> ProgramResult {
3456
self.invoke_signed(&[])

programs/system/src/instructions/create_account_with_seed.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use pinocchio::{
22
account_info::AccountInfo,
33
instruction::{AccountMeta, Instruction, Signer},
44
program::invoke_signed,
5+
program_error::ProgramError,
56
pubkey::Pubkey,
7+
sysvars::rent::Rent,
68
ProgramResult,
79
};
810

@@ -40,7 +42,31 @@ pub struct CreateAccountWithSeed<'a, 'b, 'c> {
4042
pub owner: &'c Pubkey,
4143
}
4244

43-
impl CreateAccountWithSeed<'_, '_, '_> {
45+
impl<'a, 'b, 'c> CreateAccountWithSeed<'a, 'b, 'c> {
46+
#[inline(always)]
47+
pub fn with_minimal_balance(
48+
from: &'a AccountInfo,
49+
to: &'a AccountInfo,
50+
base: Option<&'a AccountInfo>,
51+
seed: &'b str,
52+
rent_sysvar: &'a AccountInfo,
53+
space: u64,
54+
owner: &'c Pubkey,
55+
) -> Result<Self, ProgramError> {
56+
let rent = Rent::from_account_info(rent_sysvar)?;
57+
let lamports = rent.minimum_balance(space as usize);
58+
59+
Ok(Self {
60+
from,
61+
to,
62+
base,
63+
seed,
64+
lamports,
65+
space,
66+
owner,
67+
})
68+
}
69+
4470
#[inline(always)]
4571
pub fn invoke(&self) -> ProgramResult {
4672
self.invoke_signed(&[])

0 commit comments

Comments
 (0)