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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
use anchor_lang::prelude::Pubkey;
use anyhow::{anyhow, Result};
use dateparser::DateTimeUtc;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr};

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()?,
        })
    }
}

/// The set of guards available.
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[serde(rename_all = "camelCase")]
pub struct GuardSet {
    /// Last instruction check and bot tax (penalty for invalid transactions).
    pub bot_tax: Option<BotTax>,
    /// Sol payment guard (set the price for the mint in lamports).
    pub sol_payment: Option<SolPayment>,
    /// Token payment guard (set the price for the mint in spl-token amount).
    pub token_payment: Option<TokenPayment>,
    /// Start data guard (controls when minting is allowed).
    pub start_date: Option<StartDate>,
    /// Third party signer guard.
    pub third_party_signer: Option<ThirdPartySigner>,
    /// Token gate guard (restricrt access to holders of a specific token).
    pub token_gate: Option<TokenGate>,
    /// Gatekeeper guard
    pub gatekeeper: Option<Gatekeeper>,
    /// End date guard
    pub end_date: Option<EndDate>,
    /// Allow list guard
    pub allow_list: Option<AllowList>,
    /// Mint limit guard
    pub mint_limit: Option<MintLimit>,
    /// NFT Payment
    pub nft_payment: Option<NftPayment>,
    /// Redeemed amount guard
    pub redeemed_amount: Option<RedeemedAmount>,
    /// Address gate (check access against a specified address)
    pub address_gate: Option<AddressGate>,
    /// NFT gate guard (check access based on holding a specified NFT)
    pub nft_gate: Option<NftGate>,
    /// NFT burn guard (burn a specified NFT)
    pub nft_burn: Option<NftBurn>,
    /// Token burn guard (burn a specified amount of spl-token)
    pub token_burn: Option<TokenBurn>,
    /// Freeze sol payment guard (set the price for the mint in lamports with a freeze period).
    pub freeze_sol_payment: Option<FreezeSolPayment>,
    /// Freeze token payment guard (set the price for the mint in spl-token amount with a freeze period).
    pub freeze_token_payment: Option<FreezeTokenPayment>,
    /// Program gate guard (restricts the programs that can be in a mint transaction).
    pub program_gate: Option<ProgramGate>,
    /// Allocation guard (specify the maximum number of mints in a group).
    pub allocation: Option<Allocation>,
    /// Token2022 payment guard (set the price for the mint in spl-token-2022 amount).
    pub token2022_payment: Option<Token2022Payment>,
}

