anchor_lang/accounts/
account_info.rs

1//! AccountInfo can be used as a type but
2//! [Unchecked Account](crate::accounts::unchecked_account::UncheckedAccount)
3//! should be used instead.
4
5use crate::error::ErrorCode;
6use crate::{Accounts, AccountsExit, Key, Result, ToAccountInfos, ToAccountMetas};
7use solana_program::account_info::AccountInfo;
8use solana_program::instruction::AccountMeta;
9use solana_program::pubkey::Pubkey;
10use std::collections::BTreeSet;
11
12impl<'info, B> Accounts<'info, B> for AccountInfo<'info> {
13    fn try_accounts(
14        _program_id: &Pubkey,
15        accounts: &mut &[AccountInfo<'info>],
16        _ix_data: &[u8],
17        _bumps: &mut B,
18        _reallocs: &mut BTreeSet<Pubkey>,
19    ) -> Result<Self> {
20        if accounts.is_empty() {
21            return Err(ErrorCode::AccountNotEnoughKeys.into());
22        }
23        let account = &accounts[0];
24        *accounts = &accounts[1..];
25        Ok(account.clone())
26    }
27}
28
29impl ToAccountMetas for AccountInfo<'_> {
30    fn to_account_metas(&self, is_signer: Option<bool>) -> Vec<AccountMeta> {
31        let is_signer = is_signer.unwrap_or(self.is_signer);
32        let meta = match self.is_writable {
33            false => AccountMeta::new_readonly(*self.key, is_signer),
34            true => AccountMeta::new(*self.key, is_signer),
35        };
36        vec![meta]
37    }
38}
39
40impl<'info> ToAccountInfos<'info> for AccountInfo<'info> {
41    fn to_account_infos(&self) -> Vec<AccountInfo<'info>> {
42        vec![self.clone()]
43    }
44}
45
46impl<'info> AccountsExit<'info> for AccountInfo<'info> {}
47
48impl Key for AccountInfo<'_> {
49    fn key(&self) -> Pubkey {
50        *self.key
51    }
52}
OSZAR »