-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
65 lines (55 loc) · 1.74 KB
/
Copy pathlib.rs
File metadata and controls
65 lines (55 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use borsh::{BorshDeserialize, BorshSerialize};
use shank::ShankInstruction;
use solana_program::{
account_info::{next_account_info, AccountInfo},
declare_id,
entrypoint::ProgramResult,
msg,
program_error::ProgramError,
pubkey::Pubkey,
};
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(())
}