program_account.rs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #[allow(deprecated)]
  2. use crate::accounts::cpi_account::CpiAccount;
  3. use crate::error::ErrorCode;
  4. use crate::*;
  5. use solana_program::account_info::AccountInfo;
  6. use solana_program::entrypoint::ProgramResult;
  7. use solana_program::instruction::AccountMeta;
  8. use solana_program::program_error::ProgramError;
  9. use solana_program::pubkey::Pubkey;
  10. use std::collections::BTreeMap;
  11. use std::ops::{Deref, DerefMut};
  12. /// Boxed container for a deserialized `account`. Use this to reference any
  13. /// account owned by the currently executing program.
  14. #[derive(Clone)]
  15. #[deprecated(since = "0.15.0", note = "Please use Account instead")]
  16. pub struct ProgramAccount<'info, T: AccountSerialize + AccountDeserialize + Clone> {
  17. inner: Box<Inner<'info, T>>,
  18. }
  19. #[derive(Clone)]
  20. struct Inner<'info, T: AccountSerialize + AccountDeserialize + Clone> {
  21. info: AccountInfo<'info>,
  22. account: T,
  23. }
  24. #[allow(deprecated)]
  25. impl<'a, T: AccountSerialize + AccountDeserialize + Clone> ProgramAccount<'a, T> {
  26. fn new(info: AccountInfo<'a>, account: T) -> ProgramAccount<'a, T> {
  27. Self {
  28. inner: Box::new(Inner { info, account }),
  29. }
  30. }
  31. /// Deserializes the given `info` into a `ProgramAccount`.
  32. #[inline(never)]
  33. pub fn try_from(
  34. program_id: &Pubkey,
  35. info: &AccountInfo<'a>,
  36. ) -> Result<ProgramAccount<'a, T>, ProgramError> {
  37. if info.owner != program_id {
  38. return Err(ErrorCode::AccountOwnedByWrongProgram.into());
  39. }
  40. let mut data: &[u8] = &info.try_borrow_data()?;
  41. Ok(ProgramAccount::new(
  42. info.clone(),
  43. T::try_deserialize(&mut data)?,
  44. ))
  45. }
  46. /// Deserializes the given `info` into a `ProgramAccount` without checking
  47. /// the account discriminator. Be careful when using this and avoid it if
  48. /// possible.
  49. #[inline(never)]
  50. pub fn try_from_unchecked(
  51. program_id: &Pubkey,
  52. info: &AccountInfo<'a>,
  53. ) -> Result<ProgramAccount<'a, T>, ProgramError> {
  54. if info.owner != program_id {
  55. return Err(ErrorCode::AccountOwnedByWrongProgram.into());
  56. }
  57. let mut data: &[u8] = &info.try_borrow_data()?;
  58. Ok(ProgramAccount::new(
  59. info.clone(),
  60. T::try_deserialize_unchecked(&mut data)?,
  61. ))
  62. }
  63. pub fn into_inner(self) -> T {
  64. self.inner.account
  65. }
  66. }
  67. #[allow(deprecated)]
  68. impl<'info, T> Accounts<'info> for ProgramAccount<'info, T>
  69. where
  70. T: AccountSerialize + AccountDeserialize + Clone,
  71. {
  72. #[inline(never)]
  73. fn try_accounts(
  74. program_id: &Pubkey,
  75. accounts: &mut &[AccountInfo<'info>],
  76. _ix_data: &[u8],
  77. _bumps: &mut BTreeMap<String, u8>,
  78. ) -> Result<Self, ProgramError> {
  79. if accounts.is_empty() {
  80. return Err(ErrorCode::AccountNotEnoughKeys.into());
  81. }
  82. let account = &accounts[0];
  83. *accounts = &accounts[1..];
  84. ProgramAccount::try_from(program_id, account)
  85. }
  86. }
  87. #[allow(deprecated)]
  88. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> AccountsExit<'info>
  89. for ProgramAccount<'info, T>
  90. {
  91. fn exit(&self, _program_id: &Pubkey) -> ProgramResult {
  92. let info = self.to_account_info();
  93. let mut data = info.try_borrow_mut_data()?;
  94. // Chop off the header.
  95. let dst: &mut [u8] = &mut data[8..];
  96. let mut cursor = std::io::Cursor::new(dst);
  97. self.inner.account.try_serialize(&mut cursor)?;
  98. Ok(())
  99. }
  100. }
  101. #[allow(deprecated)]
  102. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> AccountsClose<'info>
  103. for ProgramAccount<'info, T>
  104. {
  105. fn close(&self, sol_destination: AccountInfo<'info>) -> ProgramResult {
  106. crate::common::close(self.to_account_info(), sol_destination)
  107. }
  108. }
  109. #[allow(deprecated)]
  110. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> ToAccountMetas
  111. for ProgramAccount<'info, T>
  112. {
  113. fn to_account_metas(&self, is_signer: Option<bool>) -> Vec<AccountMeta> {
  114. let is_signer = is_signer.unwrap_or(self.inner.info.is_signer);
  115. let meta = match self.inner.info.is_writable {
  116. false => AccountMeta::new_readonly(*self.inner.info.key, is_signer),
  117. true => AccountMeta::new(*self.inner.info.key, is_signer),
  118. };
  119. vec![meta]
  120. }
  121. }
  122. #[allow(deprecated)]
  123. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> ToAccountInfos<'info>
  124. for ProgramAccount<'info, T>
  125. {
  126. fn to_account_infos(&self) -> Vec<AccountInfo<'info>> {
  127. vec![self.inner.info.clone()]
  128. }
  129. }
  130. #[allow(deprecated)]
  131. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> AsRef<AccountInfo<'info>>
  132. for ProgramAccount<'info, T>
  133. {
  134. fn as_ref(&self) -> &AccountInfo<'info> {
  135. &self.inner.info
  136. }
  137. }
  138. #[allow(deprecated)]
  139. impl<'a, T: AccountSerialize + AccountDeserialize + Clone> Deref for ProgramAccount<'a, T> {
  140. type Target = T;
  141. fn deref(&self) -> &Self::Target {
  142. &(*self.inner).account
  143. }
  144. }
  145. #[allow(deprecated)]
  146. impl<'a, T: AccountSerialize + AccountDeserialize + Clone> DerefMut for ProgramAccount<'a, T> {
  147. fn deref_mut(&mut self) -> &mut Self::Target {
  148. #[cfg(feature = "anchor-debug")]
  149. if !self.inner.info.is_writable {
  150. solana_program::msg!("The given ProgramAccount is not mutable");
  151. panic!();
  152. }
  153. &mut DerefMut::deref_mut(&mut self.inner).account
  154. }
  155. }
  156. #[allow(deprecated)]
  157. impl<'info, T> From<CpiAccount<'info, T>> for ProgramAccount<'info, T>
  158. where
  159. T: AccountSerialize + AccountDeserialize + Clone,
  160. {
  161. fn from(a: CpiAccount<'info, T>) -> Self {
  162. Self::new(a.to_account_info(), Deref::deref(&a).clone())
  163. }
  164. }
  165. #[cfg(not(feature = "deprecated-layout"))]
  166. impl<'a, T: AccountSerialize + AccountDeserialize + Clone> Bump for ProgramAccount<'a, T> {
  167. fn seed(&self) -> u8 {
  168. self.inner.info.data.borrow()[1]
  169. }
  170. }