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
impl PackedAccounts
pub fn new_with_system_accounts(config: SystemAccountMetaConfig) -> Result<Self>
pub fn system_accounts_set(&self) -> bool
pub fn add_pre_accounts_signer(&mut self, pubkey: Pubkey)
pub fn add_pre_accounts_signer_mut(&mut self, pubkey: Pubkey)
pub fn add_pre_accounts_meta(&mut self, account_meta: AccountMeta)
pub fn add_pre_accounts_metas(&mut self, account_metas: &[AccountMeta])
Sourcepub fn add_system_accounts(
&mut self,
config: SystemAccountMetaConfig,
) -> Result<()>
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)?;Sourcepub fn insert_or_get(&mut self, pubkey: Pubkey) -> u8
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.
pub fn insert_or_get_read_only(&mut self, pubkey: Pubkey) -> u8
pub fn insert_or_get_config( &mut self, pubkey: Pubkey, is_signer: bool, is_writable: bool, ) -> u8
Sourcepub fn to_account_metas(&self) -> (Vec<AccountMeta>, usize, usize)
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.