cpi_state.rs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. use crate::error::ErrorCode;
  2. use crate::{
  3. AccountDeserialize, AccountSerialize, Accounts, AccountsExit, Key, ToAccountInfo,
  4. ToAccountInfos, ToAccountMetas,
  5. };
  6. #[allow(deprecated)]
  7. use crate::{CpiStateContext, ProgramState};
  8. use solana_program::account_info::AccountInfo;
  9. use solana_program::entrypoint::ProgramResult;
  10. use solana_program::instruction::AccountMeta;
  11. use solana_program::program_error::ProgramError;
  12. use solana_program::pubkey::Pubkey;
  13. use std::ops::{Deref, DerefMut};
  14. /// Boxed container for the program state singleton, used when the state
  15. /// is for a program not currently executing.
  16. #[derive(Clone)]
  17. #[deprecated]
  18. pub struct CpiState<'info, T: AccountSerialize + AccountDeserialize + Clone> {
  19. inner: Box<Inner<'info, T>>,
  20. }
  21. #[derive(Clone)]
  22. struct Inner<'info, T: AccountSerialize + AccountDeserialize + Clone> {
  23. info: AccountInfo<'info>,
  24. account: T,
  25. }
  26. #[allow(deprecated)]
  27. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> CpiState<'info, T> {
  28. pub fn new(i: AccountInfo<'info>, account: T) -> CpiState<'info, T> {
  29. Self {
  30. inner: Box::new(Inner { info: i, account }),
  31. }
  32. }
  33. /// Deserializes the given `info` into a `CpiState`.
  34. #[inline(never)]
  35. pub fn try_from(info: &AccountInfo<'info>) -> Result<CpiState<'info, T>, ProgramError> {
  36. let mut data: &[u8] = &info.try_borrow_data()?;
  37. Ok(CpiState::new(info.clone(), T::try_deserialize(&mut data)?))
  38. }
  39. fn seed() -> &'static str {
  40. ProgramState::<T>::seed()
  41. }
  42. pub fn address(program_id: &Pubkey) -> Pubkey {
  43. let (base, _nonce) = Pubkey::find_program_address(&[], program_id);
  44. let seed = Self::seed();
  45. let owner = program_id;
  46. Pubkey::create_with_seed(&base, seed, owner).unwrap()
  47. }
  48. /// Convenience api for creating a `CpiStateContext`.
  49. pub fn context<'a, 'b, 'c, A: Accounts<'info>>(
  50. &self,
  51. program: AccountInfo<'info>,
  52. accounts: A,
  53. ) -> CpiStateContext<'a, 'b, 'c, 'info, A> {
  54. CpiStateContext::new(program, self.inner.info.clone(), accounts)
  55. }
  56. }
  57. #[allow(deprecated)]
  58. impl<'info, T> Accounts<'info> for CpiState<'info, T>
  59. where
  60. T: AccountSerialize + AccountDeserialize + Clone,
  61. {
  62. #[inline(never)]
  63. fn try_accounts(
  64. _program_id: &Pubkey,
  65. accounts: &mut &[AccountInfo<'info>],
  66. _ix_data: &[u8],
  67. ) -> Result<Self, ProgramError> {
  68. if accounts.is_empty() {
  69. return Err(ErrorCode::AccountNotEnoughKeys.into());
  70. }
  71. let account = &accounts[0];
  72. *accounts = &accounts[1..];
  73. // No owner or address check is done here. One must use the
  74. // #[account(state = <account-name>)] constraint.
  75. CpiState::try_from(account)
  76. }
  77. }
  78. #[allow(deprecated)]
  79. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> ToAccountMetas
  80. for CpiState<'info, T>
  81. {
  82. fn to_account_metas(&self, is_signer: Option<bool>) -> Vec<AccountMeta> {
  83. let is_signer = is_signer.unwrap_or(self.inner.info.is_signer);
  84. let meta = match self.inner.info.is_writable {
  85. false => AccountMeta::new_readonly(*self.inner.info.key, is_signer),
  86. true => AccountMeta::new(*self.inner.info.key, is_signer),
  87. };
  88. vec![meta]
  89. }
  90. }
  91. #[allow(deprecated)]
  92. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> ToAccountInfos<'info>
  93. for CpiState<'info, T>
  94. {
  95. fn to_account_infos(&self) -> Vec<AccountInfo<'info>> {
  96. vec![self.inner.info.clone()]
  97. }
  98. }
  99. #[allow(deprecated)]
  100. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> ToAccountInfo<'info>
  101. for CpiState<'info, T>
  102. {
  103. fn to_account_info(&self) -> AccountInfo<'info> {
  104. self.inner.info.clone()
  105. }
  106. }
  107. #[allow(deprecated)]
  108. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> AsRef<AccountInfo<'info>>
  109. for CpiState<'info, T>
  110. {
  111. fn as_ref(&self) -> &AccountInfo<'info> {
  112. &self.inner.info
  113. }
  114. }
  115. #[allow(deprecated)]
  116. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> Deref for CpiState<'info, T> {
  117. type Target = T;
  118. fn deref(&self) -> &Self::Target {
  119. &(*self.inner).account
  120. }
  121. }
  122. #[allow(deprecated)]
  123. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> DerefMut for CpiState<'info, T> {
  124. fn deref_mut(&mut self) -> &mut Self::Target {
  125. &mut DerefMut::deref_mut(&mut self.inner).account
  126. }
  127. }
  128. #[allow(deprecated)]
  129. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> AccountsExit<'info>
  130. for CpiState<'info, T>
  131. {
  132. fn exit(&self, _program_id: &Pubkey) -> ProgramResult {
  133. // no-op
  134. Ok(())
  135. }
  136. }
  137. #[allow(deprecated)]
  138. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> Key for CpiState<'info, T> {
  139. fn key(&self) -> Pubkey {
  140. *self.inner.info.key
  141. }
  142. }