cpi_account.rs 3.6 KB

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