lib.rs 1.5 KB

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