Skip to main content

light_sdk/instruction/
packed_accounts.rs

1use std::ops::{Deref, DerefMut};
2
3use super::system_accounts::{get_light_system_account_metas, SystemAccountMetaConfig};
4
5type Inner = light_sdk_types::pack_accounts::PackedAccounts<solana_instruction::AccountMeta>;
6
7/// Packs accounts and creates indices for instruction building (client-side).
8///
9/// Wraps the generic `PackedAccounts<AccountMeta>` from sdk-types with
10/// Solana-specific system account helpers as inherent methods.
11#[derive(Debug, Default)]
12pub struct PackedAccounts(pub Inner);
13
14impl Deref for PackedAccounts {
15    type Target = Inner;
16    fn deref(&self) -> &Self::Target {
17        &self.0
18    }
19}
20
21impl DerefMut for PackedAccounts {
22    fn deref_mut(&mut self) -> &mut Self::Target {
23        &mut self.0
24    }
25}
26
27impl From<Inner> for PackedAccounts {
28    fn from(inner: Inner) -> Self {
29        Self(inner)
30    }
31}
32
33impl From<PackedAccounts> for Inner {
34    fn from(wrapper: PackedAccounts) -> Self {
35        wrapper.0
36    }
37}
38
39impl PackedAccounts {
40    /// Creates a new [`PackedAccounts`] with v1 system accounts pre-configured.
41    ///
42    /// **Use with [`cpi::v1::CpiAccounts`](crate::cpi::v1::CpiAccounts) on the program side.**
43    pub fn new_with_system_accounts(config: SystemAccountMetaConfig) -> crate::error::Result<Self> {
44        let mut accounts = Self::default();
45        accounts.add_system_accounts(config)?;
46        Ok(accounts)
47    }
48
49    /// Adds v1 Light system program accounts to the account list.
50    ///
51    /// **Use with [`cpi::v1::CpiAccounts`](crate::cpi::v1::CpiAccounts) on the program side.**
52    ///
53    /// This adds all the accounts required by the Light system program for v1 operations,
54    /// including the CPI authority, registered programs, account compression program, and Noop program.
55    pub fn add_system_accounts(
56        &mut self,
57        config: SystemAccountMetaConfig,
58    ) -> crate::error::Result<()> {
59        self.0
60            .add_system_accounts_raw(get_light_system_account_metas(config));
61        Ok(())
62    }
63
64    /// Adds v2 Light system program accounts to the account list.
65    ///
66    /// **Use with [`cpi::v2::CpiAccounts`](crate::cpi::v2::CpiAccounts) on the program side.**
67    ///
68    /// This adds all the accounts required by the Light system program for v2 operations.
69    /// V2 uses a different account layout optimized for batched state trees.
70    #[cfg(feature = "v2")]
71    pub fn add_system_accounts_v2(
72        &mut self,
73        config: SystemAccountMetaConfig,
74    ) -> crate::error::Result<()> {
75        self.0
76            .add_system_accounts_raw(super::system_accounts::get_light_system_account_metas_v2(
77                config,
78            ));
79        Ok(())
80    }
81}