miclockwork_network_program/instructions/
pool_create.rs

1use {
2    crate::{errors::*, state::*},
3    anchor_lang::{prelude::*,  solana_program::system_program},
4    std::mem::size_of,
5};
6
7#[derive(Accounts)]
8pub struct PoolCreate<'info> {
9    #[account(address = config.admin)]
10    pub admin: Signer<'info>,
11
12    #[account(
13        address = Config::pubkey(), 
14        has_one = admin
15    )]
16    pub config: Account<'info, Config>,
17
18    #[account(mut)]
19    pub payer: Signer<'info>,
20
21    #[account(
22        init,
23        seeds = [
24            SEED_POOL,
25            registry.total_pools.to_be_bytes().as_ref(),
26        ],
27        bump,
28        payer = payer,
29        space = 8 + size_of::<Pool>() + size_of::<Pubkey>(),
30    )]
31    pub pool: Account<'info, Pool>,
32
33    #[account(
34        mut,
35        seeds = [SEED_REGISTRY],
36        bump,
37        constraint = !registry.locked @ ClockworkError::RegistryLocked
38    )]
39    pub registry: Box<Account<'info, Registry>>,
40
41    #[account(address = system_program::ID)]
42    pub system_program: Program<'info, System>,
43}
44
45pub fn handler(ctx: Context<PoolCreate>) -> Result<()> {
46    // Get accounts
47    let pool = &mut ctx.accounts.pool;
48    let registry = &mut ctx.accounts.registry;
49
50    // Initialize the pool account.
51    pool.init(registry.total_pools)?;
52
53    // Increment the registry's pool counter.
54    registry.total_pools = registry.total_pools.checked_add(1).unwrap();
55
56    Ok(())
57}