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
68
69
use {
    crate::objects::*,
    anchor_lang::{prelude::*, solana_program::instruction::Instruction},
    clockwork_queue_program::objects::{CrankResponse, Queue, QueueAccount},
};

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

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

    #[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<SnapshotClose>) -> Result<CrankResponse> {
    // Get accounts
    let authority = &ctx.accounts.authority;
    let snapshot = &mut ctx.accounts.snapshot;
    let snapshot_queue = &mut ctx.accounts.snapshot_queue;

    // If this snapshot has no entries, then close immediately
    if snapshot.node_count == 0 {
        let snapshot_lamports = snapshot.to_account_info().lamports();
        **snapshot.to_account_info().lamports.borrow_mut() = 0;
        **snapshot_queue.to_account_info().lamports.borrow_mut() = snapshot_queue
            .to_account_info()
            .lamports()
            .checked_add(snapshot_lamports)
            .unwrap();
    } else {
        // Otherwise, set the status to closing
        snapshot.status = SnapshotStatus::Closing;
    }

    // If there are entries to capture, build the next instruction
    let next_instruction = if snapshot.node_count > 0 {
        let entry_pubkey = SnapshotEntry::pubkey(snapshot.key(), 0);
        Some(
            Instruction {
                program_id: crate::ID,
                accounts: vec![
                    AccountMeta::new_readonly(authority.key(), false),
                    AccountMeta::new(entry_pubkey, false),
                    AccountMeta::new(snapshot.key(), false),
                    AccountMeta::new(snapshot_queue.key(), true),
                ],
                data: clockwork_queue_program::utils::anchor_sighash("entry_close").into(),
            }
            .into(),
        )
    } else {
        None
    };

    Ok(CrankResponse { next_instruction })
}