miclockwork_network_program/instructions/
initialize.rs

1use {
2    crate::state::*,
3    anchor_lang::{prelude::*, solana_program::system_program},
4    anchor_spl::token::Mint,
5    std::mem::size_of,
6};
7
8#[derive(Accounts)]
9pub struct Initialize<'info> {
10    #[account(mut)]
11    pub admin: Signer<'info>,
12
13    #[account(
14        init,
15        seeds = [SEED_CONFIG],
16        bump,
17        payer = admin,
18        space = 8 + size_of::<Config>(),
19    )]
20    pub config: Account<'info, Config>,
21
22    #[account()]
23    pub mint: Account<'info, Mint>,
24
25    #[account(
26        init,
27        seeds = [SEED_REGISTRY],
28        bump,
29        payer = admin,
30        space = 8 + size_of::<Registry>(),
31    )]
32    pub registry: Account<'info, Registry>,
33
34    #[account(
35        init,
36        seeds = [
37            SEED_SNAPSHOT,
38            (0 as u64).to_be_bytes().as_ref(),
39        ],
40        bump,
41        payer = admin,
42        space = 8 + size_of::<Snapshot>(),
43    )]
44    pub snapshot: Account<'info, Snapshot>,
45
46    #[account(address = system_program::ID)]
47    pub system_program: Program<'info, System>,
48}
49
50pub fn handler(ctx: Context<Initialize>) -> Result<()> {
51    // Get accounts
52    let admin = &ctx.accounts.admin;
53    let config = &mut ctx.accounts.config;
54    let mint = &ctx.accounts.mint;
55    let registry = &mut ctx.accounts.registry;
56    let snapshot = &mut ctx.accounts.snapshot;
57
58    // Initialize accounts.
59    config.init(admin.key(), mint.key())?;
60    registry.init()?;
61    snapshot.init(0)?;
62
63    Ok(())
64}