I have a program and when I run the tests using anchor test
I keep getting the error that I cannot and shouldn't initialize the payer account as the program account.Here is the rust code:
use anchor_lang::prelude::*;declare_id!("CJXJk3KMD7LXXUAJRCZXEaZAN8feQrQ4VHe3jtSJoRHE");#[program]pub mod flipperapp {use super::*;pub fn initialize(ctx: Context<Initialize>) -> Result<()> { let switch_account = &mut ctx.accounts.switch_account; switch_account.state = true; Ok(())}pub fn flip(ctx: Context<Flip>) -> Result<()> { let switch_account = &mut ctx.accounts.switch_account; if switch_account.state { switch_account.state = false; } else { switch_account.state = true; } Ok(())}}#[derive(Accounts)]pub struct Initialize<'info> {#[account(init, payer = user, space = 16 + 16)]pub switch_account: Account<'info, SwitchAccount>,#[account(mut)]pub user: Signer<'info>,pub system_program: Program<'info, System>}#[derive(Accounts)]pub struct Flip<'info> {#[account(mut)]pub switch_account: Account<'info, SwitchAccount>,}#[account]pub struct SwitchAccount{pub state: bool,}
And here is the JS:
const assert = require("assert"); const { SystemProgram } = anchor.web3; describe("flipperapp", () => { // Configure the client to use the local cluster. const provider = anchor.AnchorProvider.env(); anchor.setProvider(anchor.AnchorProvider.env()); const program = anchor.workspace.Flipperapp; it("Creates a flip account", async () => { // Add your test here. const SwitchAccount = anchor.web3.Keypair.generate(); await program.methods.initialize().accounts({ accounts: { switch_account: SwitchAccount.publicKey, user: provider, system_program: SystemProgram.programId }, signers: [SwitchAccount] }).rpc(); const baseAccount = await program.account.switch_account.fetch(switch_account.publicKey) assert.ok(baseAccount.state) _baseAccount = baseAccount; }); it('Flip it', async () => { baseAccount = _baseAccount await program.rpc.flip({ accounts: { switch_account: baseAccount.publicKey, } }) const account = await program.account.switch_account.fetch(baseAccount.publicKey) assert.ok(account.state == false); }) });
The error message I get after running the test is:Error: AnchorError thrown in programs/flipperapp/src/lib.rs:26. Error Code: TryingToInitPayerAsProgramAccount. Error Number: 4101. Error Message: You cannot/should not initialize the payer account as a program account.
Please let me know what you think the issue with this code may be and why these errors keep getting thrown. thanks