whisky_common/tx_tester/
mints.rs

1use super::TxTester;
2
3impl TxTester {
4    /// ## Testing methods for mints
5    ///
6    /// Checks if a specific token is minted in the transaction.
7    pub fn token_minted(&mut self, policy_id: &str, asset_name: &str, quantity: i128) -> &mut Self {
8        let is_token_minted = self.token_minted_logic(policy_id, asset_name, quantity);
9
10        if !is_token_minted {
11            self.add_trace(
12                "token_minted",
13                &format!(
14                    "Token with policy_id: {}, asset_name: {}, quantity: {} not found in mints.",
15                    policy_id, asset_name, quantity
16                ),
17            );
18        }
19
20        self
21    }
22
23    /// ## Testing methods for mints
24    ///
25    /// Checks if a specific token is minted in the transaction and that it is the only mint.
26    pub fn only_token_minted(
27        &mut self,
28        policy_id: &str,
29        asset_name: &str,
30        quantity: i128,
31    ) -> &mut Self {
32        let is_token_minted = self.token_minted_logic(policy_id, asset_name, quantity);
33        let is_only_one_mint = self.tx_body.mints.len() == 1;
34
35        if !is_token_minted {
36            self.add_trace(
37                "only_token_minted",
38                &format!(
39                    "Token with policy_id: {}, asset_name: {}, quantity: {} not found in mints",
40                    policy_id, asset_name, quantity
41                ),
42            );
43        }
44        if !is_only_one_mint {
45            self.add_trace(
46                "only_token_minted",
47                &format!(
48                    "Expected only one mint, but found {} mints.",
49                    self.tx_body.mints.len()
50                ),
51            );
52        }
53        self
54    }
55
56    /// ## Testing methods for mints
57    ///
58    /// Checks if a specific token is minted in the transaction, ensuring that it is the only mint for the given policy ID.
59    pub fn policy_only_minted_token(
60        &mut self,
61        policy_id: &str,
62        asset_name: &str,
63        quantity: i128,
64    ) -> &mut Self {
65        let filtered_mints: Vec<_> = self
66            .tx_body
67            .mints
68            .iter()
69            .filter(|token| {
70                let mint_param = token.get_mint_parameter();
71                mint_param.policy_id == policy_id
72            })
73            .collect();
74
75        let is_token_minted = self.token_minted_logic(policy_id, asset_name, quantity);
76        let is_only_one_mint = filtered_mints.len() == 1;
77        if !is_only_one_mint {
78            self.add_trace(
79                "policy_only_minted_token",
80                &format!(
81                    "Expected only one mint for policy_id: {}, but found {} mints.",
82                    policy_id,
83                    filtered_mints.len()
84                ),
85            );
86        }
87        if !is_token_minted {
88            self.add_trace(
89                "policy_only_minted_token",
90                &format!(
91                    "Token with policy_id: {}, asset_name: {}, quantity: {} not found in mints.",
92                    policy_id, asset_name, quantity
93                ),
94            );
95        }
96        self
97    }
98
99    /// ## Testing methods for mints
100    ///
101    /// Checks if a specific policy ID is burned in the transaction, ensuring that it is the only minting (i.e. burning item).
102    pub fn check_policy_only_burn(&self, policy_id: &str) -> bool {
103        let filtered_mints: Vec<_> = self
104            .tx_body
105            .mints
106            .iter()
107            .filter(|token| {
108                let mint_param = token.get_mint_parameter();
109                mint_param.policy_id == policy_id && mint_param.amount > 0
110            })
111            .collect();
112        filtered_mints.len() == 1
113    }
114
115    pub fn token_minted_logic(&self, policy_id: &str, asset_name: &str, quantity: i128) -> bool {
116        self.tx_body.mints.iter().any(|token| {
117            let mint_param = token.get_mint_parameter();
118            mint_param.policy_id == policy_id
119                && mint_param.asset_name == asset_name
120                && mint_param.amount == quantity
121        })
122    }
123}