impl GuardSet {
    pub fn to_guard_format(&self) -> Result<mpl_candy_guard::guards::GuardSet> {
        // bot tax
        let bot_tax = if let Some(bot_tax) = &self.bot_tax {
            Some(bot_tax.to_guard_format()?)
        } else {
            None
        };
        // sol payment
        let sol_payment = if let Some(sol_payment) = &self.sol_payment {
            Some(sol_payment.to_guard_format()?)
        } else {
            None
        };
        // token payment
        let token_payment = if let Some(token_payment) = &self.token_payment {
            Some(token_payment.to_guard_format()?)
        } else {
            None
        };
        // start_date
        let start_date = if let Some(start_date) = &self.start_date {
            Some(start_date.to_guard_format()?)
        } else {
            None
        };
        // third party signer
        let third_party_signer = if let Some(third_party_signer) = &self.third_party_signer {
            Some(third_party_signer.to_guard_format()?)
        } else {
            None
        };
        // token gate
        let token_gate = if let Some(token_gate) = &self.token_gate {
            Some(token_gate.to_guard_format()?)
        } else {
            None
        };
        // gatekeeper
        let gatekeeper = if let Some(gatekeeper) = &self.gatekeeper {
            Some(gatekeeper.to_guard_format()?)
        } else {
            None
        };
        // end date
        let end_date = if let Some(end_date) = &self.end_date {
            Some(end_date.to_guard_format()?)
        } else {
            None
        };
        // allow list
        let allow_list = if let Some(allow_list) = &self.allow_list {
            Some(allow_list.to_guard_format()?)
        } else {
            None
        };
        // mint limit
        let mint_limit = if let Some(mint_limit) = &self.mint_limit {
            Some(mint_limit.to_guard_format()?)
        } else {
            None
        };
        // nft payment
        let nft_payment = if let Some(nft_payment) = &self.nft_payment {
            Some(nft_payment.to_guard_format()?)
        } else {
            None
        };
        // redeemed amount
        let redeemed_amount = if let Some(redeemed_amount) = &self.redeemed_amount {
            Some(redeemed_amount.to_guard_format()?)
        } else {
            None
        };
        // address gate
        let address_gate = if let Some(address_gate) = &self.address_gate {
            Some(address_gate.to_guard_format()?)
        } else {
            None
        };
        // nft gate
        let nft_gate = if let Some(nft_gate) = &self.nft_gate {
            Some(nft_gate.to_guard_format()?)
        } else {
            None
        };
        // nft burn
        let nft_burn = if let Some(nft_burn) = &self.nft_burn {
            Some(nft_burn.to_guard_format()?)
        } else {
            None
        };
        // token burn
        let token_burn = if let Some(token_burn) = &self.token_burn {
            Some(token_burn.to_guard_format()?)
        } else {
            None
        };
        // freeze sol payment
        let freeze_sol_payment = if let Some(freeze_sol_payment) = &self.freeze_sol_payment {
            Some(freeze_sol_payment.to_guard_format()?)
        } else {
            None
        };
        // freeze token payment
        let freeze_token_payment = if let Some(freeze_token_payment) = &self.freeze_token_payment {
            Some(freeze_token_payment.to_guard_format()?)
        } else {
            None
        };
        // program gate
        let program_gate = if let Some(program_gate) = &self.program_gate {
            Some(program_gate.to_guard_format()?)
        } else {
            None
        };
        // allocation
        let allocation = if let Some(allocation) = &self.allocation {
            Some(allocation.to_guard_format()?)
        } else {
            None
        };
        // tokwn2022 payment
        let token2022_payment = if let Some(token2022_payment) = &self.token2022_payment {
            Some(token2022_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,
            allocation,
            token2022_payment,
        })
    }
}

// Address guard

#[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,
        })
    }
}

// Alow List guard

#[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 })
    }
}

// Bot Tax guard

#[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,
        })
    }
}

// End Date guard

#[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 })
    }
}

// Gatekeeper guard

#[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,
        })
    }
}

// Mint Limit guard

#[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,
        })
    }
}

// Nft Burn guard

#[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,
        })
    }
}

// Nft Gate guard

#[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,
        })
    }
}

// Nft Payment guard

#[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,
        })
    }
}

// Redeemed Amount guard

#[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,
        })
    }
}

// Sol Payment guard

#[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 })
    }
}

// Third Party Signer guard

#[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,
        })
    }
}

// Token Burn guard

#[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,
        })
    }
}

// Token Gate guard

#[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,
        })
    }
}

// Token Payment guard

#[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,
        })
    }
}

// Freeze Sol Payment guard

#[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,
        })
    }
}

// Freeze Token Payment guard

#[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,
        })
    }
}

// ProgramGate

#[serde_as]
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[serde(rename_all = "camelCase")]
pub struct ProgramGate {
    #[serde_as(as = "Vec<DisplayFromStr>")]
    pub additional: Vec<Pubkey>,
}

impl ProgramGate {
    pub fn to_guard_format(&self) -> Result<mpl_candy_guard::guards::ProgramGate> {
        Ok(mpl_candy_guard::guards::ProgramGate {
            additional: self.additional.clone(),
        })
    }
}

// Allocation

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Allocation {
    pub id: u8,

    pub limit: u32,
}

impl Allocation {
    pub fn to_guard_format(&self) -> Result<mpl_candy_guard::guards::Allocation> {
        Ok(mpl_candy_guard::guards::Allocation {
            id: self.id,
            limit: self.limit,
        })
    }
}

// Token2022 Payment guard

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[serde(rename_all = "camelCase")]
pub struct Token2022Payment {
    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 Token2022Payment {
    pub fn to_guard_format(&self) -> Result<mpl_candy_guard::guards::Token2022Payment> {
        Ok(mpl_candy_guard::guards::Token2022Payment {
            amount: self.amount,
            mint: self.mint,
            destination_ata: self.destination_ata,
        })
    }
}