hpl_hive_control/state/
project_state.rs1use {
2 super::{AssociatedProgram, Profile, Service},
3 anchor_lang::prelude::*,
4 hpl_toolkit::prelude::*,
5};
6
7#[account]
12#[derive(ToSchema)]
13pub struct Project {
14 pub bump: u8,
16
17 pub authority: Pubkey,
19
20 pub key: Pubkey,
22
23 pub driver: Pubkey,
25
26 pub name: String,
28
29 pub services: Vec<Service>,
31
32 pub associated_programs: Vec<AssociatedProgram>,
34
35 pub profile_data_config: ProfileDataConfig,
37
38 pub profile_trees: ControlledMerkleTrees,
40
41 pub badge_criteria: Option<Vec<BadgeCriteria>>,
43
44 pub subsidize_fees: bool,
46}
47
48impl Project {
51 pub fn get_size(name: String) -> usize {
52 let mut size = 224; 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 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#[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 pub start_time: Option<i64>,
94
95 pub end_time: Option<i64>,
97
98 pub index: u16,
100
101 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 unreachable!();
125 }
126 }
127 }
128}