I'm creating a basic staking contract and just trying to initialize the pool where the reward tokens will be held. I'm receiving the following error:
TryingToInitPayerAsProgramAccount. Error Number: 4101. Error Message: You cannot/should not initialize the payer account as a program account.
I really don't think i'm initializing the payer account as a program account? Here is the Initialize context
#[derive(Accounts)]
#[instruction(bump: u8)]
pub struct Initialize<'info> {
#[account(mut)]
pub owner: Signer<'info>,
#[account(init, payer = owner, space = StakingPool::LEN + 8)]
pub staking_pool: Account<'info, StakingPool>,
pub stake_token_mint: Box<Account<'info, Mint>>,
#[account(
seeds = [
b"vault".as_ref(),
],
bump,
)]
pub token_vault: SystemAccount<'info>,
#[account(
init_if_needed,
payer = owner,
associated_token::authority = token_vault,
associated_token::mint = stake_token_mint,
)]
pub vault_ata: Box<Account<'info, TokenAccount>>,
pub system_program: Program<'info, System>,
pub token_program: Program<'info, Token>,
pub associated_token_program: Program<'info, AssociatedToken>,
}
And here is the frontend call
const [stakingPoolPDA, bump] = PublicKey.findProgramAddressSync(
[Buffer.from(Anchor.utils.bytes.utf8.encode('vault'))], // Adjust with actual seeds
programId
);
// console.log('stakingPoolPDA', stakingPoolPDA.toString());
const vaultAta = getAssociatedTokenAddressSync(
goldMintAddress,
stakingPoolPDA,
true
);
const ix = await program.methods
.initialize(bump)
.accounts({
stakeTokenMint: goldMintAddress,
tokenVault: stakingPoolPDA,
vaultAta: vaultAta,
})
.instruction();
const transaction = new Anchor.web3.Transaction();
transaction.add(ix);
let signature = await sendTransaction(transaction, connection, {
skipPreflight: true,
the actual instruction implementation is just setting some initial values for the stake pool:
pub fn initialize(ctx: Context<Initialize>, bump: u8) -> Result<()> {
msg!("Instruction: Initialize");
msg!("before staking pool");
let staking_pool: &mut Account<'_, StakingPool> = &mut ctx.accounts.staking_pool;
staking_pool.owner = ctx.accounts.owner.key();
staking_pool.staking_token_mint = ctx.accounts.stake_token_mint.key();
staking_pool.total_staked_amount = 0;
staking_pool.total_reward_amount = 0;
staking_pool.total_user_count = 0;
staking_pool.reward_per_second = 0;
staking_pool.bump = bump;
Ok(())
}
I'm thinking this has to be an issue with something not directly related to the error message, as i'm not initializing the owner/payer anywhere in my code. On the frontend, i'm only passing in the accounts that anchor can not infer (signer, system program, etc). But even when i explicitly pass in all the accounts i'm getting the same error