Following error occurs when I try to run anchor test
:
Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: Program failed to complete
at Connection.sendEncodedTransaction (node_modules/@solana/web3.js/src/connection.ts:4819:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at Connection.sendRawTransaction (node_modules/@solana/web3.js/src/connection.ts:4778:20)
at sendAndConfirmRawTransaction (node_modules/@project-serum/anchor/src/provider.ts:288:21)
at AnchorProvider.sendAndConfirm (node_modules/@project-serum/anchor/src/provider.ts:148:14)
at MethodsBuilder.rpc [as _rpcFn] (node_modules/@project-serum/anchor/src/program/namespace/rpc.ts:29:16)
Here is the program code:
use anchor_lang::prelude::*;
declare_id!("57GYguBdvQKBbRsARLpAVFmC2XvPNMa3dPP9qHhZvkoE");
#[program]
pub mod house {
use super::*;
pub fn init_house(ctx: Context<InitHouse>, payout_amount: u8) -> Result<()> {
InitHouse::initialise_house(ctx, payout_amount)
}
}
impl InitHouse<'_> {
pub fn initialise_house(ctx: Context<Self>, fee_amount: u8) -> Result<()> {
let vault = &mut ctx.accounts.vault;
// vault.fee_vault = *ctx.accounts.fee_vault.to_account_info().key;
vault.fee_amount = fee_amount;
vault.bump = *ctx.bumps.get("house-vault").unwrap();
Ok(())
}
}
#[derive(Accounts)]
pub struct InitHouse<'info> {
// space: 8 discriminator + 32 house vault account + 32 fee vault
// + 1 fee amount + 6 * 1 bet amounts + 1 bump
#[account(
init,
payer = user,
space = 8 + 32 + 32 + 1 + 6 + 1,
seeds = [
b"house-vault".as_ref(),
user.key().as_ref()
],
bump
)]
pub vault: Account<'info, HouseState>,
#[account(mut)]
pub user: Signer<'info>,
pub system_program: Program<'info, System>,
}
#[account]
pub struct HouseState {
pub house_vault: Pubkey,
pub fee_vault: Pubkey,
pub fee_amount: u8,
pub bet_amounts: [u8; 6],
pub bump: u8,
}
Here is the test script:
import * as anchor from "@project-serum/anchor";
import { web3 } from "@project-serum/anchor";
import { Program } from "@project-serum/anchor";
import { CoinFlip } from "../target/types/coin_flip";
// const SOLANA_URL = 'https://api.devnet.solana.com';
const SOLANA_URL = 'http://localhost:8899';
describe("coin-flip", () => {
anchor.setProvider(anchor.AnchorProvider.env());
const program = anchor.workspace.CoinFlip as Program<CoinFlip>;
const programOwner = (anchor.getProvider().wallet as AnchorWallet).payer;
it("Init house", async () => {
const [houseVaultPDA, _] = await web3.PublicKey
.findProgramAddress(
[
Buffer.from("house-vault"),
programOwner.publicKey.toBuffer()
],
program.programId
);
console.log('House Vault PDA: ', houseVaultPDA.toString());
await program.methods
.initHouse(new anchor.BN(95))
.accounts({
user: programOwner.publicKey,
vault: houseVaultPDA
})
.rpc();
});
});
I was trying to solve this error by myself but with no luck. I know the Instruction 0 is SystemProgram.CreateAccount
, and checked that programOwner
has enough SOL to create a PDA.