Created
May 17, 2023 14:46
-
-
Save deanmlittle/d92f1a9467f9f877b391f5566995ea97 to your computer and use it in GitHub Desktop.
Anchor counter increment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use anchor_lang::prelude::*; | |
// This is your program's public key and it will update | |
// automatically when you build the project. | |
declare_id!("HAcYnZCEXGs31qKPXEJUEjPDBnxSLLL2CxSqp4zrpHze"); | |
#[program] | |
mod plus_one { | |
use super::*; | |
pub fn initialize(ctx: Context<Initialize>) -> Result<()> { | |
ctx.accounts.counter.bump = *ctx.bumps.get("counter").unwrap(); | |
ctx.accounts.counter.count = 0u64; | |
Ok(()) | |
} | |
pub fn increment(ctx: Context<Increment>) -> Result<()> { | |
ctx.accounts.counter.count = ctx.accounts.counter.count + 1; | |
Ok(()) | |
} | |
} | |
#[derive(Accounts)] | |
pub struct Initialize<'info> { | |
#[account(mut)] | |
pub owner: Signer<'info>, | |
#[account(init, seeds = [b"counter", owner.key().as_ref()], payer = owner, bump, space = 8 + 8 + 1)] | |
pub counter: Account<'info, Counter>, | |
pub system_program: Program<'info, System>, | |
} | |
#[derive(Accounts)] | |
pub struct Increment<'info> { | |
#[account(mut)] | |
pub owner: Signer<'info>, | |
#[account(mut, seeds = [b"counter", owner.key().as_ref()], bump = counter.bump)] | |
pub counter: Account<'info, Counter>, | |
pub system_program: Program<'info, System>, | |
} | |
// let counter_seed = [b"counter", signer.key().as_ref()]; | |
// let (counter_pda, counter_bump) = Pubkey::find_program_address(&counter_seed, &Program::id()); | |
#[account] | |
pub struct Counter { | |
count: u64, | |
bump: u8 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment