1#[cfg(feature = "spl")]
2use solana_program::program_pack::Pack;
3use solana_program::{
4 account_info::AccountInfo, program_error::ProgramError, pubkey::Pubkey, system_program, sysvar,
5};
6#[cfg(feature = "spl")]
7use spl_token::state::Mint;
8
9pub fn load_signer(info: &AccountInfo<'_>) -> Result<(), ProgramError> {
12 if !info.is_signer {
13 return Err(ProgramError::MissingRequiredSignature);
14 }
15
16 Ok(())
17}
18
19pub fn load_uninitialized_pda(
23 info: &AccountInfo<'_>,
24 seeds: &[&[u8]],
25 bump: u8,
26 program_id: &Pubkey,
27) -> Result<(), ProgramError> {
28 let pda = Pubkey::find_program_address(seeds, program_id);
29
30 if info.key.ne(&pda.0) {
31 return Err(ProgramError::InvalidSeeds);
32 }
33
34 if bump.ne(&pda.1) {
35 return Err(ProgramError::InvalidSeeds);
36 }
37
38 load_system_account(info, true)
39}
40
41pub fn load_system_account(info: &AccountInfo<'_>, is_writable: bool) -> Result<(), ProgramError> {
46 if info.owner.ne(&system_program::id()) {
47 return Err(ProgramError::InvalidAccountOwner);
48 }
49
50 if !info.data_is_empty() {
51 return Err(ProgramError::AccountAlreadyInitialized);
52 }
53
54 if is_writable && !info.is_writable {
55 return Err(ProgramError::InvalidAccountData);
56 }
57
58 Ok(())
59}
60
61pub fn load_sysvar(info: &AccountInfo<'_>, key: Pubkey) -> Result<(), ProgramError> {
65 if info.owner.ne(&sysvar::id()) {
66 return Err(ProgramError::InvalidAccountOwner);
67 }
68
69 load_account(info, key, false)
70}
71
72pub fn load_account(
76 info: &AccountInfo<'_>,
77 key: Pubkey,
78 is_writable: bool,
79) -> Result<(), ProgramError> {
80 if info.key.ne(&key) {
81 return Err(ProgramError::InvalidAccountData);
82 }
83
84 if is_writable && !info.is_writable {
85 return Err(ProgramError::InvalidAccountData);
86 }
87
88 Ok(())
89}
90
91pub fn load_program(info: &AccountInfo<'_>, key: Pubkey) -> Result<(), ProgramError> {
95 if info.key.ne(&key) {
96 return Err(ProgramError::IncorrectProgramId);
97 }
98
99 if !info.executable {
100 return Err(ProgramError::InvalidAccountData);
101 }
102
103 Ok(())
104}
105
106pub fn load_any(info: &AccountInfo<'_>, is_writable: bool) -> Result<(), ProgramError> {
109 if is_writable && !info.is_writable {
110 return Err(ProgramError::InvalidAccountData);
111 }
112
113 Ok(())
114}
115
116#[cfg(feature = "spl")]
123pub fn load_mint(
124 info: &AccountInfo<'_>,
125 address: Pubkey,
126 is_writable: bool,
127) -> Result<(), ProgramError> {
128 if info.owner.ne(&spl_token::id()) {
129 return Err(ProgramError::InvalidAccountOwner);
130 }
131
132 if info.key.ne(&address) {
133 return Err(ProgramError::InvalidSeeds);
134 }
135
136 if info.data_is_empty() {
137 return Err(ProgramError::UninitializedAccount);
138 }
139
140 Mint::unpack(&info.data.borrow())?;
141
142 if is_writable && !info.is_writable {
143 return Err(ProgramError::InvalidAccountData);
144 }
145
146 Ok(())
147}
148
149#[cfg(feature = "spl")]
155pub fn load_any_mint(info: &AccountInfo<'_>, is_writable: bool) -> Result<(), ProgramError> {
156 if info.owner.ne(&spl_token::id()) {
157 return Err(ProgramError::InvalidAccountOwner);
158 }
159
160 if info.data_is_empty() {
161 return Err(ProgramError::UninitializedAccount);
162 }
163
164 Mint::unpack(&info.data.borrow())?;
165
166 if is_writable && !info.is_writable {
167 return Err(ProgramError::InvalidAccountData);
168 }
169
170 Ok(())
171}
172
173#[cfg(feature = "spl")]
181pub fn load_token_account(
182 info: &AccountInfo<'_>,
183 owner: Option<&Pubkey>,
184 mint: &Pubkey,
185 is_writable: bool,
186) -> Result<(), ProgramError> {
187 if info.owner.ne(&spl_token::id()) {
188 return Err(ProgramError::InvalidAccountOwner);
189 }
190
191 if info.data_is_empty() {
192 return Err(ProgramError::UninitializedAccount);
193 }
194
195 let account_data = info.data.borrow();
196 let account = spl_token::state::Account::unpack(&account_data)?;
197
198 if account.mint.ne(&mint) {
199 return Err(ProgramError::InvalidAccountData);
200 }
201
202 if let Some(owner) = owner {
203 if account.owner.ne(owner) {
204 return Err(ProgramError::InvalidAccountData);
205 }
206 }
207
208 if is_writable && !info.is_writable {
209 return Err(ProgramError::InvalidAccountData);
210 }
211
212 Ok(())
213}
214
215#[cfg(feature = "spl")]
222pub fn load_associated_token_account(
223 info: &AccountInfo<'_>,
224 owner: &Pubkey,
225 mint: &Pubkey,
226 is_writable: bool,
227) -> Result<(), ProgramError> {
228 if info.owner.ne(&spl_token::id()) {
229 return Err(ProgramError::InvalidAccountOwner);
230 }
231
232 if info.data_is_empty() {
233 return Err(ProgramError::UninitializedAccount);
234 }
235
236 let account_data = info.data.borrow();
237 let _ = spl_token::state::Account::unpack(&account_data)?;
238
239 let address = spl_associated_token_account::get_associated_token_address(owner, mint);
240 if info.key.ne(&address) {
241 return Err(ProgramError::InvalidSeeds);
242 }
243
244 if is_writable && !info.is_writable {
245 return Err(ProgramError::InvalidAccountData);
246 }
247
248 Ok(())
249}