lib.rs 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. use anchor_lang::prelude::*;
  2. declare_id!("E64FVeubGC4NPNF2UBJYX4AkrVowf74fRJD9q6YhwstN");
  3. #[program]
  4. pub mod lever {
  5. use super::*;
  6. pub fn initialize(_ctx: Context<InitializeLever>) -> Result<()> {
  7. Ok(())
  8. }
  9. pub fn switch_power(ctx: Context<SetPowerStatus>, name: String) -> Result<()> {
  10. let power = &mut ctx.accounts.power;
  11. power.is_on = !power.is_on;
  12. msg!("{} is pulling the power switch!", &name);
  13. match power.is_on {
  14. true => msg!("The power is now on."),
  15. false => msg!("The power is now off!"),
  16. };
  17. Ok(())
  18. }
  19. }
  20. #[derive(Accounts)]
  21. pub struct InitializeLever<'info> {
  22. #[account(init, payer = user, space = 8 + 8)]
  23. pub power: Account<'info, PowerStatus>,
  24. #[account(mut)]
  25. pub user: Signer<'info>,
  26. pub system_program: Program<'info, System>,
  27. }
  28. #[derive(Accounts)]
  29. pub struct SetPowerStatus<'info> {
  30. #[account(mut)]
  31. pub power: Account<'info, PowerStatus>,
  32. }
  33. #[account]
  34. pub struct PowerStatus {
  35. pub is_on: bool,
  36. }