1use std::{convert::TryFrom, fmt::Debug, hash::Hash};
2
3use anchor_lang::{
4 prelude::borsh::BorshSchema,
5 prelude::Pubkey,
6 prelude::*,
7 solana_program::{self, instruction::Instruction},
8 AnchorDeserialize,
9};
10use serde::{Deserialize, Serialize};
11use static_pubkey::static_pubkey;
12
13pub static PAYER_PUBKEY: Pubkey = static_pubkey!("C1ockworkPayer11111111111111111111111111111");
15
16#[derive(AnchorDeserialize, AnchorSerialize, BorshSchema, Clone, Debug, PartialEq)]
18pub struct ClockData {
19 pub slot: u64,
21 pub epoch: u64,
23 pub unix_timestamp: i64,
25}
26
27impl From<Clock> for ClockData {
28 fn from(clock: Clock) -> Self {
29 ClockData {
30 slot: clock.slot,
31 epoch: clock.epoch,
32 unix_timestamp: clock.unix_timestamp,
33 }
34 }
35}
36
37impl TryFrom<Vec<u8>> for ClockData {
38 type Error = Error;
39 fn try_from(data: Vec<u8>) -> std::result::Result<Self, Self::Error> {
40 Ok(
41 borsh::try_from_slice_with_schema::<ClockData>(data.as_slice())
42 .map_err(|_err| ErrorCode::AccountDidNotDeserialize)?,
43 )
44 }
45}
46
47#[derive(AnchorDeserialize, AnchorSerialize, Debug, Clone, PartialEq)]
49pub enum Trigger {
50 Account {
52 address: Pubkey,
54 offset: u64,
56 size: u64,
58 },
59
60 Cron {
62 schedule: String,
64
65 skippable: bool,
68 },
69
70 Now,
72
73 Slot { slot: u64 },
75
76 Epoch { epoch: u64 },
78
79 Timestamp { unix_ts: i64 },
81
82 Pyth {
84 price_feed: Pubkey,
86 equality: Equality,
88 limit: i64,
90 },
91}
92
93#[repr(u8)]
95#[derive(AnchorDeserialize, AnchorSerialize, Clone, Debug, Eq, PartialEq, Hash)]
96pub enum Equality {
97 GreaterThanOrEqual,
98 LessThanOrEqual,
99}
100
101#[derive(AnchorDeserialize, AnchorSerialize, Clone, Debug)]
103pub struct ThreadResponse {
104 pub close_to: Option<Pubkey>,
107 pub dynamic_instruction: Option<SerializableInstruction>,
110 pub trigger: Option<Trigger>,
112}
113
114impl Default for ThreadResponse {
115 fn default() -> Self {
116 return Self {
117 close_to: None,
118 dynamic_instruction: None,
119 trigger: None,
120 };
121 }
122}
123
124#[derive(
126 AnchorDeserialize,
127 AnchorSerialize,
128 Serialize,
129 Deserialize,
130 BorshSchema,
131 Clone,
132 Debug,
133 Hash,
134 PartialEq,
135)]
136pub struct SerializableInstruction {
137 pub program_id: Pubkey,
139 pub accounts: Vec<SerializableAccount>,
141 pub data: Vec<u8>,
143}
144
145impl From<Instruction> for SerializableInstruction {
146 fn from(instruction: Instruction) -> Self {
147 SerializableInstruction {
148 program_id: instruction.program_id,
149 accounts: instruction
150 .accounts
151 .iter()
152 .map(|a| SerializableAccount {
153 pubkey: a.pubkey,
154 is_signer: a.is_signer,
155 is_writable: a.is_writable,
156 })
157 .collect(),
158 data: instruction.data,
159 }
160 }
161}
162
163impl From<&SerializableInstruction> for Instruction {
164 fn from(instruction: &SerializableInstruction) -> Self {
165 Instruction {
166 program_id: instruction.program_id,
167 accounts: instruction
168 .accounts
169 .iter()
170 .map(|a| AccountMeta {
171 pubkey: a.pubkey,
172 is_signer: a.is_signer,
173 is_writable: a.is_writable,
174 })
175 .collect(),
176 data: instruction.data.clone(),
177 }
178 }
179}
180
181impl TryFrom<Vec<u8>> for SerializableInstruction {
182 type Error = Error;
183 fn try_from(data: Vec<u8>) -> std::result::Result<Self, Self::Error> {
184 Ok(
185 borsh::try_from_slice_with_schema::<SerializableInstruction>(data.as_slice())
186 .map_err(|_err| ErrorCode::AccountDidNotDeserialize)?,
187 )
188 }
189}
190
191#[derive(
193 AnchorDeserialize,
194 AnchorSerialize,
195 Serialize,
196 Deserialize,
197 BorshSchema,
198 Clone,
199 Debug,
200 Hash,
201 PartialEq,
202)]
203pub struct SerializableAccount {
204 pub pubkey: Pubkey,
206 pub is_signer: bool,
208 pub is_writable: bool,
210}
211
212impl SerializableAccount {
213 pub fn mutable(pubkey: Pubkey, signer: bool) -> Self {
215 Self {
216 pubkey,
217 is_signer: signer,
218 is_writable: true,
219 }
220 }
221
222 pub fn readonly(pubkey: Pubkey, signer: bool) -> Self {
224 Self {
225 pubkey,
226 is_signer: signer,
227 is_writable: false,
228 }
229 }
230}