miclockwork_network_program/instructions/
worker_update.rs1use {
2 crate::state::*,
3 anchor_lang::{
4 prelude::*,
5 solana_program::system_program,
6 system_program::{transfer, Transfer},
7 },
8};
9
10#[derive(Accounts)]
11#[instruction(settings: WorkerSettings)]
12pub struct WorkerUpdate<'info> {
13 #[account(mut)]
14 pub authority: Signer<'info>,
15
16 #[account(address = system_program::ID)]
17 pub system_program: Program<'info, System>,
18
19 #[account(
20 mut,
21 seeds = [
22 SEED_WORKER,
23 worker.id.to_be_bytes().as_ref(),
24 ],
25 bump,
26 has_one = authority,
27 )]
28 pub worker: Account<'info, Worker>,
29}
30
31pub fn handler(ctx: Context<WorkerUpdate>, settings: WorkerSettings) -> Result<()> {
32 let authority = &ctx.accounts.authority;
34 let worker = &mut ctx.accounts.worker;
35 let system_program = &ctx.accounts.system_program;
36
37 worker.update(settings)?;
39
40 let data_len = 8 + worker.try_to_vec()?.len();
42 worker.to_account_info().realloc(data_len, false)?;
43
44 let minimum_rent = Rent::get().unwrap().minimum_balance(data_len);
46 if minimum_rent > worker.to_account_info().lamports() {
47 transfer(
48 CpiContext::new(
49 system_program.to_account_info(),
50 Transfer {
51 from: authority.to_account_info(),
52 to: worker.to_account_info(),
53 },
54 ),
55 minimum_rent
56 .checked_sub(worker.to_account_info().lamports())
57 .unwrap(),
58 )?;
59 }
60
61 Ok(())
62}