lib.rs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. use borsh::{BorshDeserialize, BorshSerialize};
  2. use solana_program::{
  3. account_info::{next_account_info, AccountInfo},
  4. declare_id,
  5. entrypoint::ProgramResult,
  6. msg,
  7. program::{invoke, invoke_signed},
  8. program_error::ProgramError,
  9. pubkey::Pubkey,
  10. system_instruction,
  11. };
  12. mod state;
  13. use state::*;
  14. declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
  15. #[cfg(not(feature = "no-entrypoint"))]
  16. use solana_program::entrypoint;
  17. #[cfg(not(feature = "no-entrypoint"))]
  18. entrypoint!(process_instruction);
  19. pub fn process_instruction(
  20. _program_id: &Pubkey,
  21. accounts: &[AccountInfo],
  22. instruction_data: &[u8],
  23. ) -> ProgramResult {
  24. let (instruction_discriminant, instruction_data_inner) = instruction_data.split_at(1);
  25. match instruction_discriminant[0] {
  26. 0 => {
  27. msg!("Instruction: Increment");
  28. process_increment_counter(accounts, instruction_data_inner)?;
  29. }
  30. _ => {
  31. msg!("Error: unknown instruction")
  32. }
  33. }
  34. Ok(())
  35. }
  36. pub fn process_increment_counter(
  37. accounts: &[AccountInfo],
  38. instruction_data: &[u8],
  39. ) -> Result<(), ProgramError> {
  40. let account_info_iter = &mut accounts.iter();
  41. let counter_account = next_account_info(account_info_iter)?;
  42. assert!(
  43. counter_account.is_writable,
  44. "Counter account must be writable"
  45. );
  46. let mut counter = Counter::try_from_slice(&counter_account.try_borrow_mut_data()?)?;
  47. counter.count += 1;
  48. counter.serialize(&mut *counter_account.data.borrow_mut())?;
  49. msg!("Counter state incremented to {:?}", counter.count);
  50. Ok(())
  51. }