whisky_examples/tx/
complex_transaction.rs

1use whisky::*;
2
3pub struct UnlockUtxo {
4    pub script_utxo: UTxO,
5    pub redeemer: String,
6    pub script: ProvidedScriptSource,
7}
8
9pub struct MintToken {
10    pub to_mint_asset: Asset,
11    pub redeemer: String,
12    pub script: ProvidedScriptSource,
13}
14
15pub async fn complex_transaction(
16    to_unlock: &UnlockUtxo,
17    to_mint_1: &MintToken,
18    to_mint_2: &MintToken,
19    my_address: &str,
20    inputs: &[UTxO],
21    collateral: &UTxO,
22) -> Result<String, WError> {
23    let UnlockUtxo {
24        script_utxo,
25        redeemer,
26        script,
27    } = to_unlock;
28
29    let MintToken {
30        to_mint_asset: to_mint_asset_1,
31        redeemer: redeemer_1,
32        script: script_1,
33    } = to_mint_1;
34
35    let MintToken {
36        to_mint_asset: to_mint_asset_2,
37        redeemer: redeemer_2,
38        script: script_2,
39    } = to_mint_2;
40
41    let mut tx_builder = TxBuilder::new_core();
42    tx_builder
43        .spending_plutus_script_v2()
44        .tx_in(
45            &script_utxo.input.tx_hash,
46            script_utxo.input.output_index,
47            &script_utxo.output.amount,
48            &script_utxo.output.address,
49        )
50        .tx_in_inline_datum_present()
51        // .tx_in_datum_value(datum here) or provide datum value
52        .tx_in_redeemer_value(&WRedeemer {
53            data: WData::JSON(redeemer.to_string()),
54            ex_units: Budget { mem: 0, steps: 0 },
55        })
56        .tx_in_script(&script.script_cbor)
57        .mint_plutus_script_v2()
58        .mint(
59            to_mint_asset_1.quantity_i128(),
60            &to_mint_asset_1.policy(),
61            &to_mint_asset_1.name(),
62        )
63        .mint_redeemer_value(&WRedeemer {
64            data: WData::JSON(redeemer_1.to_string()),
65            ex_units: Budget { mem: 0, steps: 0 },
66        })
67        .minting_script(&script_1.script_cbor)
68        .mint_plutus_script_v2()
69        .mint(
70            to_mint_asset_2.quantity_i128(),
71            &to_mint_asset_2.policy(),
72            &to_mint_asset_2.name(),
73        )
74        .mint_redeemer_value(&WRedeemer {
75            data: WData::JSON(redeemer_2.to_string()),
76            ex_units: Budget { mem: 0, steps: 0 },
77        })
78        .minting_script(&script_2.script_cbor)
79        .change_address(my_address)
80        .tx_in_collateral(
81            &collateral.input.tx_hash,
82            collateral.input.output_index,
83            &collateral.output.amount,
84            &collateral.output.address,
85        )
86        .select_utxos_from(inputs, 5000000)
87        .input_for_evaluation(script_utxo)
88        .complete(None)
89        .await?;
90
91    Ok(tx_builder.tx_hex())
92}