whisky_common/tx_tester/
inputs.rs

1use crate::Value;
2
3use super::TxTester;
4
5impl TxTester {
6    /// ## Filtering methods for testing inputs
7    ///
8    /// Not apply filter to inputs
9    pub fn all_inputs(&mut self) -> &mut Self {
10        self.inputs_evaluating = self.tx_body.inputs.clone();
11        self
12    }
13
14    /// ## Filtering methods for testing inputs
15    ///
16    /// Filter inputs by address
17    pub fn inputs_at(&mut self, address: &str) -> &mut Self {
18        self.inputs_evaluating = self
19            .tx_body
20            .inputs
21            .iter()
22            .filter(|input| input.to_utxo().output.address == address)
23            .cloned()
24            .collect();
25        self
26    }
27
28    /// ## Filtering methods for testing inputs
29    ///
30    /// Filter inputs by unit
31    pub fn inputs_with(&mut self, unit: &str) -> &mut Self {
32        self.inputs_evaluating = self
33            .tx_body
34            .inputs
35            .iter()
36            .filter(|input| {
37                let input_value = Value::from_asset_vec(&input.to_utxo().output.amount.clone());
38                let quantity = input_value.get(unit);
39                quantity > 0
40            })
41            .cloned()
42            .collect();
43        self
44    }
45
46    /// ## Filtering methods for testing inputs
47    ///
48    /// Filter inputs by policy ID
49    pub fn inputs_with_policy(&mut self, policy_id: &str) -> &mut Self {
50        self.inputs_evaluating = self
51            .tx_body
52            .inputs
53            .iter()
54            .filter(|input| {
55                let input_value = Value::from_asset_vec(&input.to_utxo().output.amount.clone());
56                let assets = input_value.get_policy_assets(policy_id);
57                assets.len() > 0
58            })
59            .cloned()
60            .collect();
61        self
62    }
63
64    /// ## Filtering methods for testing inputs
65    ///
66    /// Filter inputs by address and policy ID
67    pub fn inputs_at_with_policy(&mut self, address: &str, policy_id: &str) -> &mut Self {
68        self.inputs_evaluating = self
69            .tx_body
70            .inputs
71            .iter()
72            .filter(|input| {
73                let utxo = input.to_utxo();
74                let input_value = Value::from_asset_vec(&utxo.output.amount.clone());
75                let assets = input_value.get_policy_assets(policy_id);
76                utxo.output.address == address && assets.len() > 0
77            })
78            .cloned()
79            .collect();
80        self
81    }
82
83    /// ## Filtering methods for testing inputs
84    ///
85    /// Filter inputs by address and unit
86    pub fn inputs_at_with(&mut self, address: &str, unit: &str) -> &mut Self {
87        self.inputs_evaluating = self
88            .tx_body
89            .inputs
90            .iter()
91            .filter(|input| {
92                let utxo = input.to_utxo();
93                let input_value = Value::from_asset_vec(&utxo.output.amount.clone());
94                let quantity = input_value.get(unit);
95                utxo.output.address == address && quantity > 0
96            })
97            .cloned()
98            .collect();
99        self
100    }
101
102    /// ## Testing methods for inputs
103    ///
104    /// *Reminder - It must be called after filtering methods for inputs*
105    ///
106    /// Check if inputs contain the expected value.
107    pub fn inputs_value(&mut self, expected_value: Value) -> &mut Self {
108        let mut value = Value::new();
109        self.inputs_evaluating.iter().for_each(|input| {
110            let utxo = input.to_utxo();
111            value.add_assets(&utxo.output.amount);
112        });
113        let is_value_correct = value.eq(&expected_value);
114        if !is_value_correct {
115            self.add_trace(
116                "inputs_value",
117                &format!(
118                    "inputs {:?} have value {:?}, expect {:?}",
119                    self.inputs_evaluating, value, expected_value
120                ),
121            );
122        }
123
124        self
125    }
126
127    /// ## Testing methods for inputs
128    ///
129    /// *Reminder - It must be called after filtering methods for inputs*
130    ///
131    /// Check if inputs contain a specific inline datum.
132    pub fn inputs_inline_datum_exist(&mut self, datum_cbor: &str) -> &mut Self {
133        let inputs_with_inline_datum: Vec<_> = self
134            .inputs_evaluating
135            .iter()
136            .filter(|input| {
137                let utxo = input.to_utxo();
138                if let Some(datum) = utxo.output.plutus_data {
139                    datum == *datum_cbor
140                } else {
141                    false
142                }
143            })
144            .cloned()
145            .collect();
146
147        if inputs_with_inline_datum.is_empty() {
148            self.add_trace(
149                "inputs_inline_datum_exist",
150                &format!("No inputs with inline datum matching: {}", datum_cbor),
151            );
152        }
153        self
154    }
155}