hpl_hive_control/state/
project_state.rs

1use {
2    super::{AssociatedProgram, Profile, Service},
3    anchor_lang::prelude::*,
4    hpl_toolkit::prelude::*,
5};
6
7/// Project State Account
8/// This account represents the cenntralized state and confnigurations of an NFT project on the Honeycomb Protocol.
9/// PDA: ['project', key]
10/// Category: project_state
11#[account]
12#[derive(ToSchema)]
13pub struct Project {
14    /// Bump value used for PDA.
15    pub bump: u8,
16
17    /// Public key of the authority controlling this project.
18    pub authority: Pubkey,
19
20    /// Unique public key identifier for this project account.
21    pub key: Pubkey,
22
23    /// Public key of the driver wallet having partial authority of this project.
24    pub driver: Pubkey,
25
26    /// Name of the project.
27    pub name: String,
28
29    /// List of honeycomb services associated with this project.
30    pub services: Vec<Service>,
31
32    /// List of programs that can interact with this project.
33    pub associated_programs: Vec<AssociatedProgram>,
34
35    /// Profile data configuration for this project.
36    pub profile_data_config: ProfileDataConfig,
37
38    /// Controlled Merkle Trees for profile data.
39    pub profile_trees: ControlledMerkleTrees,
40
41    /// List of badge criteria for this project.
42    pub badge_criteria: Option<Vec<BadgeCriteria>>,
43
44    /// Subsidize Fees
45    pub subsidize_fees: bool,
46}
47
48/// Default implementation for the `Project` struct.
49/// It sets default values for each field when creating a new `Project` instance.
50impl Project {
51    pub fn get_size(name: String) -> usize {
52        let mut size = 224; // for two empty vecs in profile_data_config
53        size += name.as_bytes().len();
54        size += ControlledMerkleTrees::get_size_for_borsh(&Profile::schema(), 0);
55        size
56    }
57    pub fn get_or_insert_badge_criteria<'a>(&'a mut self) -> &'a mut Vec<BadgeCriteria> {
58        self.badge_criteria.get_or_insert_with(Vec::new)
59    }
60    /// Sets default values for each field of the `Project` struct.
61    pub fn set_defaults(&mut self) {
62        self.bump = 0;
63        self.key = Pubkey::default();
64        self.driver = Pubkey::default();
65        self.name = "".to_string();
66        self.services = vec![];
67        self.associated_programs = vec![];
68        self.profile_data_config = ProfileDataConfig {
69            achievements: vec![],
70            custom_data_fields: vec![],
71        };
72        self.profile_trees = ControlledMerkleTrees {
73            active: 0,
74            merkle_trees: Vec::new(),
75            schema: SchemaContainer::new(Profile::schema()),
76        };
77        self.badge_criteria = None;
78        self.subsidize_fees = false;
79    }
80}
81
82/// Enum representing the different types of profile data for the `ProfileDataType` field
83/// in the `Project`.
84#[derive(AnchorSerialize, AnchorDeserialize, ToSchema, Clone)]
85pub struct ProfileDataConfig {
86    pub achievements: Vec<ShortString>,
87    pub custom_data_fields: Vec<ShortString>,
88}
89
90#[derive(AnchorSerialize, AnchorDeserialize, ToSchema, Clone)]
91pub struct BadgeCriteria {
92    /// claim start time of badge.
93    pub start_time: Option<i64>,
94
95    /// claim end time of badge.
96    pub end_time: Option<i64>,
97
98    ///  name of the badge.
99    pub index: u16,
100
101    /// badge criteria.
102    pub condition: BadgeCriteriaCondition,
103}
104
105#[derive(AnchorSerialize, AnchorDeserialize, ToSchema, Clone)]
106pub enum BadgeCriteriaCondition {
107    Public,
108    Whitelisted { root: Pubkey },
109}
110
111impl BadgeCriteriaCondition {
112    pub fn get_size(&self) -> isize {
113        match self {
114            BadgeCriteriaCondition::Public => 1,
115            BadgeCriteriaCondition::Whitelisted { .. } => 33,
116        }
117    }
118    pub fn validate(&self, _profile: &Profile) -> bool {
119        match self {
120            BadgeCriteriaCondition::Public => true,
121            _ => {
122                // This is unreachable because the condition is not implemented yet.
123                // This is a placeholder for future implementation.
124                unreachable!();
125            }
126        }
127    }
128}