1

While interacting with 2 smart contracts, I need to write a testcase in typescript where I need to pass PDA of 1st contract in second contract, like pub first_contract_pda_account: AccountInfo<'info>, I am getting error Error: AnchorError caused by account: first_contract_pda_account. Error Code: AccountNotInitialized. Error Number: 3012. Error Message: The program expected this account to be already initialized. in my second account. How to get public key generated on first account in my testcases?

Contract A

#[derive(Accounts)]
#[instruction()]
pub struct FirstContract<'info> {
    #[account(
        init,
        seeds = [b"seed"],
        bump,
        payer = payer,
        space = std::mem::size_of::<SomeStruct>() + 8
    )]
    pub first_contract_pda_account: Account<'info, SomeStruct>,
    
    #[account(mut)]
    pub payer = Signer<'info>
}

Contract B

#[derive(Accounts)]
#[instruction()]
pub struct SecondContract<'info> {
    pub first_contract_pda_account: AccountInfo<'info>,

    pub first_program_address: AccountInfo<'info>
}
1
  • my issue is resolved when I pass the exact same PDA of Contract A to Contract B
    – blackhorse
    Commented Jan 23 at 6:23

1 Answer 1

1

You can use find_program_address or create_program_address if you also have the bump available to pass into the instruction.

The only difference is that find derives the PDA and bump, so create is faster when the bump is known.

Typically contract B will get this as AccountInfo or UncheckedAccount, validate it against the PDA found with find/create, and pass it to the CPI. But generally you can skip validation if contract A already validates the PDA, which it seems to do in your case.

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.