forked from solana-developers/program-examples
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlib.rs
More file actions
47 lines (37 loc) · 1.18 KB
/
lib.rs
File metadata and controls
47 lines (37 loc) · 1.18 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
use anchor_lang::prelude::*;
declare_id!("E64FVeubGC4NPNF2UBJYX4AkrVowf74fRJD9q6YhwstN");
#[program]
pub mod lever {
use super::*;
pub fn initialize(context: Context<InitializeLeverAccountConstraints>) -> Result<()> {
Ok(())
}
pub fn switch_power(mut context: Context<SetPowerStatusAccountConstraints>, name: String) -> Result<()> {
let power = &mut context.accounts.power;
power.is_on = !power.is_on;
msg!("{} is pulling the power switch!", &name);
match power.is_on {
true => msg!("The power is now on."),
false => msg!("The power is now off!"),
};
Ok(())
}
}
#[derive(Accounts)]
pub struct InitializeLeverAccountConstraints<'info> {
#[account(init, payer = user, space = PowerStatus::DISCRIMINATOR.len() + PowerStatus::INIT_SPACE)]
pub power: Account<'info, PowerStatus>,
#[account(mut)]
pub user: Signer<'info>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
pub struct SetPowerStatusAccountConstraints<'info> {
#[account(mut)]
pub power: Account<'info, PowerStatus>,
}
#[account]
#[derive(InitSpace)]
pub struct PowerStatus {
pub is_on: bool,
}