DEV Community

Cover image for Solana Anchor convert AccountInfo to Account<'info,T>
Lazar Lukic
Lazar Lukic

Posted on

Solana Anchor convert AccountInfo to Account<'info,T>

Hey there!

In this short article I'll show you how to convert AccountInfo or UncheckedAccount to Account. We assume that the given account is an PDA.

For a given struct:

pub struct User {
    pub account: Pubkey,
}
Enter fullscreen mode Exit fullscreen mode

And in our instructions we have an:

#[derive(Accounts)]
#[instruction()]
pub struct UserInstruction<'info> {
    #[account()]
    /// CHECK:
    pub user: AccountInfo<'info>,
Enter fullscreen mode Exit fullscreen mode
pub fn get_user(ctx: Context<UserInstruction>) -> Result<()> {
    let user: Account<User> = Account::try_from(&ctx.accounts.user)?;
    // do something here
    Ok(())
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)