lib.rs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //! counter-auth is an example of a program *implementing* an external program
  2. //! interface. Here the `counter::Auth` trait, where we only allow a count
  3. //! to be incremented if it changes the counter from odd -> even or even -> odd.
  4. //! Creative, I know. :P.
  5. #![feature(proc_macro_hygiene)]
  6. use anchor_lang::prelude::*;
  7. use counter::Auth;
  8. #[program]
  9. pub mod counter_auth {
  10. use super::*;
  11. #[state]
  12. pub struct CounterAuth {}
  13. // TODO: remove this impl block after addressing
  14. // https://github.com/project-serum/anchor/issues/71.
  15. impl CounterAuth {
  16. pub fn new(_ctx: Context<Empty>) -> Result<Self, ProgramError> {
  17. Ok(Self {})
  18. }
  19. }
  20. impl<'info> Auth<'info, Empty> for CounterAuth {
  21. fn is_authorized(_ctx: Context<Empty>, current: u64, new: u64) -> ProgramResult {
  22. if current % 2 == 0 {
  23. if new % 2 == 0 {
  24. return Err(ProgramError::Custom(50)); // Arbitrary error code.
  25. }
  26. } else {
  27. if new % 2 == 1 {
  28. return Err(ProgramError::Custom(60)); // Arbitrary error code.
  29. }
  30. }
  31. Ok(())
  32. }
  33. }
  34. }
  35. #[derive(Accounts)]
  36. pub struct Empty {}