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
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    /// Closes a fiber from a thread.
102    pub fn close_fiber(ctx: Context<FiberClose>, fiber_index: u8) -> Result<()> {
103        fiber_close(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    /// Closes an existing thread account and returns the lamports to the owner.
129    /// Requires authority (owner) or thread itself to sign.
130    /// External fiber accounts should be passed via remaining_accounts.
131    pub fn close_thread(ctx: Context<ThreadClose>) -> Result<()> {
132        thread_close(ctx)
133    }
134
135    /// Executes a thread fiber with trigger validation and fee distribution.
136    /// Respects builder claim priority windows from registry configuration.
137    pub fn exec_thread(
138        ctx: Context<ThreadExec>,
139        forgo_commission: bool,
140        fiber_cursor: u8,
141    ) -> Result<()> {
142        thread_exec(ctx, forgo_commission, fiber_cursor)
143    }
144
145    /// Allows an owner to update the thread's properties (paused state, trigger).
146    pub fn update_thread(ctx: Context<ThreadUpdate>, params: ThreadUpdateParams) -> Result<()> {
147        thread_update(ctx, params)
148    }
149
150    /// Allows an owner to withdraw from a thread's lamport balance.
151    pub fn withdraw_thread(ctx: Context<ThreadWithdraw>, amount: u64) -> Result<()> {
152        thread_withdraw(ctx, amount)
153    }
154
155    /// Reports an error for a thread that failed to execute.
156    pub fn error_thread(
157        ctx: Context<ThreadError>,
158        error_code: u32,
159        error_message: String,
160    ) -> Result<()> {
161        thread_error(ctx, error_code, error_message)
162    }
163
164    /// Memo instruction that logs a message (replacement for spl-memo).
165    /// Used for tracking thread fiber execution in logs without external dependencies.
166    /// Optionally emits a signal for testing signal behaviors.
167    pub fn thread_memo(
168        ctx: Context<ThreadMemo>,
169        memo: String,
170        signal: Option<Signal>,
171    ) -> Result<Signal> {
172        instructions::thread_memo::thread_memo(ctx, memo, signal)
173    }
174
175    /// Deletes a thread - admin only, skips all checks.
176    /// Used for cleaning up stuck/broken threads.
177    pub fn delete_thread(ctx: Context<ThreadDelete>) -> Result<()> {
178        thread_delete(ctx)
179    }
180}