lib.rs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. use solana_program::{
  2. account_info::{ AccountInfo, next_account_info },
  3. entrypoint,
  4. entrypoint::ProgramResult,
  5. msg,
  6. program_error::ProgramError,
  7. pubkey::Pubkey,
  8. system_program,
  9. };
  10. entrypoint!(process_instruction);
  11. fn process_instruction(
  12. program_id: &Pubkey,
  13. accounts: &[AccountInfo],
  14. _instruction_data: &[u8],
  15. ) -> ProgramResult {
  16. // You can verify the program ID from the instruction is in fact
  17. // the program ID of your program.
  18. if system_program::check_id(program_id) {
  19. return Err(ProgramError::IncorrectProgramId)
  20. };
  21. // You can verify the list has the correct number of accounts.
  22. // This error will get thrown by default if you
  23. // try to reach past the end of the iter.
  24. if accounts.len() < 4 {
  25. msg!("This instruction requires 4 accounts:");
  26. msg!(" payer, account_to_create, account_to_change, system_program");
  27. return Err(ProgramError::NotEnoughAccountKeys)
  28. };
  29. // Accounts passed in a vector must be in the expected order.
  30. let accounts_iter = &mut accounts.iter();
  31. let _payer = next_account_info(accounts_iter)?;
  32. let account_to_create = next_account_info(accounts_iter)?;
  33. let account_to_change = next_account_info(accounts_iter)?;
  34. let system_program = next_account_info(accounts_iter)?;
  35. // You can make sure an account has NOT been initialized.
  36. msg!("New account: {}", account_to_create.key);
  37. if account_to_create.lamports() != 0 {
  38. msg!("The program expected the account to create to not yet be initialized.");
  39. return Err(ProgramError::AccountAlreadyInitialized)
  40. };
  41. // (Create account...)
  42. // You can also make sure an account has been initialized.
  43. msg!("Account to change: {}", account_to_change.key);
  44. if account_to_change.lamports() == 0 {
  45. msg!("The program expected the account to change to be initialized.");
  46. return Err(ProgramError::UninitializedAccount)
  47. };
  48. // If we want to modify an account's data, it must be owned by our program.
  49. if account_to_change.owner != program_id {
  50. msg!("Account to change does not have the correct program id.");
  51. return Err(ProgramError::IncorrectProgramId)
  52. };
  53. // You can also check pubkeys against constants.
  54. if system_program.key != &system_program::ID {
  55. return Err(ProgramError::IncorrectProgramId)
  56. };
  57. Ok(())
  58. }