antegen_thread_program/
lib.rs

1pub mod constants;
2pub mod errors;
3pub mod instructions;
4pub mod state;
5pub mod utils;
6
7pub use constants::*;
8use instructions::*;
9use state::*;
10
11use anchor_lang::prelude::*;
12use anchor_lang::solana_program::instruction::Instruction;
13use state::{SerializableInstruction, Trigger};
14
15declare_id!("AgV3xRAdyTe1wW4gTW2oAnzHiAGofsxC7jBVGGkzUQbY");
16
17#[derive(AnchorSerialize, AnchorDeserialize)]
18pub enum ThreadId {
19    Bytes(Vec<u8>),
20    Pubkey(Pubkey),
21}
22
23impl AsRef<[u8]> for ThreadId {
24    fn as_ref(&self) -> &[u8] {
25        match self {
26            ThreadId::Bytes(bytes) => bytes.as_ref(),
27            ThreadId::Pubkey(pubkey) => pubkey.as_ref(),
28        }
29    }
30}
31
32impl ThreadId {
33    pub fn len(&self) -> usize {
34        match self {
35            ThreadId::Bytes(bytes) => bytes.len(),
36            ThreadId::Pubkey(_) => 32,
37        }
38    }
39
40    pub fn to_name(&self) -> String {
41        match self {
42            ThreadId::Bytes(bytes) => String::from_utf8_lossy(bytes).to_string(),
43            ThreadId::Pubkey(pubkey) => pubkey.to_string(),
44        }
45    }
46}
47
48impl From<String> for ThreadId {
49    fn from(s: String) -> Self {
50        ThreadId::Bytes(s.into_bytes())
51    }
52}
53
54impl From<&str> for ThreadId {
55    fn from(s: &str) -> Self {
56        ThreadId::Bytes(s.as_bytes().to_vec())
57    }
58}
59
60impl From<Pubkey> for ThreadId {
61    fn from(pubkey: Pubkey) -> Self {
62        ThreadId::Pubkey(pubkey)
63    }
64}
65
66impl From<ThreadId> for Vec<u8> {
67    fn from(id: ThreadId) -> Vec<u8> {
68        match id {
69            ThreadId::Bytes(bytes) => bytes,
70            ThreadId::Pubkey(pubkey) => pubkey.to_bytes().to_vec(),
71        }
72    }
73}
74
75#[program]
76pub mod thread_program {
77    use super::*;
78
79    /// Initialize the global thread configuration.
80    pub fn init_config(ctx: Context<ConfigInit>) -> Result<()> {
81        config_init(ctx)
82    }
83
84    /// Update the global thread configuration.
85    pub fn update_config(ctx: Context<ConfigUpdate>, params: ConfigUpdateParams) -> Result<()> {
86        config_update(ctx, params)
87    }
88
89    /// Creates a fiber (instruction) for a thread.
90    pub fn create_fiber(
91        ctx: Context<FiberCreate>,
92        fiber_index: u8,
93        instruction: SerializableInstruction,
94        signer_seeds: Vec<Vec<Vec<u8>>>,
95        priority_fee: u64,
96    ) -> Result<()> {
97        let instruction: Instruction = instruction.into();
98        fiber_create(ctx, fiber_index, instruction, signer_seeds, priority_fee)
99    }
100
101    /// Deletes a fiber from a thread.
102    pub fn delete_fiber(ctx: Context<FiberDelete>, fiber_index: u8) -> Result<()> {
103        fiber_delete(ctx, fiber_index)
104    }
105
106    /// Updates a fiber's instruction and resets execution stats.
107    pub fn update_fiber(
108        ctx: Context<FiberUpdate>,
109        instruction: SerializableInstruction,
110    ) -> Result<()> {
111        let instruction: Instruction = instruction.into();
112        fiber_update(ctx, instruction)
113    }
114
115    /// Creates a new transaction thread.
116    /// Optionally creates an initial fiber if instruction is provided.
117    pub fn create_thread(
118        ctx: Context<ThreadCreate>,
119        amount: u64,
120        id: ThreadId,
121        trigger: Trigger,
122        initial_instruction: Option<SerializableInstruction>,
123        priority_fee: Option<u64>,
124    ) -> Result<()> {
125        thread_create(ctx, amount, id, trigger, initial_instruction, priority_fee)
126    }
127
128    /// Creates a new transaction thread.
129    /// Optionally creates an initial fiber if instruction is provided.
130    pub fn create_empty_thread(
131        ctx: Context<ThreadCreate>,
132        amount: u64,
133        id: ThreadId,
134        trigger: Trigger,
135    ) -> Result<()> {
136        thread_create(ctx, amount, id, trigger, None, None)
137    }
138
139    /// Closes an existing thread account and returns the lamports to the owner.
140    /// Requires authority (owner) or thread itself to sign.
141    /// External fiber accounts should be passed via remaining_accounts.
142    pub fn delete_thread(ctx: Context<ThreadDelete>) -> Result<()> {
143        thread_delete(ctx)
144    }
145
146    /// Executes a thread fiber with trigger validation and fee distribution.
147    /// Respects builder claim priority windows from registry configuration.
148    pub fn exec_thread(
149        ctx: Context<ThreadExec>,
150        forgo_commission: bool,
151        fiber_cursor: u8,
152    ) -> Result<()> {
153        thread_exec(ctx, forgo_commission, fiber_cursor)
154    }
155
156    /// Toggles a thread's pause state.
157    pub fn toggle_thread(ctx: Context<ThreadToggle>) -> Result<()> {
158        thread_toggle(ctx)
159    }
160
161    /// Allows an owner to update the thread's trigger.
162    pub fn update_thread(ctx: Context<ThreadUpdate>, new_trigger: Option<Trigger>) -> Result<()> {
163        thread_update(ctx, new_trigger)
164    }
165
166    /// Allows an owner to withdraw from a thread's lamport balance.
167    pub fn withdraw_thread(ctx: Context<ThreadWithdraw>, amount: u64) -> Result<()> {
168        thread_withdraw(ctx, amount)
169    }
170
171    /// Reports an error for a thread that failed to execute.
172    pub fn error_thread(
173        ctx: Context<ThreadError>,
174        error_code: u32,
175        error_message: String,
176    ) -> Result<()> {
177        thread_error(ctx, error_code, error_message)
178    }
179
180    /// Memo instruction that logs a message (replacement for spl-memo).
181    /// Used for tracking thread fiber execution in logs without external dependencies.
182    /// Optionally emits a signal for testing signal behaviors.
183    pub fn thread_memo(
184        ctx: Context<ThreadMemo>,
185        memo: String,
186        signal: Option<Signal>,
187    ) -> Result<Signal> {
188        instructions::thread_memo::thread_memo(ctx, memo, signal)
189    }
190
191    /// Force deletes a thread - admin only, skips all checks.
192    /// Used for cleaning up stuck/broken threads during development.
193    pub fn force_delete_thread(ctx: Context<ThreadForceDelete>) -> Result<()> {
194        thread_force_delete(ctx)
195    }
196}