1
- pub mod id;
2
-
3
- pub use id:: ID ;
4
1
use anchor_lang:: prelude:: * ;
5
2
use anchor_lang:: solana_program:: {
6
3
instruction:: Instruction , native_token:: LAMPORTS_PER_SOL , system_program,
7
4
} ;
8
5
use anchor_lang:: InstructionData ;
9
- use clockwork_sdk:: state:: { ThreadAccount } ;
6
+ use clockwork_sdk:: state:: { Thread , ThreadAccount } ;
10
7
8
+ declare_id ! ( "HsjAAC3PvCVujGdUwoaLpjbPPGbFgimpFZBHrSooGX2V" ) ;
11
9
12
10
#[ program]
13
11
pub mod counter {
14
12
use super :: * ;
15
13
16
- // Just here to delete the `Counter` account and reset the tests to a clean state
17
- pub fn delete ( _ctx : Context < Delete > ) -> Result < ( ) > {
18
- Ok ( ( ) )
19
- }
20
-
21
14
pub fn increment ( ctx : Context < Increment > ) -> Result < ( ) > {
22
15
let counter = & mut ctx. accounts . counter ;
23
-
24
16
counter. current_value = counter. current_value . checked_add ( 1 ) . unwrap ( ) ;
25
17
counter. updated_at = Clock :: get ( ) . unwrap ( ) . unix_timestamp ;
26
- msg ! ( "Counter value: {}, updated_at: {}" , counter. current_value, counter. updated_at) ;
18
+ msg ! (
19
+ "Counter value: {}, updated_at: {}" ,
20
+ counter. current_value,
21
+ counter. updated_at
22
+ ) ;
27
23
Ok ( ( ) )
28
24
}
29
25
30
26
pub fn initialize ( ctx : Context < Initialize > , thread_id : Vec < u8 > ) -> Result < ( ) > {
31
- // Get accounts
27
+ // Get accounts.
32
28
let system_program = & ctx. accounts . system_program ;
33
29
let clockwork_program = & ctx. accounts . clockwork_program ;
34
30
let payer = & ctx. accounts . payer ;
35
31
let thread = & ctx. accounts . thread ;
36
- let thread_authority_pda = & ctx. accounts . thread_authority_pda ;
37
- let bump = * ctx. bumps . get ( "thread_authority_pda" ) . unwrap ( ) ;
32
+ let thread_authority = & ctx. accounts . thread_authority ;
38
33
let counter = & mut ctx. accounts . counter ;
39
34
40
- // 1️⃣ Prepare an instruction to feed to the Thread
35
+ // 1️⃣ Prepare an instruction to be automated.
41
36
let target_ix = Instruction {
42
37
program_id : ID ,
43
38
accounts : crate :: accounts:: Increment {
44
- system_program : system_program. key ( ) ,
45
39
counter : counter. key ( ) ,
46
40
thread : thread. key ( ) ,
47
- thread_authority_pda : thread_authority_pda. key ( ) ,
48
- } . to_account_metas ( Some ( true ) ) ,
41
+ thread_authority : thread_authority. key ( ) ,
42
+ }
43
+ . to_account_metas ( Some ( true ) ) ,
49
44
data : crate :: instruction:: Increment { } . data ( ) ,
50
45
} ;
51
46
52
- // 2️⃣ Define a trigger for the Thread to execute
47
+ // 2️⃣ Define a trigger for the thread (every 10 secs).
53
48
let trigger = clockwork_sdk:: state:: Trigger :: Cron {
54
49
schedule : "*/10 * * * * * *" . into ( ) ,
55
50
skippable : true ,
56
51
} ;
57
52
58
- // 3️⃣ Create Thread via CPI
53
+ // 3️⃣ Create thread via CPI.
54
+ let bump = * ctx. bumps . get ( "thread_authority" ) . unwrap ( ) ;
59
55
clockwork_sdk:: cpi:: thread_create (
60
56
CpiContext :: new_with_signer (
61
57
clockwork_program. to_account_info ( ) ,
62
58
clockwork_sdk:: cpi:: ThreadCreate {
63
59
payer : payer. to_account_info ( ) ,
64
60
system_program : system_program. to_account_info ( ) ,
65
61
thread : thread. to_account_info ( ) ,
66
- authority : thread_authority_pda . to_account_info ( ) ,
62
+ authority : thread_authority . to_account_info ( ) ,
67
63
} ,
68
64
& [ & [ THREAD_AUTHORITY_SEED , & [ bump] ] ] ,
69
65
) ,
70
- LAMPORTS_PER_SOL , // amount
66
+ LAMPORTS_PER_SOL , // amount
71
67
thread_id, // id
72
- vec ! [ target_ix. into( ) ] , // Instructions vec
73
- trigger, // Trigger
68
+ vec ! [ target_ix. into( ) ] , // instructions
69
+ trigger, // trigger
74
70
) ?;
75
71
76
72
Ok ( ( ) )
77
73
}
78
74
79
- pub fn delete_thread ( ctx : Context < DeleteThread > ) -> Result < ( ) > {
75
+ pub fn reset ( ctx : Context < Reset > ) -> Result < ( ) > {
80
76
// Get accounts
81
77
let clockwork_program = & ctx. accounts . clockwork_program ;
82
78
let payer = & ctx. accounts . payer ;
83
79
let thread = & ctx. accounts . thread ;
84
- let thread_authority_pda = & ctx. accounts . thread_authority_pda ;
85
- let bump = * ctx. bumps . get ( "thread_authority_pda" ) . unwrap ( ) ;
80
+ let thread_authority = & ctx. accounts . thread_authority ;
86
81
87
- // The actual CPI
82
+ // Delete thread via CPI.
83
+ let bump = * ctx. bumps . get ( "thread_authority" ) . unwrap ( ) ;
88
84
clockwork_sdk:: cpi:: thread_delete ( CpiContext :: new_with_signer (
89
85
clockwork_program. to_account_info ( ) ,
90
86
clockwork_sdk:: cpi:: ThreadDelete {
91
- authority : thread_authority_pda . to_account_info ( ) ,
87
+ authority : thread_authority . to_account_info ( ) ,
92
88
close_to : payer. to_account_info ( ) ,
93
89
thread : thread. to_account_info ( ) ,
94
90
} ,
@@ -98,139 +94,96 @@ pub mod counter {
98
94
}
99
95
}
100
96
101
- /*
102
- ** Accounts
103
- */
97
+ /// The Counter account tracks a value and the timestamp of its last update.
104
98
#[ account]
105
99
#[ derive( Debug ) ]
106
100
pub struct Counter {
107
101
pub current_value : u64 ,
108
102
pub updated_at : i64 ,
109
103
}
110
104
111
- /*
112
- ** Validation Structs
113
- */
114
-
115
- /// Seed for `Counter` account Program Derived Address
116
- /// ⚠️ Make sure it matches whatever you are using on the client-side
105
+ /// Seed for deriving the `Counter` account PDA.
117
106
pub const SEED_COUNTER : & [ u8 ] = b"counter" ;
118
107
119
- /// Seed for thread_authority pda
120
- /// ⚠️ Make sure it matches whatever you are using on the client-side
108
+ /// Seed for thread_authority PDA.
121
109
pub const THREAD_AUTHORITY_SEED : & [ u8 ] = b"authority" ;
122
110
123
111
#[ derive( Accounts ) ]
124
112
pub struct Increment < ' info > {
125
- /// The system program.
126
- #[ account( address = system_program:: ID ) ]
127
- pub system_program : Program < ' info , System > ,
128
-
129
113
/// The counter account.
130
- #[ account(
131
- mut ,
132
- seeds = [ SEED_COUNTER ] ,
133
- bump,
134
- ) ]
114
+ #[ account( mut , seeds = [ SEED_COUNTER ] , bump) ]
135
115
pub counter : Account < ' info , Counter > ,
136
116
137
117
/// Verify that only this thread can execute the Increment Instruction
138
- #[ account(
139
- signer,
140
- constraint = thread. authority. eq( & thread_authority_pda. key( ) )
141
- ) ]
142
- pub thread : Account < ' info , clockwork_sdk:: state:: Thread > ,
118
+ #[ account( signer, constraint = thread. authority. eq( & thread_authority. key( ) ) ) ]
119
+ pub thread : Account < ' info , Thread > ,
143
120
144
121
/// The Thread Admin
145
122
/// The authority that was used as a seed to derive the thread address
146
- /// `thread_authority_pda` should equal `thread.thread_authority`
147
- #[ account(
148
- seeds = [ THREAD_AUTHORITY_SEED ] ,
149
- bump,
150
- ) ]
151
- pub thread_authority_pda : SystemAccount < ' info > ,
123
+ /// `thread_authority` should equal `thread.thread_authority`
124
+ #[ account( seeds = [ THREAD_AUTHORITY_SEED ] , bump) ]
125
+ pub thread_authority : SystemAccount < ' info > ,
152
126
}
153
127
154
128
#[ derive( Accounts ) ]
155
- pub struct Delete < ' info > {
156
- #[ account( mut ) ]
157
- pub payer : Signer < ' info > ,
158
-
129
+ #[ instruction( thread_id: Vec <u8 >) ]
130
+ pub struct Initialize < ' info > {
131
+ /// The counter account to initialize.
159
132
#[ account(
160
- mut ,
161
- seeds = [ SEED_COUNTER ] ,
162
- bump,
163
- close = payer
133
+ init,
134
+ payer = payer,
135
+ seeds = [ SEED_COUNTER ] ,
136
+ bump,
137
+ space = 8 + std:: mem:: size_of:: < Counter > ( ) ,
164
138
) ]
165
139
pub counter : Account < ' info , Counter > ,
166
- }
167
140
168
- #[ derive( Accounts ) ]
169
- #[ instruction( thread_id: Vec < u8 >) ]
170
- pub struct Initialize < ' info > {
171
- /// Who's paying for this Initialize Transaction
172
- /// (not to be confused with the thread executions)
141
+ /// The Clockwork thread program.
142
+ #[ account( address = clockwork_sdk:: ID ) ]
143
+ pub clockwork_program : Program < ' info , clockwork_sdk:: ThreadProgram > ,
144
+
145
+ /// The signer who will pay to initialize the program.
146
+ /// (not to be confused with the thread executions).
173
147
#[ account( mut ) ]
174
148
pub payer : Signer < ' info > ,
175
149
150
+ /// The Solana system program.
176
151
#[ account( address = system_program:: ID ) ]
177
152
pub system_program : Program < ' info , System > ,
178
153
179
- /// Clockwork Program (Thread Program)
180
- #[ account( address = clockwork_sdk:: ID ) ]
181
- pub clockwork_program : Program < ' info , clockwork_sdk:: ThreadProgram > ,
182
-
183
- /// Address to assign to the newly created Thread
184
- #[ account(
185
- mut ,
186
- address = clockwork_sdk:: state:: Thread :: pubkey( thread_authority_pda. key( ) , thread_id)
187
- ) ]
154
+ /// Address to assign to the newly created thread.
155
+ #[ account( mut , address = Thread :: pubkey( thread_authority. key( ) , thread_id) ) ]
188
156
pub thread : SystemAccount < ' info > ,
189
157
190
- /// The Thread Admin
191
- /// The address that was used as a seed to derive the thread address with
192
- /// `clockworkProvider.getThreadPDA(threadAuthority, threadId)`
193
- /// `thread_authority_pda` should equal `thread.authority`
194
- #[ account(
195
- seeds = [ THREAD_AUTHORITY_SEED ] ,
196
- bump,
197
- ) ]
198
- pub thread_authority_pda : SystemAccount < ' info > ,
199
-
200
- #[ account(
201
- init,
202
- payer = payer,
203
- seeds = [ SEED_COUNTER ] ,
204
- bump,
205
- space = 8 + std:: mem:: size_of:: < Counter > ( ) ,
206
- ) ]
207
- pub counter : Account < ' info , Counter > ,
158
+ /// The pda that will own and manage the thread.
159
+ #[ account( seeds = [ THREAD_AUTHORITY_SEED ] , bump) ]
160
+ pub thread_authority : SystemAccount < ' info > ,
208
161
}
209
162
210
163
#[ derive( Accounts ) ]
211
- pub struct DeleteThread < ' info > {
212
- /// Who's paying
164
+ pub struct Reset < ' info > {
165
+ /// The signer.
213
166
#[ account( mut ) ]
214
167
pub payer : Signer < ' info > ,
215
168
216
- /// Clockwork Program (Thread Program)
169
+ /// The Clockwork thread program.
217
170
#[ account( address = clockwork_sdk:: ID ) ]
218
171
pub clockwork_program : Program < ' info , clockwork_sdk:: ThreadProgram > ,
219
172
220
- /// Address to assign to the newly created Thread
221
- #[ account(
222
- mut ,
223
- address = thread. pubkey( ) ,
224
- constraint = thread. authority. eq( & thread_authority_pda. key( ) )
225
- ) ]
226
- pub thread : Account < ' info , clockwork_sdk:: state:: Thread > ,
173
+ /// The thread to reset.
174
+ #[ account( mut , address = thread. pubkey( ) , constraint = thread. authority. eq( & thread_authority. key( ) ) ) ]
175
+ pub thread : Account < ' info , Thread > ,
227
176
228
- /// The Thread Admin
229
- /// The address that was used as a seed to derive the thread address
230
- /// `thread_authority_pda` should equal `thread.authority`
177
+ /// The pda that owns and manages the thread.
178
+ #[ account( seeds = [ THREAD_AUTHORITY_SEED ] , bump) ]
179
+ pub thread_authority : SystemAccount < ' info > ,
180
+
181
+ /// Close the counter account
231
182
#[ account(
232
- seeds = [ THREAD_AUTHORITY_SEED ] ,
233
- bump,
183
+ mut ,
184
+ seeds = [ SEED_COUNTER ] ,
185
+ bump,
186
+ close = payer
234
187
) ]
235
- pub thread_authority_pda : SystemAccount < ' info > ,
188
+ pub counter : Account < ' info , Counter > ,
236
189
}
0 commit comments