I am following an online tutorial on working with PDAs using the anchor framework for Rust and I cannot get the following Solana program to work.
use anchor_lang::prelude::*;
declare_id!("*");
#[program]
pub mod crunchy_vs_smooth {
use super::*;
pub fn initialize(ctx: Context<Initialize>, vote_account_bump: u8) -> Result<()> {
ctx.accounts.vote_account.bump = vote_account_bump;
Ok(())
}
pub fn vote_crunchy(ctx: Context<Vote>) -> Result<()> {
ctx.accounts.vote_account.crunchy += 1;
Ok(())
}
pub fn vote_smooth(ctx: Context<Vote>) -> Result<()> {
ctx.accounts.vote_account.smooth += 1;
Ok(())
}
}
#[derive(Accounts)]
#[instruction(vote_account_bump: u8)]
struct Initialize<'info> {
#[account(init, seeds = [b"vote_account".as_ref()], bump, payer = user, space = 16+16)]
vote_account: Account<'info, VotingState>,
user: Signer<'info>,
system_program: Program<'info, System>,
}
#[derive(Accounts)]
pub struct Vote<'info> {
#[account(mut, seeds = [b"vote_account".as_ref()], bump)]
vote_account: Account<'info, VotingState>,
}
#[account]
#[derive(Default)]
pub struct VotingState {
crunchy: u64,
smooth: u64,
bump: u8,
}
I keep getting the following errors:
- relating to #[program]
unresolved import
crate
| 8 | #[program] could not find__client_accounts_initialize
in the crate root |
#[program] the trait
anchor_lang::Accounts<'_>
is not implemented forInitialize<'_>
- relating to the Initialize struct with the account "pub vote_account"
the payer specified for an init constraint must be mutable. 37 | pub struct Initialize<'info> { function or associated item
try_accounts
not found for this |
I have used different constraints for the Initialize struct and have not made anything better.
I am not able to run anchor build
because of the errors. What am I doing wrong?