sablier_thread_program/instructions/
thread_update.rs

1use crate::{constants::*, errors::SablierError, state::*};
2
3use anchor_lang::{
4    prelude::*,
5    system_program::{transfer, Transfer},
6};
7use sablier_utils::account::AccountInfoExt;
8
9/// Accounts required by the `thread_update` instruction.
10#[derive(Accounts)]
11#[instruction(settings: ThreadSettings)]
12pub struct ThreadUpdate<'info> {
13    /// The authority (owner) of the thread.
14    pub authority: Signer<'info>,
15
16    /// The payer of the reallocation.
17    #[account(mut)]
18    pub payer: Signer<'info>,
19
20    /// The Solana system program
21    pub system_program: Program<'info, System>,
22
23    /// The thread to be updated.
24    #[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    // Get accounts
40    let payer = &ctx.accounts.payer;
41    let thread = &mut ctx.accounts.thread;
42    let system_program = &ctx.accounts.system_program;
43
44    // Update the thread.
45    if let Some(fee) = settings.fee {
46        thread.fee = fee;
47    }
48
49    // If provided, update the thread's instruction set.
50    if let Some(instructions) = settings.instructions {
51        thread.instructions = instructions;
52    }
53
54    // If provided, update the rate limit.
55    if let Some(rate_limit) = settings.rate_limit {
56        thread.rate_limit = rate_limit;
57    }
58
59    // If provided, update the thread's trigger and reset the exec context.
60    if let Some(trigger) = settings.trigger {
61        // Require the thread is not in the middle of processing.
62        require!(
63            std::mem::discriminant(&thread.trigger) == std::mem::discriminant(&trigger),
64            SablierError::InvalidTriggerVariant
65        );
66        thread.trigger = trigger.clone();
67
68        // If the user updates an account trigger, the trigger context is no longer valid.
69        // Here we reset the trigger context to zero to re-prime the trigger.
70        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    // Reallocate mem for the thread account
86    thread.realloc_account()?;
87
88    // If lamports are required to maintain rent-exemption, pay them
89    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}