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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
use anchor_lang::prelude::Pubkey;
use anyhow::{anyhow, Result};
use dateparser::DateTimeUtc;
use serde::{Deserialize, Serialize};
use super::{data::price_as_lamports, to_pubkey, to_string};
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct CandyGuardData {
pub default: GuardSet,
pub groups: Option<Vec<Group>>,
}
impl CandyGuardData {
pub fn to_guard_format(&self) -> Result<mpl_candy_guard::state::CandyGuardData> {
let groups = if let Some(groups) = &self.groups {
let mut group_vec = Vec::with_capacity(groups.len());
for group in groups {
group_vec.push(group.to_guard_format()?);
}
Some(group_vec)
} else {
None
};
Ok(mpl_candy_guard::state::CandyGuardData {
default: self.default.to_guard_format()?,
groups,
})
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Group {
pub label: String,
pub guards: GuardSet,
}
impl Group {
pub fn to_guard_format(&self) -> Result<mpl_candy_guard::state::Group> {
Ok(mpl_candy_guard::state::Group {
label: self.label.clone(),
guards: self.guards.to_guard_format()?,
})
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[serde(rename_all = "camelCase")]
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>,
}
impl GuardSet {
pub fn to_guard_format(&self) -> Result<mpl_candy_guard::guards::GuardSet> {
let bot_tax = if let Some(bot_tax) = &self.bot_tax {
Some(bot_tax.to_guard_format()?)
} else {
None
};
let sol_payment = if let Some(sol_payment) = &self.sol_payment {
Some(sol_payment.to_guard_format()?)
} else {
None
};
let token_payment = if let Some(token_payment) = &self.token_payment {
Some(token_payment.to_guard_format()?)
} else {
None
};
let start_date = if let Some(start_date) = &self.start_date {
Some(start_date.to_guard_format()?)
} else {
None
};
let third_party_signer = if let Some(third_party_signer) = &self.third_party_signer {
Some(third_party_signer.to_guard_format()?)
} else {
None
};
let token_gate = if let Some(token_gate) = &self.token_gate {
Some(token_gate.to_guard_format()?)
} else {
None
};
let gatekeeper = if let Some(gatekeeper) = &self.gatekeeper {
Some(gatekeeper.to_guard_format()?)
} else {
None
};
let end_date = if let Some(end_date) = &self.end_date {
Some(end_date.to_guard_format()?)
} else {
None
};
let allow_list = if let Some(allow_list) = &self.allow_list {
Some(allow_list.to_guard_format()?)
} else {
None
};
let mint_limit = if let Some(mint_limit) = &self.mint_limit {
Some(mint_limit.to_guard_format()?)
} else {
None
};
let nft_payment = if let Some(nft_payment) = &self.nft_payment {
Some(nft_payment.to_guard_format()?)
} else {
None
};
let redeemed_amount = if let Some(redeemed_amount) = &self.redeemed_amount {
Some(redeemed_amount.to_guard_format()?)
} else {
None
};
let address_gate = if let Some(address_gate) = &self.address_gate {
Some(address_gate.to_guard_format()?)
} else {
None
};
let nft_gate = if let Some(nft_gate) = &self.nft_gate {
Some(nft_gate.to_guard_format()?)
} else {
None
};
let nft_burn = if let Some(nft_burn) = &self.nft_burn {
Some(nft_burn.to_guard_format()?)
} else {
None
};
let token_burn = if let Some(token_burn) = &self.token_burn {
Some(token_burn.to_guard_format()?)
} else {
None
};
let freeze_sol_payment = if let Some(freeze_sol_payment) = &self.freeze_sol_payment {
Some(freeze_sol_payment.to_guard_format()?)
} else {
None
};
let freeze_token_payment = if let Some(freeze_token_payment) = &self.freeze_token_payment {
Some(freeze_token_payment.to_guard_format()?)
} else {
None
};
Ok(mpl_candy_guard::guards::GuardSet {
bot_tax,
sol_payment,
token_payment,
start_date,
third_party_signer,
token_gate,
gatekeeper,
end_date,
allow_list,
mint_limit,
nft_payment,
redeemed_amount,
address_gate,
nft_gate,
nft_burn,
token_burn,
freeze_sol_payment,
freeze_token_payment,
program_gate: None,
allocation: None,
})
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct AddressGate {
#[serde(deserialize_with = "to_pubkey")]
#[serde(serialize_with = "to_string")]
pub address: Pubkey,
}
impl AddressGate {
pub fn to_guard_format(&self) -> Result<mpl_candy_guard::guards::AddressGate> {
Ok(mpl_candy_guard::guards::AddressGate {
address: self.address,
})
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[serde(rename_all = "camelCase")]
pub struct AllowList {
pub merkle_root: String,
}
impl AllowList {
pub fn to_guard_format(&self) -> Result<mpl_candy_guard::guards::AllowList> {
let root: [u8; 32] = hex::decode(&self.merkle_root)?
.try_into()
.map_err(|_| anyhow!("Invalid merkle root value: {}", self.merkle_root))?;
Ok(mpl_candy_guard::guards::AllowList { merkle_root: root })
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[serde(rename_all = "camelCase")]
pub struct BotTax {
pub value: f64,
pub last_instruction: bool,
}
impl BotTax {
pub fn to_guard_format(&self) -> Result<mpl_candy_guard::guards::BotTax> {
Ok(mpl_candy_guard::guards::BotTax {
lamports: price_as_lamports(self.value),
last_instruction: self.last_instruction,
})
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct EndDate {
pub date: String,
}
impl EndDate {
pub fn to_guard_format(&self) -> Result<mpl_candy_guard::guards::EndDate> {
let timestamp = self.date.parse::<DateTimeUtc>()?.0.timestamp();
Ok(mpl_candy_guard::guards::EndDate { date: timestamp })
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[serde(rename_all = "camelCase")]
pub struct Gatekeeper {
#[serde(deserialize_with = "to_pubkey")]
#[serde(serialize_with = "to_string")]
pub gatekeeper_network: Pubkey,
pub expire_on_use: bool,
}
impl Gatekeeper {
pub fn to_guard_format(&self) -> Result<mpl_candy_guard::guards::Gatekeeper> {
Ok(mpl_candy_guard::guards::Gatekeeper {
gatekeeper_network: self.gatekeeper_network,
expire_on_use: self.expire_on_use,
})
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct MintLimit {
pub id: u8,
pub limit: u16,
}
impl MintLimit {
pub fn to_guard_format(&self) -> Result<mpl_candy_guard::guards::MintLimit> {
Ok(mpl_candy_guard::guards::MintLimit {
id: self.id,
limit: self.limit,
})
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[serde(rename_all = "camelCase")]
pub struct NftBurn {
#[serde(deserialize_with = "to_pubkey")]
#[serde(serialize_with = "to_string")]
pub required_collection: Pubkey,
}
impl NftBurn {
pub fn to_guard_format(&self) -> Result<mpl_candy_guard::guards::NftBurn> {
Ok(mpl_candy_guard::guards::NftBurn {
required_collection: self.required_collection,
})
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[serde(rename_all = "camelCase")]
pub struct NftGate {
#[serde(deserialize_with = "to_pubkey")]
#[serde(serialize_with = "to_string")]
pub required_collection: Pubkey,
}
impl NftGate {
pub fn to_guard_format(&self) -> Result<mpl_candy_guard::guards::NftGate> {
Ok(mpl_candy_guard::guards::NftGate {
required_collection: self.required_collection,
})
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[serde(rename_all = "camelCase")]
pub struct NftPayment {
#[serde(deserialize_with = "to_pubkey")]
#[serde(serialize_with = "to_string")]
pub required_collection: Pubkey,
#[serde(deserialize_with = "to_pubkey")]
#[serde(serialize_with = "to_string")]
pub destination: Pubkey,
}
impl NftPayment {
pub fn to_guard_format(&self) -> Result<mpl_candy_guard::guards::NftPayment> {
Ok(mpl_candy_guard::guards::NftPayment {
required_collection: self.required_collection,
destination: self.destination,
})
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct RedeemedAmount {
pub maximum: u64,
}
impl RedeemedAmount {
pub fn to_guard_format(&self) -> Result<mpl_candy_guard::guards::RedeemedAmount> {
Ok(mpl_candy_guard::guards::RedeemedAmount {
maximum: self.maximum,
})
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct SolPayment {
pub value: f64,
#[serde(deserialize_with = "to_pubkey")]
#[serde(serialize_with = "to_string")]
pub destination: Pubkey,
}
impl SolPayment {
pub fn to_guard_format(&self) -> Result<mpl_candy_guard::guards::SolPayment> {
Ok(mpl_candy_guard::guards::SolPayment {
lamports: price_as_lamports(self.value),
destination: self.destination,
})
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct StartDate {
pub date: String,
}
impl StartDate {
pub fn to_guard_format(&self) -> Result<mpl_candy_guard::guards::StartDate> {
let timestamp = self.date.parse::<DateTimeUtc>()?.0.timestamp();
Ok(mpl_candy_guard::guards::StartDate { date: timestamp })
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[serde(rename_all = "camelCase")]
pub struct ThirdPartySigner {
#[serde(deserialize_with = "to_pubkey")]
#[serde(serialize_with = "to_string")]
pub signer_key: Pubkey,
}
impl ThirdPartySigner {
pub fn to_guard_format(&self) -> Result<mpl_candy_guard::guards::ThirdPartySigner> {
Ok(mpl_candy_guard::guards::ThirdPartySigner {
signer_key: self.signer_key,
})
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct TokenBurn {
pub amount: u64,
#[serde(deserialize_with = "to_pubkey")]
#[serde(serialize_with = "to_string")]
pub mint: Pubkey,
}
impl TokenBurn {
pub fn to_guard_format(&self) -> Result<mpl_candy_guard::guards::TokenBurn> {
Ok(mpl_candy_guard::guards::TokenBurn {
amount: self.amount,
mint: self.mint,
})
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct TokenGate {
pub amount: u64,
#[serde(deserialize_with = "to_pubkey")]
#[serde(serialize_with = "to_string")]
pub mint: Pubkey,
}
impl TokenGate {
pub fn to_guard_format(&self) -> Result<mpl_candy_guard::guards::TokenGate> {
Ok(mpl_candy_guard::guards::TokenGate {
amount: self.amount,
mint: self.mint,
})
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[serde(rename_all = "camelCase")]
pub struct TokenPayment {
pub amount: u64,
#[serde(deserialize_with = "to_pubkey")]
#[serde(serialize_with = "to_string")]
pub mint: Pubkey,
#[serde(deserialize_with = "to_pubkey")]
#[serde(serialize_with = "to_string")]
pub destination_ata: Pubkey,
}
impl TokenPayment {
pub fn to_guard_format(&self) -> Result<mpl_candy_guard::guards::TokenPayment> {
Ok(mpl_candy_guard::guards::TokenPayment {
amount: self.amount,
mint: self.mint,
destination_ata: self.destination_ata,
})
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[serde(rename_all = "camelCase")]
pub struct FreezeSolPayment {
pub value: f64,
#[serde(deserialize_with = "to_pubkey")]
#[serde(serialize_with = "to_string")]
pub destination: Pubkey,
}
impl FreezeSolPayment {
pub fn to_guard_format(&self) -> Result<mpl_candy_guard::guards::FreezeSolPayment> {
Ok(mpl_candy_guard::guards::FreezeSolPayment {
lamports: price_as_lamports(self.value),
destination: self.destination,
})
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[serde(rename_all = "camelCase")]
pub struct FreezeTokenPayment {
pub amount: u64,
#[serde(deserialize_with = "to_pubkey")]
#[serde(serialize_with = "to_string")]
pub mint: Pubkey,
#[serde(deserialize_with = "to_pubkey")]
#[serde(serialize_with = "to_string")]
pub destination_ata: Pubkey,
}
impl FreezeTokenPayment {
pub fn to_guard_format(&self) -> Result<mpl_candy_guard::guards::FreezeTokenPayment> {
Ok(mpl_candy_guard::guards::FreezeTokenPayment {
amount: self.amount,
mint: self.mint,
destination_ata: self.destination_ata,
})
}
}