cardano_serialization_lib/builders/
output_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
use crate::*;

/// We introduce a builder-pattern format for creating transaction outputs
/// This is because:
/// 1. Some fields (i.e. data hash) are optional, and we can't easily expose Option<> in WASM
/// 2. Some fields like amounts have many ways it could be set (some depending on other field values being known)
/// 3. Easier to adapt as the output format gets more complicated in future Cardano releases

#[wasm_bindgen]
#[derive(Clone, Debug)]
pub struct TransactionOutputBuilder {
    address: Option<Address>,
    data: Option<DataOption>,
    script_ref: Option<ScriptRef>,
}

#[wasm_bindgen]
impl TransactionOutputBuilder {
    pub fn new() -> Self {
        Self {
            address: None,
            data: None,
            script_ref: None,
        }
    }

    pub fn with_address(&self, address: &Address) -> Self {
        let mut cfg = self.clone();
        cfg.address = Some(address.clone());
        cfg
    }

    pub fn with_data_hash(&self, data_hash: &DataHash) -> Self {
        let mut cfg = self.clone();
        cfg.data = Some(DataOption::DataHash(data_hash.clone()));
        cfg
    }

    pub fn with_plutus_data(&self, data: &PlutusData) -> Self {
        let mut cfg = self.clone();
        cfg.data = Some(DataOption::Data(data.clone()));
        cfg
    }

    pub fn with_script_ref(&self, script_ref: &ScriptRef) -> Self {
        let mut cfg = self.clone();
        cfg.script_ref = Some(script_ref.clone());
        cfg
    }

    pub fn next(&self) -> Result<TransactionOutputAmountBuilder, JsError> {
        Ok(TransactionOutputAmountBuilder {
            address: self.address.clone().ok_or(JsError::from_str(
                "TransactionOutputBaseBuilder: Address missing",
            ))?,
            amount: None,
            data: self.data.clone(),
            script_ref: self.script_ref.clone(),
        })
    }
}

#[wasm_bindgen]
#[derive(Clone, Debug)]
pub struct TransactionOutputAmountBuilder {
    address: Address,
    amount: Option<Value>,
    data: Option<DataOption>,
    script_ref: Option<ScriptRef>,
}

#[wasm_bindgen]
impl TransactionOutputAmountBuilder {
    pub fn with_value(&self, amount: &Value) -> Self {
        let mut cfg = self.clone();
        cfg.amount = Some(amount.clone());
        cfg
    }

    pub fn with_coin(&self, coin: &Coin) -> Self {
        let mut cfg = self.clone();

        cfg.amount = Some(Value::new(coin));
        cfg
    }

    pub fn with_coin_and_asset(&self, coin: &Coin, multiasset: &MultiAsset) -> Self {
        let mut cfg = self.clone();

        let mut val = Value::new(coin);
        val.set_multiasset(multiasset);
        cfg.amount = Some(val.clone());
        cfg
    }

    pub fn with_asset_and_min_required_coin_by_utxo_cost(
        &self,
        multiasset: &MultiAsset,
        data_cost: &DataCost,
    ) -> Result<TransactionOutputAmountBuilder, JsError> {
        // TODO: double ada calculation needs to check if it redundant
        let mut calc = MinOutputAdaCalculator::new_empty(data_cost)?;
        if let Some(data) = &self.data {
            match data {
                DataOption::DataHash(data_hash) => calc.set_data_hash(data_hash),
                DataOption::Data(datum) => calc.set_plutus_data(datum),
            };
        }
        if let Some(script_ref) = &self.script_ref {
            calc.set_script_ref(script_ref);
        }
        let min_possible_coin = calc.calculate_ada()?;
        let mut value = Value::new(&min_possible_coin);
        value.set_multiasset(multiasset);

        let mut calc = MinOutputAdaCalculator::new_empty(data_cost)?;
        calc.set_amount(&value);
        if let Some(data) = &self.data {
            match data {
                DataOption::DataHash(data_hash) => calc.set_data_hash(data_hash),
                DataOption::Data(datum) => calc.set_plutus_data(datum),
            };
        }
        if let Some(script_ref) = &self.script_ref {
            calc.set_script_ref(script_ref);
        }
        let required_coin = calc.calculate_ada()?;

        Ok(self.with_coin_and_asset(&required_coin, &multiasset))
    }

    pub fn build(&self) -> Result<TransactionOutput, JsError> {
        Ok(TransactionOutput {
            address: self.address.clone(),
            amount: self.amount.clone().ok_or(JsError::from_str(
                "TransactionOutputAmountBuilder: amount missing",
            ))?,
            plutus_data: self.data.clone(),
            script_ref: self.script_ref.clone(),
            serialization_format: None,
        })
    }
}