antegen_thread_program/instructions/
fiber_close.rs1use crate::{errors::AntegenThreadError, *};
2use anchor_lang::prelude::*;
3
4#[derive(Accounts)]
7#[instruction(fiber_index: u8)]
8pub struct FiberClose<'info> {
9 #[account(
11 constraint = authority.key().eq(&thread.authority) || authority.key().eq(&thread.key())
12 )]
13 pub authority: Signer<'info>,
14
15 #[account(
17 mut,
18 constraint = thread.fiber_ids.contains(&fiber_index) @ AntegenThreadError::InvalidFiberIndex,
19 seeds = [
20 SEED_THREAD,
21 thread.authority.as_ref(),
22 thread.id.as_slice(),
23 ],
24 bump = thread.bump,
25 )]
26 pub thread: Account<'info, Thread>,
27
28 #[account(
30 mut,
31 constraint = fiber.thread.eq(&thread.key()) @ AntegenThreadError::InvalidFiberAccount,
32 )]
33 pub fiber: Account<'info, antegen_fiber_program::state::FiberState>,
34
35 pub fiber_program: Program<'info, antegen_fiber_program::program::AntegenFiber>,
37}
38
39pub fn fiber_close(ctx: Context<FiberClose>, fiber_index: u8) -> Result<()> {
40 let thread = &mut ctx.accounts.thread;
41
42 if thread.fiber_cursor.eq(&fiber_index) && thread.fiber_ids.len().gt(&1) {
44 thread.advance_to_next_fiber();
45 }
46
47 thread.fiber_ids.retain(|&x| x != fiber_index);
48 if thread.fiber_ids.is_empty() {
49 thread.fiber_cursor = 0;
50 }
51
52 thread.sign(|seeds| {
53 antegen_fiber_program::cpi::close_fiber(CpiContext::new_with_signer(
54 ctx.accounts.fiber_program.key(),
55 antegen_fiber_program::cpi::accounts::FiberClose {
56 thread: thread.to_account_info(),
57 fiber: ctx.accounts.fiber.to_account_info(),
58 },
59 &[seeds],
60 ))
61 })?;
62
63 Ok(())
64}