account_info.rs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. use crate::error::ErrorCode;
  2. use crate::{Accounts, AccountsExit, Key, ToAccountInfo, ToAccountInfos, ToAccountMetas};
  3. use solana_program::account_info::AccountInfo;
  4. use solana_program::entrypoint::ProgramResult;
  5. use solana_program::instruction::AccountMeta;
  6. use solana_program::program_error::ProgramError;
  7. use solana_program::pubkey::Pubkey;
  8. impl<'info> Accounts<'info> for AccountInfo<'info> {
  9. fn try_accounts(
  10. _program_id: &Pubkey,
  11. accounts: &mut &[AccountInfo<'info>],
  12. _ix_data: &[u8],
  13. ) -> Result<Self, ProgramError> {
  14. if accounts.is_empty() {
  15. return Err(ErrorCode::AccountNotEnoughKeys.into());
  16. }
  17. let account = &accounts[0];
  18. *accounts = &accounts[1..];
  19. Ok(account.clone())
  20. }
  21. }
  22. impl<'info> ToAccountMetas for AccountInfo<'info> {
  23. fn to_account_metas(&self, is_signer: Option<bool>) -> Vec<AccountMeta> {
  24. let is_signer = is_signer.unwrap_or(self.is_signer);
  25. let meta = match self.is_writable {
  26. false => AccountMeta::new_readonly(*self.key, is_signer),
  27. true => AccountMeta::new(*self.key, is_signer),
  28. };
  29. vec![meta]
  30. }
  31. }
  32. impl<'info> ToAccountInfos<'info> for AccountInfo<'info> {
  33. fn to_account_infos(&self) -> Vec<AccountInfo<'info>> {
  34. vec![self.clone()]
  35. }
  36. }
  37. impl<'info> ToAccountInfo<'info> for AccountInfo<'info> {
  38. fn to_account_info(&self) -> AccountInfo<'info> {
  39. self.clone()
  40. }
  41. }
  42. impl<'info> AccountsExit<'info> for AccountInfo<'info> {
  43. fn exit(&self, _program_id: &Pubkey) -> ProgramResult {
  44. // no-op
  45. Ok(())
  46. }
  47. }
  48. impl<'info> Key for AccountInfo<'info> {
  49. fn key(&self) -> Pubkey {
  50. *self.key
  51. }
  52. }