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