whisky_examples/tx/
unlock_fund.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
use whisky::{
    builder::{TxBuilder, WData, WRedeemer},
    core::utils::deserialize_bech32_address,
    csl::JsError,
    model::{Budget, ProvidedScriptSource, UTxO},
};

pub async fn unlock_fund(
    script_utxo: &UTxO,
    redeemer: &str,
    script: &ProvidedScriptSource,
    my_address: &str,
    inputs: &[UTxO],
    collateral: &UTxO,
) -> Result<String, JsError> {
    let mut tx_builder = TxBuilder::new_core();
    let pub_key_hash = deserialize_bech32_address(my_address).get_pub_key_hash();

    tx_builder
        // .spending_plutus_script_v1()
        .spending_plutus_script_v2()
        // .spending_plutus_script_v3()
        .tx_in(
            &script_utxo.input.tx_hash,
            script_utxo.input.output_index,
            &script_utxo.output.amount,
            &script_utxo.output.address,
        )
        .tx_in_inline_datum_present()
        // .tx_in_datum_value(datum here) or provide datum value
        .tx_in_redeemer_value(&WRedeemer {
            data: WData::JSON(redeemer.to_string()),
            ex_units: Budget { mem: 0, steps: 0 },
        })
        .tx_in_script(&script.script_cbor)
        // .spending_tx_in_reference(tx_hash, tx_index, script_hash, script_size)
        .change_address(my_address)
        .required_signer_hash(&pub_key_hash) // Extra logic impl
        .tx_in_collateral(
            &collateral.input.tx_hash,
            collateral.input.output_index,
            &collateral.output.amount,
            &collateral.output.address,
        )
        .input_for_evaluation(script_utxo)
        .select_utxos_from(inputs, 5000000)
        .complete(None)
        .await?;

    Ok(tx_builder.tx_hex())
}