spherenet-program-whitelist-interface 0.2.0

Interface of SphereNet's Program Whitelist Program
Documentation
use {
    super::{AccountState, Initializable},
    crate::state::SizeOf,
    bytemuck::{Pod, Zeroable},
    pinocchio::pubkey::Pubkey,
    shank::ShankAccount,
};

/// Offset of the authority field in the account data
pub const AUTHORITY_OFFSET: usize = 0;
/// Offset of the pending_authority field in the account data
pub const PENDING_AUTHORITY_OFFSET: usize = 32;
/// Offset of the state field in the account data
pub const STATE_OFFSET: usize = 64;

#[repr(C)]
#[derive(ShankAccount, Copy, Clone, Pod, Zeroable)]
pub struct ProgramWhitelistAccount {
    pub authority: Pubkey,
    pub pending_authority: Pubkey, // Use Pubkey::default() as sentinel
    pub state: u8,
}

impl ProgramWhitelistAccount {
    #[inline(always)]
    pub fn pack(&self) -> [u8; Self::SIZE_OF] {
        let bytes = bytemuck::bytes_of(self);
        let mut array = [0u8; Self::SIZE_OF];
        array.copy_from_slice(bytes);
        array
    }

    #[inline(always)]
    pub fn set_pending_authority(&mut self, new_pending_authority: &Pubkey) {
        self.pending_authority = *new_pending_authority;
    }

    #[inline(always)]
    pub fn clear_pending_authority(&mut self) {
        self.pending_authority = Pubkey::default();
    }

    #[inline(always)]
    pub fn pending_authority(&self) -> Option<&Pubkey> {
        if self.pending_authority == Pubkey::default() {
            None
        } else {
            Some(&self.pending_authority)
        }
    }

    #[inline(always)]
    pub fn set_initialized(&mut self) {
        self.state = AccountState::Initialized as u8;
    }
}

impl Initializable for ProgramWhitelistAccount {
    #[inline(always)]
    fn is_initialized(&self) -> bool {
        self.state != AccountState::Uninitialized as u8
    }
}