cpi_state.rs 4.1 KB

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