sablier_network_program/jobs/delete_snapshot/
process_snapshot.rs

1use anchor_lang::{prelude::*, solana_program::instruction::Instruction, InstructionData};
2use sablier_utils::thread::ThreadResponse;
3
4use crate::{constants::*, state::*};
5
6#[derive(Accounts)]
7pub struct DeleteSnapshotProcessSnapshot<'info> {
8    #[account(address = Config::pubkey())]
9    pub config: AccountLoader<'info, Config>,
10
11    #[account(
12        address = Registry::pubkey(),
13        constraint = !registry.locked
14    )]
15    pub registry: Account<'info, Registry>,
16
17    #[account(
18        mut,
19        seeds = [
20            SEED_SNAPSHOT,
21            snapshot.id.to_be_bytes().as_ref(),
22        ],
23        bump,
24        constraint = snapshot.id < registry.current_epoch
25    )]
26    pub snapshot: Account<'info, Snapshot>,
27
28    #[account(
29        mut,
30        address = config.load()?.epoch_thread
31    )]
32    pub thread: Signer<'info>,
33}
34
35pub fn handler(ctx: Context<DeleteSnapshotProcessSnapshot>) -> Result<ThreadResponse> {
36    // Get accounts
37    let config = &ctx.accounts.config;
38    let registry = &ctx.accounts.registry;
39    let snapshot = &mut ctx.accounts.snapshot;
40    let thread = &mut ctx.accounts.thread;
41
42    // If this snapshot has no entries, then close immediately
43    if snapshot.total_frames == 0 {
44        let snapshot_lamports = snapshot.get_lamports();
45        snapshot.sub_lamports(snapshot_lamports)?;
46        thread.add_lamports(snapshot_lamports)?;
47    }
48
49    // Build next instruction the thread.
50    let dynamic_instruction = if snapshot.total_frames > 0 {
51        // There are frames in this snapshot. Delete them.
52        Some(
53            Instruction {
54                program_id: crate::ID,
55                accounts: crate::accounts::DeleteSnapshotProcessFrame {
56                    config: config.key(),
57                    registry: registry.key(),
58                    snapshot: snapshot.key(),
59                    snapshot_frame: SnapshotFrame::pubkey(snapshot.key(), 0),
60                    thread: thread.key(),
61                }
62                .to_account_metas(Some(true)),
63                data: crate::instruction::DeleteSnapshotProcessFrame {}.data(),
64            }
65            .into(),
66        )
67    } else {
68        // This snaphot has no frames. We are done!
69        None
70    };
71
72    Ok(ThreadResponse {
73        dynamic_instruction,
74        close_to: None,
75        trigger: None,
76    })
77}