lib.rs 1.8 KB

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