account_info.rs 1.4 KB

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