Skip to content

Commit

Permalink
Add trivial example Plutus scripts to the API
Browse files Browse the repository at this point in the history
And also as concrete data files in the repo.

These scripts always succeed, or always fail, so are only useful for
quick sanity tests.
  • Loading branch information
dcoutts committed Jun 2, 2021
1 parent 631da09 commit 7d6e8a6
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cardano-api/src/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ module Cardano.Api (

-- ** Plutus scripts
PlutusScript,
examplePlutusScriptAlwaysSucceeds,
examplePlutusScriptAlwaysFails,

-- ** Script data
ScriptData(..),
Expand Down
47 changes: 47 additions & 0 deletions cardano-api/src/Cardano/Api/Script.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ module Cardano.Api.Script (

-- * The Plutus script language
PlutusScript(..),
examplePlutusScriptAlwaysSucceeds,
examplePlutusScriptAlwaysFails,

-- * Script data
ScriptData(..),
Expand Down Expand Up @@ -105,6 +107,7 @@ import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import Data.Type.Equality (TestEquality (..), (:~:) (Refl))
import Data.Typeable (Typeable)
import Numeric.Natural (Natural)

import Data.Aeson (Value (..), object, (.:), (.=))
import qualified Data.Aeson as Aeson
Expand Down Expand Up @@ -137,6 +140,7 @@ import qualified Cardano.Ledger.Alonzo.Language as Alonzo
import qualified Cardano.Ledger.Alonzo.Scripts as Alonzo

import qualified Plutus.V1.Ledger.Api as Plutus
import qualified Plutus.V1.Ledger.Examples as Plutus

import Cardano.Api.Eras
import Cardano.Api.HasTypeProxy
Expand Down Expand Up @@ -1009,6 +1013,49 @@ instance (IsPlutusScriptLanguage lang, Typeable lang) =>
PlutusScriptV1 -> "PlutusScriptV1"


-- | An example Plutus script that always succeeds, irrespective of inputs.
--
-- For example, if one were to use this for a payment address then it would
-- allow anyone to spend from it.
--
-- The exact script depends on the context in which it is to be used.
--
examplePlutusScriptAlwaysSucceeds :: WitCtx witctx
-> PlutusScript PlutusScriptV1
examplePlutusScriptAlwaysSucceeds =
PlutusScriptSerialised
. Plutus.alwaysSucceedingNAryFunction
. scriptArityForWitCtx

-- | An example Plutus script that always fails, irrespective of inputs.
--
-- For example, if one were to use this for a payment address then it would
-- be impossible for anyone to ever spend from it.
--
-- The exact script depends on the context in which it is to be used.
--
examplePlutusScriptAlwaysFails :: WitCtx witctx
-> PlutusScript PlutusScriptV1
examplePlutusScriptAlwaysFails =
PlutusScriptSerialised
. Plutus.alwaysFailingNAryFunction
. scriptArityForWitCtx

-- | The expected arity of the Plutus function, depending on the context in
-- which it is used.
--
-- The script inputs consist of
--
-- * the optional datum (for txins)
-- * the redeemer
-- * the Plutus representation of the tx and environment
--
scriptArityForWitCtx :: WitCtx witctx -> Natural
scriptArityForWitCtx WitCtxTxIn = 3
scriptArityForWitCtx WitCtxMint = 2
scriptArityForWitCtx WitCtxStake = 2


-- ----------------------------------------------------------------------------
-- Conversion functions
--
Expand Down
5 changes: 5 additions & 0 deletions scripts/plutus/always-fails-mint.plutus
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "PlutusScriptV1",
"description": "An example Plutus script (for minting context) that always fails",
"cborHex": "4701000020020061"
}
5 changes: 5 additions & 0 deletions scripts/plutus/always-fails-stake.plutus
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "PlutusScriptV1",
"description": "An example Plutus script (for stake context) that always fails",
"cborHex": "4701000020020061"
}
5 changes: 5 additions & 0 deletions scripts/plutus/always-fails-txin.plutus
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "PlutusScriptV1",
"description": "An example Plutus script (for tx in context) that always fails",
"cborHex": "49010000200200200601"
}
5 changes: 5 additions & 0 deletions scripts/plutus/always-succeeds-mint.plutus
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "PlutusScriptV1",
"description": "An example Plutus script (for minting context) that always succeeds",
"cborHex": "4a01000020020020000101"
}
5 changes: 5 additions & 0 deletions scripts/plutus/always-succeeds-stake.plutus
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "PlutusScriptV1",
"description": "An example Plutus script (for stake context) that always succeeds",
"cborHex": "4a01000020020020000101"
}
5 changes: 5 additions & 0 deletions scripts/plutus/always-succeeds-txin.plutus
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "PlutusScriptV1",
"description": "An example Plutus script (for tx in context) that always succeeds",
"cborHex": "4b0100002002002002000011"
}

0 comments on commit 7d6e8a6

Please sign in to comment.