use {
crate::objects::*,
anchor_lang::prelude::*,
clockwork_utils::{anchor_sighash, AccountMetaData, ExecResponse, InstructionData},
};
#[derive(Accounts)]
pub struct RegistryEpochKickoff<'info> {
#[account(address = Config::pubkey())]
pub config: Account<'info, Config>,
#[account(
mut,
seeds = [SEED_REGISTRY],
bump,
)]
pub registry: Account<'info, Registry>,
#[account(
address = snapshot.pubkey(),
constraint = snapshot.id.eq(®istry.current_epoch)
)]
pub snapshot: Account<'info, Snapshot>,
#[account(address = config.epoch_thread)]
pub thread: Signer<'info>,
}
pub fn handler(ctx: Context<RegistryEpochKickoff>) -> Result<ExecResponse> {
let config = &ctx.accounts.config;
let registry = &mut ctx.accounts.registry;
let snapshot = &ctx.accounts.snapshot;
let thread = &ctx.accounts.thread;
registry.locked = true;
let kickoff_instruction = Some(InstructionData {
program_id: crate::ID,
accounts: vec![
AccountMetaData::new_readonly(config.key(), false),
AccountMetaData::new(registry.key(), false),
AccountMetaData::new_readonly(
Snapshot::pubkey(snapshot.id.checked_add(1).unwrap()),
false,
),
AccountMetaData::new_readonly(thread.key(), true),
],
data: anchor_sighash("registry_epoch_kickoff").to_vec(),
});
let next_instruction = if snapshot.total_frames.gt(&0) {
Some(InstructionData {
program_id: crate::ID,
accounts: vec![
AccountMetaData::new_readonly(config.key(), false),
AccountMetaData::new(Fee::pubkey(Worker::pubkey(0)), false),
AccountMetaData::new_readonly(registry.key(), false),
AccountMetaData::new_readonly(snapshot.key(), false),
AccountMetaData::new_readonly(SnapshotFrame::pubkey(snapshot.key(), 0), false),
AccountMetaData::new_readonly(thread.key(), true),
AccountMetaData::new(Worker::pubkey(0), false),
],
data: anchor_sighash("worker_fees_distribute").to_vec(),
})
} else if registry.total_workers.gt(&0) {
Some(InstructionData {
program_id: crate::ID,
accounts: vec![
AccountMetaData::new_readonly(config.key(), false),
AccountMetaData::new_readonly(registry.key(), false),
AccountMetaData::new_readonly(thread.key(), true),
AccountMetaData::new_readonly(Worker::pubkey(0), false),
],
data: anchor_sighash("worker_delegations_stake").to_vec(),
})
} else {
Some(InstructionData {
program_id: crate::ID,
accounts: vec![
AccountMetaData::new_readonly(config.key(), false),
AccountMetaData::new(registry.key(), false),
AccountMetaData::new_readonly(thread.key(), true),
],
data: anchor_sighash("registry_epoch_cutover").to_vec(),
})
};
Ok(ExecResponse {
kickoff_instruction,
next_instruction,
})
}