sablier_network_program/jobs/take_snapshot/
create_entry.rs1use anchor_lang::{prelude::*, solana_program::instruction::Instruction, InstructionData};
2use anchor_spl::associated_token::get_associated_token_address;
3use sablier_utils::thread::{ThreadResponse, PAYER_PUBKEY};
4
5use crate::{constants::*, state::*};
6
7#[derive(Accounts)]
8pub struct TakeSnapshotCreateEntry<'info> {
9 #[account(address = Config::pubkey())]
10 pub config: AccountLoader<'info, Config>,
11
12 #[account(
13 address = delegation.pubkey(),
14 constraint = delegation.id == snapshot_frame.total_entries,
15 has_one = worker,
16 )]
17 pub delegation: Box<Account<'info, Delegation>>,
18
19 #[account(mut)]
20 pub payer: Signer<'info>,
21
22 #[account(
23 address = Registry::pubkey(),
24 constraint = registry.locked
25 )]
26 pub registry: Box<Account<'info, Registry>>,
27
28 #[account(
29 address = snapshot.pubkey(),
30 constraint = (registry.current_epoch + 1) == snapshot.id
31 )]
32 pub snapshot: Box<Account<'info, Snapshot>>,
33
34 #[account(
35 init,
36 seeds = [
37 SEED_SNAPSHOT_ENTRY,
38 snapshot_frame.key().as_ref(),
39 snapshot_frame.total_entries.to_be_bytes().as_ref(),
40 ],
41 bump,
42 payer = payer,
43 space = 8 + SnapshotEntry::INIT_SPACE,
44 )]
45 pub snapshot_entry: Account<'info, SnapshotEntry>,
46
47 #[account(
48 mut,
49 seeds = [
50 SEED_SNAPSHOT_FRAME,
51 snapshot_frame.snapshot.as_ref(),
52 snapshot_frame.id.to_be_bytes().as_ref(),
53 ],
54 bump,
55 has_one = snapshot,
56 constraint = (snapshot_frame.id + 1) == snapshot.total_frames,
57 )]
58 pub snapshot_frame: Box<Account<'info, SnapshotFrame>>,
59
60 pub system_program: Program<'info, System>,
61
62 #[account(address = config.load()?.epoch_thread)]
63 pub thread: Signer<'info>,
64
65 #[account(
66 address = worker.pubkey(),
67 constraint = worker.id == snapshot_frame.id,
68 )]
69 pub worker: Box<Account<'info, Worker>>,
70}
71
72pub fn handler(ctx: Context<TakeSnapshotCreateEntry>) -> Result<ThreadResponse> {
73 let config_key = ctx.accounts.config.key();
75 let config = &ctx.accounts.config.load()?;
76 let delegation = &ctx.accounts.delegation;
77 let registry = &ctx.accounts.registry;
78 let snapshot = &mut ctx.accounts.snapshot;
79 let snapshot_entry = &mut ctx.accounts.snapshot_entry;
80 let snapshot_frame = &mut ctx.accounts.snapshot_frame;
81 let system_program = &ctx.accounts.system_program;
82 let thread = &ctx.accounts.thread;
83 let worker = &ctx.accounts.worker;
84
85 snapshot_entry.init(
87 delegation.key(),
88 snapshot_frame.total_entries,
89 snapshot_frame.key(),
90 delegation.stake_amount,
91 )?;
92
93 snapshot_frame.total_entries += 1;
95
96 let dynamic_instruction = if snapshot_frame.total_entries < worker.total_delegations {
98 let next_delegation_pubkey = Delegation::pubkey(worker.pubkey(), delegation.id + 1);
100 let next_snapshot_entry_pubkey =
101 SnapshotEntry::pubkey(snapshot_frame.key(), snapshot_entry.id + 1);
102 Some(
103 Instruction {
104 program_id: crate::ID,
105 accounts: crate::accounts::TakeSnapshotCreateEntry {
106 config: config_key,
107 delegation: next_delegation_pubkey,
108 payer: PAYER_PUBKEY,
109 registry: registry.key(),
110 snapshot: snapshot.key(),
111 snapshot_entry: next_snapshot_entry_pubkey,
112 snapshot_frame: snapshot_frame.key(),
113 system_program: system_program.key(),
114 thread: thread.key(),
115 worker: worker.key(),
116 }
117 .to_account_metas(Some(true)),
118 data: crate::instruction::TakeSnapshotCreateEntry {}.data(),
119 }
120 .into(),
121 )
122 } else if snapshot.total_frames < registry.total_workers {
123 let next_snapshot_frame_pubkey =
125 SnapshotFrame::pubkey(snapshot.key(), snapshot_frame.id + 1);
126 let next_worker_pubkey = Worker::pubkey(worker.id + 1);
127 Some(
128 Instruction {
129 program_id: crate::ID,
130 accounts: crate::accounts::TakeSnapshotCreateFrame {
131 config: config_key,
132 payer: PAYER_PUBKEY,
133 registry: registry.key(),
134 snapshot: snapshot.key(),
135 snapshot_frame: next_snapshot_frame_pubkey,
136 system_program: system_program.key(),
137 thread: thread.key(),
138 worker: next_worker_pubkey,
139 worker_stake: get_associated_token_address(&next_worker_pubkey, &config.mint),
140 }
141 .to_account_metas(Some(true)),
142 data: crate::instruction::TakeSnapshotCreateFrame {}.data(),
143 }
144 .into(),
145 )
146 } else {
147 None
148 };
149
150 Ok(ThreadResponse {
151 dynamic_instruction,
152 ..ThreadResponse::default()
153 })
154}