Skip to content

Commit bdd48b6

Browse files
Small edits to counter example (#83)
* Small edits to counter example * fix tests (#84) --------- Co-authored-by: mwrites <[email protected]>
1 parent fc96585 commit bdd48b6

File tree

4 files changed

+84
-140
lines changed

4 files changed

+84
-140
lines changed

1-counter/programs/counter/src/id.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.

1-counter/programs/counter/src/lib.rs

Lines changed: 70 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,90 @@
1-
pub mod id;
2-
3-
pub use id::ID;
41
use anchor_lang::prelude::*;
52
use anchor_lang::solana_program::{
63
instruction::Instruction, native_token::LAMPORTS_PER_SOL, system_program,
74
};
85
use anchor_lang::InstructionData;
9-
use clockwork_sdk::state::{ThreadAccount};
6+
use clockwork_sdk::state::{Thread, ThreadAccount};
107

8+
declare_id!("HsjAAC3PvCVujGdUwoaLpjbPPGbFgimpFZBHrSooGX2V");
119

1210
#[program]
1311
pub mod counter {
1412
use super::*;
1513

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-
2114
pub fn increment(ctx: Context<Increment>) -> Result<()> {
2215
let counter = &mut ctx.accounts.counter;
23-
2416
counter.current_value = counter.current_value.checked_add(1).unwrap();
2517
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+
);
2723
Ok(())
2824
}
2925

3026
pub fn initialize(ctx: Context<Initialize>, thread_id: Vec<u8>) -> Result<()> {
31-
// Get accounts
27+
// Get accounts.
3228
let system_program = &ctx.accounts.system_program;
3329
let clockwork_program = &ctx.accounts.clockwork_program;
3430
let payer = &ctx.accounts.payer;
3531
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;
3833
let counter = &mut ctx.accounts.counter;
3934

40-
// 1️⃣ Prepare an instruction to feed to the Thread
35+
// 1️⃣ Prepare an instruction to be automated.
4136
let target_ix = Instruction {
4237
program_id: ID,
4338
accounts: crate::accounts::Increment {
44-
system_program: system_program.key(),
4539
counter: counter.key(),
4640
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)),
4944
data: crate::instruction::Increment {}.data(),
5045
};
5146

52-
// 2️⃣ Define a trigger for the Thread to execute
47+
// 2️⃣ Define a trigger for the thread (every 10 secs).
5348
let trigger = clockwork_sdk::state::Trigger::Cron {
5449
schedule: "*/10 * * * * * *".into(),
5550
skippable: true,
5651
};
5752

58-
// 3️⃣ Create Thread via CPI
53+
// 3️⃣ Create thread via CPI.
54+
let bump = *ctx.bumps.get("thread_authority").unwrap();
5955
clockwork_sdk::cpi::thread_create(
6056
CpiContext::new_with_signer(
6157
clockwork_program.to_account_info(),
6258
clockwork_sdk::cpi::ThreadCreate {
6359
payer: payer.to_account_info(),
6460
system_program: system_program.to_account_info(),
6561
thread: thread.to_account_info(),
66-
authority: thread_authority_pda.to_account_info(),
62+
authority: thread_authority.to_account_info(),
6763
},
6864
&[&[THREAD_AUTHORITY_SEED, &[bump]]],
6965
),
70-
LAMPORTS_PER_SOL, // amount
66+
LAMPORTS_PER_SOL, // amount
7167
thread_id, // id
72-
vec![target_ix.into()], // Instructions vec
73-
trigger, // Trigger
68+
vec![target_ix.into()], // instructions
69+
trigger, // trigger
7470
)?;
7571

7672
Ok(())
7773
}
7874

79-
pub fn delete_thread(ctx: Context<DeleteThread>) -> Result<()> {
75+
pub fn reset(ctx: Context<Reset>) -> Result<()> {
8076
// Get accounts
8177
let clockwork_program = &ctx.accounts.clockwork_program;
8278
let payer = &ctx.accounts.payer;
8379
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;
8681

87-
// The actual CPI
82+
// Delete thread via CPI.
83+
let bump = *ctx.bumps.get("thread_authority").unwrap();
8884
clockwork_sdk::cpi::thread_delete(CpiContext::new_with_signer(
8985
clockwork_program.to_account_info(),
9086
clockwork_sdk::cpi::ThreadDelete {
91-
authority: thread_authority_pda.to_account_info(),
87+
authority: thread_authority.to_account_info(),
9288
close_to: payer.to_account_info(),
9389
thread: thread.to_account_info(),
9490
},
@@ -98,139 +94,96 @@ pub mod counter {
9894
}
9995
}
10096

101-
/*
102-
** Accounts
103-
*/
97+
/// The Counter account tracks a value and the timestamp of its last update.
10498
#[account]
10599
#[derive(Debug)]
106100
pub struct Counter {
107101
pub current_value: u64,
108102
pub updated_at: i64,
109103
}
110104

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.
117106
pub const SEED_COUNTER: &[u8] = b"counter";
118107

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.
121109
pub const THREAD_AUTHORITY_SEED: &[u8] = b"authority";
122110

123111
#[derive(Accounts)]
124112
pub struct Increment<'info> {
125-
/// The system program.
126-
#[account(address = system_program::ID)]
127-
pub system_program: Program<'info, System>,
128-
129113
/// The counter account.
130-
#[account(
131-
mut,
132-
seeds = [SEED_COUNTER],
133-
bump,
134-
)]
114+
#[account(mut, seeds = [SEED_COUNTER], bump)]
135115
pub counter: Account<'info, Counter>,
136116

137117
/// 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>,
143120

144121
/// The Thread Admin
145122
/// 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>,
152126
}
153127

154128
#[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.
159132
#[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 > (),
164138
)]
165139
pub counter: Account<'info, Counter>,
166-
}
167140

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).
173147
#[account(mut)]
174148
pub payer: Signer<'info>,
175149

