0

I am trying to initialize an escrow account and I keep getting this error.

Here's the instruction code

use crate::state::*;
use anchor_lang::prelude::*;
use anchor_spl::{token::{Mint, TokenAccount, Token}, associated_token::AssociatedToken};

pub fn handler(ctx: Context<InitEscrow>) -> Result<()> {
    let escrow_account = &mut ctx.accounts.escrow_account;
    escrow_account.bump = *ctx.bumps.get("escrow_account").unwrap();

    escrow_account.borrower = ctx.accounts.borrower.key();
    escrow_account.loan = ctx.accounts.loan_account.key();
    escrow_account.nft_mint = ctx.accounts.nft_mint.key();
    escrow_account.escrow_nft_vault = ctx.accounts.escrow_nft_vault.key();
    escrow_account.token_mint = ctx.accounts.token_mint.key();
    escrow_account.escrow_token_vault = ctx.accounts.escrow_token_vault.key();

    Ok(())
}

#[derive(Accounts)]
pub struct InitEscrow<'info> {
  #[account(mut)]
    pub borrower: Signer<'info>,
    #[account(
      init, 
      payer = borrower,
      space = Escrow::LEN,
      seeds = [b"escrow", borrower.key().as_ref(), loan_account.key().as_ref()],
      bump
    )]
    pub escrow_account: Account<'info, Escrow>,
    #[account(
      seeds = [b"loan", borrower.key().as_ref()],
      bump = loan_account.bump
    )]
    pub loan_account: Account<'info, Loan>,
    pub token_mint: Account<'info, Mint>,
    #[account(
      init,
      payer = borrower,
      associated_token::mint = token_mint,
      associated_token::authority = escrow_account
    )]
    pub escrow_token_vault: Account<'info, TokenAccount>,
    pub nft_mint: Account<'info, Mint>,
    #[account(
      init,
      payer = borrower,
      associated_token::mint = nft_mint,
      associated_token::authority = escrow_account
    )]
    pub escrow_nft_vault: Account<'info, TokenAccount>,
    pub token_program: Program<'info, Token>,
    pub associated_token_program: Program<'info, AssociatedToken>,
    pub system_program: Program<'info, System>,
}

Here's the test code:

        await program.methods
            .initEscrow()
            .accounts({
                borrower: borrowerPk,
                tokenMint: tokenMintPk,
                nftMint: nftMintPk,
                escrowAccount: escrowAccountPk,
                escrowNftVault: escrowNftVaultPk,
                escrowTokenVault: escrowTokenVaultPk,
            })
            .signers([borrower])
            .rpc({
                skipPreflight: true,
            });
    });
2
  • Hey. Could you try checking the logs in the .anchor directory in your root folder, or use a try..catch() block in your ts code to catch and log the error? Either of these should give you a more helpful error message. Looking through your code though it's possible you might not setting Escrow::LEN correctly. You can try checking that. The space allocated should be the borsh serialized size for your struct + an extra 8 bytes.
    – Ademola
    Apr 30, 2023 at 20:25
  • Thanks. Got the error. It was ProgramStackError. Fixed it by boxing the escrow account.
    – denimcodes
    May 1, 2023 at 4:38

1 Answer 1

0

I used a try catch block on typescript code and found the specific error. It was ProgramStackError. Fixed it by boxing the Escrow account.

Here's the updated context code:

#[derive(Accounts)]
pub struct InitEscrow<'info> {
  #[account(mut)]
    pub borrower: Signer<'info>,
    #[account(
      init, 
      payer = borrower,
      space = Escrow::LEN,
      seeds = [b"escrow", borrower.key().as_ref(), loan_account.key().as_ref()],
      bump
    )]
    pub escrow_account: Box<Account<'info, Escrow>>,
    #[account(
      seeds = [b"loan", borrower.key().as_ref()],
      bump = loan_account.bump
    )]
    pub loan_account: Account<'info, Loan>,
    pub token_mint: Account<'info, Mint>,
    #[account(
      init,
      payer = borrower,
      associated_token::mint = token_mint,
      associated_token::authority = escrow_account
    )]
    pub escrow_token_vault: Account<'info, TokenAccount>,
    pub nft_mint: Account<'info, Mint>,
    #[account(
      init,
      payer = borrower,
      associated_token::mint = nft_mint,
      associated_token::authority = escrow_account
    )]
    pub escrow_nft_vault: Account<'info, TokenAccount>,
    pub token_program: Program<'info, Token>,
    pub associated_token_program: Program<'info, AssociatedToken>,
    pub system_program: Program<'info, System>,
}

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.