cpi_state.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. use crate::error::ErrorCode;
  2. #[allow(deprecated)]
  3. use crate::{accounts::state::ProgramState, context::CpiStateContext};
  4. use crate::{
  5. AccountDeserialize, AccountSerialize, Accounts, AccountsExit, Result, ToAccountInfos,
  6. ToAccountMetas,
  7. };
  8. use solana_program::account_info::AccountInfo;
  9. use solana_program::instruction::AccountMeta;
  10. use solana_program::pubkey::Pubkey;
  11. use std::collections::BTreeMap;
  12. use std::ops::{Deref, DerefMut};
  13. /// Boxed container for the program state singleton, used when the state
  14. /// is for a program not currently executing.
  15. #[derive(Clone)]
  16. #[deprecated]
  17. pub struct CpiState<'info, T: AccountSerialize + AccountDeserialize + Clone> {
  18. inner: Box<Inner<'info, T>>,
  19. }
  20. #[derive(Clone)]
  21. struct Inner<'info, T: AccountSerialize + AccountDeserialize + Clone> {
  22. info: AccountInfo<'info>,
  23. account: T,
  24. }
  25. #[allow(deprecated)]
  26. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> CpiState<'info, T> {
  27. pub fn new(i: AccountInfo<'info>, account: T) -> CpiState<'info, T> {
  28. Self {
  29. inner: Box::new(Inner { info: i, account }),
  30. }
  31. }
  32. /// Deserializes the given `info` into a `CpiState`.
  33. #[inline(never)]
  34. pub fn try_from(info: &AccountInfo<'info>) -> Result<CpiState<'info, T>> {
  35. let mut data: &[u8] = &info.try_borrow_data()?;
  36. Ok(CpiState::new(info.clone(), T::try_deserialize(&mut data)?))
  37. }
  38. fn seed() -> &'static str {
  39. ProgramState::<T>::seed()
  40. }
  41. pub fn address(program_id: &Pubkey) -> Pubkey {
  42. let (base, _nonce) = Pubkey::find_program_address(&[], program_id);
  43. let seed = Self::seed();
  44. let owner = program_id;
  45. Pubkey::create_with_seed(&base, seed, owner).unwrap()
  46. }
  47. /// Convenience api for creating a `CpiStateContext`.
  48. pub fn context<'a, 'b, 'c, A: Accounts<'info>>(
  49. &self,
  50. program: AccountInfo<'info>,
  51. accounts: A,
  52. ) -> CpiStateContext<'a, 'b, 'c, 'info, A> {
  53. CpiStateContext::new(program, self.inner.info.clone(), accounts)
  54. }
  55. }
  56. #[allow(deprecated)]
  57. impl<'info, T> Accounts<'info> for CpiState<'info, T>
  58. where
  59. T: AccountSerialize + AccountDeserialize + Clone,
  60. {
  61. #[inline(never)]
  62. fn try_accounts(
  63. _program_id: &Pubkey,
  64. accounts: &mut &[AccountInfo<'info>],
  65. _ix_data: &[u8],
  66. _bumps: &mut BTreeMap<String, u8>,
  67. ) -> Result<Self> {
  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> AsRef<AccountInfo<'info>>
  101. for CpiState<'info, T>
  102. {
  103. fn as_ref(&self) -> &AccountInfo<'info> {
  104. &self.inner.info
  105. }
  106. }
  107. #[allow(deprecated)]
  108. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> Deref for CpiState<'info, T> {
  109. type Target = T;
  110. fn deref(&self) -> &Self::Target {
  111. &(*self.inner).account
  112. }
  113. }
  114. #[allow(deprecated)]
  115. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> DerefMut for CpiState<'info, T> {
  116. fn deref_mut(&mut self) -> &mut Self::Target {
  117. &mut DerefMut::deref_mut(&mut self.inner).account
  118. }
  119. }
  120. #[allow(deprecated)]
  121. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> AccountsExit<'info>
  122. for CpiState<'info, T>
  123. {
  124. }