state.rs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. use crate::error::ErrorCode;
  2. #[allow(deprecated)]
  3. use crate::CpiAccount;
  4. use crate::{
  5. AccountDeserialize, AccountSerialize, Accounts, AccountsExit, Key, ToAccountInfo,
  6. ToAccountInfos, ToAccountMetas,
  7. };
  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. pub const PROGRAM_STATE_SEED: &str = "unversioned";
  15. /// Boxed container for the program state singleton.
  16. #[derive(Clone)]
  17. #[deprecated]
  18. pub struct ProgramState<'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<'a, T: AccountSerialize + AccountDeserialize + Clone> ProgramState<'a, T> {
  28. fn new(info: AccountInfo<'a>, account: T) -> ProgramState<'a, T> {
  29. Self {
  30. inner: Box::new(Inner { info, account }),
  31. }
  32. }
  33. /// Deserializes the given `info` into a `ProgramState`.
  34. #[inline(never)]
  35. pub fn try_from(
  36. program_id: &Pubkey,
  37. info: &AccountInfo<'a>,
  38. ) -> Result<ProgramState<'a, T>, ProgramError> {
  39. if info.owner != program_id {
  40. return Err(ErrorCode::AccountNotProgramOwned.into());
  41. }
  42. if info.key != &Self::address(program_id) {
  43. solana_program::msg!("Invalid state address");
  44. return Err(ErrorCode::StateInvalidAddress.into());
  45. }
  46. let mut data: &[u8] = &info.try_borrow_data()?;
  47. Ok(ProgramState::new(
  48. info.clone(),
  49. T::try_deserialize(&mut data)?,
  50. ))
  51. }
  52. pub fn seed() -> &'static str {
  53. PROGRAM_STATE_SEED
  54. }
  55. pub fn address(program_id: &Pubkey) -> Pubkey {
  56. address(program_id)
  57. }
  58. }
  59. #[allow(deprecated)]
  60. impl<'info, T> Accounts<'info> for ProgramState<'info, T>
  61. where
  62. T: AccountSerialize + AccountDeserialize + Clone,
  63. {
  64. #[inline(never)]
  65. fn try_accounts(
  66. program_id: &Pubkey,
  67. accounts: &mut &[AccountInfo<'info>],
  68. _ix_data: &[u8],
  69. ) -> Result<Self, ProgramError> {
  70. if accounts.is_empty() {
  71. return Err(ErrorCode::AccountNotEnoughKeys.into());
  72. }
  73. let account = &accounts[0];
  74. *accounts = &accounts[1..];
  75. ProgramState::try_from(program_id, account)
  76. }
  77. }
  78. #[allow(deprecated)]
  79. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> ToAccountMetas
  80. for ProgramState<'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 ProgramState<'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 ProgramState<'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 ProgramState<'info, T>
  110. {
  111. fn as_ref(&self) -> &AccountInfo<'info> {
  112. &self.inner.info
  113. }
  114. }
  115. #[allow(deprecated)]
  116. impl<'a, T: AccountSerialize + AccountDeserialize + Clone> Deref for ProgramState<'a, T> {
  117. type Target = T;
  118. fn deref(&self) -> &Self::Target {
  119. &(*self.inner).account
  120. }
  121. }
  122. #[allow(deprecated)]
  123. impl<'a, T: AccountSerialize + AccountDeserialize + Clone> DerefMut for ProgramState<'a, 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> From<CpiAccount<'info, T>> for ProgramState<'info, T>
  130. where
  131. T: AccountSerialize + AccountDeserialize + Clone,
  132. {
  133. fn from(a: CpiAccount<'info, T>) -> Self {
  134. Self::new(a.to_account_info(), Deref::deref(&a).clone())
  135. }
  136. }
  137. #[allow(deprecated)]
  138. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> AccountsExit<'info>
  139. for ProgramState<'info, T>
  140. {
  141. fn exit(&self, _program_id: &Pubkey) -> ProgramResult {
  142. let info = self.to_account_info();
  143. let mut data = info.try_borrow_mut_data()?;
  144. let dst: &mut [u8] = &mut data;
  145. let mut cursor = std::io::Cursor::new(dst);
  146. self.inner.account.try_serialize(&mut cursor)?;
  147. Ok(())
  148. }
  149. }
  150. pub fn address(program_id: &Pubkey) -> Pubkey {
  151. let (base, _nonce) = Pubkey::find_program_address(&[], program_id);
  152. let seed = PROGRAM_STATE_SEED;
  153. let owner = program_id;
  154. Pubkey::create_with_seed(&base, seed, owner).unwrap()
  155. }
  156. #[allow(deprecated)]
  157. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> Key for ProgramState<'info, T> {
  158. fn key(&self) -> Pubkey {
  159. *self.inner.info.key
  160. }
  161. }