0

I have an instruction where on the client-side, I'm passing down a game_id to the instruction to be used as part of the seed because this instruction will be called a lot when new game instances are required. However, I'm running to a 2006 error pertaining to a seed constraint being violated. I've verified that it's not the account seeds. I've entered those in correctly. So it has to be an issue with the game_id.

I've tried swapping out .as_bytes() for .as_ref() for the seed values and that doesn't work either. However, owner.key().as_ref() without including game_id in the seed at all works. But then I can't create more than 1 game instance which is not the functionality I need

rust instruction

#[derive(Accounts)]
#[instruction(game_id: String)]
pub struct CreateGame<'info> {
    #[account(
    init,
    payer = owner,
    space = 8 + GameData::MAX_SIZE,
    seeds = [GameData::SEED.as_bytes(), game_id.as_bytes()],
    bump
    )]
    pub game: Account<'info, GameData>,
    #[account(
    init,
    seeds = [WordVault::SEED.as_bytes(), game_id.as_bytes()],
    bump,
    payer = owner,
    space = 8 + WordVault::MAX_SIZE
    )]
    pub word_vault: Account<'info, WordVault>,
    #[account(
    init,
    payer = owner,
    seeds = [GameTreasury::SEED.as_bytes(), game_id.as_bytes()],
    space = 8 + GameTreasury::MAX_SIZE,
    bump,
    )]
    pub game_treasury: Account<'info, GameTreasury>,
    #[account(mut)]
    pub global_game_data: Account<'info, GlobalGameData>,
    #[account(mut)]
    pub owner: Signer<'info>,
    pub system_program: Program<'info, System>,
}

client-side hook where instruction is called

const initializeGame = async (wallet: WalletContextState, connection: Connection, gameID: string): Promise<void> => {
        if (!wallet) return;

        let [gameAcctPDA] = web3.PublicKey.findProgramAddressSync(
            [Buffer.from( "GSEED" ), Buffer.from( gameID )],
            PROGRAM_ID
        );
        console.log('gameAcct: ', gameAcctPDA.toString());

        let [wordVaultAcctPDA] = web3.PublicKey.findProgramAddressSync(
            [Buffer.from("WVSEED"), Buffer.from( gameID )],
            PROGRAM_ID
        );
        console.log('wordVaultAcct: ', wordVaultAcctPDA.toString())

        let [gameTreasuryPDA, bump] = web3.PublicKey.findProgramAddressSync(
            [Buffer.from("GTSEED"), Buffer.from( gameID )],
            PROGRAM_ID
        );
        console.log('moneyVaultAcct: ', gameTreasuryPDA.toString())
        console.log('game id: ', gameID)

        const gameAccounts: CreateGameInstructionAccounts = {
            game: gameAcctPDA,
            wordVault: wordVaultAcctPDA,
            gameTreasury: gameTreasuryPDA,
            globalGameData: DEVNET_GLOBAL_GAME_DATA_ACCOUNT,
            owner: wallet.publicKey!
        }

        const gameCreationArgs: CreateGameInstructionArgs = {
            name: 'Test',
            minDeposit: 0.1,
            gameId: gameID
        }

        const transactionInstruction = createCreateGameInstruction(gameAccounts, gameCreationArgs);

        try {
            const tx = new Transaction().add(transactionInstruction);
            tx!.feePayer = wallet!.publicKey!;

            const latestBlockhash = await connection.getLatestBlockhash();
            tx.recentBlockhash = latestBlockhash.blockhash;

            const sent = await wallet.wallet?.adapter?.sendTransaction(tx, connection)
            console.log('tx data: ', sent);
        } catch (error) {
            console.log('error creating game', error);
        }
    };

view where hook is being triggered

const handleCreateGame = async () => {
        // TODO: pull current game id from global game data state
        const gameID = `game_${1}`
        await initializeGame(wallet!, devNetConnection, gameID)
    }
1
  • Have you checked the endianess? Afaik Solana rust defaults to little-endian, whereas typescript is kind of inconsistent with this. Try inversing your Buffer.from call on the client side. Alternatively you could also just have GameID as a u128 or even u256 which would allow you to create tons of games and make serialization simpler.
    – McBain
    Commented Mar 21 at 5:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.