lib.rs 887 B

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