sablier_network_program/jobs/distribute_fees/
process_frame.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 DistributeFeesProcessFrame<'info> {
8    #[account(address = Config::pubkey())]
9    pub config: AccountLoader<'info, Config>,
10
11    #[account(
12        mut,
13        seeds = [
14            SEED_FEE,
15            fee.worker.as_ref(),
16        ],
17        bump,
18        has_one = worker,
19    )]
20    pub fee: Account<'info, Fee>,
21
22    #[account(address = Registry::pubkey())]
23    pub registry: Account<'info, Registry>,
24
25    #[account(
26        address = snapshot.pubkey(),
27        constraint = snapshot.id == registry.current_epoch
28    )]
29    pub snapshot: Account<'info, Snapshot>,
30
31    #[account(
32        address = snapshot_frame.pubkey(),
33        has_one = snapshot,
34        has_one = worker,
35    )]
36    pub snapshot_frame: Account<'info, SnapshotFrame>,
37
38    #[account(address = config.load()?.epoch_thread)]
39    pub thread: Signer<'info>,
40
41    #[account(mut)]
42    pub worker: Account<'info, Worker>,
43}
44
45pub fn handler(ctx: Context<DistributeFeesProcessFrame>) -> Result<ThreadResponse> {
46    // Get accounts.
47    let config = &ctx.accounts.config;
48    let fee = &mut ctx.accounts.fee;
49    let registry = &ctx.accounts.registry;
50    let snapshot = &ctx.accounts.snapshot;
51    let snapshot_frame = &ctx.accounts.snapshot_frame;
52    let thread = &ctx.accounts.thread;
53    let worker = &mut ctx.accounts.worker;
54
55    // Calculate the fee account's usuable balance.
56    let fee_lamport_balance = fee.get_lamports();
57    let fee_data_len = 8 + Fee::INIT_SPACE;
58    let fee_rent_balance = Rent::get()?.minimum_balance(fee_data_len);
59    let fee_usable_balance = fee_lamport_balance - fee_rent_balance;
60
61    // Calculate the commission to be retained by the worker.
62    let commission_balance = fee_usable_balance * worker.commission_rate / 100;
63
64    // Transfer commission to the worker.
65    fee.sub_lamports(commission_balance)?;
66    worker.add_lamports(commission_balance)?;
67
68    // Increment the worker's commission balance.
69    worker.commission_balance += commission_balance;
70
71    // Record the balance that is distributable to delegations.
72    fee.distributable_balance = fee_usable_balance - commission_balance;
73
74    // Build next instruction for the thread.
75    let dynamic_instruction = if snapshot_frame.total_entries > 0 {
76        // This snapshot frame has entries. Distribute fees to the delegations associated with the entries.
77        let delegation_pubkey = Delegation::pubkey(worker.key(), 0);
78        let snapshot_entry_pubkey = SnapshotEntry::pubkey(snapshot_frame.key(), 0);
79        Some(
80            Instruction {
81                program_id: crate::ID,
82                accounts: crate::accounts::DistributeFeesProcessEntry {
83                    config: config.key(),
84                    delegation: delegation_pubkey,
85                    fee: fee.key(),
86                    registry: registry.key(),
87                    snapshot: snapshot.key(),
88                    snapshot_entry: snapshot_entry_pubkey.key(),
89                    snapshot_frame: snapshot_frame.key(),
90                    thread: thread.key(),
91                    worker: worker.key(),
92                }
93                .to_account_metas(Some(true)),
94                data: crate::instruction::DistributeFeesProcessEntry {}.data(),
95            }
96            .into(),
97        )
98    } else if (snapshot_frame.id + 1) < snapshot.total_frames {
99        // This frame has no entries. Move on to the next frame.
100        let next_worker_pubkey = Worker::pubkey(worker.id + 1);
101        let next_snapshot_frame_pubkey =
102            SnapshotFrame::pubkey(snapshot.key(), snapshot_frame.id + 1);
103        Some(
104            Instruction {
105                program_id: crate::ID,
106                accounts: crate::accounts::DistributeFeesProcessFrame {
107                    config: config.key(),
108                    fee: Fee::pubkey(next_worker_pubkey),
109                    registry: registry.key(),
110                    snapshot: snapshot.key(),
111                    snapshot_frame: next_snapshot_frame_pubkey,
112                    thread: thread.key(),
113                    worker: next_worker_pubkey,
114                }
115                .to_account_metas(Some(true)),
116                data: crate::instruction::DistributeFeesProcessFrame {}.data(),
117            }
118            .into(),
119        )
120    } else {
121        None
122    };
123
124    Ok(ThreadResponse {
125        dynamic_instruction,
126        close_to: None,
127        trigger: None,
128    })
129}