antegen_thread_program/instructions/
fiber_create.rs1use crate::{errors::*, *};
2use anchor_lang::prelude::*;
3use antegen_fiber_program::state::SerializableInstruction;
4
5#[derive(Accounts)]
9#[instruction(fiber_index: u8)]
10pub struct FiberCreate<'info> {
11 #[account(
13 constraint = authority.key().eq(&thread.authority) || authority.key().eq(&thread.key())
14 )]
15 pub authority: Signer<'info>,
16
17 #[account(
19 mut,
20 seeds = [
21 SEED_THREAD,
22 thread.authority.as_ref(),
23 thread.id.as_slice(),
24 ],
25 bump = thread.bump,
26 )]
27 pub thread: Account<'info, Thread>,
28
29 #[account(mut)]
31 pub fiber: UncheckedAccount<'info>,
32
33 pub fiber_program: Program<'info, antegen_fiber_program::program::AntegenFiber>,
35
36 #[account(address = anchor_lang::system_program::ID)]
37 pub system_program: Program<'info, System>,
38}
39
40pub fn fiber_create(
41 ctx: Context<FiberCreate>,
42 fiber_index: u8,
43 instruction: SerializableInstruction,
44 priority_fee: u64,
45) -> Result<()> {
46 let thread = &mut ctx.accounts.thread;
47
48 if instruction.program_id.eq(&crate::ID)
50 && instruction.data.len().ge(&8)
51 && instruction.data[..8].eq(crate::instruction::DeleteThread::DISCRIMINATOR)
52 {
53 return Err(AntegenThreadError::InvalidInstruction.into());
54 }
55
56 if ctx.accounts.fiber.to_account_info().data_len() == 0 {
58 let space = 8 + antegen_fiber_program::state::FiberState::INIT_SPACE;
59 let rent_lamports = Rent::get()?.minimum_balance(space);
60 **thread.to_account_info().try_borrow_mut_lamports()? -= rent_lamports;
61 **ctx.accounts.fiber.to_account_info().try_borrow_mut_lamports()? += rent_lamports;
62 }
63
64 thread.sign(|seeds| {
65 antegen_fiber_program::cpi::create_fiber(
66 CpiContext::new_with_signer(
67 ctx.accounts.fiber_program.key(),
68 antegen_fiber_program::cpi::accounts::FiberCreate {
69 thread: thread.to_account_info(),
70 fiber: ctx.accounts.fiber.to_account_info(),
71 system_program: ctx.accounts.system_program.to_account_info(),
72 },
73 &[seeds],
74 ),
75 fiber_index,
76 instruction,
77 priority_fee,
78 )
79 })?;
80
81 if !thread.fiber_ids.contains(&fiber_index) {
83 thread.fiber_ids.push(fiber_index);
84 thread.fiber_ids.sort();
85 }
86 if fiber_index >= thread.fiber_next_id {
87 thread.fiber_next_id = fiber_index.saturating_add(1);
88 }
89
90 Ok(())
91}