miclockwork_thread_program/instructions/
thread_delete.rs

1use {crate::state::*, anchor_lang::prelude::*};
2
3/// Accounts required by the `thread_delete` instruction.
4#[derive(Accounts)]
5pub struct ThreadDelete<'info> {
6    /// The authority (owner) of the thread.
7    #[account(
8        constraint = authority.key().eq(&thread.authority) || authority.key().eq(&thread.key())
9    )]
10    pub authority: Signer<'info>,
11
12    /// The address to return the data rent lamports to.
13    #[account(mut)]
14    pub close_to: SystemAccount<'info>,
15
16    /// The thread to be delete.
17    #[account(
18        mut,
19        seeds = [
20            SEED_THREAD,
21            thread.authority.as_ref(),
22            thread.id.as_slice(),
23        ],
24        bump = thread.bump,
25    )]
26    pub thread: Account<'info, Thread>,
27}
28
29pub fn handler(ctx: Context<ThreadDelete>) -> Result<()> {
30    let thread = &ctx.accounts.thread;
31    let close_to = &ctx.accounts.close_to;
32
33    let thread_lamports = thread.to_account_info().lamports();
34    **thread.to_account_info().try_borrow_mut_lamports()? = thread
35        .to_account_info()
36        .lamports()
37        .checked_sub(thread_lamports)
38        .unwrap();
39    **close_to.to_account_info().try_borrow_mut_lamports()? = close_to
40        .to_account_info()
41        .lamports()
42        .checked_add(thread_lamports)
43        .unwrap();
44
45    Ok(())
46}