Skip to content

Commit

Permalink
Adjust required-redeemer.plutus to require an inline datum of 42
Browse files Browse the repository at this point in the history
Add documentation for inline datum usage
Implement inline-datum-present cli parser to indicate that an inline
datum is present
  • Loading branch information
Jimbo4350 committed Jun 8, 2022
1 parent 7608488 commit 4817c30
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 16 deletions.
1 change: 1 addition & 0 deletions cardano-api/src/Cardano/Api/Script.hs
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,7 @@ type ScriptRedeemer = ScriptData

data ScriptDatum witctx where
ScriptDatumForTxIn :: ScriptData -> ScriptDatum WitCtxTxIn
InlineScriptDatum :: ScriptDatum WitCtxTxIn
NoScriptDatumForMint :: ScriptDatum WitCtxMint
NoScriptDatumForStake :: ScriptDatum WitCtxStake

Expand Down
12 changes: 10 additions & 2 deletions cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -322,14 +322,22 @@ pScriptRedeemerOrFile scriptFlagPrefix =
pScriptDatumOrFile :: String -> WitCtx witctx -> Parser (ScriptDatumOrFile witctx)
pScriptDatumOrFile scriptFlagPrefix witctx =
case witctx of
WitCtxTxIn -> ScriptDatumOrFileForTxIn <$>
WitCtxTxIn -> (ScriptDatumOrFileForTxIn <$>
pScriptDataOrFile
(scriptFlagPrefix ++ "-datum")
"The script datum, in JSON syntax."
"The script datum, in the given JSON file."
"The script datum, in the given JSON file.") <|>
pInlineDatumPresent scriptFlagPrefix
WitCtxMint -> pure NoScriptDatumOrFileForMint
WitCtxStake -> pure NoScriptDatumOrFileForStake

pInlineDatumPresent :: String -> Parser (ScriptDatumOrFile WitCtxTxIn)
pInlineDatumPresent scriptFlagPrefix =
flag' InlineDatumPresentAtTxIn
( long (scriptFlagPrefix ++ "-inline-datum-present")
<> Opt.help "Inline datum present at transaction input."
)

pScriptDataOrFile :: String -> String -> String -> Parser ScriptDataOrFile
pScriptDataOrFile dataFlagPrefix helpTextForValue helpTextForFile =
pScriptDataCborFile
Expand Down
1 change: 1 addition & 0 deletions cardano-cli/src/Cardano/CLI/Shelley/Run/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,7 @@ readScriptDatumOrFile :: ScriptDatumOrFile witctx
-> ExceptT ShelleyTxCmdError IO (ScriptDatum witctx)
readScriptDatumOrFile (ScriptDatumOrFileForTxIn df) = ScriptDatumForTxIn <$>
readScriptDataOrFile df
readScriptDatumOrFile InlineDatumPresentAtTxIn = pure InlineScriptDatum
readScriptDatumOrFile NoScriptDatumOrFileForMint = pure NoScriptDatumForMint
readScriptDatumOrFile NoScriptDatumOrFileForStake = pure NoScriptDatumForStake

Expand Down
3 changes: 3 additions & 0 deletions cardano-cli/src/Cardano/CLI/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,12 @@ data ScriptWitnessFiles witctx where

deriving instance Show (ScriptWitnessFiles witctx)

-- TODO: Do we want the ability to specify datums for non-spending scripts?
-- Currently we can't (although its not necessary)
data ScriptDatumOrFile witctx where
ScriptDatumOrFileForTxIn :: ScriptDataOrFile
-> ScriptDatumOrFile WitCtxTxIn
InlineDatumPresentAtTxIn :: ScriptDatumOrFile WitCtxTxIn

NoScriptDatumOrFileForMint :: ScriptDatumOrFile WitCtxMint
NoScriptDatumOrFileForStake :: ScriptDatumOrFile WitCtxStake
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,26 @@

A reference script is a script that exists at a particular transaction output. It can be used to witness, for example, a UTxO at the corresponding script address of said reference script. This is useful because the script does not have to be included in the transaction anymore, which significantly reduces the transaction size.

## What is an inline datum?

