cpi_state.rs 4.1 KB

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