Skip to content

Commit 937b400

Browse files
committed
Use env vars
1 parent 9da291b commit 937b400

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

docs/docs/how-to/commit-script-utxo.md

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,16 @@ You will also need:
2222
- `cardano-cli` in your `PATH`.
2323
- `hydra-node` and `hydra-tui` in your `PATH`.
2424

25-
## Step 1: Create the script
25+
## Step 1: Set Up Your Environment
26+
27+
To avoid specifying the network identifier and the path to the node's socket in every command, you can set the following environment variables in your shell. Make sure to replace the values with the ones that are appropriate for your setup (e.g., for the pre-production testnet, the magic is `1`).
28+
29+
```shell
30+
export CARDANO_NODE_SOCKET_PATH=<path-to>/node.socket
31+
export CARDANO_TESTNET_MAGIC=1
32+
```
33+
34+
## Step 2: Create the script
2635

2736
For this tutorial, we will use a simple "always true" validator script. This script will always succeed, regardless of the redeemer or datum.
2837

@@ -36,14 +45,14 @@ Create a file named `always-true.plutus` with the following content:
3645
}
3746
```
3847

39-
## Step 2: Create the script address
48+
## Step 3: Create the script address
4049

4150
Now, we need to create an address for this script. We can do this using `cardano-cli`:
4251

4352
```shell
4453
cardano-cli address build \
4554
--payment-script-file always-true.plutus \
46-
--testnet-magic 1 \
55+
--testnet-magic $CARDANO_TESTNET_MAGIC \
4756
--out-file script.addr
4857
```
4958

@@ -53,17 +62,16 @@ This will create a file named `script.addr` containing the script address. You c
5362
cat script.addr
5463
```
5564

56-
## Step 3: Lock funds in the script address
65+
## Step 4: Lock funds in the script address
5766

5867
Before we can commit a script UTxO, we need to create one. This is done by sending funds to the script address.
5968

6069
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 will use `alice-funds.sk`:
6170

6271
```shell
6372
cardano-cli query utxo \
64-
--address $(cardano-cli address build --payment-verification-key-file hydra-cluster/config/credentials/alice-funds.vk --testnet-magic 1) \
65-
--testnet-magic 1 \
66-
--socket-path testnets/preprod/node.socket
73+
--address $(cardano-cli address build --payment-verification-key-file hydra-cluster/config/credentials/alice-funds.vk --testnet-magic $CARDANO_TESTNET_MAGIC) \
74+
--testnet-magic $CARDANO_TESTNET_MAGIC
6775
```
6876

6977
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 `<UTXO_TXIX>` containing `100 ADA`. We will send `10 ADA` to the script address.
@@ -88,9 +96,8 @@ cardano-cli conway transaction build \
8896
--tx-in <UTXO_TXIX> \
8997
--tx-out $(cat script.addr)+10000000 \
9098
--tx-out-inline-datum-file datum.json \
91-
--change-address $(cardano-cli address build --payment-verification-key-file hydra-cluster/config/credentials/alice-funds.vk --testnet-magic 1) \
92-
--testnet-magic 1 \
93-
--socket-path testnets/preprod/node.socket \
99+
--change-address $(cardano-cli address build --payment-verification-key-file hydra-cluster/config/credentials/alice-funds.vk --testnet-magic $CARDANO_TESTNET_MAGIC) \
100+
--testnet-magic $CARDANO_TESTNET_MAGIC \
94101
--out-file tx.raw
95102
```
96103

@@ -106,20 +113,18 @@ cardano-cli conway transaction sign \
106113

107114
cardano-cli conway transaction submit \
108115
--tx-file tx.signed \
109-
--testnet-magic 1 \
110-
--socket-path testnets/preprod/node.socket
116+
--testnet-magic $CARDANO_TESTNET_MAGIC
111117
```
112118

113119
Once the transaction is confirmed, you can query the script address to see the newly created UTxO:
114120

115121
```shell
116122
cardano-cli query utxo \
117123
--address $(cat script.addr) \
118-
--testnet-magic 1 \
119-
--socket-path testnets/preprod/node.socket
124+
--testnet-magic $CARDANO_TESTNET_MAGIC
120125
```
121126

122-
## Step 4: Prepare the commit
127+
## Step 5: Prepare the commit
123128

124129
Now we are ready to prepare the commit. We will 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 is just a blueprint that we will send to the `hydra-node` to get a properly drafted commit transaction.
125130

@@ -134,14 +139,14 @@ cardano-cli conway transaction build-raw \
134139
--tx-in-inline-datum-present \
135140
--tx-in-redeemer-value 42 \
136141
--tx-in-execution-units '(0, 0)' \
137-
--tx-out $(cardano-cli address build --payment-verification-key-file hydra-cluster/config/credentials/alice-funds.vk --testnet-magic 1)+10000000 \
142+
--tx-out $(cardano-cli address build --payment-verification-key-file hydra-cluster/config/credentials/alice-funds.vk --testnet-magic $CARDANO_TESTNET_MAGIC)+10000000 \
138143
--fee 0 \
139144
--out-file tx.json
140145
```
141146

142147
A real-world script, like one written in [Aiken](https://aiken-lang.org/), would use the datum to carry state and the redeemer to provide input for validation. Our "always-true" script doesn't actually check any of these, but they are still required fields for a valid transaction that spends a script UTxO. Note that we use `--tx-in-inline-datum-present` because the datum was already included on-chain when we created the script UTxO. We also provide `--tx-in-execution-units`. This is required for any script spend to tell the network how much computational resource to budget for the script's execution. Since our script does nothing, we can use `(0, 0)`.
143148

144-
## Step 5: Commit the script UTxO
149+
## Step 6: Commit the script UTxO
145150

146151
This final step is very similar to the standard commit tutorial. We will start a `hydra-node`, initialize a Head, and then use the blueprint transaction to get a commit transaction from the `hydra-node`.
147152

@@ -157,7 +162,7 @@ You can use the following script to query the necessary information and generate
157162
# Set variables
158163
SCRIPT_UTXO_TXIX="<SCRIPT_UTXO_TXIX>"
159164
BLUEPRINT_JSON=$(cat tx.json)
160-
UTXO_JSON=$(cardano-cli query utxo --tx-in ${SCRIPT_UTXO_TXIX} --testnet-magic 1 --output-json)
165+
UTXO_JSON=$(cardano-cli query utxo --tx-in ${SCRIPT_UTXO_TXIX} --testnet-magic $CARDANO_TESTNET_MAGIC --output-json)
161166

162167
# Create the request body
163168
jq -n \
@@ -185,8 +190,7 @@ cardano-cli conway transaction sign \
185190

186191
cardano-cli conway transaction submit \
187192
--tx-file signed-tx.json \
188-
--socket-path testnets/preprod/node.socket \
189-
--testnet-magic 1
193+
--testnet-magic $CARDANO_TESTNET_MAGIC
190194
```
191195

192196
And that's it! After the transaction is confirmed on-chain, your script UTxO will be committed to the Head.

0 commit comments

Comments
 (0)