An inline datum, is a datum that exists at a transaction output. We no longer have to include a datum within our transaction for our plutus spending scripts. Instead we can specify the transaction output where our datum exists to be used in conjunction with our Plutus spending script. This reduces the overall size of our transaction.

### An example of using a Plutus V2 reference script

Below is an example that shows how to use a Plutus spending script. Here we discuss a [shell script example of how to use a reference script to spend a tx input](scripts/plutus/example-reference-script-usage.sh). This is a step-by-step process involving:
Below is an example that shows how to use a Plutus spending script and an inline datum. Here we discuss a [shell script example of how to use a reference script to spend a tx input](scripts/plutus/example-babbage-script-usage.sh). This is a step-by-step process involving:

+ the creation of the `Required Redeemer` Plutus txin script
+ the creation of the `Required Redeemer` Plutus script at a transaction output (creation of the reference script)
+ sending ada and a datum to the Plutus script address
+ the creation of the inline datum at a transaction output
+ sending ada to the Plutus script address
+ spending ada at the Plutus script address using the Plutus reference script

In this example we will use the [Required Redeemer](scripts/plutus/scripts/v2/required-redeemer.plutus) Plutus spending script. In order to execute a reference Plutus spending script, we require the following:

- Collateral tx input(s) - these are provided and are forfeited in the event the Plutus script fails to execute.
- A Plutus tx output with accompanying datum hash. This is the tx output that sits at the Plutus script address. It must have a datum hash, otherwise, it is unspendable.
- A Plutus tx output. This is the tx output that sits at the Plutus script address. Note because we are using an inline datum, it is not mandatory that the utxo at the plutus script address has a datum hash.
- The reference transaction input containing the corresponding Plutus script. We must create the transaction output containing the reference Plutus script.
- An inline datum at a transaction output. The Plutus spending script requires a datum/datum hash.

#### Creating the `Required Redeemer` Plutus spending script

Expand Down Expand Up @@ -48,10 +54,10 @@ cabal install cardano-node
```

To start your babbage cluster, you need to run the `example/run/all.sh` shell script.
The remainder of this guide provides a brief walkthrough of the [shell script example](scripts/plutus/example-reference-script-usage.sh) that automatically creates a reference script and spends the utxo at
The remainder of this guide provides a brief walkthrough of the [shell script example](scripts/plutus/example-babbage-script-usage.sh) that automatically creates a reference script and spends the utxo at
the reference script's corresponding script address.

#### Creating a reference script at a transaction output and
#### Creating a reference script at a transaction output, inline datum and
#### sending ada to the script address (with a datum)

In order to use a reference script, we must first create this script at a particular transaction output.
Expand All @@ -65,36 +71,36 @@ cardano-cli transaction build \
--tx-in "$txin" \
--tx-out "$utxoaddr+$lovelace" \
--tx-out "$plutusscriptaddr+$lovelace" \
--tx-out-datum-hash "$scriptdatumhash" \
--tx-out "$dummyaddress+$lovelaceattxindiv3" \
--tx-out-inline-datum-file "$datumfilepath" \
--tx-out-reference-script-file "$plutusscriptinuse" \
--protocol-params-file "$WORK/pparams.json" \
--out-file "$WORK/create-datum-output.body"
```

The following should be noted about this build command:

Firstly, we are sending ada to the plutus script address along with a datum hash. This is reflected in the following lines:
Firstly, we are sending ada to the plutus script address. Note again we do not need to include a datum hash because we will use an inline datum that sits at a different transaction output. This is reflected in the following lines:

```bash
...
--tx-out "$plutusscriptaddr+$lovelace" \
--tx-out-datum-hash "$scriptdatumhash" \
...
```

We have seen this before in the [plutus-spending-script-example.md](doc/reference/plutus/plutus-spending-script-example.md).

Secondly, we are creating a reference script at a tx output:
Secondly, we are creating a reference script and an inline datum at a tx output:

```bash
...
--tx-out "$dummyaddress+$lovelaceattxindiv3" \
--tx-out-inline-datum-file "$datumfilepath" \
--tx-out-reference-script-file "$plutusscriptinuse" \
...
```

Specifying the `--reference-script-file` after the `--tx-out` option will construct a transaction that creates a reference script at that transaction output.
Specifying the `--reference-script-file` after the `--tx-out` option will construct a transaction that creates a reference script at that transaction output. Likewise when you specify `--tx-out-inline-datum-file` you will also create an inline datum at that transaction output.

