-
Notifications
You must be signed in to change notification settings - Fork 165
system: Add create account helper #282
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
| @@ -1,5 +1,71 @@ | ||
| #![no_std] | ||
|
|
||
| use pinocchio::{ | ||
| account_info::AccountInfo, | ||
| instruction::Signer, | ||
| pubkey::Pubkey, | ||
| sysvars::{rent::Rent, Sysvar}, | ||
| ProgramResult, | ||
| }; | ||
|
|
||
| use crate::instructions::{Assign, CreateAccount, Transfer}; | ||
|
|
||
| pub mod instructions; | ||
|
|
||
| pinocchio_pubkey::declare_id!("11111111111111111111111111111111"); | ||
|
|
||
| /// Create an account with a minimal balance to be rent-exempt. | ||
| /// | ||
| /// The account will be funded by the `payer` if its current lamports | ||
| /// are insufficient for rent-exemption. | ||
| #[inline(always)] | ||
| pub fn create_account_with_minimal_balance( | ||
| account: &AccountInfo, | ||
| space: usize, | ||
| owner: &Pubkey, | ||
| payer: &AccountInfo, | ||
| signers: &[Signer], | ||
| rent_sysvar: Option<&AccountInfo>, | ||
| ) -> ProgramResult { | ||
| let lamports = if let Some(rent_sysvar) = rent_sysvar { | ||
| let rent = Rent::from_account_info(rent_sysvar)?; | ||
| rent.minimum_balance(space) | ||
| } else { | ||
| Rent::get()?.minimum_balance(space) | ||
| }; | ||
|
|
||
| if account.lamports() == 0 { | ||
| // Create the account if it does not exist. | ||
| CreateAccount { | ||
febo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| from: payer, | ||
| to: account, | ||
| lamports, | ||
| space: space as u64, | ||
| owner, | ||
| } | ||
| .invoke_signed(signers) | ||
| } else { | ||
febo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let required_lamports = lamports.saturating_sub(account.lamports()); | ||
|
|
||
| // Transfer lamports from `payer` to `account` if needed. | ||
| if required_lamports > 0 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the same here can be applied, i believe.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing here, no difference. |
||
| Transfer { | ||
| from: payer, | ||
| to: account, | ||
| lamports: required_lamports, | ||
| } | ||
| .invoke()?; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we make this |
||
| } | ||
|
|
||
| // Assign the account to the specified owner. | ||
| Assign { account, owner }.invoke_signed(signers)?; | ||
|
|
||
| // Allocate the required space for the account. | ||
| // | ||
| // SAFETY: There are no active borrows of the `account`. | ||
| // This was checked by the `Assign` CPI above. | ||
| unsafe { account.resize_unchecked(space)? }; | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what do you think about using hints from
pinocchio::hintmodule?i expect account is not exist, in the majority of cases
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It made no difference in my tests. Also, I had a look at the asm and it seems the compiler is doing the right thing already. Did you notice a difference?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the same for the lib (i didn't test it in the real app).
As a part of library it compiles in the expected instructions.
Just wanted to make extra sure this logic won't be miscompiled in the large program.