5

If I deploy a program P1 which initialises a new account A1 and update the rvalue to for instance 9

#[derive(Accounts)]
pub struct Initialize<'info> {
    
    #[account(init, payer = signer, space = 8 + 8)]
    pub new_account: Account<'info, NewAccount>,
    #[account(mut)]
    pub signer: Signer<'info>,
    pub system_program: Program<'info, System>,
}

#[account]
pub struct NewAccount {
    rvalue: u64
}

Now if we deploy Program P2 and want to read the rvalue as 9 which is saved in A1. Any ideas how to do that ?

1
  • Can I get the repo link? I am facing the same issue. Commented Oct 18, 2023 at 11:36

1 Answer 1

4

That shouldn't be a problem, all accounts are readable by programs, regardless of who owns them. You just need access to the NewAccount structure on P2 so Anchor knows how to deserialise it.

To do this, you can import P1 using the no-entrypoint feature on your Cargo.toml. With that flag, you can access everything on P1 as a library instead of a standalone program.

The Cargo.toml would look something like:

...

[dependencies.P1]
path = "../programs/P1"
features = ["no-entrypoint"]

Once you have imported P1 on P2, it's as easy as creating a new instruction context and providing the account you initialised as one of the accounts of the instruction.

use P1::state::NewAccount;

#[derive(Accounts)]
pub struct UseNewAccount<'info> {
 
    pub new_account: Account<'info, NewAccount>,

    ...

}

And then, on the function that handles the instruction, you'll be able to use ctx.accounts.new_account.rvalue which will give you 9.

I think I'm not missing anything, let me know otherwise :)

Edit 01: The explanation above has turned into a complete example on this github repository .

4
  • So on solana devnet i deployed program at 721WuW2yfmZvyAVJu4hY4tjELtUNq6CLbM4HUhGSKGQ4 and used it to init a new_account at 8P98zdUzy78TReciQ17C3spiYrRN2vWb2zy6JSnGom9D Now I've deployed a program at D9FmsxUEsDRZyMffypMeLVTPoSax7JQ5jjp3nrVNKWzQ But I am unable to fetch the data. It keeps saying The given account is not owned by the executing program Commented Jul 20, 2022 at 6:35
  • Have you perhaps marked new_account as #[account(mut)] (making it mutable) on P2? I haven't tried but I assume you can't mutate an account you don't own and that's where the error is coming from.
    – nf0x
    Commented Jul 20, 2022 at 12:05
  • I actually did not mark it as #[account(mut)]. Would love if someone can try the above mentioned solution so that I can check if only I am facing this problem Commented Jul 20, 2022 at 12:30
  • There you go, this worked on my end: github.com/nf0x/deserialise-pda-from-a-different-program. If you compile it, change the program ids to the ones on your target, and run anchor test, the tests should pass. Note that there are hardcoded program ids on tests/T.ts, change those too :)
    – nf0x
    Commented Jul 20, 2022 at 14:09

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.