sablier_thread_program/instructions/
thread_update.rs1use crate::{constants::*, errors::SablierError, state::*};
2
3use anchor_lang::{
4 prelude::*,
5 system_program::{transfer, Transfer},
6};
7use sablier_utils::account::AccountInfoExt;
8
9#[derive(Accounts)]
11#[instruction(settings: ThreadSettings)]
12pub struct ThreadUpdate<'info> {
13 pub authority: Signer<'info>,
15
16 #[account(mut)]
18 pub payer: Signer<'info>,
19
20 pub system_program: Program<'info, System>,
22
23 #[account(
25 mut,
26 seeds = [
27 SEED_THREAD,
28 thread.authority.as_ref(),
29 thread.id.as_slice(),
30 thread.domain.as_ref().unwrap_or(&Vec::new()).as_slice()
31 ],
32 bump = thread.bump,
33 has_one = authority,
34 )]
35 pub thread: Account<'info, Thread>,
36}
37
38pub fn handler(ctx: Context<ThreadUpdate>, settings: ThreadSettings) -> Result<()> {
39 let payer = &ctx.accounts.payer;
41 let thread = &mut ctx.accounts.thread;
42 let system_program = &ctx.accounts.system_program;
43
44 if let Some(fee) = settings.fee {
46 thread.fee = fee;
47 }
48
49 if let Some(instructions) = settings.instructions {
51 thread.instructions = instructions;
52 }
53
54 if let Some(rate_limit) = settings.rate_limit {
56 thread.rate_limit = rate_limit;
57 }
58
59 if let Some(trigger) = settings.trigger {
61 require!(
63 std::mem::discriminant(&thread.trigger) == std::mem::discriminant(&trigger),
64 SablierError::InvalidTriggerVariant
65 );
66 thread.trigger = trigger.clone();
67
68 if thread.exec_context.is_some() {
71 thread.exec_context = Some(ExecContext {
72 trigger_context: match trigger {
73 Trigger::Account {
74 address: _,
75 offset: _,
76 size: _,
77 } => TriggerContext::Account { data_hash: 0 },
78 _ => thread.exec_context.unwrap().trigger_context,
79 },
80 ..thread.exec_context.unwrap()
81 });
82 }
83 }
84
85 thread.realloc_account()?;
87
88 let data_len = thread.data_len();
90 let minimum_rent = Rent::get()?.minimum_balance(data_len);
91 let thread_lamports = thread.get_lamports();
92 if minimum_rent > thread_lamports {
93 transfer(
94 CpiContext::new(
95 system_program.to_account_info(),
96 Transfer {
97 from: payer.to_account_info(),
98 to: thread.to_account_info(),
99 },
100 ),
101 minimum_rent - thread_lamports,
102 )?;
103 }
104
105 Ok(())
106}