antegen_thread_program/instructions/
fiber_close.rs1use crate::*;
2use anchor_lang::prelude::*;
3
4#[derive(Accounts)]
6#[instruction(fiber_index: u8)]
7pub struct FiberClose<'info> {
8 #[account(
10 constraint = authority.key().eq(&thread.authority) || authority.key().eq(&thread.key())
11 )]
12 pub authority: Signer<'info>,
13
14 #[account(mut)]
16 pub close_to: SystemAccount<'info>,
17
18 #[account(
20 mut,
21 seeds = [
22 SEED_THREAD,
23 thread.authority.as_ref(),
24 thread.id.as_slice(),
25 ],
26 bump = thread.bump,
27 )]
28 pub thread: Account<'info, Thread>,
29
30 #[account(
32 mut,
33 seeds = [
34 SEED_THREAD_FIBER,
35 thread.key().as_ref(),
36 &[fiber_index],
37 ],
38 bump,
39 close = close_to,
40 )]
41 pub fiber: Option<Account<'info, FiberState>>,
42}
43
44pub fn fiber_close(ctx: Context<FiberClose>, fiber_index: u8) -> Result<()> {
45 let thread = &mut ctx.accounts.thread;
46
47 if fiber_index == 0 && thread.default_fiber.is_some() {
49 thread.default_fiber = None;
51 thread.default_fiber_priority_fee = 0;
52
53 if thread.fiber_cursor == 0 && thread.fiber_ids.len() > 1 {
55 thread.advance_to_next_fiber();
56 }
57
58 thread.fiber_ids.retain(|&x| x != 0);
60
61 if thread.fiber_ids.is_empty() {
63 thread.fiber_cursor = 0;
64 }
65 } else {
66 require!(
68 ctx.accounts.fiber.is_some(),
69 crate::errors::AntegenThreadError::FiberAccountRequired
70 );
71
72 if thread.fiber_cursor == fiber_index && thread.fiber_ids.len() > 1 {
74 thread.advance_to_next_fiber();
75 }
76
77 thread.fiber_ids.retain(|&x| x != fiber_index);
79
80 if thread.fiber_ids.is_empty() {
82 thread.fiber_cursor = 0;
83 }
84
85 }
87
88 Ok(())
89}