1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
use std::collections::HashSet;
use anchor_lang::{prelude::*, AnchorDeserialize};
use solana_program::program_memory::sol_memcmp;
use crate::{errors::CandyGuardError, guards::*, utils::fixed_length_string};
use mpl_candy_guard_derive::GuardSet;
pub const DATA_OFFSET: usize = 8 + 32 + 1 + 32;
pub const MAX_LABEL_SIZE: usize = 6;
pub const SEED: &[u8] = b"candy_guard";
#[account]
#[derive(Default)]
pub struct CandyGuard {
pub base: Pubkey,
pub bump: u8,
pub authority: Pubkey,
}
#[derive(AnchorSerialize, AnchorDeserialize, Clone, Debug)]
pub struct CandyGuardData {
pub default: GuardSet,
pub groups: Option<Vec<Group>>,
}
#[derive(AnchorSerialize, AnchorDeserialize, Clone, Debug)]
pub struct Group {
pub label: String,
pub guards: GuardSet,
}
#[derive(GuardSet, AnchorSerialize, AnchorDeserialize, Clone, Debug)]
pub struct GuardSet {
pub bot_tax: Option<BotTax>,
pub sol_payment: Option<SolPayment>,
pub token_payment: Option<TokenPayment>,
pub start_date: Option<StartDate>,
pub third_party_signer: Option<ThirdPartySigner>,
pub token_gate: Option<TokenGate>,
pub gatekeeper: Option<Gatekeeper>,
pub end_date: Option<EndDate>,
pub allow_list: Option<AllowList>,
pub mint_limit: Option<MintLimit>,
pub nft_payment: Option<NftPayment>,
pub redeemed_amount: Option<RedeemedAmount>,
pub address_gate: Option<AddressGate>,
pub nft_gate: Option<NftGate>,
pub nft_burn: Option<NftBurn>,
pub token_burn: Option<TokenBurn>,
pub freeze_sol_payment: Option<FreezeSolPayment>,
pub freeze_token_payment: Option<FreezeTokenPayment>,
pub program_gate: Option<ProgramGate>,
pub allocation: Option<Allocation>,
}
#[derive(AnchorSerialize, AnchorDeserialize, Clone, Debug)]
pub enum GuardType {
BotTax,
SolPayment,
TokenPayment,
StartDate,
ThirdPartySigner,
TokenGate,
Gatekeeper,
EndDate,
AllowList,
MintLimit,
NftPayment,
RedeemedAmount,
AddressGate,
NftGate,
NftBurn,
TokenBurn,
FreezeSolPayment,
FreezeTokenPayment,
ProgramGate,
Allocation,
}
impl GuardType {
pub fn as_mask(guard_type: GuardType) -> u64 {
0b1u64 << (guard_type as u8)
}
}
impl CandyGuardData {
pub fn save(&self, data: &mut [u8]) -> Result<()> {
let mut cursor = 0;
let _ = self.default.to_data(data)?;
cursor += self.default.size();
let group_counter = if let Some(groups) = &self.groups {
groups.len() as u32
} else {
0
};
data[cursor..cursor + 4].copy_from_slice(&u32::to_le_bytes(group_counter));
cursor += 4;
if let Some(groups) = &self.groups {
for group in groups {
let label = fixed_length_string(group.label.to_string(), MAX_LABEL_SIZE)?;
data[cursor..cursor + MAX_LABEL_SIZE].copy_from_slice(label.as_bytes());
cursor += MAX_LABEL_SIZE;
let _ = group.guards.to_data(&mut data[cursor..])?;
cursor += group.guards.size();
}
}
Ok(())
}
pub fn load(data: &[u8]) -> Result<Box<Self>> {
let (default, _) = GuardSet::from_data(data)?;
let mut cursor = default.size();
let group_counter = u32::from_le_bytes(*arrayref::array_ref![data, cursor, 4]);
cursor += 4;
let groups = if group_counter > 0 {
let mut groups = Vec::with_capacity(group_counter as usize);
for _i in 0..group_counter {
let slice: &[u8] = &data[cursor..cursor + MAX_LABEL_SIZE];
let label = String::from_utf8(slice.to_vec())
.map_err(|_| CandyGuardError::DeserializationError)?;
cursor += MAX_LABEL_SIZE;
let (guards, _) = GuardSet::from_data(&data[cursor..])?;
cursor += guards.size();
groups.push(Group { label, guards });
}
Some(groups)
} else {
None
};
if data.len() != cursor {
msg!("Read {} bytes, received {} bytes", cursor, data.len());
return err!(CandyGuardError::DeserializationError);
}
Ok(Box::new(Self { default, groups }))
}
pub fn active_set(data: &[u8], label: Option<String>) -> Result<Box<GuardSet>> {
let (mut default, _) = GuardSet::from_data(data)?;
let mut cursor = default.size();
let group_counter = u32::from_le_bytes(*arrayref::array_ref![data, cursor, 4]);
cursor += 4;
if group_counter > 0 {
if let Some(label) = label {
let group_label = fixed_length_string(label, MAX_LABEL_SIZE)?;
let label_slice = group_label.as_bytes();
for _i in 0..group_counter {
if sol_memcmp(label_slice, &data[cursor..], label_slice.len()) == 0 {
cursor += MAX_LABEL_SIZE;
let (guards, _) = GuardSet::from_data(&data[cursor..])?;
default.merge(guards);
return Ok(Box::new(default));
} else {
cursor += MAX_LABEL_SIZE;
let features = u64::from_le_bytes(*arrayref::array_ref![data, cursor, 8]);
cursor += GuardSet::bytes_count(features);
}
}
return err!(CandyGuardError::GroupNotFound);
}
return err!(CandyGuardError::RequiredGroupLabelNotFound);
} else if label.is_some() {
return err!(CandyGuardError::GroupNotFound);
}
Ok(Box::new(default))
}
pub fn account_size(&self) -> usize {
DATA_OFFSET + self.size()
}
pub fn size(&self) -> usize {
let mut size = self.default.size();
size += 4; if let Some(groups) = &self.groups {
size += groups
.iter()
.map(|group| MAX_LABEL_SIZE + group.guards.size())
.sum::<usize>();
}
size
}
pub fn verify(&self) -> Result<()> {
let mut labels = HashSet::new();
if let Some(groups) = &self.groups {
for group in groups {
if labels.contains(&group.label) {
return err!(CandyGuardError::DuplicatedGroupLabel);
}
labels.insert(group.label.clone());
}
}
GuardSet::verify(self)
}
}