|
| 1 | +use solana_program::msg; |
| 2 | +use steel::*; |
| 3 | + |
| 4 | +pub fn process_check_accounts(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult { |
| 5 | + // Load accounts. |
| 6 | + // You can verify the list has the correct number of accounts. |
| 7 | + let [signer_info, account_to_create_info, account_to_change_info, system_program] = accounts |
| 8 | + else { |
| 9 | + return Err(ProgramError::NotEnoughAccountKeys); |
| 10 | + }; |
| 11 | + |
| 12 | + // You can verify if an account is a signer |
| 13 | + signer_info.is_signer()?; |
| 14 | + |
| 15 | + // You can verify the program ID from the instruction is in fact |
| 16 | + // the program ID of your program. |
| 17 | + if system_program.is_program(&system_program::ID).is_err() { |
| 18 | + return Err(ProgramError::IncorrectProgramId); |
| 19 | + }; |
| 20 | + |
| 21 | + // You can make sure an account has NOT been initialized. |
| 22 | + |
| 23 | + msg!("New account: {}", account_to_create_info.key); |
| 24 | + if account_to_create_info.lamports() != 0 { |
| 25 | + msg!("The program expected the account to create to not yet be initialized."); |
| 26 | + return Err(ProgramError::AccountAlreadyInitialized); |
| 27 | + }; |
| 28 | + // (Create account...) |
| 29 | + |
| 30 | + // You can also make sure an account has been initialized. |
| 31 | + msg!("Account to change: {}", account_to_change_info.key); |
| 32 | + if account_to_change_info.lamports() == 0 { |
| 33 | + msg!("The program expected the account to change to be initialized."); |
| 34 | + return Err(ProgramError::UninitializedAccount); |
| 35 | + }; |
| 36 | + |
| 37 | + // If we want to modify an account's data, it must be owned by our program. |
| 38 | + if account_to_change_info.owner != &steel_api::ID { |
| 39 | + msg!("Account to change does not have the correct program id."); |
| 40 | + return Err(ProgramError::IncorrectProgramId); |
| 41 | + }; |
| 42 | + |
| 43 | + // You can also check pubkeys against constants. |
| 44 | + if system_program.key != &system_program::ID { |
| 45 | + return Err(ProgramError::IncorrectProgramId); |
| 46 | + }; |
| 47 | + |
| 48 | + Ok(()) |
| 49 | +} |
0 commit comments