use anchor_lang::{prelude::*, solana_program::instruction::Instruction, InstructionData};
use anchor_spl::associated_token::get_associated_token_address;
use sablier_utils::thread::{ThreadResponse, PAYER_PUBKEY};
use crate::{constants::*, state::*};
#[derive(Accounts)]
pub struct TakeSnapshotCreateEntry<'info> {
#[account(address = Config::pubkey())]
pub config: AccountLoader<'info, Config>,
#[account(
address = delegation.pubkey(),
constraint = delegation.id == snapshot_frame.total_entries,
has_one = worker,
)]
pub delegation: Box<Account<'info, Delegation>>,
#[account(mut)]
pub payer: Signer<'info>,
#[account(
address = Registry::pubkey(),
constraint = registry.locked
)]
pub registry: Box<Account<'info, Registry>>,
#[account(
address = snapshot.pubkey(),
constraint = (registry.current_epoch + 1) == snapshot.id
)]
pub snapshot: Box<Account<'info, Snapshot>>,
#[account(
init,
seeds = [
SEED_SNAPSHOT_ENTRY,
snapshot_frame.key().as_ref(),
snapshot_frame.total_entries.to_be_bytes().as_ref(),
],
bump,
payer = payer,
space = 8 + SnapshotEntry::INIT_SPACE,
)]
pub snapshot_entry: Account<'info, SnapshotEntry>,
#[account(
mut,
seeds = [
SEED_SNAPSHOT_FRAME,
snapshot_frame.snapshot.as_ref(),
snapshot_frame.id.to_be_bytes().as_ref(),
],
bump,
has_one = snapshot,
constraint = (snapshot_frame.id + 1) == snapshot.total_frames,
)]
pub snapshot_frame: Box<Account<'info, SnapshotFrame>>,
pub system_program: Program<'info, System>,
#[account(address = config.load()?.epoch_thread)]
pub thread: Signer<'info>,
#[account(
address = worker.pubkey(),
constraint = worker.id == snapshot_frame.id,
)]
pub worker: Box<Account<'info, Worker>>,
}
pub fn handler(ctx: Context<TakeSnapshotCreateEntry>) -> Result<ThreadResponse> {
let config_key = ctx.accounts.config.key();
let config = &ctx.accounts.config.load()?;
let delegation = &ctx.accounts.delegation;
let registry = &ctx.accounts.registry;
let snapshot = &mut ctx.accounts.snapshot;
let snapshot_entry = &mut ctx.accounts.snapshot_entry;
let snapshot_frame = &mut ctx.accounts.snapshot_frame;
let system_program = &ctx.accounts.system_program;
let thread = &ctx.accounts.thread;
let worker = &ctx.accounts.worker;
snapshot_entry.init(
delegation.key(),
snapshot_frame.total_entries,
snapshot_frame.key(),
delegation.stake_amount,
)?;
snapshot_frame.total_entries += 1;
let dynamic_instruction = if snapshot_frame.total_entries < worker.total_delegations {
let next_delegation_pubkey = Delegation::pubkey(worker.pubkey(), delegation.id + 1);
let next_snapshot_entry_pubkey =
SnapshotEntry::pubkey(snapshot_frame.key(), snapshot_entry.id + 1);
Some(
Instruction {
program_id: crate::ID,
accounts: crate::accounts::TakeSnapshotCreateEntry {
config: config_key,
delegation: next_delegation_pubkey,
payer: PAYER_PUBKEY,
registry: registry.key(),
snapshot: snapshot.key(),
snapshot_entry: next_snapshot_entry_pubkey,
snapshot_frame: snapshot_frame.key(),
system_program: system_program.key(),
thread: thread.key(),
worker: worker.key(),
}
.to_account_metas(Some(true)),
data: crate::instruction::TakeSnapshotCreateEntry {}.data(),
}
.into(),
)
} else if snapshot.total_frames < registry.total_workers {
let next_snapshot_frame_pubkey =
SnapshotFrame::pubkey(snapshot.key(), snapshot_frame.id + 1);
let next_worker_pubkey = Worker::pubkey(worker.id + 1);
Some(
Instruction {
program_id: crate::ID,
accounts: crate::accounts::TakeSnapshotCreateFrame {
config: config_key,
payer: PAYER_PUBKEY,
registry: registry.key(),
snapshot: snapshot.key(),
snapshot_frame: next_snapshot_frame_pubkey,
system_program: system_program.key(),
thread: thread.key(),
worker: next_worker_pubkey,
worker_stake: get_associated_token_address(&next_worker_pubkey, &config.mint),
}
.to_account_metas(Some(true)),
data: crate::instruction::TakeSnapshotCreateFrame {}.data(),
}
.into(),
)
} else {
None
};
Ok(ThreadResponse {
dynamic_instruction,
..ThreadResponse::default()
})
}