#![doc = include_str!("../README.md")]
use solana_program::{
account_info::AccountInfo, entrypoint::ProgramResult, instruction::Instruction,
};
mod instruction_stabilizer;
pub fn invoke(instruction: &Instruction, account_infos: &[AccountInfo]) -> ProgramResult {
invoke_signed(instruction, account_infos, &[])
}
pub fn invoke_unchecked(instruction: &Instruction, account_infos: &[AccountInfo]) -> ProgramResult {
invoke_signed_unchecked(instruction, account_infos, &[])
}
pub fn invoke_signed(
instruction: &Instruction,
account_infos: &[AccountInfo],
signers_seeds: &[&[&[u8]]],
) -> ProgramResult {
for account_meta in instruction.accounts.iter() {
for account_info in account_infos.iter() {
if account_meta.pubkey == *account_info.key {
if account_meta.is_writable {
let _ = account_info.try_borrow_mut_lamports()?;
let _ = account_info.try_borrow_mut_data()?;
} else {
let _ = account_info.try_borrow_lamports()?;
let _ = account_info.try_borrow_data()?;
}
break;
}
}
}
invoke_signed_unchecked(instruction, account_infos, signers_seeds)
}
pub fn invoke_signed_unchecked(
instruction: &Instruction,
account_infos: &[AccountInfo],
signers_seeds: &[&[&[u8]]],
) -> ProgramResult {
#[cfg(target_os = "solana")]
{
use instruction_stabilizer::InstructionStabilizer;
let stabilizer = InstructionStabilizer::stabilize(instruction);
let instruction_addr = stabilizer.instruction_addr();
let result = unsafe {
solana_program::syscalls::sol_invoke_signed_rust(
instruction_addr,
account_infos as *const _ as *const u8,
account_infos.len() as u64,
signers_seeds as *const _ as *const u8,
signers_seeds.len() as u64,
)
};
match result {
solana_program::entrypoint::SUCCESS => Ok(()),
_ => Err(result.into()),
}
}
#[cfg(not(target_os = "solana"))]
{
core::hint::black_box((instruction, account_infos, signers_seeds));
panic!("not supported when target_os != solana");
}
}