2

The front end is below

const newVideo = async () => { console.log("New Video Module") //const wallet = useWallet() let [state_pda] = await anchor.web3.PublicKey.findProgramAddress( [utf8.encode('state'), wallet.publicKey.toBuffer()], program.programId, ) console.log(state_pda is , ${state_pda.toString()});

const stateInfo = await program.account.stateAccount.fetch(state_pda);
console.log("stateInfo is ", stateInfo);

let [video_pda] = await anchor.web3.PublicKey.findProgramAddress(
  [utf8.encode('video'), stateInfo.videoCount.toArrayLike(Buffer, 'be', 8)],
  program.programId,
);

The smart contract is

/// CreateState context
#[derive(Accounts)]
pub struct CreateState<'info> {
    // Authenticating state account
    #[account(
        init,
        seeds = [b"state".as_ref()],
        bump,
        payer = authority,
        space = size_of::<StateAccount>() + 8
    )]
    pub state: Account<'info, StateAccount>,

    // Authority (this is signer who paid transaction fee)
    #[account(mut)]
    pub authority: Signer<'info>,

    /// System program
    /// CHECK: Simple test account for tiktok
    pub system_program: UncheckedAccount<'info>,

    // Token program
    #[account(constraint = token_program.key == &token::ID)]
    pub token_program: Program<'info, Token>,
}

/// CreateVideo context
#[derive(Accounts)]
pub struct CreateVideo<'info> {
    // Authenticate state account
    #[account(mut, seeds = [b"state".as_ref()], bump)]
    pub state: Account<'info, StateAccount>,

    // Authenticate video account
    #[account(
        init,
        // Video account use string "video" and index of video as seeds
        seeds = [b"video".as_ref(), state.video_count.to_be_bytes().as_ref()],
        bump,
        payer = authority,
        space = size_of::<VideoAccount>() + TEXT_LENGTH + USER_NAME_LENGTH + USER_URL_LENGTH+VIDEO_URL_LENGTH+8+32*NUMBER_OF_ALLOWED_LIKES_SPACE // 32 bits in a pubkey and we have 5
    )]
    pub video: Account<'info, VideoAccount>,

    // Authority (this is signer who paid transaction fee)
    #[account(mut)]
    pub authority: Signer<'info>,

    /// System program
    /// CHECK: Simple test account for tiktok
    pub system_program: UncheckedAccount<'info>,

    // Token program
    #[account(constraint = token_program.key == &token::ID)]
    pub token_program: Program<'info, Token>,

    // Clock to save time
    pub clock: Sysvar<'info, Clock>,
}

1
  • Can someone help out with the solution?
    – holyaustin
    Commented Jul 13, 2023 at 18:40

1 Answer 1

3

You’re including wallet in the PDA of state on the front end, but the seeds on the smart contract don’t include wallet. Also I’d use Buffer.from(x, “utf8”) instead of utf8.encode.

2
  • Which line of code above will these changes be effected? Be more explicit Please.
    – holyaustin
    Commented Jul 15, 2023 at 8:37
  • seeds = [b"state".as_ref()], Should include wallet
    – Noah
    Commented Jul 16, 2023 at 18:06

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.