lib.rs 1.0 KB

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