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

47 lines
1.1 KiB
Rust

use anchor_lang::prelude::*;
use anchor_lang::solana_program::system_program;
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
#[program]
mod basic_2 {
use super::*;
pub fn create(ctx: Context<Create>, authority: Pubkey) -> ProgramResult {
let counter = &mut ctx.accounts.counter;
counter.authority = authority;
counter.count = 0;
Ok(())
}
pub fn increment(ctx: Context<Increment>) -> ProgramResult {
let counter = &mut ctx.accounts.counter;
counter.count += 1;
Ok(())
}
}
#[derive(Accounts)]
pub struct Create<'info> {
#[account(init, payer = user, space = 8 + 40)]
pub counter: Account<'info, Counter>,
#[account(signer)]
pub user: AccountInfo<'info>,
#[account(address = system_program::ID)]
pub system_program: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct Increment<'info> {
#[account(mut, has_one = authority)]
pub counter: Account<'info, Counter>,
#[account(signer)]
pub authority: AccountInfo<'info>,
}
#[account]
pub struct Counter {
pub authority: Pubkey,
pub count: u64,
}