1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use {
    crate::objects::*,
    anchor_lang::{
        prelude::*,
        solana_program::{instruction::Instruction, system_program},
    },
    anchor_spl::token::Mint,
    clockwork_queue_program::objects::{Queue, Trigger},
    std::mem::size_of,
};

#[derive(Accounts)]
pub struct Initialize<'info> {
    #[account(mut)]
    pub admin: Signer<'info>,

    #[account(
        init,
        address = Authority::pubkey(),
        payer = admin,
        space = 8 + size_of::<Authority>(),
    )]
    pub authority: Account<'info, Authority>,

    #[account(address = clockwork_queue_program::ID)]
    pub clockwork_program: Program<'info, clockwork_queue_program::program::QueueProgram>,

    #[account(
        init,
        address = Config::pubkey(),
        payer = admin,
        space = 8 + size_of::<Config>(),
    )]
    pub config: Account<'info, Config>,

    #[account(
        init,
        address = Rotator::pubkey(),
        payer = admin,
        space = 8 + size_of::<Rotator>(),
    )]
    pub rotator: Account<'info, Rotator>,

    #[account()]
    pub mint: Account<'info, Mint>,

    #[account(
        init,
        address = Registry::pubkey(),
        payer = admin,
        space = 8 + size_of::<Registry>(),
    )]
    pub registry: Account<'info, Registry>,

    #[account(
        init,
        address = Snapshot::pubkey(0),
        payer = admin,
        space = 8 + size_of::<Snapshot>(),
    )]
    pub snapshot: Account<'info, Snapshot>,

    #[account(address = Queue::pubkey(authority.key(), "snapshot".into()))]
    pub snapshot_queue: SystemAccount<'info>,

    #[account(address = system_program::ID)]
    pub system_program: Program<'info, System>,
}

pub fn handler<'info>(ctx: Context<'_, '_, '_, 'info, Initialize<'info>>) -> Result<()> {
    // Get accounts
    let admin = &ctx.accounts.admin;
    let authority = &ctx.accounts.authority;
    let clockwork_program = &ctx.accounts.clockwork_program;
    let config = &mut ctx.accounts.config;
    let rotator = &mut ctx.accounts.rotator;
    let mint = &ctx.accounts.mint;
    let registry = &mut ctx.accounts.registry;
    let snapshot = &mut ctx.accounts.snapshot;
    let snapshot_queue = &ctx.accounts.snapshot_queue;
    let system_program = &ctx.accounts.system_program;

    // Initialize accounts
    config.init(admin.key(), mint.key())?;
    registry.init()?;
    rotator.init()?;

    // Setup the first snapshot
    registry.new_snapshot(snapshot)?;
    registry.rotate_snapshot(None, snapshot)?;

    // Create a queue to take snapshots of the registry
    let bump = *ctx.bumps.get("authority").unwrap();
    let snapshot_kickoff_ix = Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new_readonly(authority.key(), false),
            AccountMeta::new(registry.key(), false),
            AccountMeta::new_readonly(snapshot_queue.key(), true),
        ],
        data: clockwork_queue_program::utils::anchor_sighash("snapshot_kickoff").into(),
    };
    clockwork_queue_program::cpi::queue_create(
        CpiContext::new_with_signer(
            clockwork_program.to_account_info(),
            clockwork_queue_program::cpi::accounts::QueueCreate {
                authority: authority.to_account_info(),
                payer: admin.to_account_info(),
                queue: snapshot_queue.to_account_info(),
                system_program: system_program.to_account_info(),
            },
            &[&[SEED_AUTHORITY, &[bump]]],
        ),
        "snapshot".into(),
        snapshot_kickoff_ix.into(),
        Trigger::Cron {
            schedule: "0 * * * * * *".into(),
            skippable: true,
        },
    )?;

    Ok(())
}