use crate::*;
use anchor_lang::prelude::*;
use anchor_lang::solana_program::instruction::Instruction;
pub trait FiberInstructionProcessor {
fn get_instruction(&self, executor: &Pubkey) -> Result<Instruction>;
}
#[account]
#[derive(Debug, InitSpace)]
pub struct FiberState {
pub thread: Pubkey,
pub fiber_index: u8,
#[max_len(1024)]
pub compiled_instruction: Vec<u8>,
pub last_executed: i64,
pub exec_count: u64,
pub priority_fee: u64,
}
impl FiberState {
pub fn pubkey(thread: Pubkey, fiber_index: u8) -> Pubkey {
Pubkey::find_program_address(&[SEED_THREAD_FIBER, thread.as_ref(), &[fiber_index]], &crate::ID).0
}
}
impl FiberInstructionProcessor for FiberState {
fn get_instruction(&self, executor: &Pubkey) -> Result<Instruction> {
let compiled = CompiledInstructionV0::try_from_slice(&self.compiled_instruction)?;
let mut instruction = decompile_instruction(&compiled)?;
for acc in instruction.accounts.iter_mut() {
if acc.pubkey.eq(&PAYER_PUBKEY) {
acc.pubkey = *executor;
}
}
Ok(instruction)
}
}