lib.rs 1.7 KB

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