clockwork_pool/instructions/
pool_update.rs

1use {
2    crate::state::*,
3    anchor_lang::{
4        prelude::*,
5        solana_program::system_program,
6        system_program::{transfer, Transfer},
7    },
8    std::mem::size_of,
9};
10
11#[derive(Accounts)]
12#[instruction(settings: PoolSettings)]
13pub struct PoolUpdate<'info> {
14    #[account(seeds = [SEED_CONFIG], bump, has_one = pool_authority)]
15    pub config: Account<'info, Config>,
16
17    #[account(mut)]
18    pub payer: Signer<'info>,
19
20    #[account(mut, seeds = [SEED_POOL, pool.name.as_bytes()], bump)]
21    pub pool: Account<'info, Pool>,
22
23    #[account()]
24    pub pool_authority: Signer<'info>,
25
26    #[account(address = system_program::ID)]
27    pub system_program: Program<'info, System>,
28}
29
30pub fn handler(ctx: Context<PoolUpdate>, settings: PoolSettings) -> Result<()> {
31    // Get accounts
32    let payer = &ctx.accounts.payer;
33    let pool = &mut ctx.accounts.pool;
34    let system_program = &ctx.accounts.system_program;
35
36    // Update the pool settings
37    pool.update(&settings)?;
38
39    // Reallocate memory for the pool account
40    let data_len = 8 + size_of::<Pool>() + settings.size.checked_mul(size_of::<Pubkey>()).unwrap();
41    pool.to_account_info().realloc(data_len, false)?;
42
43    // If lamports are required to maintain rent-exemption, pay them
44    let minimum_rent = Rent::get().unwrap().minimum_balance(data_len);
45    if minimum_rent > pool.to_account_info().lamports() {
46        transfer(
47            CpiContext::new(
48                system_program.to_account_info(),
49                Transfer {
50                    from: payer.to_account_info(),
51                    to: pool.to_account_info(),
52                },
53            ),
54            minimum_rent
55                .checked_sub(pool.to_account_info().lamports())
56                .unwrap(),
57        )?;
58    }
59
60    Ok(())
61}