pub struct CreateAccount<'a, 'b: 'a> {
pub payer: CpiAuthority<'a, 'b>,
pub to: CpiAuthority<'a, 'b>,
pub program_id: &'a Pubkey,
pub space: Option<usize>,
pub lamports: Option<u64>,
}Expand description
Arguments to create an account reliably. If the account already has lamports, it will be topped up to the required rent, allocated with the specified amount of space and assigned to the specified program.
§Examples
use sealevel_tools::{
account_info::{
try_next_enumerated_account, AccountInfoConstraints, Payer, Program,
WritableAccount,
},
cpi::system_program::CreateAccount,
entrypoint::{NoStdAccountInfo, ProgramResult},
pubkey::Pubkey,
};
fn process_instruction(
program_id: &Pubkey,
accounts: &[NoStdAccountInfo],
instruction_data: &[u8],
) -> ProgramResult {
let mut accounts_iter = accounts.iter().enumerate();
// Next account must writable signer (A.K.A. our payer).
let (_, payer) =
try_next_enumerated_account::<Payer>(&mut accounts_iter, Default::default())?;
let (new_thing_addr, new_thing_bump) =
Pubkey::find_program_address(&[b"thing"], program_id);
// Next account must be writable data account matching PDA address.
let (_, new_account) = try_next_enumerated_account::<WritableAccount>(
&mut accounts_iter,
AccountInfoConstraints {
key: Some(&new_thing_addr),
..Default::default()
},
)?;
CreateAccount {
payer: payer.as_cpi_authority(),
to: new_account.as_cpi_authority(Some(&[b"thing", &[new_thing_bump]])),
program_id,
space: Some(16),
lamports: None,
}
.try_into_invoke()?;
Ok(())
}Use Self::try_invoke_and_serialize to create a new data account and serialize data to it.
use sealevel_tools::{
borsh::{BorshDeserialize, BorshSerialize},
account::{AccountSerde, BorshAccountSchema},
account_info::{
try_next_enumerated_account, AccountInfoConstraints, Payer, Program,
WritableAccount,
},
cpi::system_program::CreateAccount,
discriminator::{Discriminate, Discriminator},
entrypoint::{NoStdAccountInfo, ProgramResult},
pubkey::Pubkey,
};
#[derive(Debug, BorshDeserialize, BorshSerialize)]
pub struct Thing {
pub data: u64,
}
impl Discriminate<4> for Thing {
const DISCRIMINATOR: [u8; 4] = Discriminator::Sha2(b"Thing").to_bytes();
}
fn process_instruction(
program_id: &Pubkey,
accounts: &[NoStdAccountInfo],
instruction_data: &[u8],
) -> ProgramResult {
let mut accounts_iter = accounts.iter().enumerate();
// Next account must writable signer (A.K.A. our payer).
let (_, payer) =
try_next_enumerated_account::<Payer>(&mut accounts_iter, Default::default())?;
let (new_thing_addr, new_thing_bump) =
Pubkey::find_program_address(&[b"thing"], program_id);
// Next account must be writable data account matching PDA address.
let (_, new_account) = try_next_enumerated_account::<WritableAccount>(
&mut accounts_iter,
AccountInfoConstraints {
key: Some(&new_thing_addr),
..Default::default()
},
)?;
let thing = BorshAccountSchema(Thing { data: 420 });
CreateAccount {
payer: payer.as_cpi_authority(),
to: new_account.as_cpi_authority(Some(&[b"thing", &[new_thing_bump]])),
program_id,
space: None,
lamports: None,
}
.try_invoke_and_serialize(&thing)?;
Ok(())
}Fields§
§payer: CpiAuthority<'a, 'b>The account that will pay for the rent.
§Notes
Pass in None for CpiAuthority::signer_seeds if the payer is passed in as a signer.
to: CpiAuthority<'a, 'b>The account to be created.
§Notes
Pass in None for CpiAuthority::signer_seeds if the account is passed in as a random keypair.
program_id: &'a PubkeyThe program to assign the account to.
space: Option<usize>The space to allocate for the account. If None, defaults to zero for Self::try_into_invoke and will be determined by AccountSerde::try_account_space for Self::try_invoke_and_serialize.
lamports: Option<u64>Implementations§
Source§impl<'a, 'b: 'a> CreateAccount<'a, 'b>
impl<'a, 'b: 'a> CreateAccount<'a, 'b>
Sourcepub fn try_into_invoke(self) -> Result<Account<'b, true>, ProgramError>
pub fn try_into_invoke(self) -> Result<Account<'b, true>, ProgramError>
Try to consume arguments to perform CPI calls.
Sourcepub fn try_invoke_and_serialize<const DISC_LEN: usize, T: AccountSerde<DISC_LEN>>(
self,
account_data: &T,
) -> Result<Account<'a, true>, ProgramError>
pub fn try_invoke_and_serialize<const DISC_LEN: usize, T: AccountSerde<DISC_LEN>>( self, account_data: &T, ) -> Result<Account<'a, true>, ProgramError>
Try to consume arguments to create a new data account and serialize data to it using the account’s implemented AccountSerde, which includes its discriminator. This method uses Self::try_into_invoke to create the account and then serializes the data to this account.
The space to allocate for the account. If not specified, the space will be determined by AccountSerde::try_account_space.
Trait Implementations§
Source§impl<'a, 'b: 'a> Clone for CreateAccount<'a, 'b>
impl<'a, 'b: 'a> Clone for CreateAccount<'a, 'b>
impl<'a, 'b: 'a> Eq for CreateAccount<'a, 'b>
Source§impl<'a, 'b: 'a> PartialEq for CreateAccount<'a, 'b>
impl<'a, 'b: 'a> PartialEq for CreateAccount<'a, 'b>
impl<'a, 'b: 'a> StructuralPartialEq for CreateAccount<'a, 'b>
Auto Trait Implementations§
impl<'a, 'b> !Send for CreateAccount<'a, 'b>
impl<'a, 'b> !Sync for CreateAccount<'a, 'b>
impl<'a, 'b> Freeze for CreateAccount<'a, 'b>
impl<'a, 'b> RefUnwindSafe for CreateAccount<'a, 'b>
impl<'a, 'b> Unpin for CreateAccount<'a, 'b>
impl<'a, 'b> UnsafeUnpin for CreateAccount<'a, 'b>
impl<'a, 'b> UnwindSafe for CreateAccount<'a, 'b>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more