light_sdk/instruction/
system_accounts.rs

1use light_sdk_types::constants::{
2    ACCOUNT_COMPRESSION_PROGRAM_ID, CPI_AUTHORITY_PDA_SEED, LIGHT_SYSTEM_PROGRAM_ID,
3    NOOP_PROGRAM_ID,
4};
5
6use crate::{find_cpi_signer_macro, AccountMeta, Pubkey};
7
8#[derive(Debug, Default, Copy, Clone)]
9pub struct SystemAccountMetaConfig {
10    pub self_program: Pubkey,
11    pub cpi_context: Option<Pubkey>,
12    pub sol_compression_recipient: Option<Pubkey>,
13    pub sol_pool_pda: Option<Pubkey>,
14}
15
16impl SystemAccountMetaConfig {
17    pub fn new(self_program: Pubkey) -> Self {
18        Self {
19            self_program,
20            cpi_context: None,
21            sol_compression_recipient: None,
22            sol_pool_pda: None,
23        }
24    }
25
26    pub fn new_with_cpi_context(self_program: Pubkey, cpi_context: Pubkey) -> Self {
27        Self {
28            self_program,
29            cpi_context: Some(cpi_context),
30            sol_compression_recipient: None,
31            sol_pool_pda: None,
32        }
33    }
34}
35
36#[derive(Debug, Copy, Clone)]
37pub struct SystemAccountPubkeys {
38    pub light_sytem_program: Pubkey,
39    pub system_program: Pubkey,
40    pub account_compression_program: Pubkey,
41    pub account_compression_authority: Pubkey,
42    pub registered_program_pda: Pubkey,
43    pub noop_program: Pubkey,
44    pub sol_pool_pda: Pubkey,
45}
46
47impl Default for SystemAccountPubkeys {
48    fn default() -> Self {
49        Self {
50            light_sytem_program: Pubkey::from(LIGHT_SYSTEM_PROGRAM_ID),
51            system_program: Pubkey::default(),
52            account_compression_program: Pubkey::from(ACCOUNT_COMPRESSION_PROGRAM_ID),
53            account_compression_authority: Pubkey::find_program_address(
54                &[CPI_AUTHORITY_PDA_SEED],
55                &Pubkey::from(LIGHT_SYSTEM_PROGRAM_ID),
56            )
57            .0,
58            registered_program_pda: Pubkey::find_program_address(
59                &[LIGHT_SYSTEM_PROGRAM_ID.as_slice()],
60                &Pubkey::from(ACCOUNT_COMPRESSION_PROGRAM_ID),
61            )
62            .0,
63            noop_program: Pubkey::from(NOOP_PROGRAM_ID),
64            // TODO: add correct pubkey
65            sol_pool_pda: Pubkey::default(),
66        }
67    }
68}
69
70pub fn get_light_system_account_metas(config: SystemAccountMetaConfig) -> Vec<AccountMeta> {
71    let cpi_signer = find_cpi_signer_macro!(&config.self_program).0;
72    let default_pubkeys = SystemAccountPubkeys::default();
73
74    let mut vec = vec![
75        AccountMeta::new_readonly(default_pubkeys.light_sytem_program, false),
76        AccountMeta::new_readonly(cpi_signer, false),
77        AccountMeta::new_readonly(default_pubkeys.registered_program_pda, false),
78        AccountMeta::new_readonly(default_pubkeys.noop_program, false),
79        AccountMeta::new_readonly(default_pubkeys.account_compression_authority, false),
80        AccountMeta::new_readonly(default_pubkeys.account_compression_program, false),
81        AccountMeta::new_readonly(config.self_program, false),
82    ];
83
84    if let Some(pubkey) = config.sol_pool_pda {
85        vec.push(AccountMeta {
86            pubkey,
87            is_signer: false,
88            is_writable: true,
89        });
90    }
91    if let Some(pubkey) = config.sol_compression_recipient {
92        vec.push(AccountMeta {
93            pubkey,
94            is_signer: false,
95            is_writable: true,
96        });
97    }
98    vec.push(AccountMeta::new_readonly(
99        default_pubkeys.system_program,
100        false,
101    ));
102    if let Some(pubkey) = config.cpi_context {
103        vec.push(AccountMeta {
104            pubkey,
105            is_signer: false,
106            is_writable: true,
107        });
108    }
109    vec
110}
111
112/// Can be used in client to add system account metas.
113///
114/// We need the program id account infos in the outer instruction.
115/// Account Metas:
116/// 1. Light System Program
117/// 2. Account Compression Program
118/// 3. System Program
119/// 4. CPI Signer
120/// 5. Registered Program PDA
121/// 6. Account Compression Authority
122#[cfg(feature = "v2")]
123pub fn get_light_system_account_metas_small(config: SystemAccountMetaConfig) -> Vec<AccountMeta> {
124    let cpi_signer = find_cpi_signer_macro!(&config.self_program).0;
125    let default_pubkeys = SystemAccountPubkeys::default();
126
127    let mut vec = vec![
128        AccountMeta::new_readonly(default_pubkeys.light_sytem_program, false),
129        AccountMeta::new_readonly(default_pubkeys.account_compression_program, false),
130        AccountMeta::new_readonly(default_pubkeys.system_program, false),
131        AccountMeta::new_readonly(cpi_signer, false),
132        AccountMeta::new_readonly(default_pubkeys.registered_program_pda, false),
133        AccountMeta::new_readonly(default_pubkeys.account_compression_authority, false),
134    ];
135
136    if let Some(pubkey) = config.sol_pool_pda {
137        vec.push(AccountMeta {
138            pubkey,
139            is_signer: false,
140            is_writable: true,
141        });
142    }
143    if let Some(pubkey) = config.sol_compression_recipient {
144        vec.push(AccountMeta {
145            pubkey,
146            is_signer: false,
147            is_writable: true,
148        });
149    }
150    if let Some(pubkey) = config.cpi_context {
151        vec.push(AccountMeta {
152            pubkey,
153            is_signer: false,
154            is_writable: true,
155        });
156    }
157    vec
158}