1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use {
    crate::objects::*,
    anchor_lang::{prelude::*, solana_program::instruction::Instruction},
    clockwork_queue_program::objects::{CrankResponse, Queue, QueueAccount},
};

#[derive(Accounts)]
pub struct SnapshotRotate<'info> {
    #[account(address = Authority::pubkey())]
    pub authority: Account<'info, Authority>,

    #[account(address = Config::pubkey())]
    pub config: Account<'info, Config>,

    #[account(
        mut,
        address = current_snapshot.pubkey(),
        constraint = current_snapshot.status == SnapshotStatus::Current
    )]
    pub current_snapshot: Account<'info, Snapshot>,

    #[account(
        mut,
        address = next_snapshot.pubkey(),
        constraint = current_snapshot.id.checked_add(1).unwrap().eq(&next_snapshot.id)
    )]
    pub next_snapshot: Account<'info, Snapshot>,

    #[account(mut, address = Registry::pubkey())]
    pub registry: Account<'info, Registry>,

    #[account(
        address = snapshot_queue.pubkey(),
        constraint = snapshot_queue.id.eq("snapshot"),
        has_one = authority,
        signer,
    )]
    pub snapshot_queue: Account<'info, Queue>,
}

pub fn handler(ctx: Context<SnapshotRotate>) -> Result<CrankResponse> {
    // Get accounts
    let authority = &ctx.accounts.authority;
    let current_snapshot = &mut ctx.accounts.current_snapshot;
    let next_snapshot = &mut ctx.accounts.next_snapshot;
    let registry = &mut ctx.accounts.registry;
    let snapshot_queue = &ctx.accounts.snapshot_queue;

    // Rotate the snapshot
    registry.rotate_snapshot(Some(current_snapshot), next_snapshot)?;

    // Build the next instruction
    let next_instruction = Some(
        Instruction {
            program_id: crate::ID,
            accounts: vec![
                AccountMeta::new_readonly(authority.key(), false),
                AccountMeta::new(current_snapshot.key(), false),
                AccountMeta::new(snapshot_queue.key(), true),
            ],
            data: clockwork_queue_program::utils::anchor_sighash("snapshot_close").into(),
        }
        .into(),
    );

    Ok(CrankResponse { next_instruction })
}