sablier_network_program/jobs/stake_delegations/
process_worker.rs

1use anchor_lang::{prelude::*, solana_program::instruction::Instruction, InstructionData};
2use anchor_spl::associated_token::get_associated_token_address;
3use sablier_utils::thread::ThreadResponse;
4
5use crate::state::*;
6
7#[derive(Accounts)]
8pub struct StakeDelegationsProcessWorker<'info> {
9    #[account(address = Config::pubkey())]
10    pub config: AccountLoader<'info, Config>,
11
12    #[account(
13        address = Registry::pubkey(),
14        constraint = registry.locked
15    )]
16    pub registry: Account<'info, Registry>,
17
18    #[account(address = config.load()?.epoch_thread)]
19    pub thread: Signer<'info>,
20
21    #[account(address = worker.pubkey())]
22    pub worker: Account<'info, Worker>,
23}
24
25pub fn handler(ctx: Context<StakeDelegationsProcessWorker>) -> Result<ThreadResponse> {
26    // Get accounts.
27    let config_key = ctx.accounts.config.key();
28    let config = &ctx.accounts.config.load()?;
29    let registry = &ctx.accounts.registry;
30    let thread = &ctx.accounts.thread;
31    let worker = &ctx.accounts.worker;
32
33    // Build the next instruction for the thread.
34    let dynamic_instruction = if worker.total_delegations > 0 {
35        // This worker has delegations. Stake their deposits.
36        let delegation_pubkey = Delegation::pubkey(worker.key(), 0);
37        Some(
38            Instruction {
39                program_id: crate::ID,
40                accounts: crate::accounts::StakeDelegationsProcessDelegation {
41                    config: config_key,
42                    delegation: delegation_pubkey,
43                    delegation_stake: get_associated_token_address(
44                        &delegation_pubkey,
45                        &config.mint,
46                    ),
47                    registry: registry.key(),
48                    thread: thread.key(),
49                    token_program: anchor_spl::token::ID,
50                    worker: worker.key(),
51                    worker_stake: get_associated_token_address(&worker.key(), &config.mint),
52                }
53                .to_account_metas(Some(true)),
54                data: crate::instruction::StakeDelegationsProcessDelegation {}.data(),
55            }
56            .into(),
57        )
58    } else if (worker.id + 1) < registry.total_workers {
59        // This worker has no delegations. Move on to the next worker.
60        Some(
61            Instruction {
62                program_id: crate::ID,
63                accounts: crate::accounts::StakeDelegationsProcessWorker {
64                    config: config_key,
65                    registry: registry.key(),
66                    thread: thread.key(),
67                    worker: Worker::pubkey(worker.id + 1),
68                }
69                .to_account_metas(Some(true)),
70                data: crate::instruction::StakeDelegationsProcessWorker {}.data(),
71            }
72            .into(),
73        )
74    } else {
75        None
76    };
77
78    Ok(ThreadResponse {
79        dynamic_instruction,
80        close_to: None,
81        trigger: None,
82    })
83}