sablier_network_program/jobs/delete_snapshot/
process_entry.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 DeleteSnapshotProcessEntry<'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        seeds = [
31            SEED_SNAPSHOT_ENTRY,
32            snapshot_entry.snapshot_frame.as_ref(),
33            snapshot_entry.id.to_be_bytes().as_ref(),
34        ],
35        bump,
36        has_one = snapshot_frame
37    )]
38    pub snapshot_entry: Account<'info, SnapshotEntry>,
39
40    #[account(
41        mut,
42        seeds = [
43            SEED_SNAPSHOT_FRAME,
44            snapshot_frame.snapshot.as_ref(),
45            snapshot_frame.id.to_be_bytes().as_ref(),
46        ],
47        bump,
48        has_one = snapshot,
49    )]
50    pub snapshot_frame: Account<'info, SnapshotFrame>,
51
52    #[account(
53        mut,
54        address = config.load()?.epoch_thread
55    )]
56    pub thread: Signer<'info>,
57}
58
59pub fn handler(ctx: Context<DeleteSnapshotProcessEntry>) -> Result<ThreadResponse> {
60    // Get accounts
61    let config = &ctx.accounts.config;
62    let registry = &ctx.accounts.registry;
63    let snapshot = &mut ctx.accounts.snapshot;
64    let snapshot_entry = &mut ctx.accounts.snapshot_entry;
65    let snapshot_frame = &mut ctx.accounts.snapshot_frame;
66    let thread = &mut ctx.accounts.thread;
67
68    // Close the snapshot entry account.
69    let snapshot_entry_lamports = snapshot_entry.get_lamports();
70    snapshot_entry.sub_lamports(snapshot_entry_lamports)?;
71    thread.add_lamports(snapshot_entry_lamports)?;
72
73    // If this frame has no more entries, then close the frame account.
74    if (snapshot_entry.id + 1) == snapshot_frame.total_entries {
75        let snapshot_frame_lamports = snapshot_frame.get_lamports();
76        snapshot_frame.sub_lamports(snapshot_frame_lamports)?;
77        thread.add_lamports(snapshot_frame_lamports)?;
78
79        // If this is also the last frame in the snapshot, then close the snapshot account.
80        if (snapshot_frame.id + 1) == snapshot.total_frames {
81            let snapshot_lamports = snapshot.get_lamports();
82            snapshot.sub_lamports(snapshot_lamports)?;
83            thread.add_lamports(snapshot_frame_lamports)?;
84        }
85    }
86
87    // Build the next instruction
88    let dynamic_instruction = if (snapshot_entry.id + 1) < snapshot_frame.total_entries {
89        // Move on to the next entry.
90        Some(
91            Instruction {
92                program_id: crate::ID,
93                accounts: crate::accounts::DeleteSnapshotProcessEntry {
94                    config: config.key(),
95                    registry: registry.key(),
96                    snapshot: snapshot.key(),
97                    snapshot_entry: SnapshotEntry::pubkey(
98                        snapshot_frame.key(),
99                        snapshot_entry.id + 1,
100                    ),
101                    snapshot_frame: snapshot_frame.key(),
102                    thread: thread.key(),
103                }
104                .to_account_metas(Some(true)),
105                data: crate::instruction::DeleteSnapshotProcessEntry {}.data(),
106            }
107            .into(),
108        )
109    } else if (snapshot_frame.id + 1) < snapshot.total_frames {
110        // This frame has no more entries. Move onto the next frame.
111        Some(
112            Instruction {
113                program_id: crate::ID,
114                accounts: crate::accounts::DeleteSnapshotProcessFrame {
115                    config: config.key(),
116                    registry: registry.key(),
117                    snapshot: snapshot.key(),
118                    snapshot_frame: SnapshotFrame::pubkey(snapshot.key(), snapshot_frame.id + 1),
119                    thread: thread.key(),
120                }
121                .to_account_metas(Some(true)),
122                data: crate::instruction::DeleteSnapshotProcessFrame {}.data(),
123            }
124            .into(),
125        )
126    } else {
127        // This frame as no more entires and it was the last frame in the snapshot. We are done!
128        None
129    };
130
131    Ok(ThreadResponse {
132        dynamic_instruction,
133        close_to: None,
134        trigger: None,
135    })
136}