pinocchio_token/instructions/
close_account.rs

1use solana_account_view::AccountView;
2use solana_instruction_view::{
3    cpi::{invoke_signed, Signer},
4    InstructionAccount, InstructionView,
5};
6use solana_program_error::ProgramResult;
7
8/// Close an account by transferring all its SOL to the destination account.
9///
10/// ### Accounts:
11///   0. `[WRITE]` The account to close.
12///   1. `[WRITE]` The destination account.
13///   2. `[SIGNER]` The account's owner.
14pub struct CloseAccount<'a> {
15    /// Token Account.
16    pub account: &'a AccountView,
17    /// Destination Account
18    pub destination: &'a AccountView,
19    /// Owner Account
20    pub authority: &'a AccountView,
21}
22
23impl CloseAccount<'_> {
24    #[inline(always)]
25    pub fn invoke(&self) -> ProgramResult {
26        self.invoke_signed(&[])
27    }
28
29    #[inline(always)]
30    pub fn invoke_signed(&self, signers: &[Signer]) -> ProgramResult {
31        // Instruction accounts
32        let instruction_accounts: [InstructionAccount; 3] = [
33            InstructionAccount::writable(self.account.address()),
34            InstructionAccount::writable(self.destination.address()),
35            InstructionAccount::readonly_signer(self.authority.address()),
36        ];
37
38        let instruction = InstructionView {
39            program_id: &crate::ID,
40            accounts: &instruction_accounts,
41            data: &[9],
42        };
43
44        invoke_signed(
45            &instruction,
46            &[self.account, self.destination, self.authority],
47            signers,
48        )
49    }
50}