karima_anchor_lang/
cpi_state.rs1use crate::error::ErrorCode;
2use crate::{
3 AccountDeserialize, AccountSerialize, Accounts, AccountsExit, Key, ToAccountInfo,
4 ToAccountInfos, ToAccountMetas,
5};
6#[allow(deprecated)]
7use crate::{CpiStateContext, ProgramState};
8use solana_program::account_info::AccountInfo;
9use solana_program::entrypoint::ProgramResult;
10use solana_program::instruction::AccountMeta;
11use solana_program::program_error::ProgramError;
12use solana_program::pubkey::Pubkey;
13use std::ops::{Deref, DerefMut};
14
15#[derive(Clone)]
18#[deprecated]
19pub struct CpiState<'info, T: AccountSerialize + AccountDeserialize + Clone> {
20 inner: Box<Inner<'info, T>>,
21}
22
23#[derive(Clone)]
24struct Inner<'info, T: AccountSerialize + AccountDeserialize + Clone> {
25 info: AccountInfo<'info>,
26 account: T,
27}
28
29#[allow(deprecated)]
30impl<'info, T: AccountSerialize + AccountDeserialize + Clone> CpiState<'info, T> {
31 pub fn new(i: AccountInfo<'info>, account: T) -> CpiState<'info, T> {
32 Self {
33 inner: Box::new(Inner { info: i, account }),
34 }
35 }
36
37 #[inline(never)]
39 pub fn try_from(info: &AccountInfo<'info>) -> Result<CpiState<'info, T>, ProgramError> {
40 let mut data: &[u8] = &info.try_borrow_data()?;
41 Ok(CpiState::new(info.clone(), T::try_deserialize(&mut data)?))
42 }
43
44 fn seed() -> &'static str {
45 ProgramState::<T>::seed()
46 }
47
48 pub fn address(program_id: &Pubkey) -> Pubkey {
49 let (base, _nonce) = Pubkey::find_program_address(&[], program_id);
50 let seed = Self::seed();
51 let owner = program_id;
52 Pubkey::create_with_seed(&base, seed, owner).unwrap()
53 }
54
55 pub fn context<'a, 'b, 'c, A: Accounts<'info>>(
57 &self,
58 program: AccountInfo<'info>,
59 accounts: A,
60 ) -> CpiStateContext<'a, 'b, 'c, 'info, A> {
61 CpiStateContext::new(program, self.inner.info.clone(), accounts)
62 }
63}
64
65#[allow(deprecated)]
66impl<'info, T> Accounts<'info> for CpiState<'info, T>
67where
68 T: AccountSerialize + AccountDeserialize + Clone,
69{
70 #[inline(never)]
71 fn try_accounts(
72 _program_id: &Pubkey,
73 accounts: &mut &[AccountInfo<'info>],
74 _ix_data: &[u8],
75 ) -> Result<Self, ProgramError> {
76 if accounts.is_empty() {
77 return Err(ErrorCode::AccountNotEnoughKeys.into());
78 }
79 let account = &accounts[0];
80 *accounts = &accounts[1..];
81
82 CpiState::try_from(account)
86 }
87}
88
89#[allow(deprecated)]
90impl<'info, T: AccountSerialize + AccountDeserialize + Clone> ToAccountMetas
91 for CpiState<'info, T>
92{
93 fn to_account_metas(&self, is_signer: Option<bool>) -> Vec<AccountMeta> {
94 let is_signer = is_signer.unwrap_or(self.inner.info.is_signer);
95 let meta = match self.inner.info.is_writable {
96 false => AccountMeta::new_readonly(*self.inner.info.key, is_signer),
97 true => AccountMeta::new(*self.inner.info.key, is_signer),
98 };
99 vec![meta]
100 }
101}
102
103#[allow(deprecated)]
104impl<'info, T: AccountSerialize + AccountDeserialize + Clone> ToAccountInfos<'info>
105 for CpiState<'info, T>
106{
107 fn to_account_infos(&self) -> Vec<AccountInfo<'info>> {
108 vec![self.inner.info.clone()]
109 }
110}
111
112#[allow(deprecated)]
113impl<'info, T: AccountSerialize + AccountDeserialize + Clone> ToAccountInfo<'info>
114 for CpiState<'info, T>
115{
116 fn to_account_info(&self) -> AccountInfo<'info> {
117 self.inner.info.clone()
118 }
119}
120
121#[allow(deprecated)]
122impl<'info, T: AccountSerialize + AccountDeserialize + Clone> AsRef<AccountInfo<'info>>
123 for CpiState<'info, T>
124{
125 fn as_ref(&self) -> &AccountInfo<'info> {
126 &self.inner.info
127 }
128}
129
130#[allow(deprecated)]
131impl<'info, T: AccountSerialize + AccountDeserialize + Clone> Deref for CpiState<'info, T> {
132 type Target = T;
133
134 fn deref(&self) -> &Self::Target {
135 &(*self.inner).account
136 }
137}
138
139#[allow(deprecated)]
140impl<'info, T: AccountSerialize + AccountDeserialize + Clone> DerefMut for CpiState<'info, T> {
141 fn deref_mut(&mut self) -> &mut Self::Target {
142 &mut DerefMut::deref_mut(&mut self.inner).account
143 }
144}
145
146#[allow(deprecated)]
147impl<'info, T: AccountSerialize + AccountDeserialize + Clone> AccountsExit<'info>
148 for CpiState<'info, T>
149{
150 fn exit(&self, _program_id: &Pubkey) -> ProgramResult {
151 Ok(())
153 }
154}
155
156#[allow(deprecated)]
157impl<'info, T: AccountSerialize + AccountDeserialize + Clone> Key for CpiState<'info, T> {
158 fn key(&self) -> Pubkey {
159 *self.inner.info.key
160 }
161}