-
Notifications
You must be signed in to change notification settings - Fork 165
Use Create Account Checked & Create Account with Seed Checked #204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
48f6f6f
e4e9837
01cef1f
c709b11
c518668
3305c0c
68cf5be
4d1d45f
f5088a7
39eb37d
96029a2
21b9838
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,8 @@ use pinocchio::{ | |
| instruction::{AccountMeta, Instruction, Signer}, | ||
| program::invoke_signed, | ||
| pubkey::Pubkey, | ||
| sysvars::rent::Rent, | ||
| program_error::ProgramError, | ||
| ProgramResult, | ||
| }; | ||
|
|
||
|
|
@@ -28,7 +30,34 @@ 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); | ||
|
|
||
| if from.lamports() < lamports { | ||
| return Err(ProgramError::InsufficientFunds); | ||
| } | ||
|
|
||
| if !to.data_is_empty() { | ||
| return Err(ProgramError::InvalidAccountData); | ||
| } | ||
|
||
|
|
||
| Ok(Self { | ||
| from, | ||
| to, | ||
| lamports, | ||
| space, | ||
| owner, | ||
| }) | ||
| } | ||
|
|
||
| #[inline(always)] | ||
| pub fn invoke(&self) -> ProgramResult { | ||
| self.invoke_signed(&[]) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,9 @@ use pinocchio::{ | |
| account_info::AccountInfo, | ||
| instruction::{AccountMeta, Instruction, Signer}, | ||
| program::invoke_signed, | ||
| pubkey::Pubkey, | ||
| program_error::ProgramError, | ||
| pubkey::{Pubkey, MAX_SEED_LEN}, | ||
| sysvars::rent::Rent, | ||
| ProgramResult, | ||
| }; | ||
|
|
||
|
|
@@ -40,7 +42,42 @@ 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( | ||
zubayr1 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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); | ||
|
|
||
| if seed.len() > MAX_SEED_LEN { | ||
| return Err(ProgramError::InvalidInstructionData); | ||
| } | ||
|
|
||
| if from.lamports() < lamports { | ||
| return Err(ProgramError::InsufficientFunds); | ||
| } | ||
|
|
||
| if !to.data_is_empty() { | ||
| return Err(ProgramError::InvalidAccountData); | ||
| } | ||
|
||
|
|
||
| Ok(Self { | ||
| from, | ||
| to, | ||
| base, | ||
| seed, | ||
| lamports, | ||
| space, | ||
| owner, | ||
| }) | ||
| } | ||
|
|
||
| #[inline(always)] | ||
| pub fn invoke(&self) -> ProgramResult { | ||
| self.invoke_signed(&[]) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.