We sign and then submit as usual:

Expand All @@ -113,7 +119,7 @@ Now that there is ada at our script address, we must construct the appropriate t
Because we are using the `build` command, we should only note the following:

`$plutusutxotxin` - This is the tx input that sits at the Plutus script address (NB: It has a datum hash).
`tx-in-reference` - This specifies the reference input you are using to witness a transaction input.
`tx-in-reference` - This specifies the reference input you are using to witness a transaction input. In this case the reference input contains both a reference script and an inline datum.
`plutus-script-v2`- This specifies the version of the reference script at the reference input.
`reference-tx-in-datum-file` - This is the datum to be used with the reference script.
`reference-tx-in-redeemer-file` - This is the redeemer to be used with the reference script.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ $CARDANO_CLI transaction build \
--tx-in "$txin" \
--tx-out "$utxoaddr+$lovelaceattxindiv3" \
--tx-out "$plutusscriptaddr+$lovelaceattxindiv3" \
--tx-out-datum-hash "$scriptdatumhash" \
--tx-out-inline-datum-file "$datumfilepath" \
--tx-out "$dummyaddress+$lovelaceattxindiv3" \
--tx-out-reference-script-file "$plutusscriptinuse" \
--protocol-params-file "$WORK/pparams.json" \
Expand Down Expand Up @@ -147,7 +147,7 @@ $CARDANO_CLI transaction build \
--tx-in "$plutuslockedutxotxin" \
--tx-in-reference "$plutusreferencescripttxin" \
--plutus-script-v2 \
--reference-tx-in-datum-file "$datumfilepath" \
--reference-tx-in-inline-datum-present \
--reference-tx-in-redeemer-file "$redeemerfilepath" \
--tx-out "$dummyaddress2+10000000" \
--protocol-params-file "$WORK/pparams.json"
Expand Down
2 changes: 1 addition & 1 deletion scripts/plutus/scripts/v2/required-redeemer.plutus
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"type": "PlutusScriptV2",
"description": "",
"cborHex": "59077e59077b010000323322323232323232323232323232332232323232322223232533532325335333573466e3c00522010001c01b101b101c376600a6666ae68cdc39aab9d5002480008cc8848cc00400c008c8c8c8c8c8c8c8c8c8c8c8c8c8cccd5cd19b8735573aa018900011999999999999111111111110919999999999980080680600580500480400380300280200180119a80a80b1aba1500c33501501635742a01666a02a02e6ae854028ccd54065d7280c1aba150093335501975ca0306ae854020cd4054080d5d0a803999aa80c810bad35742a00c6464646666ae68cdc39aab9d5002480008cc8848cc00400c008c8c8c8cccd5cd19b8735573aa004900011991091980080180119a815bad35742a00460586ae84d5d1280111931901719ab9c02f02e02c135573ca00226ea8004d5d0a8011919191999ab9a3370e6aae754009200023322123300100300233502b75a6ae854008c0b0d5d09aba2500223263202e33573805e05c05826aae7940044dd50009aba135744a004464c6405466ae700ac0a80a04d55cf280089baa00135742a00a66a02aeb8d5d0a802199aa80c80e90009aba150033335501975c40026ae854008c07cd5d09aba2500223263202633573804e04c04826ae8940044d5d1280089aba25001135744a00226ae8940044d5d1280089aba25001135744a00226ae8940044d5d1280089aab9e5001137540026ae854008c03cd5d09aba2500223263201833573803203002c202e264c6402e66ae7124010350543500017135573ca00226ea800448c88c008dd6000990009aa80b111999aab9f0012500a233500930043574200460066ae880080508c8c8cccd5cd19b8735573aa004900011991091980080180118061aba150023005357426ae8940088c98c8050cd5ce00a80a00909aab9e5001137540024646464646666ae68cdc39aab9d5004480008cccc888848cccc00401401000c008c8c8c8cccd5cd19b8735573aa0049000119910919800801801180a9aba1500233500f014357426ae8940088c98c8064cd5ce00d00c80b89aab9e5001137540026ae854010ccd54021d728039aba150033232323333573466e1d4005200423212223002004357426aae79400c8cccd5cd19b875002480088c84888c004010dd71aba135573ca00846666ae68cdc3a801a400042444006464c6403666ae7007006c06406005c4d55cea80089baa00135742a00466a016eb8d5d09aba2500223263201533573802c02a02626ae8940044d5d1280089aab9e500113754002266aa002eb9d6889119118011bab00132001355013223233335573e0044a010466a00e66442466002006004600c6aae754008c014d55cf280118021aba200301213574200222440042442446600200800624464646666ae68cdc3a800a40004642446004006600a6ae84d55cf280191999ab9a3370ea0049001109100091931900819ab9c01101000e00d135573aa00226ea80048c8c8cccd5cd19b875001480188c848888c010014c01cd5d09aab9e500323333573466e1d400920042321222230020053009357426aae7940108cccd5cd19b875003480088c848888c004014c01cd5d09aab9e500523333573466e1d40112000232122223003005375c6ae84d55cf280311931900819ab9c01101000e00d00c00b135573aa00226ea80048c8c8cccd5cd19b8735573aa004900011991091980080180118029aba15002375a6ae84d5d1280111931900619ab9c00d00c00a135573ca00226ea80048c8cccd5cd19b8735573aa002900011bae357426aae7940088c98c8028cd5ce00580500409baa001232323232323333573466e1d4005200c21222222200323333573466e1d4009200a21222222200423333573466e1d400d2008233221222222233001009008375c6ae854014dd69aba135744a00a46666ae68cdc3a8022400c4664424444444660040120106eb8d5d0a8039bae357426ae89401c8cccd5cd19b875005480108cc8848888888cc018024020c030d5d0a8049bae357426ae8940248cccd5cd19b875006480088c848888888c01c020c034d5d09aab9e500b23333573466e1d401d2000232122222223005008300e357426aae7940308c98c804ccd5ce00a00980880800780700680600589aab9d5004135573ca00626aae7940084d55cf280089baa0012323232323333573466e1d400520022333222122333001005004003375a6ae854010dd69aba15003375a6ae84d5d1280191999ab9a3370ea0049000119091180100198041aba135573ca00c464c6401866ae700340300280244d55cea80189aba25001135573ca00226ea80048c8c8cccd5cd19b875001480088c8488c00400cdd71aba135573ca00646666ae68cdc3a8012400046424460040066eb8d5d09aab9e500423263200933573801401200e00c26aae7540044dd500089119191999ab9a3370ea00290021091100091999ab9a3370ea00490011190911180180218031aba135573ca00846666ae68cdc3a801a400042444004464c6401466ae7002c02802001c0184d55cea80089baa0012323333573466e1d40052002200823333573466e1d40092000200823263200633573800e00c00800626aae74dd5000a4c240029210350543100122002122001112323001001223300330020020011"
"cborHex": "59087f59087c01000032323322323322323232323232323232323232323322323232323232322223232533532323253350021001101f32533500121021101f32333553021120013212330012253350022100310010025010253353253335002153335001102221022210222153335002102221333573466ebc00800409008c8408c854ccd400840888408c84cc088008004c034cd540948004dd4240a826a0240022a02200266aa0466aa6038240024646a00244440046a00244002646a0024444444444440186a0044400464a66a6603a00291100101e101f376600a6666ae68cdc39aab9d5002480008cc8848cc00400c008c8c8c8c8c8c8c8c8c8c8c8c8c8cccd5cd19b8735573aa018900011999999999999111111111110919999999999980080680600580500480400380300280200180119a80a80b1aba1500c33501501635742a01666a02a02e6ae854028ccd54069d7280c9aba150093335501a75ca0326ae854020cd4054084d5d0a803999aa80d0113ad35742a00c6464646666ae68cdc39aab9d5002480008cc8848cc00400c008c8c8c8cccd5cd19b8735573aa004900011991091980080180119a8163ad35742a004605a6ae84d5d1280111931901799ab9c03002f02d135573ca00226ea8004d5d0a8011919191999ab9a3370e6aae754009200023322123300100300233502c75a6ae854008c0b4d5d09aba2500223263202f33573806005e05a26aae7940044dd50009aba135744a004464c6405666ae700b00ac0a44d55cf280089baa00135742a00a66a02aeb8d5d0a802199aa80d00f10009aba150033335501a75c40026ae854008c080d5d09aba2500223263202733573805004e04a26ae8940044d5d1280089aba25001135744a00226ae8940044d5d1280089aba25001135744a00226ae8940044d5d1280089aab9e5001137540026ae854008c040d5d09aba2500223263201933573803403202e2030264c6403066ae7124010350543500018135573ca00226ea800448c88c008dd6000990009aa80e911999aab9f0012501e233501d30043574200460066ae880080548c8c8cccd5cd19b8735573aa004900011991091980080180118069aba150023005357426ae8940088c98c8054cd5ce00b00a80989aab9e5001137540024646464646666ae68cdc39aab9d5004480008cccc888848cccc00401401000c008c8c8c8cccd5cd19b8735573aa0049000119910919800801801180b1aba1500233500e015357426ae8940088c98c8068cd5ce00d80d00c09aab9e5001137540026ae854010ccd54025d728041aba150033232323333573466e1d400520042300b357426aae79400c8cccd5cd19b875002480088c84888c004010dd71aba135573ca00846666ae68cdc3a801a400042444006464c6403866ae700740700680640604d55cea80089baa00135742a00466a014eb8d5d09aba2500223263201633573802e02c02826ae8940044d5d1280089aab9e500113754002424446004008266aa002eb9d6889119118011bab00132001355019223233335573e0044a036466a03466442466002006004600c6aae754008c014d55cf280118021aba200301213574200224464646666ae68cdc3a800a400046a00e600a6ae84d55cf280191999ab9a3370ea00490011280391931900919ab9c01301201000f135573aa00226ea800448488c00800c44880048c8c8cccd5cd19b875001480188c848888c010014c01cd5d09aab9e500323333573466e1d400920042321222230020053009357426aae7940108cccd5cd19b875003480088c848888c004014c01cd5d09aab9e500523333573466e1d40112000232122223003005375c6ae84d55cf280311931900819ab9c01101000e00d00c00b135573aa00226ea80048c8c8cccd5cd19b8735573aa004900011991091980080180118029aba15002375a6ae84d5d1280111931900619ab9c00d00c00a135573ca00226ea80048c8cccd5cd19b8735573aa002900011bae357426aae7940088c98c8028cd5ce00580500409baa001232323232323333573466e1d4005200c21222222200323333573466e1d4009200a21222222200423333573466e1d400d2008233221222222233001009008375c6ae854014dd69aba135744a00a46666ae68cdc3a8022400c4664424444444660040120106eb8d5d0a8039bae357426ae89401c8cccd5cd19b875005480108cc8848888888cc018024020c030d5d0a8049bae357426ae8940248cccd5cd19b875006480088c848888888c01c020c034d5d09aab9e500b23333573466e1d401d2000232122222223005008300e357426aae7940308c98c804ccd5ce00a00980880800780700680600589aab9d5004135573ca00626aae7940084d55cf280089baa0012323232323333573466e1d400520022333222122333001005004003375a6ae854010dd69aba15003375a6ae84d5d1280191999ab9a3370ea0049000119091180100198041aba135573ca00c464c6401866ae700340300280244d55cea80189aba25001135573ca00226ea80048c8c8cccd5cd19b875001480088c8488c00400cdd71aba135573ca00646666ae68cdc3a8012400046424460040066eb8d5d09aab9e500423263200933573801401200e00c26aae7540044dd500089119191999ab9a3370ea00290021091100091999ab9a3370ea00490011190911180180218031aba135573ca00846666ae68cdc3a801a400042444004464c6401466ae7002c02802001c0184d55cea80089baa0012323333573466e1d40052002200a23333573466e1d40092000200a23263200633573800e00c00800626aae74dd5000a4c24002920103505431003200135500822112253350011500a22133500b3004002335530061200100400122333573466e3c00800401000c488008488004c8004d5401088448894cd40044d400c88004884ccd401488008c010008ccd54c01c480040140100044488008488488cc00401000c4488c008004448c8c00400488cc00cc0080080041"
}

0 comments on commit 4817c30

Please sign in to comment.