miclockwork_thread_program/state/
thread.rs1use std::mem::size_of;
2
3use anchor_lang::{prelude::*, AnchorDeserialize, AnchorSerialize};
4use miclockwork_utils::thread::{ClockData, SerializableInstruction, Trigger};
5
6pub use miclockwork_utils::thread::Equality;
7
8pub const SEED_THREAD: &[u8] = b"thread";
9
10pub const NEXT_INSTRUCTION_SIZE: usize = 1232;
12
13#[account]
15#[derive(Debug)]
16pub struct Thread {
17 pub authority: Pubkey,
19 pub bump: u8,
21 pub created_at: ClockData,
23 pub exec_context: Option<ExecContext>,
25 pub fee: u64,
27 pub id: Vec<u8>,
29 pub instructions: Vec<SerializableInstruction>,
31 pub name: String,
33 pub next_instruction: Option<SerializableInstruction>,
35 pub paused: bool,
37 pub rate_limit: u64,
39 pub trigger: Trigger,
41}
42
43impl Thread {
44 pub fn pubkey(authority: Pubkey, id: Vec<u8>) -> Pubkey {
46 Pubkey::find_program_address(
47 &[SEED_THREAD, authority.as_ref(), id.as_slice()],
48 &crate::ID,
49 )
50 .0
51 }
52}
53
54impl PartialEq for Thread {
55 fn eq(&self, other: &Self) -> bool {
56 self.authority.eq(&other.authority) && self.id.eq(&other.id)
57 }
58}
59
60impl Eq for Thread {}
61
62pub trait ThreadAccount {
64 fn pubkey(&self) -> Pubkey;
66
67 fn realloc(&mut self) -> Result<()>;
69}
70
71impl ThreadAccount for Account<'_, Thread> {
72 fn pubkey(&self) -> Pubkey {
73 Thread::pubkey(self.authority, self.id.clone())
74 }
75
76 fn realloc(&mut self) -> Result<()> {
77 let data_len = vec![
79 8,
80 size_of::<Thread>(),
81 self.id.len(),
82 self.instructions.try_to_vec()?.len(),
83 self.trigger.try_to_vec()?.len(),
84 NEXT_INSTRUCTION_SIZE,
85 ]
86 .iter()
87 .sum();
88 self.to_account_info().realloc(data_len, false)?;
89 Ok(())
90 }
91}
92
93#[derive(AnchorDeserialize, AnchorSerialize, Clone, Copy, Debug, PartialEq, Eq)]
95pub struct ExecContext {
96 pub exec_index: u64,
98
99 pub execs_since_reimbursement: u64,
102
103 pub execs_since_slot: u64,
105
106 pub last_exec_at: u64,
108
109 pub trigger_context: TriggerContext,
111}
112
113#[derive(AnchorDeserialize, AnchorSerialize, Clone, Copy, Debug, PartialEq, Eq)]
115pub enum TriggerContext {
116 Account {
118 data_hash: u64,
120 },
121
122 Cron {
124 started_at: i64,
126 },
127
128 Now,
130
131 Slot {
133 started_at: u64,
135 },
136
137 Epoch {
139 started_at: u64,
141 },
142
143 Timestamp {
145 started_at: i64,
147 },
148
149 Pyth { price: i64 },
151}
152
153#[derive(AnchorSerialize, AnchorDeserialize)]
155pub struct ThreadSettings {
156 pub fee: Option<u64>,
157 pub instructions: Option<Vec<SerializableInstruction>>,
158 pub name: Option<String>,
159 pub rate_limit: Option<u64>,
160 pub trigger: Option<Trigger>,
161}