lib.rs 966 B

123456789101112131415161718192021222324252627282930313233
  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. use anchor_lang::prelude::*;
  6. use counter::Auth;
  7. #[program]
  8. pub mod counter_auth {
  9. use super::*;
  10. #[state]
  11. pub struct CounterAuth;
  12. impl<'info> Auth<'info, Empty> for CounterAuth {
  13. fn is_authorized(_ctx: Context<Empty>, current: u64, new: u64) -> ProgramResult {
  14. if current % 2 == 0 {
  15. if new % 2 == 0 {
  16. return Err(ProgramError::Custom(50)); // Arbitrary error code.
  17. }
  18. } else {
  19. if new % 2 == 1 {
  20. return Err(ProgramError::Custom(60)); // Arbitrary error code.
  21. }
  22. }
  23. Ok(())
  24. }
  25. }
  26. }
  27. #[derive(Accounts)]
  28. pub struct Empty {}