clockwork_crank/instructions/
queue_update.rs

1use {
2    crate::state::*,
3    anchor_lang::{prelude::*, system_program::{transfer, Transfer}, solana_program::system_program},
4};
5
6
7#[derive(Accounts)]
8#[instruction(kickoff_instruction: Option<InstructionData>, trigger: Option<Trigger>)]
9pub struct QueueUpdate<'info> {
10    #[account(mut)]
11    pub authority: Signer<'info>,
12
13    #[account(
14        mut,
15        seeds = [
16            SEED_QUEUE, 
17            queue.authority.key().as_ref(),
18            queue.id.as_bytes(),
19        ],
20        bump,
21        has_one = authority,
22    )]
23    pub queue: Account<'info, Queue>,
24
25    #[account(address = system_program::ID)]
26    pub system_program: Program<'info, System>,
27}
28
29pub fn handler(ctx: Context<QueueUpdate>, kickoff_instruction: Option<InstructionData>, trigger: Option<Trigger>) -> Result<()> {
30    // Get accounts
31    let authority = &ctx.accounts.authority;
32    let queue = &mut ctx.accounts.queue;
33    let system_program = &ctx.accounts.system_program;
34
35    // If provided, update the queue's first instruction
36    if let Some(kickoff_instruction) = kickoff_instruction {
37        queue.kickoff_instruction = kickoff_instruction;
38    }
39
40    // If provided, update the queue's trigger and reset the exec context
41    if let Some(trigger) = trigger {
42        queue.trigger = trigger;
43        queue.exec_context = None;
44    }
45
46    // Reallocate mem for the queue account
47    queue.realloc()?;
48
49    // If lamports are required to maintain rent-exemption, pay them
50    let data_len = 8 + queue.try_to_vec()?.len();
51    let minimum_rent = Rent::get().unwrap().minimum_balance(data_len);
52    if minimum_rent > queue.to_account_info().lamports() {
53        transfer(
54            CpiContext::new(
55                system_program.to_account_info(),
56                Transfer {
57                    from: authority.to_account_info(),
58                    to: queue.to_account_info(),
59                },
60            ),
61            minimum_rent
62                .checked_sub(queue.to_account_info().lamports())
63                .unwrap(),
64        )?;
65    }
66
67    Ok(())
68}