Skip to main content

spherenet_program_whitelist_interface/state/
account.rs

1use {
2    super::{AccountState, Initializable},
3    crate::state::SizeOf,
4    bytemuck::{Pod, Zeroable},
5    pinocchio::pubkey::Pubkey,
6    shank::ShankAccount,
7};
8
9/// Offset of the authority field in the account data
10pub const AUTHORITY_OFFSET: usize = 0;
11/// Offset of the pending_authority field in the account data
12pub const PENDING_AUTHORITY_OFFSET: usize = 32;
13/// Offset of the state field in the account data
14pub const STATE_OFFSET: usize = 64;
15
16#[repr(C)]
17#[derive(ShankAccount, Copy, Clone, Pod, Zeroable)]
18pub struct ProgramWhitelistAccount {
19    pub authority: Pubkey,
20    pub pending_authority: Pubkey, // Use Pubkey::default() as sentinel
21    pub state: u8,
22}
23
24impl ProgramWhitelistAccount {
25    #[inline(always)]
26    pub fn pack(&self) -> [u8; Self::SIZE_OF] {
27        let bytes = bytemuck::bytes_of(self);
28        let mut array = [0u8; Self::SIZE_OF];
29        array.copy_from_slice(bytes);
30        array
31    }
32
33    #[inline(always)]
34    pub fn set_pending_authority(&mut self, new_pending_authority: &Pubkey) {
35        self.pending_authority = *new_pending_authority;
36    }
37
38    #[inline(always)]
39    pub fn clear_pending_authority(&mut self) {
40        self.pending_authority = Pubkey::default();
41    }
42
43    #[inline(always)]
44    pub fn pending_authority(&self) -> Option<&Pubkey> {
45        if self.pending_authority == Pubkey::default() {
46            None
47        } else {
48            Some(&self.pending_authority)
49        }
50    }
51}
52
53impl Initializable for ProgramWhitelistAccount {
54    #[inline(always)]
55    fn is_initialized(&self) -> bool {
56        self.state != AccountState::Uninitialized as u8
57    }
58}