miclockwork_network_program/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()]
15    pub admin: Signer<'info>,
16
17    #[account(
18        address = Config::pubkey(), 
19        has_one = admin
20    )]
21    pub config: Account<'info, Config>,
22
23    #[account(mut)]
24    pub payer: Signer<'info>,
25
26    #[account(mut, address = pool.pubkey())]
27    pub pool: Account<'info, Pool>,
28
29    #[account(address = system_program::ID)]
30    pub system_program: Program<'info, System>,
31}
32
33pub fn handler(ctx: Context<PoolUpdate>, settings: PoolSettings) -> Result<()> {
34    // Get accounts
35    let payer = &ctx.accounts.payer;
36    let pool = &mut ctx.accounts.pool;
37    let system_program = &ctx.accounts.system_program;
38
39    // Update the pool settings
40    pool.update(&settings)?;
41
42    // Reallocate memory for the pool account
43    let data_len = 8 + size_of::<Pool>() + settings.size.checked_mul(size_of::<Pubkey>()).unwrap();
44    pool.to_account_info().realloc(data_len, false)?;
45
46    // If lamports are required to maintain rent-exemption, pay them
47    let minimum_rent = Rent::get().unwrap().minimum_balance(data_len);
48    if minimum_rent > pool.to_account_info().lamports() {
49        transfer(
50            CpiContext::new(
51                system_program.to_account_info(),
52                Transfer {
53                    from: payer.to_account_info(),
54                    to: pool.to_account_info(),
55                },
56            ),
57            minimum_rent
58                .checked_sub(pool.to_account_info().lamports())
59                .unwrap(),
60        )?;
61    }
62
63    Ok(())
64}