cpi_account.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. use crate::error::ErrorCode;
  2. use crate::*;
  3. use solana_program::account_info::AccountInfo;
  4. use solana_program::entrypoint::ProgramResult;
  5. use solana_program::instruction::AccountMeta;
  6. use solana_program::program_error::ProgramError;
  7. use solana_program::pubkey::Pubkey;
  8. use std::ops::{Deref, DerefMut};
  9. /// Container for any account *not* owned by the current program.
  10. #[derive(Clone)]
  11. pub struct CpiAccount<'a, T: AccountDeserialize + Clone> {
  12. info: AccountInfo<'a>,
  13. account: Box<T>,
  14. }
  15. impl<'a, T: AccountDeserialize + Clone> CpiAccount<'a, T> {
  16. fn new(info: AccountInfo<'a>, account: Box<T>) -> CpiAccount<'a, T> {
  17. Self { info, account }
  18. }
  19. /// Deserializes the given `info` into a `CpiAccount`.
  20. pub fn try_from(info: &AccountInfo<'a>) -> Result<CpiAccount<'a, T>, ProgramError> {
  21. let mut data: &[u8] = &info.try_borrow_data()?;
  22. Ok(CpiAccount::new(
  23. info.clone(),
  24. Box::new(T::try_deserialize(&mut data)?),
  25. ))
  26. }
  27. pub fn try_from_unchecked(info: &AccountInfo<'a>) -> Result<CpiAccount<'a, T>, ProgramError> {
  28. Self::try_from(info)
  29. }
  30. /// Reloads the account from storage. This is useful, for example, when
  31. /// observing side effects after CPI.
  32. pub fn reload(&mut self) -> ProgramResult {
  33. let mut data: &[u8] = &self.info.try_borrow_data()?;
  34. self.account = Box::new(T::try_deserialize(&mut data)?);
  35. Ok(())
  36. }
  37. }
  38. impl<'info, T> Accounts<'info> for CpiAccount<'info, T>
  39. where
  40. T: AccountDeserialize + Clone,
  41. {
  42. #[inline(never)]
  43. fn try_accounts(
  44. _program_id: &Pubkey,
  45. accounts: &mut &[AccountInfo<'info>],
  46. _ix_data: &[u8],
  47. ) -> Result<Self, ProgramError> {
  48. if accounts.is_empty() {
  49. return Err(ErrorCode::AccountNotEnoughKeys.into());
  50. }
  51. let account = &accounts[0];
  52. *accounts = &accounts[1..];
  53. // No owner check is done here.
  54. let pa = CpiAccount::try_from(account)?;
  55. Ok(pa)
  56. }
  57. }
  58. impl<'info, T: AccountDeserialize + Clone> ToAccountMetas for CpiAccount<'info, T> {
  59. fn to_account_metas(&self, is_signer: Option<bool>) -> Vec<AccountMeta> {
  60. let is_signer = is_signer.unwrap_or(self.info.is_signer);
  61. let meta = match self.info.is_writable {
  62. false => AccountMeta::new_readonly(*self.info.key, is_signer),
  63. true => AccountMeta::new(*self.info.key, is_signer),
  64. };
  65. vec![meta]
  66. }
  67. }
  68. impl<'info, T: AccountDeserialize + Clone> ToAccountInfos<'info> for CpiAccount<'info, T> {
  69. fn to_account_infos(&self) -> Vec<AccountInfo<'info>> {
  70. vec![self.info.clone()]
  71. }
  72. }
  73. impl<'info, T: AccountDeserialize + Clone> ToAccountInfo<'info> for CpiAccount<'info, T> {
  74. fn to_account_info(&self) -> AccountInfo<'info> {
  75. self.info.clone()
  76. }
  77. }
  78. impl<'info, T: AccountDeserialize + Clone> AsRef<AccountInfo<'info>> for CpiAccount<'info, T> {
  79. fn as_ref(&self) -> &AccountInfo<'info> {
  80. &self.info
  81. }
  82. }
  83. impl<'a, T: AccountDeserialize + Clone> Deref for CpiAccount<'a, T> {
  84. type Target = T;
  85. fn deref(&self) -> &Self::Target {
  86. &self.account
  87. }
  88. }
  89. impl<'a, T: AccountDeserialize + Clone> DerefMut for CpiAccount<'a, T> {
  90. fn deref_mut(&mut self) -> &mut Self::Target {
  91. &mut self.account
  92. }
  93. }
  94. impl<'info, T: AccountDeserialize + Clone> AccountsExit<'info> for CpiAccount<'info, T> {
  95. fn exit(&self, _program_id: &Pubkey) -> ProgramResult {
  96. // no-op
  97. Ok(())
  98. }
  99. }
  100. impl<'info, T: AccountDeserialize + Clone> Key for CpiAccount<'info, T> {
  101. fn key(&self) -> Pubkey {
  102. *self.info.key
  103. }
  104. }
  105. impl<'info, T> From<Account<'info, T>> for CpiAccount<'info, T>
  106. where
  107. T: AccountSerialize + AccountDeserialize + Owner + Clone,
  108. {
  109. fn from(a: Account<'info, T>) -> Self {
  110. Self::new(a.to_account_info(), Box::new(a.into_inner()))
  111. }
  112. }