(
program_id: &Pubkey,
signer_account: &'a AccountInfo<'b>,
passed_in_pda: &'a AccountInfo<'b>,
system_program: &'a AccountInfo<'b>,
rent: &Rent,
_instruction_data: &[u8],
| 266 | } |
| 267 | |
| 268 | pub fn create_and_check_pda<'a, 'b>( |
| 269 | program_id: &Pubkey, |
| 270 | signer_account: &'a AccountInfo<'b>, |
| 271 | passed_in_pda: &'a AccountInfo<'b>, |
| 272 | system_program: &'a AccountInfo<'b>, |
| 273 | rent: &Rent, |
| 274 | _instruction_data: &[u8], |
| 275 | domain_separation_seed: &[u8], |
| 276 | number_storage_bytes: u64, |
| 277 | lamports: u64, |
| 278 | rent_exempt: bool, |
| 279 | ) -> Result<(), ProgramError> { |
| 280 | let derived_pubkey = |
| 281 | Pubkey::find_program_address(&[_instruction_data, domain_separation_seed], program_id); |
| 282 | |
| 283 | if derived_pubkey.0 != *passed_in_pda.key { |
| 284 | msg!("Passed-in pda pubkey != on-chain derived pda pubkey."); |
| 285 | msg!("On-chain derived pda pubkey {:?}", derived_pubkey); |
| 286 | msg!("Passed-in pda pubkey {:?}", *passed_in_pda.key); |
| 287 | msg!("Instruction data seed {:?}", _instruction_data); |
| 288 | return Err(ProgramError::InvalidInstructionData); |
| 289 | } |
| 290 | |
| 291 | let mut account_lamports = lamports; |
| 292 | if rent_exempt { |
| 293 | account_lamports += rent.minimum_balance(number_storage_bytes.try_into().unwrap()); |
| 294 | } else { |
| 295 | account_lamports += rent.minimum_balance(number_storage_bytes.try_into().unwrap()) / 365; |
| 296 | } |
| 297 | msg!("account_lamports: {}", account_lamports); |
| 298 | invoke_signed( |
| 299 | &system_instruction::create_account( |
| 300 | signer_account.key, // from_pubkey |
| 301 | passed_in_pda.key, // to_pubkey |
| 302 | account_lamports, // lamports |
| 303 | number_storage_bytes, // space |
| 304 | program_id, // owner |
| 305 | ), |
| 306 | &[ |
| 307 | signer_account.clone(), |
| 308 | passed_in_pda.clone(), |
| 309 | system_program.clone(), |
| 310 | ], |
| 311 | &[&[ |
| 312 | _instruction_data, |
| 313 | domain_separation_seed, |
| 314 | &[derived_pubkey.1], |
| 315 | ]], |
| 316 | )?; |
| 317 | |
| 318 | // Check for rent exemption |
| 319 | if rent_exempt |
| 320 | && !rent.is_exempt( |
| 321 | **passed_in_pda.lamports.borrow(), |
| 322 | number_storage_bytes.try_into().unwrap(), |
| 323 | ) |
| 324 | { |
| 325 | msg!("Account is not rent exempt."); |
no outgoing calls
no test coverage detected