PackedAccounts

Struct PackedAccounts 

Source
pub struct PackedAccounts {
    pub pre_accounts: Vec<AccountMeta>,
    /* private fields */
}
Expand description

Builder for organizing accounts into compressed account instructions.

Manages three categories of accounts:

  • Pre-accounts: Signers and other custom accounts that come before system accounts.
  • System accounts: Light system program accounts (authority, trees, queues).
  • Packed accounts: Dynamically tracked deduplicted accounts.

§Example

let mut accounts = PackedAccounts::default();

// Add signer
accounts.add_pre_accounts_signer(payer_pubkey);

// Add system accounts (use v2 if feature is enabled)
let config = SystemAccountMetaConfig::new(program_id);
#[cfg(feature = "v2")]
accounts.add_system_accounts_v2(config)?;
#[cfg(not(feature = "v2"))]
accounts.add_system_accounts(config)?;

// Add and track tree accounts
let tree_index = accounts.insert_or_get(merkle_tree_pubkey);

// Get final account metas
let (metas, system_offset, tree_offset) = accounts.to_account_metas();

Fields§

§pre_accounts: Vec<AccountMeta>

Accounts that must come before system accounts (e.g., signers, fee payer).

Implementations§

Source§

impl PackedAccounts

Source

pub fn new_with_system_accounts(config: SystemAccountMetaConfig) -> Result<Self>

Source

pub fn system_accounts_set(&self) -> bool

Source

pub fn add_pre_accounts_signer(&mut self, pubkey: Pubkey)

Source

pub fn add_pre_accounts_signer_mut(&mut self, pubkey: Pubkey)

Source

pub fn add_pre_accounts_meta(&mut self, account_meta: AccountMeta)

Source

pub fn add_pre_accounts_metas(&mut self, account_metas: &[AccountMeta])

Source

pub fn add_system_accounts( &mut self, config: SystemAccountMetaConfig, ) -> Result<()>

Adds v1 Light system program accounts to the account list.

Use with cpi::v1::CpiAccounts on the program side.

This adds all the accounts required by the Light system program for v1 operations, including the CPI authority, registered programs, account compression program, and Noop program.

§Example
let mut accounts = PackedAccounts::default();
let config = SystemAccountMetaConfig::new(program_id);
accounts.add_system_accounts(config)?;
Source

pub fn insert_or_get(&mut self, pubkey: Pubkey) -> u8

Returns the index of the provided pubkey in the collection.

If the provided pubkey is not a part of the collection, it gets inserted with a next_index.

If the privided pubkey already exists in the collection, its already existing index is returned.

Source

pub fn insert_or_get_read_only(&mut self, pubkey: Pubkey) -> u8

Source

pub fn insert_or_get_config( &mut self, pubkey: Pubkey, is_signer: bool, is_writable: bool, ) -> u8

Source

pub fn to_account_metas(&self) -> (Vec<AccountMeta>, usize, usize)

Converts the collection of accounts to a vector of AccountMeta, which can be used as remaining accounts in instructions or CPI calls.

§Returns

A tuple of (account_metas, system_accounts_offset, packed_accounts_offset):

  • account_metas: All accounts concatenated in order: [pre_accounts][system_accounts][packed_accounts]
  • system_accounts_offset: Index where system accounts start (= pre_accounts.len())
  • packed_accounts_offset: Index where packed accounts start (= pre_accounts.len() + system_accounts.len())

The system_accounts_offset can be used to slice the accounts when creating CpiAccounts:

let accounts_for_cpi = &ctx.remaining_accounts[system_accounts_offset..];
let cpi_accounts = CpiAccounts::new(fee_payer, accounts_for_cpi, cpi_signer)?;

The offset can be hardcoded if your program always has the same pre-accounts layout, or passed as a field in your instruction data.

Source

pub fn packed_pubkeys(&self) -> Vec<Pubkey>

Source

pub fn add_custom_system_accounts<T: AccountMetasVec>( &mut self, accounts: T, ) -> Result<()>

Trait Implementations§

Source§

impl Debug for PackedAccounts

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for PackedAccounts

Source§

fn default() -> PackedAccounts

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.