crate_token/
account_validators.rs

1//! Validate accounts
2
3use anchor_lang::prelude::*;
4use vipers::{assert_keys_eq, invariant};
5
6use crate::{Issue, NewCrate, SetFeeTo, SetFeeToSetter, SetFees, Withdraw};
7use anchor_lang::Key;
8use vipers::validate::Validate;
9
10impl<'info> Validate<'info> for NewCrate<'info> {
11    fn validate(&self) -> Result<()> {
12        assert_keys_eq!(
13            self.crate_mint.mint_authority.unwrap(),
14            self.crate_token,
15            "crate_mint.mint_authority"
16        );
17
18        let freeze_authority = self.crate_mint.freeze_authority.unwrap();
19        invariant!(
20            freeze_authority == self.crate_token.key()
21                || freeze_authority == self.issue_authority.key(),
22            InvalidFreezeAuthority
23        );
24
25        invariant!(self.crate_mint.supply == 0, "supply must be zero");
26        Ok(())
27    }
28}
29
30impl<'info> Validate<'info> for SetFees<'info> {
31    fn validate(&self) -> Result<()> {
32        assert_keys_eq!(
33            self.crate_token.fee_setter_authority,
34            self.fee_setter,
35            "crate_token.fee_setter_authority"
36        );
37        Ok(())
38    }
39}
40
41impl<'info> Validate<'info> for SetFeeTo<'info> {
42    fn validate(&self) -> Result<()> {
43        assert_keys_eq!(
44            self.crate_token.fee_to_setter,
45            self.fee_to_setter,
46            "crate_token.fee_to_setter"
47        );
48        Ok(())
49    }
50}
51
52impl<'info> Validate<'info> for SetFeeToSetter<'info> {
53    fn validate(&self) -> Result<()> {
54        assert_keys_eq!(
55            self.crate_token.fee_to_setter,
56            self.fee_to_setter,
57            "crate_token.fee_to_setter"
58        );
59        Ok(())
60    }
61}
62
63impl<'info> Validate<'info> for Issue<'info> {
64    fn validate(&self) -> Result<()> {
65        assert_keys_eq!(
66            self.crate_token.mint,
67            self.crate_mint.key(),
68            "crate_token.mint"
69        );
70        assert_keys_eq!(
71            self.crate_token.issue_authority,
72            self.issue_authority,
73            "crate_token.issue_authority"
74        );
75
76        assert_keys_eq!(
77            self.mint_destination.mint,
78            self.crate_token.mint,
79            "mint_destination.mint"
80        );
81
82        // only validate fee destinations if there are fees
83        if self.crate_token.issue_fee_bps != 0 {
84            assert_keys_eq!(
85                self.author_fee_destination.mint,
86                self.crate_token.mint,
87                "author_fee_destination.mint"
88            );
89            assert_keys_eq!(
90                self.author_fee_destination.owner,
91                self.crate_token.author_fee_to,
92                "author_fee_destination.owner"
93            );
94            assert_keys_eq!(
95                self.protocol_fee_destination.mint,
96                self.crate_token.mint,
97                "protocol_fee_destination.mint"
98            );
99            assert_keys_eq!(
100                self.protocol_fee_destination.owner,
101                crate::FEE_TO_ADDRESS,
102                "fee to mismatch"
103            );
104        }
105
106        Ok(())
107    }
108}
109
110impl<'info> Validate<'info> for Withdraw<'info> {
111    fn validate(&self) -> Result<()> {
112        assert_keys_eq!(
113            self.crate_underlying.owner,
114            self.crate_token,
115            "crate_underlying.owner"
116        );
117        assert_keys_eq!(
118            self.withdraw_authority,
119            self.crate_token.withdraw_authority,
120            "withdraw_authority"
121        );
122
123        assert_keys_eq!(
124            self.withdraw_destination.mint,
125            self.crate_underlying.mint,
126            "withdraw_destination.mint"
127        );
128
129        // only validate fee destinations if there are fees
130        if self.crate_token.withdraw_fee_bps != 0 {
131            assert_keys_eq!(
132                self.author_fee_destination.mint,
133                self.crate_underlying.mint,
134                "author_fee_destination.mint"
135            );
136            assert_keys_eq!(
137                self.author_fee_destination.owner,
138                self.crate_token.author_fee_to,
139                "author_fee_destination.owner"
140            );
141            assert_keys_eq!(
142                self.protocol_fee_destination.mint,
143                self.crate_underlying.mint,
144                "protocol_fee_destination.mint"
145            );
146            assert_keys_eq!(
147                self.protocol_fee_destination.owner,
148                crate::FEE_TO_ADDRESS,
149                "fee to mismatch"
150            );
151        }
152
153        Ok(())
154    }
155}