Skip to main content

antegen_thread_program/
lib.rs

1pub mod constants;
2pub mod errors;
3pub mod instructions;
4pub mod state;
5pub mod utils;
6
7/// Fiber program re-exports for direct CPI access.
8/// Use when your program is executed via `thread_exec` and needs to
9/// manage fibers directly (cannot CPI back to thread program due to reentrancy).
10pub mod fiber {
11    pub use antegen_fiber_program::cpi;
12    pub use antegen_fiber_program::program::AntegenFiber;
13    pub use antegen_fiber_program::state::{
14        decompile_instruction, CompiledInstructionV0, FiberState,
15    };
16    pub use antegen_fiber_program::ID;
17}
18
19pub use constants::*;
20pub use crate::program::AntegenThread;
21use instructions::*;
22use state::*;
23
24use anchor_lang::prelude::*;
25use state::{SerializableInstruction, Trigger};
26
27declare_id!("AgTv5w1UvUb6zeqkThwMrztGu9hpepBu8YLghuR4dpSx");
28
29#[derive(AnchorSerialize, AnchorDeserialize)]
30pub enum ThreadId {
31    Bytes(Vec<u8>),
32    Pubkey(Pubkey),
33}
34
35impl AsRef<[u8]> for ThreadId {
36    fn as_ref(&self) -> &[u8] {
37        match self {
38            ThreadId::Bytes(bytes) => bytes.as_ref(),
39            ThreadId::Pubkey(pubkey) => pubkey.as_ref(),
40        }
41    }
42}
43
44impl ThreadId {
45    pub fn len(&self) -> usize {
46        match self {
47            ThreadId::Bytes(bytes) => bytes.len(),
48            ThreadId::Pubkey(_) => 32,
49        }
50    }
51
52    pub fn to_name(&self) -> String {
53        match self {
54            ThreadId::Bytes(bytes) => String::from_utf8_lossy(bytes).to_string(),
55            ThreadId::Pubkey(pubkey) => pubkey.to_string(),
56        }
57    }
58}
59
60impl From<String> for ThreadId {
61    fn from(s: String) -> Self {
62        ThreadId::Bytes(s.into_bytes())
63    }
64}
65
66impl From<&str> for ThreadId {
67    fn from(s: &str) -> Self {
68        ThreadId::Bytes(s.as_bytes().to_vec())
69    }
70}
71
72impl From<Pubkey> for ThreadId {
73    fn from(pubkey: Pubkey) -> Self {
74        ThreadId::Pubkey(pubkey)
75    }
76}
77
78impl From<ThreadId> for Vec<u8> {
79    fn from(id: ThreadId) -> Vec<u8> {
80        match id {
81            ThreadId::Bytes(bytes) => bytes,
82            ThreadId::Pubkey(pubkey) => pubkey.to_bytes().to_vec(),
83        }
84    }
85}
86
87#[program]
88pub mod antegen_thread {
89    use super::*;
90
91    /// Initialize the global thread configuration.
92    pub fn init_config(ctx: Context<ConfigInit>) -> Result<()> {
93        config_init(ctx)
94    }
95
96    /// Update the global thread configuration.
97    pub fn update_config(ctx: Context<ConfigUpdate>, params: ConfigUpdateParams) -> Result<()> {
98        config_update(ctx, params)
99    }
100
101    /// Creates a fiber (instruction) for a thread via CPI to Fiber Program.
102    pub fn create_fiber(
103        ctx: Context<FiberCreate>,
104        fiber_index: u8,
105        instruction: SerializableInstruction,
106        priority_fee: u64,
107    ) -> Result<()> {
108        fiber_create(ctx, fiber_index, instruction, priority_fee)
109    }
110
111    /// Closes a fiber from a thread via CPI to Fiber Program.
112    pub fn close_fiber(ctx: Context<FiberClose>, fiber_index: u8) -> Result<()> {
113        fiber_close(ctx, fiber_index)
114    }
115
116    /// Updates a fiber's instruction via CPI to Fiber Program.
117    /// Initializes the fiber if it doesn't exist (thread PDA pays rent).
118    /// If `track` is true, adds the fiber_index to thread.fiber_ids.
119    pub fn update_fiber(
120        ctx: Context<FiberUpdate>,
121        fiber_index: u8,
122        instruction: SerializableInstruction,
123        priority_fee: Option<u64>,
124        track: bool,
125    ) -> Result<()> {
126        fiber_update(ctx, fiber_index, instruction, priority_fee, track)
127    }
128
129    /// Swaps source fiber's instruction into target fiber, closes source.
130    /// Target keeps its PDA/index, source is deleted.
131    pub fn swap_fiber(ctx: Context<FiberSwap>, source_fiber_index: u8) -> Result<()> {
132        instructions::fiber_swap::fiber_swap(ctx, source_fiber_index)
133    }
134
135    /// Creates a new transaction thread.
136    /// Optionally creates fiber index 0 if `instruction` is provided.
137    pub fn create_thread(
138        ctx: Context<ThreadCreate>,
139        amount: u64,
140        id: ThreadId,
141        trigger: Trigger,
142        paused: Option<bool>,
143        instruction: Option<SerializableInstruction>,
144        priority_fee: Option<u64>,
145    ) -> Result<()> {
146        thread_create(ctx, amount, id, trigger, paused, instruction, priority_fee)
147    }
148
149    /// Closes an existing thread account and returns the lamports to the owner.
150    /// Requires authority (owner) or thread itself to sign.
151    /// External fiber accounts should be passed via remaining_accounts.
152    pub fn close_thread<'info>(
153        ctx: Context<'info, ThreadClose<'info>>,
154    ) -> Result<()> {
155        thread_close(ctx)
156    }
157
158    /// Executes a thread fiber with trigger validation and fee distribution.
159    /// Respects builder claim priority windows from registry configuration.
160    pub fn exec_thread(
161        ctx: Context<ThreadExec>,
162        forgo_commission: bool,
163        fiber_cursor: u8,
164    ) -> Result<()> {
165        thread_exec(ctx, forgo_commission, fiber_cursor)
166    }
167
168    /// Allows an owner to update the thread's properties (paused state, trigger).
169    pub fn update_thread(ctx: Context<ThreadUpdate>, params: ThreadUpdateParams) -> Result<()> {
170        thread_update(ctx, params)
171    }
172
173    /// Allows an owner to withdraw from a thread's lamport balance.
174    pub fn withdraw_thread(ctx: Context<ThreadWithdraw>, amount: u64) -> Result<()> {
175        thread_withdraw(ctx, amount)
176    }
177
178    /// Memo instruction that logs a message (replacement for spl-memo).
179    /// Used for tracking thread fiber execution in logs without external dependencies.
180    /// Optionally emits a signal for testing signal behaviors.
181    pub fn thread_memo(
182        ctx: Context<ThreadMemo>,
183        memo: String,
184        signal: Option<Signal>,
185    ) -> Result<Signal> {
186        instructions::thread_memo::thread_memo(ctx, memo, signal)
187    }
188
189    /// Deletes a thread - admin only, skips all checks.
190    /// Used for cleaning up stuck/broken threads.
191    pub fn delete_thread(ctx: Context<ThreadDelete>) -> Result<()> {
192        thread_delete(ctx)
193    }
194}