cardano_serialization_lib/builders/
withdrawals_builder.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use crate::*;
use hashlink::LinkedHashMap;

#[wasm_bindgen]
#[derive(Clone, Debug)]
pub struct WithdrawalsBuilder {
    withdrawals: LinkedHashMap<RewardAddress, (Coin, Option<ScriptWitnessType>)>,
}

#[wasm_bindgen]
impl WithdrawalsBuilder {
    pub fn new() -> Self {
        Self {
            withdrawals: LinkedHashMap::new(),
        }
    }

    pub fn add(&mut self, address: &RewardAddress, coin: &Coin) -> Result<(), JsError> {
        if address.payment_cred().has_script_hash() {
            return Err(JsError::from_str(
                "Your address has a required script witness.\
                Please use .add_with_plutus_witness or .add_with_native_script instead.",
            ));
        }

        self.withdrawals
            .insert(address.clone(), (coin.clone(), None));

        Ok(())
    }

    pub fn add_with_plutus_witness(
        &mut self,
        address: &RewardAddress,
        coin: &Coin,
        witness: &PlutusWitness,
    ) -> Result<(), JsError> {
        if !address.payment_cred().has_script_hash() {
            return Err(JsError::from_str(
                "Your address does not have a required script witness.\
                Please use .add instead.",
            ));
        }

        self.withdrawals.insert(
            address.clone(),
            (
                coin.clone(),
                Some(ScriptWitnessType::PlutusScriptWitness(witness.clone())),
            ),
        );
        Ok(())
    }

    pub fn add_with_native_script(
        &mut self,
        address: &RewardAddress,
        coin: &Coin,
        native_script_source: &NativeScriptSource,
    ) -> Result<(), JsError> {
        if !address.payment_cred().has_script_hash() {
            return Err(JsError::from_str(
                "Your address does not have a required script witness.\
                Please use .add instead.",
            ));
        }

        self.withdrawals.insert(
            address.clone(),
            (
                coin.clone(),
                Some(ScriptWitnessType::NativeScriptWitness(
                    native_script_source.0.clone(),
                )),
            ),
        );
        Ok(())
    }

    pub(crate) fn get_required_signers(&self) -> Ed25519KeyHashes {
        let mut set = Ed25519KeyHashes::new();
        for (address, (_, script_wit)) in &self.withdrawals {
            let req_signature = address.payment_cred().to_keyhash();
            if let Some(req_signature) = req_signature {
                set.add_move(req_signature);
            }

            if let Some(script_wit) = script_wit {
                if let Some(script_wit) = script_wit.get_required_signers() {
                    set.extend_move(script_wit);
                }
            }
        }
        set
    }

    pub fn get_plutus_witnesses(&self) -> PlutusWitnesses {
        let tag = RedeemerTag::new_reward();
        let mut scripts = PlutusWitnesses::new();
        for (i, (_, (_, script_wit))) in self.withdrawals.iter().enumerate() {
            if let Some(ScriptWitnessType::PlutusScriptWitness(s)) = script_wit {
                let index = BigNum::from(i);
                scripts.add(&s.clone_with_redeemer_index_and_tag(&index, &tag));
            }
        }
        scripts
    }

    pub fn get_ref_inputs(&self) -> TransactionInputs {
        let mut inputs = Vec::new();
        for (_, (_, script_wit)) in self.withdrawals.iter() {
            match script_wit {
                Some(script_witness) => {
                    if let Some(input) = script_witness.get_script_ref_input() {
                        inputs.push(input);
                    }
                    if let Some(input) = script_witness.get_datum_ref_input() {
                        inputs.push(input);
                    }
                }
                None => {}
            }
        }
        TransactionInputs::from_vec(inputs)
    }

    pub fn get_native_scripts(&self) -> NativeScripts {
        let mut scripts = NativeScripts::new();
        for (_, (_, script_wit)) in self.withdrawals.iter() {
            if let Some(ScriptWitnessType::NativeScriptWitness(
                NativeScriptSourceEnum::NativeScript(script, _),
            )) = script_wit
            {
                scripts.add(script);
            }
        }
        scripts
    }

    pub(crate) fn get_used_plutus_lang_versions(&self) -> BTreeSet<Language> {
        let mut used_langs = BTreeSet::new();
        for (_, (_, script_wit)) in &self.withdrawals {
            if let Some(ScriptWitnessType::PlutusScriptWitness(s)) = script_wit {
                used_langs.insert(s.script.language());
            }
        }
        used_langs
    }

    pub fn get_total_withdrawals(&self) -> Result<Value, JsError> {
        let mut total = Coin::zero();
        for (_, (coin, _)) in &self.withdrawals {
            total = total.checked_add(coin)?;
        }
        Ok(Value::new(&total))
    }

    pub fn has_plutus_scripts(&self) -> bool {
        for (_, (_, script_wit)) in &self.withdrawals {
            if let Some(ScriptWitnessType::PlutusScriptWitness(_)) = script_wit {
                return true;
            }
        }
        false
    }

    //return only ref inputs that are script refs with added size
    //used for calculating the fee for the transaction
    //another ref input and also script ref input without size are filtered out
    pub(crate) fn get_script_ref_inputs_with_size(
        &self,
    ) -> impl Iterator<Item = (&TransactionInput, usize)> {
        self.withdrawals.iter()
            .filter_map(|(_, (_, script_wit))| script_wit.as_ref())
            .filter_map(|script_wit| script_wit.get_script_ref_input_with_size())
    }

    pub fn build(&self) -> Withdrawals {
        let map = self
            .withdrawals
            .iter()
            .map(|(k, (v, _))| (k.clone(), v.clone()))
            .collect();
        Withdrawals(map)
    }
}