150+
/// The Solana system program.
176151
#[account(address = system_program::ID)]
177152
pub system_program: Program<'info, System>,
178153

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))]
188156
pub thread: SystemAccount<'info>,
189157

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>,
208161
}
209162

210163
#[derive(Accounts)]
211-
pub struct DeleteThread<'info> {
212-
/// Who's paying
164+
pub struct Reset<'info> {
165+
/// The signer.
213166
#[account(mut)]
214167
pub payer: Signer<'info>,
215168

216-
/// Clockwork Program (Thread Program)
169+
/// The Clockwork thread program.
217170
#[account(address = clockwork_sdk::ID)]
218171
pub clockwork_program: Program<'info, clockwork_sdk::ThreadProgram>,
219172

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>,
227176

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
231182
#[account(
232-
seeds = [THREAD_AUTHORITY_SEED],
233-
bump,
183+
mut,
184+
seeds = [SEED_COUNTER],
185+
bump,
186+
close = payer
234187
)]
235-
pub thread_authority_pda: SystemAccount<'info>,
188+
pub counter: Account<'info, Counter>,
236189
}

1-counter/tests/counter.ts

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Counter } from "../target/types/counter";
66
import { print_address, print_thread, waitForThreadExec } from "../../utils/helpers";
77

88
// 0️⃣ Import the Clockwork SDK.
9-
import { ClockworkProvider, PAYER_PUBKEY } from "@clockwork-xyz/sdk";
9+
import { ClockworkProvider } from "@clockwork-xyz/sdk";
1010

1111

1212
const provider = anchor.AnchorProvider.env();
@@ -55,7 +55,7 @@ describe("counter", () => {
5555
systemProgram: SystemProgram.programId,
5656
clockworkProgram: clockworkProvider.threadProgram.programId,
5757
thread: threadAddress,
58-
threadAuthorityPda: threadAuthority,
58+
threadAuthority: threadAuthority,
5959
counter: counter,
6060
})
6161
.rpc();
@@ -84,22 +84,16 @@ describe("counter", () => {
8484
// Just some cleanup to reset the test to a clean state
8585
afterEach(async () => {
8686
try {
87-
await program.methods
88-
.deleteThread()
89-
.accounts({
90-
payer: wallet.publicKey,
91-
clockworkProgram: clockworkProvider.threadProgram.programId,
92-
thread: threadAddress,
93-
threadAuthorityPda: threadAuthority,
94-
})
95-
.rpc();
96-
await program.methods
97-
.delete()
98-
.accounts({
99-
payer: wallet.publicKey,
100-
counter: counter,
101-
})
102-
.rpc();
103-
} catch (e) {}
87+
await program.methods
88+
.reset()
89+
.accounts({
90+
payer: wallet.publicKey,
91+
clockworkProgram: clockworkProvider.threadProgram.programId,
92+
counter: counter,
93+
thread: threadAddress,
94+
threadAuthority: threadAuthority,
95+
})
96+
.rpc();
97+
} catch (e) { }
10498
})
10599
});

scripts/deploy.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ echo "project_name: $crate_name"
3232
echo "program_id: $program_id"
3333

3434
# Update program IDs
35-
replace_in_file 's/^declare_id!(".*");/declare_id!("'${program_id}'");/g' "programs/$crate_name/src/id.rs"
35+
replace_in_file 's/^declare_id!(".*");/declare_id!("'${program_id}'");/g' "programs/$crate_name/src/lib.rs"
3636
replace_in_file 's/^'${crate_name}' = ".*"/'${crate_name}' = "'${program_id}'"/g' Anchor.toml
3737

3838
# Rebuild with new program ID

0 commit comments

Comments
 (0)