impl ReadUnchecked {
pub fn process(accounts: &[AccountView]) -> ProgramResult {
let [vault, ..] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};
let data = vault.try_borrow_data()?;
let _state = VaultState::from_bytes(&data)?;
Ok(())
}
}
impl<'a> Handler<'a> for ReadChecked {
fn process(program_id: &Address, accounts: &[AccountView]) -> ProgramResult {
let [vault, ..] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};
if vault.owner() != program_id {
return Err(ProgramError::IllegalOwner);
}
let _data = vault.try_borrow_data()?;
Ok(())
}
}
pub trait ReadDelegated {
fn process(accounts: &[AccountView]) -> ProgramResult {
let [vault, ..] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};
require_owned(vault)?;
let _data = vault.try_borrow_data()?;
Ok(())
}
}
impl ReadSysvar {
pub fn process(accounts: &[AccountView]) -> ProgramResult {
let [rent_acc, ..] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};
let _rent = Rent::from_account_view(rent_acc)?;
Ok(())
}
}
impl<'a> Handler<'a> for CastUnchecked {
fn process(program_id: &Address, accounts: &[AccountView]) -> ProgramResult {
let [vault, ..] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};
if vault.owner() != program_id {
return Err(ProgramError::IllegalOwner);
}
let data = unsafe { vault.borrow_data_unchecked() };
let _state = VaultState::from_bytes(data)?;
Ok(())
}
}
pub trait WithdrawNoSigner {
fn process(program_id: &Address, accounts: &[AccountView]) -> ProgramResult {
let [vault, authority, ..] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};
if vault.owner() != program_id {
return Err(ProgramError::IllegalOwner);
}
let state = VaultState::load(vault)?;
if authority.address() != &state.authority {
return Err(ProgramError::IncorrectAuthority);
}
Ok(())
}
}
impl WithdrawChecked {
pub fn process(program_id: &Address, accounts: &[AccountView]) -> ProgramResult {
let [vault, authority, ..] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};
if vault.owner() != program_id {
return Err(ProgramError::IllegalOwner);
}
let state = VaultState::load(vault)?;
if authority.address() != &state.authority {
return Err(ProgramError::IncorrectAuthority);
}
if !authority.is_signer() {
return Err(ProgramError::MissingRequiredSignature);
}
Ok(())
}
}
impl<'a> Handler<'a> for ClaimRewards {
fn process(program_id: &Address, accounts: &[AccountView]) -> ProgramResult {
let [config, ..] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};
if config.owner() != program_id {
return Err(ProgramError::IllegalOwner);
}
let _cfg = Config::load(config)?;
Ok(())
}
}
pub trait ClaimRewardsChecked {
fn process(program_id: &Address, accounts: &[AccountView]) -> ProgramResult {
let [config, ..] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};
if config.owner() != program_id {
return Err(ProgramError::IllegalOwner);
}
if config.address() != &CONFIG_PDA {
return Err(ProgramError::InvalidAccount);
}
let _cfg = Config::load(config)?;
Ok(())
}
}
impl CastLengthChecked {
pub fn process(program_id: &Address, accounts: &[AccountView]) -> ProgramResult {
let [vault, ..] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};
if vault.owner() != program_id {
return Err(ProgramError::IllegalOwner);
}
if vault.data_len() < core::mem::size_of::<VaultState>() {
return Err(ProgramError::AccountDataTooSmall);
}
let data = unsafe { vault.borrow_data_unchecked() };
let _state = VaultState::from_bytes(data)?;
Ok(())
}
}
impl<'a> Handler<'a> for CpiArbitrary {
fn process(accounts: &[AccountView]) -> ProgramResult {
let [target, payer, ..] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};
let ix = InstructionView {
program_id: target.address(),
accounts: &[],
data: &[],
};
invoke(&ix, &[target, payer])?;
Ok(())
}
}
pub trait CpiChecked {
fn process(accounts: &[AccountView]) -> ProgramResult {
let [target, payer, ..] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};
if target.address() != &pinocchio_token::ID {
return Err(ProgramError::IncorrectProgramId);
}
let ix = InstructionView {
program_id: target.address(),
accounts: &[],
data: &[],
};
invoke(&ix, &[target, payer])?;
Ok(())
}
}
impl CpiConst {
pub fn process(accounts: &[AccountView]) -> ProgramResult {
let [payer, ..] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};
let ix = InstructionView {
program_id: &pinocchio_system::ID,
accounts: &[],
data: &[],
};
invoke(&ix, &[payer])?;
Ok(())
}
}