3

I was looking at the previous posts in this same forum and I think there are some caveats to be made on this topic.

1 Answer 1

2

When using the close method of Anchor

#[account(mut, close = receiver)]
pub data_account: Account<'info, MyData>,
#[account(mut)]
pub receiver: SystemAccount<'info>

Only the account space is removed and the lamports remained in the account.

The way in which I currently solved this problem is by sending the lamports to the initializer account before deleting the account, which remains at 0 (is 890880 lamports). The occupied space is 1213 (9333360 lamports). I share how the final function turned out.

use anchor_lang::{
prelude::*,
solana_program::system_instruction,
solana_program::pubkey::Pubkey,
}; 
use crate::state::accounts::*;
use crate::errors::ErrorCode;

pub fn delete(
        ctx: Context<Delete>
    ) -> Result<()> {
        require!(ctx.accounts.user.key() == ctx.accounts.x.pubkey.key(), ErrorCode::PubkeyError);
        let lamport: u64 = 9333360 - 890880;
        **ctx.accounts.x.to_account_info().try_borrow_mut_lamports()? -= lamport;
        **ctx.accounts.user.to_account_info().try_borrow_mut_lamports()? += lamport;
        Ok(())
    }

#[derive(Accounts)]
pub struct Delete<'info> {
    #[account(mut, seeds = [b"Account"], bump = main_account.bump_original)]
    pub main_account: Account<'info, MainAccount>,
    #[account(
        mut, 
        seeds = [x.seed.to_be_bytes().as_ref()], 
        bump = x.bump_original,
        close = user
    )]
    pub x: Account<'info, Sell>,
    /// CHECK: This is not dangerous
    #[account(mut, signer)]
    pub user: AccountInfo<'info>,
    pub system_program: Program<'info, System>,
}

If anyone has another way to perform this procedure, I would appreciate your comment. Greetings.

1
  • Is this a bug or is this the way it has to be done? I believed the first way is supposed return all lamports.
    – ZeroNine
    Feb 3, 2023 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.