clockwork_scheduler/instructions/
queue_new.rs

1use {
2    crate::state::*,
3    anchor_lang::{prelude::*, system_program::{transfer, Transfer}, solana_program::system_program},
4    std::mem::size_of
5};
6
7#[derive(Accounts)]
8#[instruction(balance: u64, name: String, schedule: String)]
9pub struct QueueNew<'info> {
10    #[account()]
11    pub authority: Signer<'info>,
12
13    #[account(mut)]
14    pub payer: Signer<'info>,
15
16    #[account(
17        init,
18        seeds = [
19            SEED_QUEUE, 
20            authority.key().as_ref(),
21            name.as_bytes(),
22        ],
23        bump,
24        payer = payer,
25        space = 8 + size_of::<Queue>() + name.len(),
26    )]
27    pub queue: Account<'info, Queue>,
28
29    #[account(address = system_program::ID)]
30    pub system_program: Program<'info, System>,
31}
32
33pub fn handler(ctx: Context<QueueNew>, balance: u64, name: String, schedule: String) -> Result<()> {
34    // Get accounts
35    let authority = &ctx.accounts.authority;
36    let payer = &mut ctx.accounts.payer;
37    let queue = &mut ctx.accounts.queue;
38    let system_program = &ctx.accounts.system_program;
39
40    // Initialize accounts
41    queue.new(authority.key(), name, schedule)?;
42
43    // Transfer balance into the queue
44    transfer(
45        CpiContext::new(
46            system_program.to_account_info(),
47            Transfer {
48                from: payer.to_account_info(),
49                to: queue.to_account_info(),
50            },
51        ),
52        balance,
53    )?;
54
55    Ok(())
56}