0

I am trying to anchor init an PDA with a vault account, however accounts are not getting created.

Rust Code:

pub struct Initialize<'info> {
    /// CHECK: This is not dangerous because we don't read or write from this account
    #[account(mut,signer)]
    pub initializer: AccountInfo<'info>,
    /// CHECK: This is not dangerous because we don't read or write from this account
    pub mint: AccountInfo<'info>,
    #[account(init,seeds=[mint.key.as_ref()], bump, payer=initializer, space=201,)]
    pub escrow_account: Box<Account<'info, EscrowAccount>>,
    #[account(init, payer = initializer, token::mint = mint, token::authority = escrow_account,)]
    pub vault_account: Account<'info, TokenAccount>,
    #[account(
        mut,
        constraint = initializer_deposit_token_account.amount >= 1,
        constraint=initializer_deposit_token_account.owner == initializer.key(),
        constraint=initializer_deposit_token_account.mint == mint.key()
    )]
    pub initializer_deposit_token_account: Account<'info, TokenAccount>,
    /// CHECK: This is not dangerous because we don't read or write from this account
    pub system_program: AccountInfo<'info>,
    pub rent: Sysvar<'info, Rent>,
    /// CHECK: This is not dangerous because we don't read or write from this account
    pub token_program: AccountInfo<'info>,
    pub token: Program<'info, Token>,
}

TS Code:

    const [escrowAccount, _nounce] = await PublicKey.findProgramAddress(
      [nftMint.toBuffer()],
      program.programId
    );

    let [vault_account_address, stateBump] = await anchor.web3.PublicKey.findProgramAddress(
      [Buffer.from("vault"),nftMint.toBuffer()], program.programId,
    );

let tx=await program.methods.initialize(_nounce,new anchor.BN(0.001),new anchor.BN(1),new anchor.BN(2),new anchor.BN(20)).accounts({
        initializer: wallet.publicKey,
        escrowAccount: escrowAccount,
        vaultAccount: vault_account_address,
        mint: nftMint,
        systemProgram: anchor.web3.SystemProgram.programId,
        rent: anchor.web3.SYSVAR_RENT_PUBKEY,
        tokenProgram: TOKEN_PROGRAM_ID,
        token: TOKEN_PROGRAM_ID,
        initializerDepositTokenAccount: new PublicKey("8Hj9mdExiKSNKWkb1TuNKCRqyxnkUDGFTWDAXH2iGkoR"),
      })
      .preInstructions([
        await program.account.escrowAccount.createInstruction(wallet.payer),
      //   await createAssociatedTokenAccountInstruction(wallet.publicKey, vault_account_address, escrowAccount, nftMint, program.programId,
      //     ASSOCIATED_TOKEN_PROGRAM_ID),
      ])
      .rpc()

Error Log:

Error: Signature verification failed
    at Transaction.serialize (/node_modules/@solana/web3.js/src/transaction.ts:736:13)
    at AnchorProvider.sendAndConfirm (/node_modules/@project-serum/anchor/src/provider.ts:145:22)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at MethodsBuilder.rpc [as _rpcFn] (/node_modules/@project-serum/anchor/src/program/namespace/rpc.ts:29:16)
end init: 
    1) Initialize escrow
    ✔ Exchange escrow state
    ✔ Initialize escrow and cancel escrow


  3 passing (2s)
  1 failing

  1) smart-contract:
       Initialize escrow:
     Error: Account does not exist FdrJWszxvctmDX8HHfWNufVJcxFeGZdRWjeuScddJTc9
      at AccountClient.fetch (node_modules/@project-serum/anchor/src/program/namespace/account.ts:154:13)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)

2
  • The error says one of the account you passed in the API doesn't exist which means it doesn't exist on the blockchain. Are you using localnet or devnet for the testing?
    – 0xShuk
    Sep 9, 2022 at 9:28
  • using devnet here, this account is the escrow PDA that I am trying to create using anchor init macro Sep 9, 2022 at 11:12

2 Answers 2

1

you can try A couple of things

  1. In your rust code update the seeds for the escrow account as mint.key().as_ref()
  2. and use const [escrowAccount, _nounce] = await anchor.web3.PublicKey.findProgramAddress( [nftMint.toBuffer()], program.programId ); to derive the PDA and console log it to confirm that the same account is being derived.

Also for your vault_account you have not declared any seeds in rust code but in the TS code you are using following seeds [Buffer.from("vault"),nftMint.toBuffer()] to derive the address so this may also be causing the error.

0
0

You must first search for your PDA.

const [PDA, _bump] = await PublicKey
  .findProgramAddress(
    [
      wallet.publicKey.toBuffer(),
    ],
    program.programId
  )

Then replace it.

1
  • Thanks for the response @Mateo, I am creating escorw account with: const [escrowAccount, _nounce] = await PublicKey.findProgramAddress( [nftMint.toBuffer()], program.programId ); and using the derived value in initialize TS call (updated TS code) Sep 11, 2022 at 15:02

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.