I'm looking for clarification on when and why I should use the 'AccountInfo' type instead of passing an account as a function parameter in anchor_lang.
For example:
Both following programs do the same thing. Both have been tested to work.
Using AccountInfo:
#[program]
mod hello_friend {
use super::*;
pub fn initialize(ctx: Context<Initialize>, friend_address: Pubkey) -> Result<()> {
ctx.accounts.new_account.friend_address = friend_address;
Ok(())
}
}
#[derive(Accounts)]
pub struct Initialize<'info> {
#[account(init, payer = signer, space = 8 + 32)]
pub new_account: Account<'info, NewAccount>,
#[account(mut)]
pub signer: Signer<'info>,
pub system_program: Program<'info, System>,
}
#[account]
pub struct NewAccount {
friend_address: Pubkey,
}
Not using AccountInfo:
#[program]
mod hello_friend {
use super::*;
pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
ctx.accounts.new_account.friend_address = ctx.accounts.friend_address.key();
Ok(())
}
}
#[derive(Accounts)]
pub struct Initialize<'info> {
#[account(init, payer = signer, space = 8 + 32)]
pub new_account: Account<'info, NewAccount>,
/// CHECK: Your friend's wallet address
pub friend_address: AccountInfo<'info>,
#[account(mut)]
pub signer: Signer<'info>,
pub system_program: Program<'info, System>,
}
#[account]
pub struct NewAccount {
friend_address: Pubkey,
}
Is there a reason why I would use one method over the other? Which one is best practice?