Skip to main content

token_acl/
state.rs

1use bytemuck::{Pod, Zeroable};
2use solana_program_error::ProgramError;
3use spl_pod::primitives::PodBool;
4
5use crate::error::TokenAclError;
6use solana_program::pubkey::Pubkey;
7
8pub const FLAG_ACCOUNT_SEED_PREFIX: &'static [u8] = b"FLAG_ACCOUNT";
9
10#[repr(C)]
11#[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)]
12pub struct MintConfig {
13    pub discriminator: u8,
14    pub bump: u8,
15    pub enable_permissionless_thaw: PodBool,
16    pub enable_permissionless_freeze: PodBool,
17    pub mint: Pubkey,
18    pub freeze_authority: Pubkey,
19    pub gating_program: Pubkey,
20}
21
22impl MintConfig {
23    pub const SEED_PREFIX: &'static [u8] = b"MINT_CONFIG";
24    pub const DISCRIMINATOR: u8 = 1;
25    pub const LEN: usize = 1 + 32 + 32 + 32 + 1 + 1 + 1;
26
27    pub fn is_permissionless_thaw_enabled(&self) -> bool {
28        Into::<bool>::into(self.enable_permissionless_thaw)
29    }
30
31    pub fn is_permissionless_freeze_enabled(&self) -> bool {
32        Into::<bool>::into(self.enable_permissionless_freeze)
33    }
34}
35
36#[inline(always)]
37pub fn load_mint_config(data: &[u8]) -> Result<&MintConfig, ProgramError> {
38    bytemuck::try_from_bytes::<MintConfig>(data)
39        .map_err(|_| TokenAclError::InvalidMintConfig.into())
40        .and_then(|cfg: &MintConfig| {
41            if cfg.discriminator == MintConfig::DISCRIMINATOR {
42                Ok(cfg)
43            } else {
44                Err(TokenAclError::InvalidMintConfig.into())
45            }
46        })
47}
48
49#[inline(always)]
50pub fn load_mint_config_mut(data: &mut [u8]) -> Result<&mut MintConfig, ProgramError> {
51    bytemuck::try_from_bytes_mut::<MintConfig>(data)
52        .map_err(|_| TokenAclError::InvalidMintConfig.into())
53        .and_then(|cfg: &mut MintConfig| {
54            if cfg.discriminator == MintConfig::DISCRIMINATOR {
55                Ok(cfg)
56            } else {
57                Err(TokenAclError::InvalidMintConfig.into())
58            }
59        })
60}