79 lines
1.6 KiB
Rust
79 lines
1.6 KiB
Rust
use anchor_lang::prelude::*;
|
|
use std::ops::DerefMut;
|
|
|
|
declare_id!("CwrqeMj2U8tFr1Rhkgwc84tpAsqbt9pTt2a4taoTADPr");
|
|
|
|
#[program]
|
|
pub mod basic_4 {
|
|
use super::*;
|
|
|
|
pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
|
|
let counter = ctx.accounts.counter.deref_mut();
|
|
let bump = *ctx.bumps.get("counter").ok_or(ErrorCode::CannotGetBump)?;
|
|
|
|
*counter = Counter {
|
|
authority: *ctx.accounts.authority.key,
|
|
count: 0,
|
|
bump,
|
|
};
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn increment(ctx: Context<Increment>) -> Result<()> {
|
|
require_keys_eq!(
|
|
ctx.accounts.authority.key(),
|
|
ctx.accounts.counter.authority,
|
|
ErrorCode::Unauthorized
|
|
);
|
|
|
|
ctx.accounts.counter.count += 1;
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
#[derive(Accounts)]
|
|
pub struct Initialize<'info> {
|
|
#[account(
|
|
init,
|
|
payer = authority,
|
|
space = Counter::SIZE,
|
|
seeds = [b"counter"],
|
|
bump
|
|
)]
|
|
counter: Account<'info, Counter>,
|
|
#[account(mut)]
|
|
authority: Signer<'info>,
|
|
system_program: Program<'info, System>,
|
|
}
|
|
|
|
#[derive(Accounts)]
|
|
pub struct Increment<'info> {
|
|
#[account(
|
|
mut,
|
|
seeds = [b"counter"],
|
|
bump = counter.bump
|
|
)]
|
|
counter: Account<'info, Counter>,
|
|
authority: Signer<'info>,
|
|
}
|
|
|
|
#[account]
|
|
pub struct Counter {
|
|
pub authority: Pubkey,
|
|
pub count: u64,
|
|
pub bump: u8,
|
|
}
|
|
|
|
impl Counter {
|
|
pub const SIZE: usize = 8 + 32 + 8 + 1;
|
|
}
|
|
|
|
#[error_code]
|
|
pub enum ErrorCode {
|
|
#[msg("You are not authorized to perform this action.")]
|
|
Unauthorized,
|
|
#[msg("Cannot get the bump.")]
|
|
CannotGetBump,
|
|
}
|