1

In my Solana program, users can create program-owned accounts.

Although maybe they want to close it because it is no longer necessary and want to recoup the account rent/fees.

  1. I assume that closing program accounts can only occur from within the program itself, which requires making a dedicated program instruction - is this common?

  2. If I close the entire Solana program, will all the program-owned accounts also be closed at the same time? If so who will receive all the account fees - the upgrade auth?

1 Answer 1

3

To close a program account, you'll need dedicated instruction in your program as you mentioned.

If you're using Anchor here's a basic reference that you can add additional constraints to: https://github.com/coral-xyz/sealevel-attacks/blob/master/programs/9-closing-accounts/recommended/src/lib.rs

use anchor_lang::prelude::*;

declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

#[program]
pub mod closing_accounts_recommended {
    use super::*;

    pub fn close(ctx: Context<Close>) -> ProgramResult {
        Ok(())
    }
}

#[derive(Accounts)]
pub struct Close<'info> {
    #[account(mut, close = destination)]
    account: Account<'info, Data>,
    #[account(mut)]
    destination: AccountInfo<'info>,
}

#[account]
pub struct Data {
    data: u64,
}

If you close the program, I don't think any of the existing program accounts get closed.

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.