How to initialize a PDA before the CPI call? other wise its giving me AccountOwnedByWrongProgram

How to initialize a PDA before the CPI call? other wise its giving me AccountOwnedByWrongProgram

Manage alerts

Loading saved threads...

Abhishek Faliya · External communityPost link
External question — Solana Stack Exchange Author: Abhishek Faliya Original post: https://solana.stackexchange.com/questions/5018 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I'm trying to do a CPI call to my main program, which is #[derive(Accounts)] pub struct RegisterClient<'info> { #[account(mut)] pub owner: Signer<'info>, #[account(init, payer= owner, space=56,seeds= ["client".as_ref(), owner.key().as_ref()], bump)] pub client: AccountLoader<'info, Client>, pub system_program: Program<'info, System>, } from this program use anchor_lang::prelude::*; use sdk_agent; use sdk_agent::instruction::RegisterClient as Reg; use sdk_agent::Client; use sdk_agent::RegisterClient; declare_id!("As7EtU3YKAiKmagtnFXnQPCdRXsYs7rk3cXGzN5WN2HF"); #[program] pub mod sample_program { use anchor_lang::solana_program::{ instruction, program::{invoke, invoke_signed, invoke_unchecked}, system_instruction::create_account, system_program, }; use super::*; pub fn cpi(ctx: Context<CPI>) -> Result<()> { msg!("here"); let client_name = String::from("testing"); let cpi_program = ctx.accounts.agent.to_account_info(); let account = vec![ ctx.accounts.owner.to_account_info(), ctx.accounts.client.to_account_info(), ctx.accounts.system_program.to_account_info(), ]; invoke( &create_account( ctx.accounts.owner.key, &ctx.accounts.client.key(), 1280640, 56, ctx.accounts.agent.key, ), &account, )?; msg!("here"); let accounts = sdk_agent::cpi::accounts::RegisterClient { owner: ctx.accounts.owner.to_account_info(), client: ctx.accounts.client.to_account_info(), system_program: ctx.accounts.system_program.to_account_info(), }; let cpi_context = CpiContext::new(cpi_program, accounts); sdk_agent::cpi::register_client(cpi_context, client_name)?; // invoke(&instruction, accounts.as_slice()); msg!("here"); Ok(()) } } #[derive(Accounts)] pub struct CPI<'info> { #[account(mut)] pub owner: Signer<'info>, pub client: AccountLoader<'info, Client>, ///CHECK pub agent: UncheckedAccount<'info>, pub system_program: Program<'info, System>, } here client account should be initialized before hand,otherwise its throwing error, i tried to use create_account But the the program is not reaching there , Probably gets stuck here checking this pub client: AccountLoader<'info, Client>, i can't convert it to Unchecked Account or Account_info since the Cpi_context require it to be account_loader struct. I tried to initialize this account in the tests const _tx = new anchor.web3.Transaction().add( anchor.web3.SystemProgram.createAccount({ fromPubkey: program.provider.publicKey, newAccountPubkey: client, space: 56, programId: agent, lamports, }) ); await program.provider.sendAndConfirm(mint_tx); But signature verificatrion is failing , cause maybe it requires The Client seed or keypair to sign it as well. can anyone help me tackle this?
Quote
Report
Fedoras · External communityPost link
External answer — Solana Stack Exchange Author: Fedoras Original post: https://solana.stackexchange.com/a/5019 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I feel like you should take a look at this implementation for account loader: pub fn try_from_unchecked( _program_id: &Pubkey, acc_info: &AccountInfo<'info> ) -> Result<AccountLoader<'info, T>> Constructs a new Loader from an uninitialized account. So I would suggest you to pass account as unchecked account to the “cpi” instruction of your second program. And then your second program constructs account loader from it and invokes your first program via CPI. This way you won’t need to initialize this account beforehand. However, if you want to initialize program account, you should understand that since you are using anchor, anchor assigns 8 bytes discriminator to the account so it will know how to work with data. So at least for this reason you should avoid initialising accounts externally, unless u accurately copy the logic of discriminator generation.
Quote
Report

Post Reply

Checking account access…
How to initialize a PDA before the CPI call? other wise its giving me AccountOwnedByWrongProgram | Forex.com.bd