miclockwork_thread_program/instructions/
thread_instruction_add.rs1use anchor_lang::{
2 prelude::*,
3 solana_program::system_program,
4 system_program::{transfer, Transfer},
5};
6
7use crate::state::*;
8
9#[derive(Accounts)]
11#[instruction(instruction: SerializableInstruction)]
12pub struct ThreadInstructionAdd<'info> {
13 #[account(mut)]
15 pub authority: Signer<'info>,
16
17 #[account(address = system_program::ID)]
19 pub system_program: Program<'info, System>,
20
21 #[account(
23 mut,
24 seeds = [
25 SEED_THREAD,
26 thread.authority.as_ref(),
27 thread.id.as_slice(),
28 ],
29 bump = thread.bump,
30 has_one = authority
31 )]
32 pub thread: Account<'info, Thread>,
33}
34
35pub fn handler(
36 ctx: Context<ThreadInstructionAdd>,
37 instruction: SerializableInstruction,
38) -> Result<()> {
39 let authority = &ctx.accounts.authority;
41 let thread = &mut ctx.accounts.thread;
42 let system_program = &ctx.accounts.system_program;
43
44 thread.instructions.push(instruction);
46
47 thread.realloc()?;
49
50 let data_len = thread.to_account_info().data_len();
52 let minimum_rent = Rent::get().unwrap().minimum_balance(data_len);
53 if minimum_rent > thread.to_account_info().lamports() {
54 transfer(
55 CpiContext::new(
56 system_program.to_account_info(),
57 Transfer {
58 from: authority.to_account_info(),
59 to: thread.to_account_info(),
60 },
61 ),
62 minimum_rent
63 .checked_sub(thread.to_account_info().lamports())
64 .unwrap(),
65 )?;
66 }
67
68 Ok(())
69}