I am a newbie and playing around a bit but cant figure out how to solve this error. My counter program works as it should in the playground and I can call the increase and decrease functions from my app but cant initialize one for some reason.
const callProgram = useCallback(async () => {
if (!publicKey) throw new WalletNotConnectedError();
if (!wallet) throw new WalletNotConnectedError();
const program = await getProgram(connection, wallet)
const counterKey = Keypair.generate().publicKey
const init = await program.methods
.initalize()
.accounts({
set: counterKey,
user: publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
})
.rpc()
.catch((err) => {
console.log('err', err)
})
console.log('initalizeAcc called', { init })
//works
const increase = await program.methods
.increaseCounter(new BN(2, undefined, "le"))
.accounts({
set: new PublicKey('6uzQe1NzrZ7NmeM9qz5pkYD8ViKeobmdM5VAPaV6pBHB'),
})
.rpc()
console.log('increaseCounter called', { increase })
const decrease = await program.methods
.decreaseCounter(new BN(6, undefined, "le"))
.accounts({
set: new PublicKey('6uzQe1NzrZ7NmeM9qz5pkYD8ViKeobmdM5VAPaV6pBHB'),
})
.rpc()
console.log('decreaseCounter called', { decrease })
}, [publicKey, sendTransaction, connection])
the initialize call always fails with the signature verification error.
my program looks like this
// Import anchor
use anchor_lang::prelude::*;
declare_id!("53fUjUVA7GCU2r279UD43NjCXRaR2dnocwwDZQKvAf1w");
#[account]
pub struct Counter {
count: u8,
}
#[derive(Accounts)]
pub struct Initalize<'info> {
#[account(init, payer = user, space = 8 + 1)]
pub set: Account<'info, Counter>,
#[account(mut)]
pub user: Signer<'info>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
pub struct UpdateCounter<'info> {
#[account(mut)]
pub set: Account<'info, Counter>,
}
#[program]
mod my_counter {
use super::*;
pub fn initalize(ctx: Context<Initalize>) -> Result<()> {
ctx.accounts.set.count = 0;
msg!("Initalize account");
Ok(())
}
pub fn decrease_counter(ctx: Context<UpdateCounter>, number: u8) -> Result<()> {
ctx.accounts.set.count -= number;
msg!("Decrease counter");
Ok(())
}
pub fn increase_counter(ctx: Context<UpdateCounter>, number: u8) -> Result<()> {
if number >= 5 {
return err!(MyError::MaxStepSize);
}
ctx.accounts.set.count += number;
msg!("Increased counter");
Ok(())
}
}
#[error_code]
pub enum MyError {
#[msg("Only positive numbers supported")]
DataInputInvalid,
#[msg("Max step size is 5")]
MaxStepSize,
}
Please use seed for init PDA that will better than init some random pubkey
https://book.anchor-lang.com/anchor_in_depth/PDAs.html