Mutation
contract Counter {
mapping(address user => uint256 count) public counter;
function increment(address to, uint256 amount) external {
counter[to] += amount;
}
}#[account]
pub struct Counter {
pub user: Pubkey,
pub count: u64,
}
#[derive(Accounts)]
pub struct Increment {
#[account(init, seeds = [b"user", user.key()], bump, space = 8 + 32 + 8)
pub counter: Account<'info, Counter>,
pub user: Signer<'info>,
}
pub fn increment(ctx: Context<Increment>, amount: u64) -> Result<()> {
ctx.accounts.counter = ctx.accounts.counter.checked_add(amount).unwrap();
Ok(())
}Last updated