whisky_csl/tx_builder/
buildable.rs

1use whisky_common::{TxBuildable, *};
2
3use crate::WhiskyCSL;
4
5impl TxBuildable for WhiskyCSL {
6    fn reset_builder(&mut self) -> &mut Self {
7        self.core.reset_after_build();
8        self
9    }
10
11    fn set_protocol_params(&mut self, protocol_params: Protocol) -> &mut Self {
12        self.core.protocol_params = protocol_params.clone();
13        self
14    }
15
16    fn set_tx_builder_body(&mut self, tx_builder_body: TxBuilderBody) -> &mut Self {
17        self.tx_builder_body = tx_builder_body.clone();
18        self
19    }
20
21    /// ## Transaction building method
22    ///
23    /// Serialize the transaction body
24    ///
25    /// ### Arguments
26    ///
27    /// * `tx_builder_body` - The transaction builder body information
28    /// * `params` - Optional protocol parameters, default as Cardano mainnet configuration
29    ///
30    /// ### Returns
31    ///
32    /// * `String` - the built transaction hex
33    fn serialize_tx_body(&mut self) -> Result<String, WError> {
34        if self.tx_builder_body.change_address.is_empty() {
35            return Err(WError::new(
36                "serialize_tx_body",
37                "change address cannot be empty",
38            ));
39        }
40        self.add_all_inputs()?
41            .add_all_outputs()?
42            .add_all_collaterals()?
43            .add_all_reference_inputs()?
44            .add_all_withdrawals()?
45            .add_all_mints()?
46            .add_all_certificates()?
47            .add_all_votes()?
48            .add_validity_range()?
49            .add_all_required_signature()?
50            .add_all_metadata()?
51            .add_script_hash()?
52            .set_fee_if_needed()?
53            .add_collateral_return()?
54            .add_change_utxo()?;
55
56        self.core.build_tx(true)
57    }
58
59    fn unbalanced_serialize_tx_body(&mut self) -> Result<String, WError> {
60        if self.tx_builder_body.change_address.is_empty() {
61            return Err(WError::new(
62                "serialize_tx_body",
63                "change address cannot be empty",
64            ));
65        }
66        self.add_all_inputs()?
67            .add_all_outputs()?
68            .add_all_collaterals()?
69            .add_all_reference_inputs()?
70            .add_all_withdrawals()?
71            .add_all_mints()?
72            .add_all_certificates()?
73            .add_all_votes()?
74            .add_validity_range()?
75            .add_all_required_signature()?
76            .add_all_metadata()?
77            .add_script_hash()?
78            .set_fee_if_needed()?
79            .add_collateral_return()?;
80
81        self.core.build_tx(false)
82    }
83
84    /// ## Transaction building method
85    ///
86    /// Complete the signing process
87    ///
88    /// ### Returns
89    ///
90    /// * `String` - The signed transaction in hex
91    fn complete_signing(&mut self) -> Result<String, WError> {
92        let signing_keys = self.tx_builder_body.signing_key.clone();
93        self.add_all_signing_keys(
94            &signing_keys
95                .iter()
96                .map(|s| s.as_str())
97                .collect::<Vec<&str>>(),
98        )?;
99        Ok(self.core.tx_hex.to_string())
100    }
101
102    fn tx_hex(&mut self) -> String {
103        self.core.tx_hex.clone()
104    }
105}