sablier_thread_program/instructions/
thread_withdraw.rs

1use {
2    crate::{constants::*, errors::*, state::*},
3    anchor_lang::prelude::*,
4};
5
6/// Accounts required by the `thread_withdraw` instruction.
7#[derive(Accounts)]
8#[instruction(amount: u64)]
9pub struct ThreadWithdraw<'info> {
10    /// The authority (owner) of the thread.
11    pub authority: Signer<'info>,
12
13    /// The account to withdraw lamports to.
14    #[account(mut)]
15    pub pay_to: SystemAccount<'info>,
16
17    /// The thread to be.
18    #[account(
19        mut,
20        seeds = [
21            SEED_THREAD,
22            thread.authority.as_ref(),
23            thread.id.as_slice(),
24            thread.domain.as_ref().unwrap_or(&Vec::new()).as_slice()
25        ],
26        bump = thread.bump,
27        has_one = authority,
28    )]
29    pub thread: Account<'info, Thread>,
30}
31
32pub fn handler(ctx: Context<ThreadWithdraw>, amount: u64) -> Result<()> {
33    // Get accounts
34    let pay_to = &mut ctx.accounts.pay_to;
35    let thread = &mut ctx.accounts.thread;
36
37    // Calculate the minimum rent threshold
38    let data_len = 8 + thread.try_to_vec()?.len();
39    let minimum_rent = Rent::get()?.minimum_balance(data_len);
40    let post_balance = thread.get_lamports() - amount;
41
42    require!(
43        post_balance > minimum_rent,
44        SablierError::WithdrawalTooLarge
45    );
46
47    // Withdraw balance from thread to the pay_to account
48    thread.sub_lamports(amount)?;
49    pay_to.add_lamports(amount)?;
50
51    Ok(())
52}