spherenet-program-whitelist-interface 0.1.2

Interface of SphereNet's Program Whitelist Program
Documentation
//! Onchain functionality for the `RemoveEntry` instruction

use {
    crate::{
        onchain::{write_bytes, UNINIT_BYTE},
        program::ID,
    },
    core::{mem::MaybeUninit, slice::from_raw_parts},
    pinocchio::{
        account_info::AccountInfo,
        instruction::{AccountMeta, Instruction, Signer},
        program::invoke_signed,
        pubkey::Pubkey,
        ProgramResult,
    },
};

/// Closes a specific whitelist entry account, transferring its rent
/// lamports to a designated destination account.
///
/// ### Accounts:
///   0. `[]` The whitelist account (Used for validation)
///   1. `[SIGNER]` The whitelist authority
///   2. `[WRITE]` The whitelist entry account to close
///   3. `[WRITE]` The destination account for reclaimed lamports
///   4. `[]` System program
pub struct RemoveEntry<'a> {
    /// The whitelist account (Used for validation)
    pub whitelist_account: &'a AccountInfo,
    /// The whitelist authority
    pub whitelist_authority: &'a AccountInfo,
    /// The whitelist entry account to close
    pub whitelist_entry_account: &'a AccountInfo,
    /// The destination account for reclaimed lamports
    pub destination_account: &'a AccountInfo,
    /// System program
    pub system_program: &'a AccountInfo,
    /// program authority public key
    pub program_authority: Pubkey,
}

impl<'a> RemoveEntry<'a> {
    #[inline(always)]
    pub fn invoke(&self) -> ProgramResult {
        self.invoke_signed(&[])
    }

    /// Creates the instruction data for the RemoveEntry instruction
    fn create_instruction_data(program_authority: &Pubkey) -> [MaybeUninit<u8>; 33] {
        let mut instruction_data = [UNINIT_BYTE; 33];

        // Set discriminator as u8 at offset [0]
        write_bytes(&mut instruction_data, &[5]);
        // Set program_authority as Pubkey at offset [1..33]
        write_bytes(&mut instruction_data[1..33], program_authority.as_ref());

        instruction_data
    }

    pub fn invoke_signed(&self, signers: &[Signer]) -> ProgramResult {
        // account metadata
        let account_metas: [AccountMeta; 5] = [
            AccountMeta::readonly(self.whitelist_account.key()),
            AccountMeta::readonly_signer(self.whitelist_authority.key()),
            AccountMeta::writable(self.whitelist_entry_account.key()),
            AccountMeta::writable(self.destination_account.key()),
            AccountMeta::readonly(self.system_program.key()),
        ];

        // Create instruction data
        let instruction_data = Self::create_instruction_data(&self.program_authority);

        let instruction = Instruction {
            program_id: &ID,
            accounts: &account_metas,
            data: unsafe { from_raw_parts(instruction_data.as_ptr() as _, 33) },
        };

        invoke_signed(
            &instruction,
            &[
                self.whitelist_account,
                self.whitelist_authority,
                self.whitelist_entry_account,
                self.destination_account,
                self.system_program,
            ],
            signers,
        )
    }
}

/// Convenience helper for the `RemoveEntry` instruction
pub fn remove_entry(
    whitelist_account: &AccountInfo,
    whitelist_authority: &AccountInfo,
    whitelist_entry_account: &AccountInfo,
    destination_account: &AccountInfo,
    system_program: &AccountInfo,
    program_authority: Pubkey,
) -> ProgramResult {
    let ix = RemoveEntry {
        whitelist_account,
        whitelist_authority,
        whitelist_entry_account,
        destination_account,
        system_program,
        program_authority,
    };
    ix.invoke()
}

/// Convenience helper for the `RemoveEntry` instruction with the given
/// signers.
pub fn remove_entry_with_seed(
    whitelist_account: &AccountInfo,
    whitelist_authority: &AccountInfo,
    whitelist_entry_account: &AccountInfo,
    destination_account: &AccountInfo,
    system_program: &AccountInfo,
    program_authority: Pubkey,
    signer_seeds: &[Signer],
) -> ProgramResult {
    let ix = RemoveEntry {
        whitelist_account,
        whitelist_authority,
        whitelist_entry_account,
        destination_account,
        system_program,
        program_authority,
    };
    ix.invoke_signed(signer_seeds)
}

#[cfg(test)]
mod tests {
    use {super::*, crate::instructions::ProgramWhitelistInstruction};

    #[test]
    fn test_instruction_data_creation() {
        // Create a dummy pubkey for program authority
        let program_authority_bytes = [42u8; 32]; // Use a fixed value for testing
        let program_authority = Pubkey::from(program_authority_bytes);

        // Create instruction data using our function
        let instruction_data = RemoveEntry::create_instruction_data(&program_authority);

        // Create an instruction with this data for parsing
        let instruction = Instruction {
            program_id: &ID,
            accounts: &[], // Accounts not relevant for this test
            data: unsafe { from_raw_parts(instruction_data.as_ptr() as _, 33) },
        };

        // Parse the instruction data
        let parsed_ix = ProgramWhitelistInstruction::unpack(instruction.data).unwrap();

        // Verify that the parsed instruction matches what we expect
        match parsed_ix {
            ProgramWhitelistInstruction::RemoveEntry {
                program_authority: parsed_program_authority,
            } => {
                assert_eq!(parsed_program_authority, program_authority);
            }
            _ => panic!("Parsed incorrect instruction type"),
        }
    }
}