|
| 1 | +# Plutus Minting Scripts |
| 2 | + |
| 3 | +## What is a Plutus minting script? |
| 4 | + |
| 5 | +This is a type of Plutus script that is required to validate the minting of multi-asset tokens. We can do this using Mary era scripts, however, Plutus scripts allow us to encode more logic beyond requiring verification keys and timelocks. The Plutus minting script expects only a redeemer in order to successfully validate the minting of a multi-asset. |
| 6 | + |
| 7 | +### An example of using a Plutus minting script |
| 8 | + |
| 9 | +Below is an example that shows how to use a Plutus minting script. This is a step-by-step |
| 10 | +process involving: |
| 11 | + |
| 12 | ++ the creation of the `AlwaysSucceeds` Plutus minting script (i.e. anybody can mint) |
| 13 | ++ the creation of a transaction that mints multi-assets using the `AlwaysSucceeds` Plutus minting script |
| 14 | + |
| 15 | +In this example we will use the [anyone can mint](../../../plutus-example/plutus-example/src/Cardano/PlutusExample/MintingScript.hs) Plutus minting script. In order to execute a Plutus minting script, we require the following: |
| 16 | + |
| 17 | +- Collateral tx input(s) - these are provided and are forfeited in the event the Plutus script fails to execute. |
| 18 | +- The Plutus script should be serialized in the text envelope format. `cardano-cli` expects Plutus scripts to be serialized in the text envelope format. |
| 19 | +- A redeemer. |
| 20 | + |
| 21 | +#### Creating the `AlwaysSucceeds` Plutus minting script |
| 22 | + |
| 23 | +The plutus-example executable will automagically generate several Plutus scripts in the CLI-compatible text envelope format. |
| 24 | + |
| 25 | +Run the following commands: |
| 26 | + |
| 27 | +```bash |
| 28 | +cd plutus-example |
| 29 | + |
| 30 | +cabal run exe:plutus-example |
| 31 | +``` |
| 32 | + |
| 33 | +This will output `anyone-can-mint.plutus` in the `generated-plutus-scripts` dir. |
| 34 | + |
| 35 | +#### Setting up a local Alonzo node cluster |
| 36 | + |
| 37 | +There is a convenient script that will set up an Alonzo cluster immediately on your local machine. |
| 38 | + |
| 39 | +Run the following command: |
| 40 | + |
| 41 | +```bash |
| 42 | +cabal install cardano-cli |
| 43 | +cabal install cardano-node |
| 44 | +./scripts/byron-to-alonzo/mkfiles.sh alonzo |
| 45 | +``` |
| 46 | + |
| 47 | +Follow the instructions displayed in the terminal to start your Alonzo cluster. |
| 48 | + |
| 49 | +#### Minting multi-assets using the Plutus minting script |
| 50 | + |
| 51 | +We need the policy ID of our Plutus script and a redeemer. In this case, we can use any redeemer because the minting script always succeeds: |
| 52 | + |
| 53 | +```bash |
| 54 | +> cardano-cli transaction policyid --script-file scripts/plutus/scripts/anyone-can-mint.plutus |
| 55 | +> fda1b6b487bee2e7f64ecf24d24b1224342484c0195ee1b7b943db50 |
| 56 | +``` |
| 57 | + |
| 58 | +There is an example redeemer at: `scripts/plutus/data/42.redeemer` |
| 59 | + |
| 60 | +For more information regarding `tx-in-collateral` and `mint-execution-units` see [here](plutus-spending-script-example.md). |
| 61 | + |
| 62 | +```bash |
| 63 | +cardano-cli transaction build-raw \ |
| 64 | + --alonzo-era \ |
| 65 | + --fee "$txfee" \ |
| 66 | + --tx-in "$txin" \ |
| 67 | + --tx-in-collateral "$txinCollateral" \ |
| 68 | + --mint-script-file "scripts/plutus/scripts/anyone-can-mint.plutus" \ |
| 69 | + --mint-redeemer-file "scripts/plutus/data/42.redeemer" \ |
| 70 | + --mint-execution-units "($plutusrequiredspace, $plutusrequiredtime)" \ |
| 71 | + --tx-out "$dummyaddress+$spendable + 5 2dce00a8d52ccd0c53be5165dd7a7e8e1d08d87f05f8f91047ca5d0b.4D696C6C6172436F696E0A" \ |
| 72 | + --mint "5 2dce00a8d52ccd0c53be5165dd7a7e8e1d08d87f05f8f91047ca5d0b.4D696C6C6172436F696E0A" \ |
| 73 | + --protocol-params-file pparams.json \ |
| 74 | + --out-file "plutusmint.body" |
| 75 | + |
| 76 | +cardano-cli transaction sign \ |
| 77 | + --tx-body-file "plutusmint.body" \ |
| 78 | + --testnet-magic 42 \ |
| 79 | + --signing-key-file "$skey" \ |
| 80 | + --out-file "plutusmint.tx" |
| 81 | +``` |
| 82 | + |
| 83 | +You can use the [simple-minting-policy.sh](../../../scripts/plutus/simple-minting-policy.sh) in conjunction with the [mkfiles.sh alonzo](../../../scripts/byron-to-alonzo/mkfiles.sh) script to automagically run the `AlwaysSucceeds` minting script. |
| 84 | + |
| 85 | + |
0 commit comments