12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- use borsh::{BorshDeserialize, BorshSerialize};
- use shank::ShankInstruction;
- use solana_program::{
- account_info::{next_account_info, AccountInfo},
- declare_id,
- entrypoint::ProgramResult,
- msg,
- program::{invoke, invoke_signed},
- program_error::ProgramError,
- pubkey::Pubkey,
- system_instruction,
- };
- mod state;
- use state::*;
- declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
- #[cfg(not(feature = "no-entrypoint"))]
- use solana_program::entrypoint;
- #[cfg(not(feature = "no-entrypoint"))]
- entrypoint!(process_instruction);
- #[derive(ShankInstruction, BorshDeserialize, BorshSerialize)]
- pub enum Instruction {
- #[account(0, writable, name = "counter", desc = "Counter account to increment")]
- Increment,
- }
- pub fn process_instruction(
- _program_id: &Pubkey,
- accounts: &[AccountInfo],
- instruction_data: &[u8],
- ) -> ProgramResult {
- let (instruction_discriminant, instruction_data_inner) = instruction_data.split_at(1);
- match instruction_discriminant[0] {
- 0 => {
- msg!("Instruction: Increment");
- process_increment_counter(accounts, instruction_data_inner)?;
- }
- _ => {
- msg!("Error: unknown instruction")
- }
- }
- Ok(())
- }
- pub fn process_increment_counter(
- accounts: &[AccountInfo],
- instruction_data: &[u8],
- ) -> Result<(), ProgramError> {
- let account_info_iter = &mut accounts.iter();
- let counter_account = next_account_info(account_info_iter)?;
- assert!(
- counter_account.is_writable,
- "Counter account must be writable"
- );
- let mut counter = Counter::try_from_slice(&counter_account.try_borrow_mut_data()?)?;
- counter.count += 1;
- counter.serialize(&mut *counter_account.data.borrow_mut())?;
- msg!("Counter state incremented to {:?}", counter.count);
- Ok(())
- }
|