whisky/builder/
tx_out.rs

1use crate::*;
2
3use super::{TxBuilder, WData};
4
5impl TxBuilder {
6    /// ## Transaction building method
7    ///
8    /// Add a transaction output to the TxBuilder instance
9    ///
10    /// ### Arguments
11    ///
12    /// * `address` - The address
13    /// * `amount` - The amount of assets
14    ///
15    /// ### Returns
16    ///
17    /// * `Self` - The TxBuilder instance
18    pub fn tx_out(&mut self, address: &str, amount: &[Asset]) -> &mut Self {
19        if self.tx_output.is_some() {
20            let tx_output = self.tx_output.take();
21            self.tx_builder_body.outputs.push(tx_output.unwrap());
22        }
23        self.tx_output = Some(Output {
24            address: address.to_string(),
25            amount: amount.to_vec(),
26            datum: None,
27            reference_script: None,
28        });
29        self
30    }
31
32    /// ## Transaction building method
33    ///
34    /// Set the transaction output datum hash value in the TxBuilder instance
35    ///
36    /// ### Arguments
37    ///
38    /// * `data` - The datum hash value
39    ///
40    /// ### Returns
41    ///
42    /// * `Self` - The TxBuilder instance
43    pub fn tx_out_datum_hash_value(&mut self, data: &WData) -> &mut Self {
44        let tx_output = self.tx_output.take();
45        if tx_output.is_none() {
46            panic!("Undefined output")
47        }
48        let mut tx_output = tx_output.unwrap();
49        match data.to_cbor() {
50            Ok(raw_data) => {
51                tx_output.datum = Some(Datum::Hash(raw_data));
52                self.tx_output = Some(tx_output);
53            }
54            Err(_) => {
55                panic!("Error converting datum to CBOR");
56            }
57        }
58        self
59    }
60
61    /// ## Transaction building method
62    ///
63    /// Set the transaction output embedded datum value in the TxBuilder instance
64    ///
65    /// ### Arguments
66    ///
67    /// * `data` - The embedded datum value
68    ///
69    /// ### Returns
70    ///
71    /// * `Self` - The TxBuilder instance
72    pub fn tx_out_datum_embed_value(&mut self, data: &WData) -> &mut Self {
73        let tx_output = self.tx_output.take();
74        if tx_output.is_none() {
75            panic!("Undefined output")
76        }
77        let mut tx_output = tx_output.unwrap();
78        match data.to_cbor() {
79            Ok(raw_data) => {
80                tx_output.datum = Some(Datum::Embedded(raw_data));
81                self.tx_output = Some(tx_output);
82            }
83            Err(_) => {
84                panic!("Error converting datum to CBOR");
85            }
86        }
87        self
88    }
89
90    /// ## Transaction building method
91    ///
92    /// Set the transaction output inline datum value in the TxBuilder instance
93    ///
94    /// ### Arguments
95    ///
96    /// * `data` - The inline datum value
97    ///
98    /// ### Returns
99    ///
100    /// * `Self` - The TxBuilder instance
101    pub fn tx_out_inline_datum_value(&mut self, data: &WData) -> &mut Self {
102        let tx_output = self.tx_output.take();
103        if tx_output.is_none() {
104            panic!("Undefined output")
105        }
106        let mut tx_output = tx_output.unwrap();
107        match data.to_cbor() {
108            Ok(raw_data) => {
109                tx_output.datum = Some(Datum::Inline(raw_data));
110                self.tx_output = Some(tx_output);
111            }
112            Err(_) => {
113                panic!("Error converting datum to CBOR");
114            }
115        }
116        self
117    }
118
119    /// ## Transaction building method
120    ///
121    /// Add a transaction output reference script to the TxBuilder instance
122    ///
123    /// ### Arguments
124    ///
125    /// * `script_cbor` - The script in CBOR format
126    /// * `version` - The language version, if the language version is None, the script is assumed to be a Native Script
127    ///
128    /// ### Returns
129    ///
130    /// * `Self` - The TxBuilder instance
131    pub fn tx_out_reference_script(
132        &mut self,
133        script_cbor: &str,
134        version: Option<LanguageVersion>,
135    ) -> &mut Self {
136        let tx_output = self.tx_output.take();
137        if tx_output.is_none() {
138            panic!("Undefined output")
139        }
140        let mut tx_output = tx_output.unwrap();
141        match version {
142            Some(language_version) => {
143                tx_output.reference_script = Some(OutputScriptSource::ProvidedScriptSource(
144                    ProvidedScriptSource {
145                        script_cbor: script_cbor.to_string(),
146                        language_version,
147                    },
148                ));
149            }
150            None => {
151                tx_output.reference_script = Some(OutputScriptSource::ProvidedSimpleScriptSource(
152                    ProvidedSimpleScriptSource {
153                        script_cbor: script_cbor.to_string(),
154                    },
155                ))
156            }
157        }
158
159        self.tx_output = Some(tx_output);
160        self
161    }
162}