Quantcast
Channel: Recent Questions - Solana Stack Exchange
Viewing all articles
Browse latest Browse all 7906

Error: AnchorError caused by account: monster. Error Code: ConstraintSeeds. Error Number: 2006. Error Message: A seeds constraint was violated

$
0
0

As the title says, I encountered an error while running,hope everyone can help. Thank you very much everyone

test.ts

  it("spawn monster", async () => {    const [gameAddress] = createGameAddress(); // core    const [playerAddress] = createPlayerAddress(gameAddress);    const playerAccount = await program.account.player.fetch(playerAddress);    const monsterIndex = playerAccount.nextMonsterIndex.subn(1); // core    const [monsterAddress] = createMonsterAddress(gameAddress, monsterIndex);    const createMonsterSignature = await program.methods      .spawnMonster()      .accountsPartial({        game: gameAddress,        playerAccount: playerAddress,        monster: monsterAddress,        player: player.publicKey,        systemProgram: anchor.web3.SystemProgram.programId,      })      .rpc();    await confirmTransaction(createMonsterSignature, provider);    console.log("Create monster success");  });

player.rs

#[derive(Accounts)]pub struct CreatePlayer<'info> {    pub game: Box<Account<'info, Game>>,    #[account(        init,        seeds = [            b"PLAYER",            game.key().as_ref(),            player.key().as_ref()        ],        bump,        payer = player,        space = ANCHOR_DISCRIMINATOR + Player::INIT_SPACE    )]    pub player_account: Account<'info, Player>,    #[account(mut)]    pub player: Signer<'info>,    pub system_program: Program<'info, System>,}

PDA monster

const createMonsterAddress = (  gameAddress: PublicKey,  monsterIndex: anchor.BN) =>  findProgramAddress([    Buffer.from(MONSTER_SEEDS),    gameAddress.toBuffer(),    monsterIndex.toArrayLike(Buffer, "le", MONSTER_INDEX_BYTE_LENGTH),    player.publicKey.toBuffer(),  ]);

instruction.rs

#[derive(Accounts)]pub struct SpawnMonster<'info> {    pub game: Box<Account<'info, Game>>,    #[account(        mut,        has_one = game,        has_one = player,    )]    pub player_account: Box<Account<'info, Player>>,    #[account(        init,        seeds = [            b"MONSTER",            game.key().as_ref(),            player.key().as_ref(),            player_account.next_monster_index.to_le_bytes().as_ref()        ],        bump,        payer = player,        space = ANCHOR_DISCRIMINATOR + Monster::INIT_SPACE    )]    pub monster: Account<'info, Monster>,    #[account(mut)]    pub player: Signer<'info>,    pub system_program: Program<'info, System>,}

Viewing all articles
Browse latest Browse all 7906

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>