use {
crate::{constants::*, state::*},
anchor_lang::{
prelude::*,
system_program::{transfer, Transfer},
},
};
#[derive(Accounts)]
#[instruction(settings: WorkerSettings)]
pub struct WorkerUpdate<'info> {
#[account(mut)]
pub authority: Signer<'info>,
pub system_program: Program<'info, System>,
#[account(
mut,
seeds = [
SEED_WORKER,
worker.id.to_be_bytes().as_ref(),
],
bump,
has_one = authority,
)]
pub worker: Account<'info, Worker>,
}
pub fn handler(ctx: Context<WorkerUpdate>, settings: WorkerSettings) -> Result<()> {
let authority = &ctx.accounts.authority;
let worker = &mut ctx.accounts.worker;
let system_program = &ctx.accounts.system_program;
worker.update(settings)?;
let data_len = 8 + Worker::INIT_SPACE;
let minimum_rent = Rent::get()?.minimum_balance(data_len);
let worker_lamports = worker.get_lamports();
if minimum_rent > worker_lamports {
transfer(
CpiContext::new(
system_program.to_account_info(),
Transfer {
from: authority.to_account_info(),
to: worker.to_account_info(),
},
),
minimum_rent - worker_lamports,
)?;
}
Ok(())
}