sablier_network_program/instructions/
worker_update.rs

1use {
2    crate::{constants::*, state::*},
3    anchor_lang::{
4        prelude::*,
5        system_program::{transfer, Transfer},
6    },
7};
8
9#[derive(Accounts)]
10#[instruction(settings: WorkerSettings)]
11pub struct WorkerUpdate<'info> {
12    #[account(mut)]
13    pub authority: Signer<'info>,
14
15    pub system_program: Program<'info, System>,
16
17    #[account(
18        mut,
19        seeds = [
20            SEED_WORKER,
21            worker.id.to_be_bytes().as_ref(),
22        ],
23        bump,
24        has_one = authority,
25    )]
26    pub worker: Account<'info, Worker>,
27}
28
29pub fn handler(ctx: Context<WorkerUpdate>, settings: WorkerSettings) -> Result<()> {
30    // Get accounts
31    let authority = &ctx.accounts.authority;
32    let worker = &mut ctx.accounts.worker;
33    let system_program = &ctx.accounts.system_program;
34
35    // Update the worker
36    worker.update(settings)?;
37
38    // If lamports are required to maintain rent-exemption, pay them
39    let data_len = 8 + Worker::INIT_SPACE;
40    let minimum_rent = Rent::get()?.minimum_balance(data_len);
41    let worker_lamports = worker.get_lamports();
42    if minimum_rent > worker_lamports {
43        transfer(
44            CpiContext::new(
45                system_program.to_account_info(),
46                Transfer {
47                    from: authority.to_account_info(),
48                    to: worker.to_account_info(),
49                },
50            ),
51            minimum_rent - worker_lamports,
52        )?;
53    }
54
55    Ok(())
56}