state.rs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #[allow(deprecated)]
  2. use crate::accounts::cpi_account::CpiAccount;
  3. use crate::error::ErrorCode;
  4. use crate::{
  5. AccountDeserialize, AccountSerialize, Accounts, AccountsExit, Result, ToAccountInfo,
  6. ToAccountInfos, 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. pub const PROGRAM_STATE_SEED: &str = "unversioned";
  14. /// Boxed container for the program state singleton.
  15. #[derive(Clone)]
  16. #[deprecated]
  17. pub struct ProgramState<'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<'a, T: AccountSerialize + AccountDeserialize + Clone> ProgramState<'a, T> {
  27. fn new(info: AccountInfo<'a>, account: T) -> ProgramState<'a, T> {
  28. Self {
  29. inner: Box::new(Inner { info, account }),
  30. }
  31. }
  32. /// Deserializes the given `info` into a `ProgramState`.
  33. #[inline(never)]
  34. pub fn try_from(program_id: &Pubkey, info: &AccountInfo<'a>) -> Result<ProgramState<'a, T>> {
  35. if info.owner != program_id {
  36. return Err(ErrorCode::AccountOwnedByWrongProgram.into());
  37. }
  38. if info.key != &Self::address(program_id) {
  39. solana_program::msg!("Invalid state address");
  40. return Err(ErrorCode::StateInvalidAddress.into());
  41. }
  42. let mut data: &[u8] = &info.try_borrow_data()?;
  43. Ok(ProgramState::new(
  44. info.clone(),
  45. T::try_deserialize(&mut data)?,
  46. ))
  47. }
  48. pub fn seed() -> &'static str {
  49. PROGRAM_STATE_SEED
  50. }
  51. pub fn address(program_id: &Pubkey) -> Pubkey {
  52. address(program_id)
  53. }
  54. }
  55. #[allow(deprecated)]
  56. impl<'info, T> Accounts<'info> for ProgramState<'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. _bumps: &mut BTreeMap<String, u8>,
  66. ) -> Result<Self> {
  67. if accounts.is_empty() {
  68. return Err(ErrorCode::AccountNotEnoughKeys.into());
  69. }
  70. let account = &accounts[0];
  71. *accounts = &accounts[1..];
  72. ProgramState::try_from(program_id, account)
  73. }
  74. }
  75. #[allow(deprecated)]
  76. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> ToAccountMetas
  77. for ProgramState<'info, T>
  78. {
  79. fn to_account_metas(&self, is_signer: Option<bool>) -> Vec<AccountMeta> {
  80. let is_signer = is_signer.unwrap_or(self.inner.info.is_signer);
  81. let meta = match self.inner.info.is_writable {
  82. false => AccountMeta::new_readonly(*self.inner.info.key, is_signer),
  83. true => AccountMeta::new(*self.inner.info.key, is_signer),
  84. };
  85. vec![meta]
  86. }
  87. }
  88. #[allow(deprecated)]
  89. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> ToAccountInfos<'info>
  90. for ProgramState<'info, T>
  91. {
  92. fn to_account_infos(&self) -> Vec<AccountInfo<'info>> {
  93. vec![self.inner.info.clone()]
  94. }
  95. }
  96. #[allow(deprecated)]
  97. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> AsRef<AccountInfo<'info>>
  98. for ProgramState<'info, T>
  99. {
  100. fn as_ref(&self) -> &AccountInfo<'info> {
  101. &self.inner.info
  102. }
  103. }
  104. #[allow(deprecated)]
  105. impl<'a, T: AccountSerialize + AccountDeserialize + Clone> Deref for ProgramState<'a, T> {
  106. type Target = T;
  107. fn deref(&self) -> &Self::Target {
  108. &(*self.inner).account
  109. }
  110. }
  111. #[allow(deprecated)]
  112. impl<'a, T: AccountSerialize + AccountDeserialize + Clone> DerefMut for ProgramState<'a, T> {
  113. fn deref_mut(&mut self) -> &mut Self::Target {
  114. &mut DerefMut::deref_mut(&mut self.inner).account
  115. }
  116. }
  117. #[allow(deprecated)]
  118. impl<'info, T> From<CpiAccount<'info, T>> for ProgramState<'info, T>
  119. where
  120. T: AccountSerialize + AccountDeserialize + Clone,
  121. {
  122. fn from(a: CpiAccount<'info, T>) -> Self {
  123. Self::new(a.to_account_info(), Deref::deref(&a).clone())
  124. }
  125. }
  126. #[allow(deprecated)]
  127. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> AccountsExit<'info>
  128. for ProgramState<'info, T>
  129. {
  130. fn exit(&self, _program_id: &Pubkey) -> Result<()> {
  131. let info = self.to_account_info();
  132. let mut data = info.try_borrow_mut_data()?;
  133. let dst: &mut [u8] = &mut data;
  134. let mut cursor = std::io::Cursor::new(dst);
  135. self.inner.account.try_serialize(&mut cursor)?;
  136. Ok(())
  137. }
  138. }
  139. pub fn address(program_id: &Pubkey) -> Pubkey {
  140. let (base, _nonce) = Pubkey::find_program_address(&[], program_id);
  141. let seed = PROGRAM_STATE_SEED;
  142. let owner = program_id;
  143. Pubkey::create_with_seed(&base, seed, owner).unwrap()
  144. }