use anchor_lang::{
prelude::*,
system_program::{transfer, Transfer},
};
use sablier_utils::account::AccountInfoExt;
use crate::{constants::*, state::*};
#[derive(Accounts)]
#[instruction(instruction: SerializableInstruction)]
pub struct ThreadInstructionAdd<'info> {
#[account(mut)]
pub authority: Signer<'info>,
pub system_program: Program<'info, System>,
#[account(
mut,
seeds = [
SEED_THREAD,
thread.authority.as_ref(),
thread.id.as_slice(),
thread.domain.as_ref().unwrap_or(&Vec::new()).as_slice()
],
bump = thread.bump,
has_one = authority
)]
pub thread: Account<'info, Thread>,
}
pub fn handler(
ctx: Context<ThreadInstructionAdd>,
instruction: SerializableInstruction,
) -> Result<()> {
let authority = &ctx.accounts.authority;
let thread = &mut ctx.accounts.thread;
let system_program = &ctx.accounts.system_program;
thread.instructions.push(instruction);
thread.realloc_account()?;
let data_len = thread.data_len();
let minimum_rent = Rent::get()?.minimum_balance(data_len);
let thread_lamports = thread.get_lamports();
if minimum_rent > thread_lamports {
transfer(
CpiContext::new(
system_program.to_account_info(),
Transfer {
from: authority.to_account_info(),
to: thread.to_account_info(),
},
),
minimum_rent - thread_lamports,
)?;
}
Ok(())
}