|
| 1 | +--- |
| 2 | +sidebar_position: 2 |
| 3 | +--- |
| 4 | + |
| 5 | +# Commit a Script UTxO into a Head |
| 6 | + |
| 7 | +This guide provides a walkthrough on how to commit a UTxO from a script into a Hydra Head. |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +This tutorial assumes you are familiar with the process of committing a standard UTxO to a Hydra Head. If you are not, please first read the [Commit using a blueprint](./commit-blueprint.md) tutorial. |
| 12 | + |
| 13 | +You will also need: |
| 14 | + |
| 15 | +- A running `cardano-node` connected to a testnet. |
| 16 | +- `cardano-cli` in your `PATH`. |
| 17 | +- `hydra-node` and `hydra-tui` in your `PATH`. |
| 18 | + |
| 19 | +## Step 1: Create the script |
| 20 | + |
| 21 | +For this tutorial, we'll use a simple "always true" validator script. This script will always succeed, regardless of the redeemer or datum. |
| 22 | + |
| 23 | +Create a file named `always-true.plutus` with the following content: |
| 24 | + |
| 25 | +```json |
| 26 | +{ |
| 27 | + "type": "PlutusScriptV2", |
| 28 | + "description": "Always true validator", |
| 29 | + "cborHex": "49480100002221200101" |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +## Step 2: Create the script address |
| 34 | + |
| 35 | +Now, we need to create an address for this script. We can do this using `cardano-cli`: |
| 36 | + |
| 37 | +```shell |
| 38 | +cardano-cli address build \ |
| 39 | + --payment-script-file always-true.plutus \ |
| 40 | + --testnet-magic 1 \ |
| 41 | + --out-file script.addr |
| 42 | +``` |
| 43 | + |
| 44 | +This will create a file named `script.addr` containing the script address. You can inspect the address using `cat`: |
| 45 | + |
| 46 | +```shell |
| 47 | +cat script.addr |
| 48 | +``` |
| 49 | + |
| 50 | +## Step 3: Lock funds in the script address |
| 51 | + |
| 52 | +Before we can commit a script UTxO, we need to create one. This is done by sending funds to the script address. |
| 53 | + |
| 54 | +First, find a UTxO in your wallet that you can use. You can query your wallet's UTxOs using `cardano-cli`. For this example, we'll use `alice-funds.sk`: |
| 55 | + |
| 56 | +```shell |
| 57 | +cardano-cli query utxo \ |
| 58 | + --address $(cardano-cli address build --payment-verification-key-file hydra-cluster/config/credentials/alice-funds.vk --testnet-magic 1) \ |
| 59 | + --testnet-magic 1 \ |
| 60 | + --socket-path testnets/preprod/node.socket |
| 61 | +``` |
| 62 | + |
| 63 | +Pick a UTxO from the output and use it to build a transaction that sends funds to the script address. Let's say you picked a UTxO with TxHash `a5a11c..` and index `0` containing `100 ADA`. We'll send `10 ADA` to the script address: |
| 64 | + |
| 65 | +```shell |
| 66 | +cardano-cli conway transaction build \ |
| 67 | + --tx-in <UTXO_TXIX> \ |
| 68 | + --tx-out $(cat script.addr)+10000000 \ |
| 69 | + --tx-out-datum-embed-value 42 \ |
| 70 | + --change-address $(cardano-cli address build --payment-verification-key-file hydra-cluster/config/credentials/alice-funds.vk --testnet-magic 1) \ |
| 71 | + --testnet-magic 1 \ |
| 72 | + --socket-path testnets/preprod/node.socket \ |
| 73 | + --out-file tx.raw |
| 74 | +``` |
| 75 | +Note that we are also providing a datum value `42`. Even though our script does not use it, a datum is required for all script UTxOs. |
| 76 | + |
| 77 | +Now, sign and submit the transaction: |
| 78 | + |
| 79 | +```shell |
| 80 | +cardano-cli conway transaction sign \ |
| 81 | + --tx-body-file tx.raw \ |
| 82 | + --signing-key-file hydra-cluster/config/credentials/alice-funds.sk \ |
| 83 | + --out-file tx.signed |
| 84 | + |
| 85 | +cardano-cli conway transaction submit \ |
| 86 | + --tx-file tx.signed \ |
| 87 | + --testnet-magic 1 \ |
| 88 | + --socket-path testnets/preprod/node.socket |
| 89 | +``` |
| 90 | + |
| 91 | +Once the transaction is confirmed, you can query the script address to see the newly created UTxO: |
| 92 | + |
| 93 | +```shell |
| 94 | +cardano-cli query utxo \ |
| 95 | + --address $(cat script.addr) \ |
| 96 | + --testnet-magic 1 \ |
| 97 | + --socket-path testnets/preprod/node.socket |
| 98 | +``` |
| 99 | + |
| 100 | +## Step 4: Prepare the commit |
| 101 | + |
| 102 | +Now we are ready to prepare the commit. We'll create a blueprint transaction that spends the script UTxO. Note that this transaction is not meant to be signed and submitted to the Cardano network. It's just a blueprint that we'll send to the `hydra-node` to get a properly drafted commit transaction. |
| 103 | + |
| 104 | +In this transaction, we'll spend the script UTxO and send the funds to our own wallet address. |
| 105 | + |
| 106 | +```shell |
| 107 | +cardano-cli conway transaction build-raw \ |
| 108 | + --tx-in <SCRIPT_UTXO_TXIX> \ |
| 109 | + --tx-in-script-file always-true.plutus \ |
| 110 | + --tx-in-datum-value 42 \ |
| 111 | + --tx-in-reedeemer-value 42 \ |
| 112 | + --tx-out $(cardano-cli address build --payment-verification-key-file hydra-cluster/config/credentials/alice-funds.vk --testnet-magic 1)+10000000 \ |
| 113 | + --fee 0 \ |
| 114 | + --out-file tx.json |
| 115 | +``` |
| 116 | +As you can see, we are providing the script file, the datum, and a redeemer. Our "always-true" script doesn't actually check any of these, but they are required for a valid transaction spending a script UTxO. |
| 117 | + |
| 118 | +## Step 5: Commit the script UTxO |
| 119 | + |
| 120 | +This final step is very similar to the standard commit tutorial. We'll start a `hydra-node`, initialize a Head, and then use the blueprint transaction to get a commit transaction from the `hydra-node`. |
| 121 | + |
| 122 | +First, start the `hydra-node` and `hydra-tui` as explained in the [Commit using a blueprint](./commit-blueprint.md#step-5) tutorial and initialize a Head. |
| 123 | + |
| 124 | +Once the Head is in the `Initializing` state, you can send the HTTP request to the `/commit` API path. |
| 125 | + |
| 126 | +Just like in the other tutorial, you need to assemble a JSON request body. The `blueprintTx` is the `tx.json` file we just created, and the `utxo` is the script UTxO you are committing. |
| 127 | + |
| 128 | +```json |
| 129 | +{ |
| 130 | + "blueprintTx": { |
| 131 | + "type": "Tx BabbageEra", |
| 132 | + "description": "", |
| 133 | + "cborHex": "84a40081825820a5a11c227e92622321b7d526e0b7ade44c084f706e931e80829037cba55365f5000181825839000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f021a000f42400300a0f5f6" |
| 134 | + }, |
| 135 | + "utxo": { |
| 136 | + "<SCRIPT_UTXO_TXIX>": { |
| 137 | + "address": "<SCRIPT_ADDRESS>", |
| 138 | + "datum": "42", |
| 139 | + "datumhash": null, |
| 140 | + "inlineDatum": true, |
| 141 | + "referenceScript": null, |
| 142 | + "value": { |
| 143 | + "lovelace": 10000000 |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +Save this to a `commit-request.json` file and use `curl` to send it to the `hydra-node`: |
| 151 | + |
| 152 | +```shell |
| 153 | +curl -X POST 127.0.0.1:4001/commit \ |
| 154 | + --data @commit-request.json |
| 155 | +``` |
| 156 | + |
| 157 | +This will give you the commit transaction, which you can then sign and submit as usual: |
| 158 | + |
| 159 | +```shell |
| 160 | +cardano-cli conway transaction sign \ |
| 161 | + --tx-file commit-tx.json \ |
| 162 | + --signing-key-file hydra-cluster/config/credentials/alice-funds.sk \ |
| 163 | + --out-file signed-tx.json |
| 164 | + |
| 165 | +cardano-cli conway transaction submit \ |
| 166 | + --tx-file signed-tx.json \ |
| 167 | + --socket-path testnets/preprod/node.socket \ |
| 168 | + --testnet-magic 1 |
| 169 | +``` |
| 170 | + |
| 171 | +And that's it! After the transaction is confirmed on-chain, your script UTxO will be committed to the Head. |
0 commit comments