use anchor_lang::prelude::*;
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
#[program]
pub mod example {
use super::*;
pub fn initialize(ctx: Context<Initialize>, initial_value: u64) -> Result<()> {
let account = &mut ctx.accounts.my_account;
account.data = initial_value;
account.authority = ctx.accounts.authority.key();
msg!("Initialized with value: {}", initial_value);
Ok(())
}
pub fn update(ctx: Context<Update>, new_value: u64) -> Result<()> {
let account = &mut ctx.accounts.my_account;
require!(new_value > account.data, ErrorCode::InvalidValue);
account.data = new_value;
msg!("Updated to: {}", new_value);
Ok(())
}
pub fn close_account(ctx: Context<CloseAccount>) -> Result<()> {
msg!("Closing account: {}", ctx.accounts.my_account.key());
Ok(())
}
}
#[derive(Accounts)]
pub struct Initialize<'info> {
#[account(
init,
payer = authority,
space = 8 + MyAccount::INIT_SPACE
)]
pub my_account: Account<'info, MyAccount>,
#[account(mut)]
pub authority: Signer<'info>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
pub struct Update<'info> {
#[account(
mut,
has_one = authority
)]
pub my_account: Account<'info, MyAccount>,
pub authority: Signer<'info>,
}
#[derive(Accounts)]
pub struct CloseAccount<'info> {
#[account(
mut,
has_one = authority,
close = authority
)]
pub my_account: Account<'info, MyAccount>,
#[account(mut)]
pub authority: Signer<'info>,
}
#[account]
#[derive(InitSpace)]
pub struct MyAccount {
pub data: u64,
pub authority: Pubkey,
}
#[error_code]
pub enum ErrorCode {
#[msg("New value must be greater than current value")]
InvalidValue,
}