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

pub struct UnlockUtxo {
    pub script_utxo: UTxO,
    pub redeemer: String,
    pub script: ProvidedScriptSource,
}

pub struct MintToken {
    pub to_mint_asset: Asset,
    pub redeemer: String,
    pub script: ProvidedScriptSource,
}

pub async fn complex_transaction(
    to_unlock: &UnlockUtxo,
    to_mint_1: &MintToken,
    to_mint_2: &MintToken,
    my_address: &str,
    inputs: &[UTxO],
    collateral: &UTxO,
) -> Result<String, JsError> {
    let UnlockUtxo {
        script_utxo,
        redeemer,
        script,
    } = to_unlock;

    let MintToken {
        to_mint_asset: to_mint_asset_1,
        redeemer: redeemer_1,
        script: script_1,
    } = to_mint_1;

    let MintToken {
        to_mint_asset: to_mint_asset_2,
        redeemer: redeemer_2,
        script: script_2,
    } = to_mint_2;

    let mut tx_builder = TxBuilder::new_core();
    tx_builder
        .spending_plutus_script_v2()
        .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)
        .mint_plutus_script_v2()
        .mint(
            to_mint_asset_1.quantity_i128(),
            &to_mint_asset_1.policy(),
            &to_mint_asset_1.name(),
        )
        .mint_redeemer_value(&WRedeemer {
            data: WData::JSON(redeemer_1.to_string()),
            ex_units: Budget { mem: 0, steps: 0 },
        })
        .minting_script(&script_1.script_cbor)
        .mint_plutus_script_v2()
        .mint(
            to_mint_asset_2.quantity_i128(),
            &to_mint_asset_2.policy(),
            &to_mint_asset_2.name(),
        )
        .mint_redeemer_value(&WRedeemer {
            data: WData::JSON(redeemer_2.to_string()),
            ex_units: Budget { mem: 0, steps: 0 },
        })
        .minting_script(&script_2.script_cbor)
        .change_address(my_address)
        .tx_in_collateral(
            &collateral.input.tx_hash,
            collateral.input.output_index,
            &collateral.output.amount,
            &collateral.output.address,
        )
        .select_utxos_from(inputs, 5000000)
        .input_for_evaluation(script_utxo)
        .complete(None)
        .await?;

    Ok(tx_builder.tx_hex())
}