Skip to main content

antegen_thread_program/instructions/
thread_delete.rs

1use crate::{errors::AntegenThreadError, state::ThreadConfig, *};
2use anchor_lang::prelude::*;
3
4/// Force delete a thread - admin only, skips all checks.
5/// Used for cleaning up stuck/broken threads that may not deserialize.
6#[derive(Accounts)]
7pub struct ThreadDelete<'info> {
8    /// The config admin (must sign)
9    #[account(
10        mut,
11        constraint = admin.key() == config.admin @ AntegenThreadError::InvalidConfigAdmin,
12    )]
13    pub admin: Signer<'info>,
14
15    /// The config account
16    #[account(
17        seeds = [SEED_CONFIG],
18        bump = config.bump,
19    )]
20    pub config: Account<'info, ThreadConfig>,
21
22    /// CHECK: The thread to delete - unchecked so we can close broken/undeserializable accounts
23    #[account(mut)]
24    pub thread: UncheckedAccount<'info>,
25}
26
27pub fn thread_delete(ctx: Context<ThreadDelete>) -> Result<()> {
28    let admin = &ctx.accounts.admin;
29    let thread = &ctx.accounts.thread;
30
31    // Transfer all lamports from thread to admin
32    let thread_lamports = thread.lamports();
33    **thread.try_borrow_mut_lamports()? -= thread_lamports;
34    **admin.try_borrow_mut_lamports()? += thread_lamports;
35
36    // Zero out account data to mark as closed
37    thread.try_borrow_mut_data()?.fill(0);
38
39    msg!("Deleting thread (admin)");
40    Ok(())
41}