-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.rs
83 lines (62 loc) · 2.61 KB
/
lib.rs
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use byteorder::{ByteOrder, LittleEndian};
use solana_program::{
account_info::{next_account_info, AccountInfo},
entrypoint,
entrypoint::ProgramResult,
info,
program_error::ProgramError,
pubkey::Pubkey,
};
use std::mem;
// Declare and export the program's entrypoint
entrypoint!(process_instruction);
// Program entrypoint's implementation
fn process_instruction(
program_id: &Pubkey,
accounts: &[AccountInfo],
_instruction_data: &[u8]
) -> ProgramResult {
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> EXAMPLES: HELLO DEV <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
// Iterating accounts is safer then indexing
let accounts_iter = &mut accounts.iter();
// Get the account to say hello to
let account = next_account_info(accounts_iter)?;
// The account must be owned by the program in order to modify its data
if account.owner != program_id {
info!("Greeted account does not have the correct program id");
return Err(ProgramError::IncorrectProgramId);
}
// The data must be large enough to hold a u64 count
if account.try_data_len()? < mem::size_of::<u32>() {
info!("Greeted account data length too small for u32");
return Err(ProgramError::InvalidAccountData);
}
// Increment and store the number of times the account has been greeted
let mut data = account.try_borrow_mut_data()?;
let mut num_greets = LittleEndian::read_u32(&data);
num_greets += 1;
LittleEndian::write_u32(&mut data[0..], num_greets);
info!("Hello Dev! You're looking well today.");
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> EXAMPLES: HELLO DEV <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
// Iterating accounts is safer then indexing
let accounts_iter = &mut accounts.iter();
// Get the account to say hello to
let account = next_account_info(accounts_iter)?;
// The account must be owned by the program in order to modify its data
if account.owner != program_id {
info!("Account does not have the correct program id");
return Err(ProgramError::IncorrectProgramId);
}
// The data must be large enough to hold a u64 count
if account.try_data_len()? < mem::size_of::<u32>() {
info!("Account data length too small for u32");
return Err(ProgramError::InvalidAccountData);
}
// Increment and store the number of times the account has been greeted
let mut data = account.try_borrow_mut_data()?;
let mut sample_counter = LittleEndian::read_u32(&data);
sample_counter += 1;
LittleEndian::write_u32(&mut data[0..], sample_counter);
info!("Sample Program");
Ok(())
}