antegen-thread-program 4.0.0

Solana program for Antegen - automation and scheduling threads
Documentation
use {
    crate::{errors::*, *},
    anchor_lang::prelude::*,
};

/// Accounts required by the `thread_withdraw` instruction.
#[derive(Accounts)]
#[instruction(amount: u64)]
pub struct ThreadWithdraw<'info> {
    /// The authority (owner) of the thread.
    #[account()]
    pub authority: Signer<'info>,

    /// CHECK: The account to withdraw lamports to.
    #[account(mut)]
    pub pay_to: UncheckedAccount<'info>,

    /// The thread to be.
    #[account(
        mut,
        has_one = authority,
        seeds = [
            SEED_THREAD,
            thread.authority.as_ref(),
            thread.id.as_slice(),
        ],
        bump = thread.bump,
    )]
    pub thread: Account<'info, Thread>,
}

pub fn thread_withdraw(ctx: Context<ThreadWithdraw>, amount: u64) -> Result<()> {
    // Get accounts
    let pay_to = &mut ctx.accounts.pay_to;
    let thread = &mut ctx.accounts.thread;

    // Calculate the minimum rent threshold
    let data_len = 8 + borsh::to_vec(&**thread)?.len();
    let minimum_rent = Rent::get().unwrap().minimum_balance(data_len);
    let post_balance = thread
        .to_account_info()
        .lamports()
        .checked_sub(amount)
        .unwrap();
    require!(
        post_balance.gt(&minimum_rent),
        AntegenThreadError::WithdrawalTooLarge
    );

    // Withdraw balance from thread to the pay_to account
    **thread.to_account_info().try_borrow_mut_lamports()? -= amount;
    **pay_to.try_borrow_mut_lamports()? += amount;
    Ok(())
}