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
use {
    crate::{errors::*, state::*},
    anchor_lang::{prelude::*,  solana_program::system_program},
    std::mem::size_of,
};

#[derive(Accounts)]
pub struct PoolCreate<'info> {
    #[account(address = config.admin)]
    pub admin: Signer<'info>,

    #[account(
        address = Config::pubkey(), 
        has_one = admin
    )]
    pub config: Account<'info, Config>,

    #[account(mut)]
    pub payer: Signer<'info>,

    #[account(
        init,
        seeds = [
            SEED_POOL,
            registry.total_pools.to_be_bytes().as_ref(),
        ],
        bump,
        payer = payer,
        space = 8 + size_of::<Pool>() + size_of::<Pubkey>(),
    )]
    pub pool: Account<'info, Pool>,

    #[account(
        mut,
        seeds = [SEED_REGISTRY],
        bump,
        constraint = !registry.locked @ ClockworkError::RegistryLocked
    )]
    pub registry: Box<Account<'info, Registry>>,

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

pub fn handler(ctx: Context<PoolCreate>) -> Result<()> {
    // Get accounts
    let pool = &mut ctx.accounts.pool;
    let registry = &mut ctx.accounts.registry;

    // Initialize the pool account.
    pool.init(registry.total_pools)?;

    // Increment the registry's pool counter.
    registry.total_pools = registry.total_pools.checked_add(1).unwrap();

    Ok(())
}