anchor/examples/tutorial/basic-4/programs/basic-4/src/lib.rs

44 lines
904 B
Rust
Raw Normal View History

2021-01-22 05:19:43 -08:00
// #region code
use anchor_lang::prelude::*;
#[program]
2021-01-23 00:18:50 -08:00
pub mod basic_4 {
2021-01-22 05:19:43 -08:00
use super::*;
#[state]
2021-01-23 16:54:13 -08:00
pub struct Counter {
authority: Pubkey,
count: u64,
2021-01-22 05:19:43 -08:00
}
2021-01-23 16:54:13 -08:00
impl Counter {
2021-01-23 17:07:38 -08:00
pub fn new(ctx: Context<Auth>) -> Result<Self> {
2021-01-23 16:54:13 -08:00
Ok(Self {
authority: *ctx.accounts.authority.key,
count: 0,
})
}
2021-01-23 17:07:38 -08:00
pub fn increment(&mut self, ctx: Context<Auth>) -> Result<()> {
2021-01-23 16:54:13 -08:00
if &self.authority != ctx.accounts.authority.key {
return Err(ErrorCode::Unauthorized.into());
}
self.count += 1;
Ok(())
2021-01-22 05:19:43 -08:00
}
}
}
2021-01-23 00:18:50 -08:00
#[derive(Accounts)]
2021-01-23 16:54:13 -08:00
pub struct Auth<'info> {
#[account(signer)]
authority: AccountInfo<'info>,
}
2021-01-22 05:19:43 -08:00
// #endregion code
2021-01-23 16:54:13 -08:00
#[error]
pub enum ErrorCode {
#[msg("You are not authorized to perform this action.")]
Unauthorized,
}