use {
crate::{
solana_program::{account_info::AccountInfo, instruction::AccountMeta, pubkey::Pubkey},
Accounts, Bumps, ToAccountInfos, ToAccountMetas,
},
std::fmt,
};
pub struct Context<'info, T: Bumps> {
pub program_id: &'info Pubkey,
pub accounts: &'info mut T,
pub remaining_accounts: &'info [AccountInfo<'info>],
pub bumps: T::Bumps,
}
impl<T> fmt::Debug for Context<'_, T>
where
T: fmt::Debug + Bumps,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Context")
.field("program_id", &self.program_id)
.field("accounts", &self.accounts)
.field("remaining_accounts", &self.remaining_accounts)
.field("bumps", &self.bumps)
.finish()
}
}
impl<'info, T> Context<'info, T>
where
T: Bumps + Accounts<'info, T::Bumps>,
{
pub fn new(
program_id: &'info Pubkey,
accounts: &'info mut T,
remaining_accounts: &'info [AccountInfo<'info>],
bumps: T::Bumps,
) -> Self {
Self {
program_id,
accounts,
remaining_accounts,
bumps,
}
}
}
pub struct CpiContext<'a, 'b, 'c, 'info, T>
where
T: ToAccountMetas + ToAccountInfos<'info>,
{
pub accounts: T,
pub remaining_accounts: Vec<AccountInfo<'info>>,
pub program_id: Pubkey,
pub signer_seeds: &'a [&'b [&'c [u8]]],
}
impl<'a, 'b, 'c, 'info, T> CpiContext<'a, 'b, 'c, 'info, T>
where
T: ToAccountMetas + ToAccountInfos<'info>,
{
#[must_use]
pub fn new(program_id: Pubkey, accounts: T) -> Self {
Self {
accounts,
program_id,
remaining_accounts: Vec::new(),
signer_seeds: &[],
}
}
#[must_use]
pub fn new_with_signer(
program_id: Pubkey,
accounts: T,
signer_seeds: &'a [&'b [&'c [u8]]],
) -> Self {
Self {
accounts,
program_id,
signer_seeds,
remaining_accounts: Vec::new(),
}
}
#[must_use]
pub fn with_signer(mut self, signer_seeds: &'a [&'b [&'c [u8]]]) -> Self {
self.signer_seeds = signer_seeds;
self
}
#[must_use]
pub fn with_remaining_accounts(mut self, ra: Vec<AccountInfo<'info>>) -> Self {
self.remaining_accounts = ra;
self
}
}
impl<'info, T: ToAccountInfos<'info> + ToAccountMetas> ToAccountInfos<'info>
for CpiContext<'_, '_, '_, 'info, T>
{
fn to_account_infos(&self) -> Vec<AccountInfo<'info>> {
let mut infos = self.accounts.to_account_infos();
infos.extend_from_slice(&self.remaining_accounts);
infos
}
}
impl<'info, T: ToAccountInfos<'info> + ToAccountMetas> ToAccountMetas
for CpiContext<'_, '_, '_, 'info, T>
{
fn to_account_metas(&self, is_signer: Option<bool>) -> Vec<AccountMeta> {
let mut metas = self.accounts.to_account_metas(is_signer);
metas.append(
&mut self
.remaining_accounts
.iter()
.map(|acc| match acc.is_writable {
false => AccountMeta::new_readonly(*acc.key, acc.is_signer),
true => AccountMeta::new(*acc.key, acc.is_signer),
})
.collect(),
);
metas
}
}