clockwork_network/instructions/
snapshot_rotate.rs

1use {
2    crate::state::*,
3    anchor_lang::{prelude::*, solana_program::instruction::Instruction},
4    clockwork_crank::state::{CrankResponse, Queue, SEED_QUEUE},
5};
6
7#[derive(Accounts)]
8pub struct SnapshotRotate<'info> {
9    #[account(seeds = [SEED_AUTHORITY], bump)]
10    pub authority: Account<'info, Authority>,
11
12    #[account(seeds = [SEED_CONFIG], bump)]
13    pub config: Account<'info, Config>,
14
15    #[account(
16        mut,
17        seeds = [
18            SEED_SNAPSHOT,
19            current_snapshot.id.to_be_bytes().as_ref()
20        ],
21        bump,
22        constraint = current_snapshot.status == SnapshotStatus::Current
23    )]
24    pub current_snapshot: Account<'info, Snapshot>,
25
26    #[account(
27        mut,
28        seeds = [
29            SEED_SNAPSHOT,
30            current_snapshot.id.checked_add(1).unwrap().to_be_bytes().as_ref()
31        ],
32        bump,
33    )]
34    pub next_snapshot: Account<'info, Snapshot>,
35
36    #[account(mut, seeds = [SEED_REGISTRY], bump)]
37    pub registry: Account<'info, Registry>,
38
39    #[account(
40        signer, 
41        seeds = [
42            SEED_QUEUE, 
43            authority.key().as_ref(), 
44            "snapshot".as_bytes()
45        ], 
46        seeds::program = clockwork_crank::ID,
47        bump,
48        has_one = authority
49    )]
50    pub snapshot_queue: Account<'info, Queue>,
51}
52
53pub fn handler(ctx: Context<SnapshotRotate>) -> Result<CrankResponse> {
54    // Get accounts
55    let authority = &ctx.accounts.authority;
56    let current_snapshot = &mut ctx.accounts.current_snapshot;
57    let next_snapshot = &mut ctx.accounts.next_snapshot;
58    let registry = &mut ctx.accounts.registry;
59    let snapshot_queue = &ctx.accounts.snapshot_queue;
60
61    // Rotate the snapshot
62    registry.rotate_snapshot(Some(current_snapshot), next_snapshot)?;
63
64    // Build the next instruction
65    let next_instruction = Some(
66        Instruction {
67            program_id: crate::ID,
68            accounts: vec![
69                AccountMeta::new_readonly(authority.key(), false),
70                AccountMeta::new(current_snapshot.key(), false),
71                AccountMeta::new(snapshot_queue.key(), true),
72            ],
73            data: clockwork_crank::anchor::sighash("snapshot_close").into(),
74        }.into()
75    );
76
77    Ok(CrankResponse { next_instruction })
78}