Skip to main content

antegen_thread_program/instructions/
fiber_update.rs

1use crate::{errors::*, *};
2use anchor_lang::prelude::*;
3use antegen_fiber_program::{
4    program::AntegenFiber,
5    state::SerializableInstruction,
6};
7
8/// Accounts required by the `fiber_update` instruction.
9/// Validates authority, CPIs to Fiber Program to update (or init) the fiber.
10/// Thread PDA pays for fiber init if needed.
11#[derive(Accounts)]
12#[instruction(fiber_index: u8)]
13pub struct FiberUpdate<'info> {
14    /// The authority of the thread or the thread itself
15    #[account(
16        constraint = authority.key().eq(&thread.authority) || authority.key().eq(&thread.key())
17    )]
18    pub authority: Signer<'info>,
19
20    /// The thread the fiber belongs to
21    #[account(
22        mut,
23        seeds = [SEED_THREAD, thread.authority.as_ref(), thread.id.as_slice()],
24        bump = thread.bump,
25    )]
26    pub thread: Account<'info, Thread>,
27
28    /// CHECK: The fiber account to update (may not exist yet, validated by Fiber Program via CPI)
29    #[account(mut)]
30    pub fiber: UncheckedAccount<'info>,
31
32    /// The Fiber Program for CPI
33    pub fiber_program: Program<'info, AntegenFiber>,
34
35    pub system_program: Program<'info, System>,
36}
37
38pub fn fiber_update(
39    ctx: Context<FiberUpdate>,
40    fiber_index: u8,
41    instruction: SerializableInstruction,
42    priority_fee: Option<u64>,
43    track: bool,
44) -> Result<()> {
45    // Prevent thread_delete instructions in fibers
46    if instruction.program_id.eq(&crate::ID)
47        && instruction.data.len() >= 8
48        && instruction.data[..8].eq(crate::instruction::DeleteThread::DISCRIMINATOR)
49    {
50        return Err(AntegenThreadError::InvalidInstruction.into());
51    }
52
53    let thread = &mut ctx.accounts.thread;
54
55    // Track the fiber in the thread's fiber_ids before CPI
56    if track && !thread.fiber_ids.contains(&fiber_index) {
57        thread.fiber_ids.push(fiber_index);
58        thread.fiber_ids.sort();
59        if fiber_index >= thread.fiber_next_id {
60            thread.fiber_next_id = fiber_index.saturating_add(1);
61        }
62    }
63
64    // Pre-fund fiber account from thread PDA if not yet initialized
65    let fiber_info = ctx.accounts.fiber.to_account_info();
66    if fiber_info.data_len() == 0 {
67        let space = 8 + antegen_fiber_program::state::FiberState::INIT_SPACE;
68        let rent_lamports = Rent::get()?.minimum_balance(space);
69        **thread.to_account_info().try_borrow_mut_lamports()? -= rent_lamports;
70        **fiber_info.try_borrow_mut_lamports()? += rent_lamports;
71    }
72
73    // CPI to Fiber Program's update_fiber
74    thread.sign(|signer| {
75        antegen_fiber_program::cpi::update_fiber(
76            CpiContext::new_with_signer(
77                ctx.accounts.fiber_program.key(),
78                antegen_fiber_program::cpi::accounts::FiberUpdate {
79                    thread: thread.to_account_info(),
80                    fiber: ctx.accounts.fiber.to_account_info(),
81                    system_program: ctx.accounts.system_program.to_account_info(),
82                },
83                &[signer],
84            ),
85            fiber_index,
86            instruction,
87            priority_fee,
88        )
89    })?;
90
91    Ok(())
92}