lib.rs 810 B

1234567891011121314151617181920212223242526272829303132333435
  1. use anchor_lang::prelude::*;
  2. #[program]
  3. pub mod misc2 {
  4. use super::*;
  5. #[state]
  6. pub struct MyState {
  7. pub data: u64,
  8. pub auth: Pubkey,
  9. }
  10. impl MyState {
  11. pub fn new(ctx: Context<Auth>) -> Result<Self, ProgramError> {
  12. Ok(Self {
  13. data: 0,
  14. auth: *ctx.accounts.authority.key,
  15. })
  16. }
  17. pub fn set_data(&mut self, ctx: Context<Auth>, data: u64) -> Result<(), ProgramError> {
  18. if self.auth != *ctx.accounts.authority.key {
  19. return Err(ProgramError::Custom(1234)); // Arbitrary error code.
  20. }
  21. self.data = data;
  22. Ok(())
  23. }
  24. }
  25. }
  26. #[derive(Accounts)]
  27. pub struct Auth<'info> {
  28. #[account(signer)]
  29. pub authority: AccountInfo<'info>,
  30. }