clockwork_utils/
thread.rs

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
13/// The stand-in pubkey for delegating a payer address to a worker. All workers are re-imbursed by the user for lamports spent during this delegation.
14pub static PAYER_PUBKEY: Pubkey = static_pubkey!("C1ockworkPayer11111111111111111111111111111");
15
16/// The clock object, representing a specific moment in time recorded by a Solana cluster.
17#[derive(AnchorDeserialize, AnchorSerialize, BorshSchema, Clone, Debug, PartialEq)]
18pub struct ClockData {
19    /// The current slot.
20    pub slot: u64,
21    /// The bank epoch.
22    pub epoch: u64,
23    /// The current unix timestamp.
24    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/// The triggering conditions of a thread.
48#[derive(AnchorDeserialize, AnchorSerialize, Debug, Clone, PartialEq)]
49pub enum Trigger {
50    /// Allows a thread to be kicked off whenever the data of an account changes.
51    Account {
52        /// The address of the account to monitor.
53        address: Pubkey,
54        /// The byte offset of the account data to monitor.
55        offset: u64,
56        /// The size of the byte slice to monitor (must be less than 1kb)
57        size: u64,
58    },
59
60    /// Allows an thread to be kicked off according to a one-time or recurring schedule.
61    Cron {
62        /// The schedule in cron syntax. Value must be parsable by the `clockwork_cron` package.
63        schedule: String,
64
65        /// Boolean value indicating whether triggering moments may be skipped if they are missed (e.g. due to network downtime).
66        /// If false, any "missed" triggering moments will simply be executed as soon as the network comes back online.
67        skippable: bool,
68    },
69
70    /// Allows an thread to be kicked off as soon as it's created.
71    Now,
72
73    /// Allows a thread to be kicked off according to a slot.
74    Slot {
75        slot: u64,
76    },
77
78    /// Allows a thread to be kicked off according to an epoch number.
79    Epoch {
80        epoch: u64,
81    },
82
83    Timestamp {
84        unix_ts: i64,
85    },
86}
87
88/// A response value target programs can return to update the thread.
89#[derive(AnchorDeserialize, AnchorSerialize, Clone, Debug)]
90pub struct ThreadResponse {
91    /// If set, the thread will automatically close and return lamports to the provided address.
92    /// If dynamic_instruction is also set, close_to will take precedence and the dynamic instruction will not be executed.
93    pub close_to: Option<Pubkey>,
94    /// A dynamic instruction to execute next.
95    /// If close_to is also set, it will take precedence and the dynamic instruction will not be executed.
96    pub dynamic_instruction: Option<SerializableInstruction>,
97    /// Value to update the thread trigger to.
98    pub trigger: Option<Trigger>,
99}
100
101impl Default for ThreadResponse {
102    fn default() -> Self {
103        return Self {
104            close_to: None,
105            dynamic_instruction: None,
106            trigger: None,
107        };
108    }
109}
110
111/// The data needed execute an instruction on Solana.
112#[derive(
113    AnchorDeserialize,
114    AnchorSerialize,
115    Serialize,
116    Deserialize,
117    BorshSchema,
118    Clone,
119    Debug,
120    Hash,
121    PartialEq,
122)]
123pub struct SerializableInstruction {
124    /// Pubkey of the instruction processor that executes this instruction
125    pub program_id: Pubkey,
126    /// Metadata for what accounts should be passed to the instruction processor
127    pub accounts: Vec<SerializableAccount>,
128    /// Opaque data passed to the instruction processor
129    pub data: Vec<u8>,
130}
131
132impl From<Instruction> for SerializableInstruction {
133    fn from(instruction: Instruction) -> Self {
134        SerializableInstruction {
135            program_id: instruction.program_id,
136            accounts: instruction
137                .accounts
138                .iter()
139                .map(|a| SerializableAccount {
140                    pubkey: a.pubkey,
141                    is_signer: a.is_signer,
142                    is_writable: a.is_writable,
143                })
144                .collect(),
145            data: instruction.data,
146        }
147    }
148}
149
150impl From<&SerializableInstruction> for Instruction {
151    fn from(instruction: &SerializableInstruction) -> Self {
152        Instruction {
153            program_id: instruction.program_id,
154            accounts: instruction
155                .accounts
156                .iter()
157                .map(|a| AccountMeta {
158                    pubkey: a.pubkey,
159                    is_signer: a.is_signer,
160                    is_writable: a.is_writable,
161                })
162                .collect(),
163            data: instruction.data.clone(),
164        }
165    }
166}
167
168impl TryFrom<Vec<u8>> for SerializableInstruction {
169    type Error = Error;
170    fn try_from(data: Vec<u8>) -> std::result::Result<Self, Self::Error> {
171        Ok(
172            borsh::try_from_slice_with_schema::<SerializableInstruction>(data.as_slice())
173                .map_err(|_err| ErrorCode::AccountDidNotDeserialize)?,
174        )
175    }
176}
177
178/// Account metadata needed to execute an instruction on Solana.
179#[derive(
180    AnchorDeserialize,
181    AnchorSerialize,
182    Serialize,
183    Deserialize,
184    BorshSchema,
185    Clone,
186    Debug,
187    Hash,
188    PartialEq,
189)]
190pub struct SerializableAccount {
191    /// An account's public key
192    pub pubkey: Pubkey,
193    /// True if an Instruction requires a Transaction signature matching `pubkey`.
194    pub is_signer: bool,
195    /// True if the `pubkey` can be loaded as a read-write account.
196    pub is_writable: bool,
197}
198
199impl SerializableAccount {
200    /// Construct metadata for a writable account.
201    pub fn mutable(pubkey: Pubkey, signer: bool) -> Self {
202        Self {
203            pubkey,
204            is_signer: signer,
205            is_writable: true,
206        }
207    }
208
209    /// Construct metadata for a read-only account.
210    pub fn readonly(pubkey: Pubkey, signer: bool) -> Self {
211        Self {
212            pubkey,
213            is_signer: signer,
214            is_writable: false,
215        }
216    }
217}