program_account.rs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. use crate::error::ErrorCode;
  2. use crate::{
  3. AccountDeserialize, AccountSerialize, Accounts, AccountsClose, AccountsExit, CpiAccount,
  4. ToAccountInfo, ToAccountInfos, ToAccountMetas,
  5. };
  6. use solana_program::account_info::AccountInfo;
  7. use solana_program::entrypoint::ProgramResult;
  8. use solana_program::instruction::AccountMeta;
  9. use solana_program::program_error::ProgramError;
  10. use solana_program::pubkey::Pubkey;
  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. pub struct ProgramAccount<'info, T: AccountSerialize + AccountDeserialize + Clone> {
  16. inner: Box<Inner<'info, T>>,
  17. }
  18. #[derive(Clone)]
  19. struct Inner<'info, T: AccountSerialize + AccountDeserialize + Clone> {
  20. info: AccountInfo<'info>,
  21. account: T,
  22. }
  23. impl<'a, T: AccountSerialize + AccountDeserialize + Clone> ProgramAccount<'a, T> {
  24. pub fn new(info: AccountInfo<'a>, account: T) -> ProgramAccount<'a, T> {
  25. Self {
  26. inner: Box::new(Inner { info, account }),
  27. }
  28. }
  29. /// Deserializes the given `info` into a `ProgramAccount`.
  30. #[inline(never)]
  31. pub fn try_from(info: &AccountInfo<'a>) -> Result<ProgramAccount<'a, T>, ProgramError> {
  32. let mut data: &[u8] = &info.try_borrow_data()?;
  33. Ok(ProgramAccount::new(
  34. info.clone(),
  35. T::try_deserialize(&mut data)?,
  36. ))
  37. }
  38. /// Deserializes the zero-initialized `info` into a `ProgramAccount` without
  39. /// checking the account type. This should only be used upon program account
  40. /// initialization (since the entire account data array is zeroed and thus
  41. /// no account type is set).
  42. #[inline(never)]
  43. pub fn try_from_unchecked(
  44. info: &AccountInfo<'a>,
  45. ) -> Result<ProgramAccount<'a, T>, ProgramError> {
  46. let mut data: &[u8] = &info.try_borrow_data()?;
  47. Ok(ProgramAccount::new(
  48. info.clone(),
  49. T::try_deserialize_unchecked(&mut data)?,
  50. ))
  51. }
  52. pub fn into_inner(self) -> T {
  53. self.inner.account
  54. }
  55. }
  56. impl<'info, T> Accounts<'info> for ProgramAccount<'info, T>
  57. where
  58. T: AccountSerialize + AccountDeserialize + Clone,
  59. {
  60. #[inline(never)]
  61. fn try_accounts(
  62. program_id: &Pubkey,
  63. accounts: &mut &[AccountInfo<'info>],
  64. _ix_data: &[u8],
  65. ) -> Result<Self, ProgramError> {
  66. if accounts.is_empty() {
  67. return Err(ErrorCode::AccountNotEnoughKeys.into());
  68. }
  69. let account = &accounts[0];
  70. *accounts = &accounts[1..];
  71. let pa = ProgramAccount::try_from(account)?;
  72. if pa.inner.info.owner != program_id {
  73. return Err(ErrorCode::AccountNotProgramOwned.into());
  74. }
  75. Ok(pa)
  76. }
  77. }
  78. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> AccountsExit<'info>
  79. for ProgramAccount<'info, T>
  80. {
  81. fn exit(&self, _program_id: &Pubkey) -> ProgramResult {
  82. let info = self.to_account_info();
  83. let mut data = info.try_borrow_mut_data()?;
  84. let dst: &mut [u8] = &mut data;
  85. let mut cursor = std::io::Cursor::new(dst);
  86. self.inner.account.try_serialize(&mut cursor)?;
  87. Ok(())
  88. }
  89. }
  90. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> AccountsClose<'info>
  91. for ProgramAccount<'info, T>
  92. {
  93. fn close(&self, sol_destination: AccountInfo<'info>) -> ProgramResult {
  94. crate::common::close(self.to_account_info(), sol_destination)
  95. }
  96. }
  97. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> ToAccountMetas
  98. for ProgramAccount<'info, T>
  99. {
  100. fn to_account_metas(&self, is_signer: Option<bool>) -> Vec<AccountMeta> {
  101. let is_signer = is_signer.unwrap_or(self.inner.info.is_signer);
  102. let meta = match self.inner.info.is_writable {
  103. false => AccountMeta::new_readonly(*self.inner.info.key, is_signer),
  104. true => AccountMeta::new(*self.inner.info.key, is_signer),
  105. };
  106. vec![meta]
  107. }
  108. }
  109. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> ToAccountInfos<'info>
  110. for ProgramAccount<'info, T>
  111. {
  112. fn to_account_infos(&self) -> Vec<AccountInfo<'info>> {
  113. vec![self.inner.info.clone()]
  114. }
  115. }
  116. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> ToAccountInfo<'info>
  117. for ProgramAccount<'info, T>
  118. {
  119. fn to_account_info(&self) -> AccountInfo<'info> {
  120. self.inner.info.clone()
  121. }
  122. }
  123. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> AsRef<AccountInfo<'info>>
  124. for ProgramAccount<'info, T>
  125. {
  126. fn as_ref(&self) -> &AccountInfo<'info> {
  127. &self.inner.info
  128. }
  129. }
  130. impl<'a, T: AccountSerialize + AccountDeserialize + Clone> Deref for ProgramAccount<'a, T> {
  131. type Target = T;
  132. fn deref(&self) -> &Self::Target {
  133. &(*self.inner).account
  134. }
  135. }
  136. impl<'a, T: AccountSerialize + AccountDeserialize + Clone> DerefMut for ProgramAccount<'a, T> {
  137. fn deref_mut(&mut self) -> &mut Self::Target {
  138. &mut DerefMut::deref_mut(&mut self.inner).account
  139. }
  140. }
  141. impl<'info, T> From<CpiAccount<'info, T>> for ProgramAccount<'info, T>
  142. where
  143. T: AccountSerialize + AccountDeserialize + Clone,
  144. {
  145. fn from(a: CpiAccount<'info, T>) -> Self {
  146. Self::new(a.to_account_info(), Deref::deref(&a).clone())
  147. }
  148. }