sablier_network_program/jobs/distribute_fees/
process_snapshot.rs

1use anchor_lang::{prelude::*, solana_program::instruction::Instruction, InstructionData};
2use sablier_utils::thread::ThreadResponse;
3
4use crate::{constants::*, state::*};
5
6// DONE Payout yield.
7//      Transfer lamports collected by Fee accounts to Delegation accounts based on the stake balance distributions of the current Epoch's SnapshotEntries.
8
9// DONE Process unstake requests.
10//      For each "unstake request" transfer tokens from the Worker stake account to the Delegation authority's token account.
11//      Decrement the Delegation's stake balance by the amount unstaked.
12
13// DONE Lock delegated stakes.
14//      Transfer tokens from the Delegation's stake account to the Worker's stake account.
15//      Increment the Delegation's stake balance by the amount moved.
16
17// DONE Take a snapshot.
18//      Capture a snapshot (cumulative sum) of the total stake and broken-down delegation balances.
19//      SnapshotFrames capture worker-level aggregate stake balances.
20//      SnapshotEntries capture delegation-level individual stake balances.
21
22// DONE Cutover from current epoch to new epoch.
23
24#[derive(Accounts)]
25pub struct DistributeFeesProcessSnapshot<'info> {
26    #[account(address = Config::pubkey())]
27    pub config: AccountLoader<'info, Config>,
28
29    #[account(seeds = [SEED_REGISTRY], bump)]
30    pub registry: Account<'info, Registry>,
31
32    #[account(
33        address = snapshot.pubkey(),
34        constraint = snapshot.id == registry.current_epoch
35    )]
36    pub snapshot: Account<'info, Snapshot>,
37
38    #[account(address = config.load()?.epoch_thread)]
39    pub thread: Signer<'info>,
40}
41
42pub fn handler(ctx: Context<DistributeFeesProcessSnapshot>) -> Result<ThreadResponse> {
43    let config = &ctx.accounts.config;
44    let registry = &mut ctx.accounts.registry;
45    let snapshot = &ctx.accounts.snapshot;
46    let thread = &ctx.accounts.thread;
47
48    Ok(ThreadResponse {
49        dynamic_instruction: if snapshot.total_frames > 0 {
50            Some(
51                Instruction {
52                    program_id: crate::ID,
53                    accounts: crate::accounts::DistributeFeesProcessFrame {
54                        config: config.key(),
55                        fee: Fee::pubkey(Worker::pubkey(0)),
56                        registry: registry.key(),
57                        snapshot: snapshot.key(),
58                        snapshot_frame: SnapshotFrame::pubkey(snapshot.key(), 0),
59                        thread: thread.key(),
60                        worker: Worker::pubkey(0),
61                    }
62                    .to_account_metas(Some(true)),
63                    data: crate::instruction::DistributeFeesProcessFrame {}.data(),
64                }
65                .into(),
66            )
67        } else {
68            None
69        },
70        close_to: None,
71        trigger: None,
72    })
73}