antegen_fiber_program/instructions/
close.rs1use crate::errors::AntegenFiberError;
2use crate::state::Fiber;
3use anchor_lang::prelude::*;
4
5#[derive(Accounts)]
9pub struct Close<'info> {
10 #[account(mut)]
12 pub thread: Signer<'info>,
13
14 #[account(mut)]
16 pub fiber: UncheckedAccount<'info>,
17}
18
19pub fn close(ctx: Context<Close>) -> Result<()> {
20 let fiber_info = ctx.accounts.fiber.to_account_info();
21 let thread_info = ctx.accounts.thread.to_account_info();
22
23 let read = {
24 let data = fiber_info.try_borrow_data()?;
25 Fiber::try_deserialize(&mut &data[..])?
26 };
27 require!(
28 read.thread() == thread_info.key(),
29 AntegenFiberError::InvalidFiberPDA
30 );
31
32 sweep_fiber_lamports(&fiber_info, &thread_info)?;
33 Ok(())
34}
35
36pub(crate) fn sweep_fiber_lamports<'info>(
40 fiber: &AccountInfo<'info>,
41 thread: &AccountInfo<'info>,
42) -> Result<()> {
43 let fiber_lamports = fiber.lamports();
44 **thread.try_borrow_mut_lamports()? = thread
45 .lamports()
46 .checked_add(fiber_lamports)
47 .ok_or(ProgramError::ArithmeticOverflow)?;
48 **fiber.try_borrow_mut_lamports()? = 0;
49
50 let mut data = fiber.try_borrow_mut_data()?;
51 for byte in data.iter_mut() {
52 *byte = 0;
53 }
54 if data.len() >= 8 {
57 data[..8].copy_from_slice(&[0xff; 8]);
58 }
59 Ok(())
60}