I have this instruction with the associated context accounts:
pub fn create_battleground(
ctx: Context<CreateBattleground>,
participants_cap: u32,
entry_fee: u64,
) -> Result<()> {
*ctx.accounts.battleground_state = BattlegroundState {
bump: *ctx.bumps.get("battleground_state").unwrap(),
id: ctx.accounts.battle_royale_state.last_battleground_id,
participants_cap,
participants: 0,
status: BattlegroundStatus::Preparing,
pot_mint: ctx.accounts.pot_mint.key(),
entry_fee,
};
ctx.accounts.battle_royale_state.last_battleground_id += 1;
Ok(())
}
#[derive(Accounts)]
pub struct CreateBattleground<'info> {
#[account(mut)]
pub creator: Signer<'info>,
/// CHECK: Checking correspondance with battle royale state
#[account(mut)]
pub game_master: AccountInfo<'info>,
#[account(
mut,
seeds = [
BATTLE_ROYALE_STATE_SEEDS.as_bytes(),
battle_royale_state.game_master.as_ref(),
],
bump = battle_royale_state.bump,
has_one = game_master,
)]
pub battle_royale_state: Account<'info, BattleRoyaleState>,
/// CHECK: Checking correspondance with battle royale state
#[account(
mut,
seeds = [
BATTLEGROUND_AUTHORITY_SEEDS.as_bytes(),
battle_royale_state.key().as_ref(),
battle_royale_state.last_battleground_id.to_be_bytes().as_ref(),
],
bump,
)]
pub authority: AccountInfo<'info>,
/// The mint of the token used to pay the entry fee
#[account(owner = token::ID)]
pub pot_mint: Account<'info, Mint>,
#[account(
init,
payer = creator,
space = BattlegroundState::LEN,
seeds = [
BATTLEGROUND_STATE_SEEDS.as_bytes(),
battle_royale_state.key().as_ref(),
battle_royale_state.last_battleground_id.to_be_bytes().as_ref(),
],
bump,
)]
pub battleground_state: Account<'info, BattlegroundState>,
/// The account receiving entry fees
#[account(
init,
payer = creator,
associated_token::mint = pot_mint,
associated_token::authority = authority,
)]
pub pot_account: Account<'info, TokenAccount>,
/// The account receiving the fee taken on each entry fee
#[account(
init_if_needed,
payer = creator,
associated_token::mint = pot_mint,
associated_token::authority = game_master,
)]
pub dev_account: Account<'info, TokenAccount>,
/// Solana ecosystem program addresses
pub token_program: Program<'info, Token>,
pub associated_token_program: Program<'info, AssociatedToken>,
pub system_program: Program<'info, System>,
pub rent: Sysvar<'info, Rent>,
}
In my tests, I call the function like so:
this.addresses = {
authority: anchor.web3.PublicKey.findProgramAddressSync(
[
BATTLEGROUND_AUTHORITY_SEEDS,
this.battleRoyale.addresses.battleRoyale.toBuffer(),
new anchor.BN(this.id).toBuffer("le", 8),
],
BATTLE_ROYALE_PROGRAM_ID
)[0],
battleground: anchor.web3.PublicKey.findProgramAddressSync(
[
BATTLEGROUND_STATE_SEEDS,
this.battleRoyale.addresses.battleRoyale.toBuffer(),
new anchor.BN(this.id).toBuffer("le", 8),
],
BATTLE_ROYALE_PROGRAM_ID
)[0],
};
const potAccount = await getAssociatedTokenAddress(potMint, this.addresses.authority, true);
const devAccount = await getAssociatedTokenAddress(
potMint,
this.battleRoyale.addresses.gameMaster,
true
);
await program.methods
.createBattleground(participantsCap, entryFee)
.accounts({
gameMaster: this.battleRoyale.addresses.gameMaster,
creator: this.provider.publicKey,
battleRoyaleState: this.battleRoyale.addresses.battleRoyaleStateKey,
authority: this.addresses.authority,
potMint,
battlegroundState: battlegroundAddress,
potAccount,
devAccount,
tokenProgram: TOKEN_PROGRAM_ID,
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
systemProgram: anchor.web3.SystemProgram.programId,
})
.rpc();
However I get this error:
Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: Cross-program invocation with unauthorized signer or writable account
at Connection.sendEncodedTransaction (node_modules/@solana/web3.js/src/connection.ts:4546:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at Connection.sendRawTransaction (node_modules/@solana/web3.js/src/connection.ts:4505: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)
I tried adding mut
on every account, double checked seeds, remo