Skip to content

Commit 511c60c

Browse files
committed
New cardano-testnet:plutus-examples library component.
1 parent e9600ef commit 511c60c

15 files changed

+1066
-0
lines changed

cardano-testnet/cardano-testnet.cabal

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ extra-doc-files: CHANGELOG.md
1717

1818
common project-config
1919
default-language: Haskell2010
20+
default-extensions: ImportQualifiedPost
2021
build-depends: base >= 4.14 && < 5
2122

2223
ghc-options: -Wall
@@ -109,7 +110,51 @@ library
109110

110111
autogen-modules: Paths_cardano_testnet
111112

113+
library plutus-example
114+
import: project-config
115+
116+
hs-source-dirs: plutus-example
117+
118+
ghc-options: -Wno-unused-packages
119+
120+
default-extensions: ViewPatterns
121+
122+
build-depends: aeson
123+
, bytestring
124+
, cardano-api
125+
, cardano-cli
126+
, cardano-ledger-alonzo
127+
, cardano-ledger-babbage
128+
, cardano-ledger-core
129+
, cardano-ledger-shelley
130+
, cardano-slotting
131+
, cardano-strict-containers
132+
, containers
133+
, ouroboros-consensus
134+
, plutus-core
135+
, plutus-ledger-api >= 1.21.0.0
136+
, plutus-script-utils >= 1.3.0.0
137+
, plutus-tx >= 1.21.0.0
138+
, plutus-tx-plugin >= 1.21.0.0
139+
, serialise
140+
, text
141+
, transformers
142+
, transformers-except
112143

144+
exposed-modules: Cardano.Testnet.PlutusExample.AlwaysFails
145+
Cardano.Testnet.PlutusExample.AlwaysSucceeds
146+
Cardano.Testnet.PlutusExample.PlutusVersion1.CustomDatumRedeemerGuess
147+
Cardano.Testnet.PlutusExample.PlutusVersion1.DatumRedeemerGuess
148+
Cardano.Testnet.PlutusExample.PlutusVersion1.Loop
149+
Cardano.Testnet.PlutusExample.PlutusVersion1.MintingScript
150+
Cardano.Testnet.PlutusExample.PlutusVersion1.RedeemerContextScripts
151+
Cardano.Testnet.PlutusExample.PlutusVersion1.Sum
152+
Cardano.Testnet.PlutusExample.PlutusVersion2.EcdsaSecp256k1Loop
153+
Cardano.Testnet.PlutusExample.PlutusVersion2.MintingScript
154+
Cardano.Testnet.PlutusExample.PlutusVersion2.RedeemerContextEquivalence
155+
Cardano.Testnet.PlutusExample.PlutusVersion2.RequireRedeemer
156+
Cardano.Testnet.PlutusExample.PlutusVersion2.SchnorrSecp256k1Loop
157+
Cardano.Testnet.PlutusExample.PlutusVersion2.StakeScript
113158

114159
executable cardano-testnet
115160
import: project-config
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE NoImplicitPrelude #-}
3+
{-# LANGUAGE TemplateHaskell #-}
4+
5+
module Cardano.Testnet.PlutusExample.AlwaysFails
6+
( alwaysFailsScript
7+
, alwaysFailsScriptShortBs
8+
) where
9+
10+
import qualified Cardano.Api.Shelley as Api
11+
12+
import Prelude hiding (($))
13+
14+
import Codec.Serialise
15+
import qualified Data.ByteString.Lazy as LBS
16+
import qualified Data.ByteString.Short as SBS
17+
18+
import qualified Plutus.Script.Utils.Scripts as Plutus
19+
import qualified PlutusTx
20+
import PlutusTx.Prelude hiding (Semigroup (..), unless, (.))
21+
22+
{-# INLINABLE mkValidator #-}
23+
mkValidator :: BuiltinData -> BuiltinData -> BuiltinData -> ()
24+
mkValidator _ _ _ = PlutusTx.Prelude.error ()
25+
26+
validator :: Plutus.Validator
27+
validator = Plutus.mkValidatorScript $$(PlutusTx.compile [|| mkValidator ||])
28+
29+
script :: Plutus.Script
30+
script = Plutus.unValidatorScript validator
31+
32+
alwaysFailsScriptShortBs :: SBS.ShortByteString
33+
alwaysFailsScriptShortBs = SBS.toShort . LBS.toStrict $ serialise script
34+
35+
alwaysFailsScript :: Api.PlutusScript lang
36+
alwaysFailsScript = Api.PlutusScriptSerialised alwaysFailsScriptShortBs
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE NoImplicitPrelude #-}
3+
{-# LANGUAGE TemplateHaskell #-}
4+
5+
module Cardano.Testnet.PlutusExample.AlwaysSucceeds
6+
( alwaysSucceedsScript
7+
, alwaysSucceedsScriptShortBs
8+
) where
9+
10+
import qualified Cardano.Api.Shelley as Api
11+
12+
import Prelude hiding (($))
13+
14+
import Codec.Serialise
15+
import qualified Data.ByteString.Lazy as LBS
16+
import qualified Data.ByteString.Short as SBS
17+
18+
import qualified Plutus.Script.Utils.Scripts as Plutus
19+
import qualified PlutusTx
20+
import PlutusTx.Prelude hiding (Semigroup (..), unless, (.))
21+
22+
{-# INLINABLE mkValidator #-}
23+
mkValidator :: BuiltinData -> BuiltinData -> BuiltinData -> ()
24+
mkValidator _ _ _ = ()
25+
26+
validator :: Plutus.Validator
27+
validator = Plutus.mkValidatorScript $$(PlutusTx.compile [|| mkValidator ||])
28+
29+
script :: Plutus.Script
30+
script = Plutus.unValidatorScript validator
31+
32+
alwaysSucceedsScriptShortBs :: SBS.ShortByteString
33+
alwaysSucceedsScriptShortBs = SBS.toShort . LBS.toStrict $ serialise script
34+
35+
alwaysSucceedsScript :: Api.PlutusScript lang
36+
alwaysSucceedsScript = Api.PlutusScriptSerialised alwaysSucceedsScriptShortBs
37+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE NoImplicitPrelude #-}
3+
{-# LANGUAGE TemplateHaskell #-}
4+
{-# LANGUAGE TypeApplications #-}
5+
{-# LANGUAGE TypeFamilies #-}
6+
7+
module Cardano.Testnet.PlutusExample.PlutusVersion1.CustomDatumRedeemerGuess
8+
( MyCustomDatum(..)
9+
, MyCustomRedeemer(..)
10+
, customGuessScript
11+
, customDatumRedeemerGuessScriptAsShortBs
12+
) where
13+
14+
import Cardano.Api.Shelley (PlutusScript (..), PlutusScriptV1)
15+
16+
import qualified PlutusLedgerApi.V1.Contexts as V1
17+
18+
import Prelude hiding (($), (&&), (==))
19+
20+
import Codec.Serialise
21+
import qualified Data.ByteString.Lazy as LBS
22+
import qualified Data.ByteString.Short as SBS
23+
24+
import qualified Plutus.Script.Utils.Scripts as Plutus
25+
import qualified Plutus.Script.Utils.Typed as Scripts
26+
import qualified PlutusTx
27+
import PlutusTx.Prelude hiding (Semigroup ((<>)), unless, (.))
28+
29+
newtype MyCustomDatum = MyCustomDatum Integer
30+
newtype MyCustomRedeemer = MyCustomRedeemer Integer
31+
32+
PlutusTx.unstableMakeIsData ''MyCustomDatum
33+
PlutusTx.unstableMakeIsData ''MyCustomRedeemer
34+
35+
{-# INLINABLE mkValidator #-}
36+
mkValidator :: MyCustomDatum -> MyCustomRedeemer -> V1.ScriptContext -> Bool
37+
mkValidator (MyCustomDatum d) (MyCustomRedeemer r) _ =
38+
d == 42 && r == 42
39+
40+
validator :: Plutus.Validator
41+
validator = Plutus.mkValidatorScript
42+
$$(PlutusTx.compile [|| wrap ||])
43+
where
44+
wrap = Scripts.mkUntypedValidator mkValidator
45+
46+
script :: Plutus.Script
47+
script = Plutus.unValidatorScript validator
48+
49+
customDatumRedeemerGuessScriptAsShortBs :: SBS.ShortByteString
50+
customDatumRedeemerGuessScriptAsShortBs = SBS.toShort . LBS.toStrict $ serialise script
51+
52+
customGuessScript :: PlutusScript PlutusScriptV1
53+
customGuessScript = PlutusScriptSerialised customDatumRedeemerGuessScriptAsShortBs
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE NoImplicitPrelude #-}
3+
{-# LANGUAGE OverloadedStrings #-}
4+
{-# LANGUAGE TemplateHaskell #-}
5+
{-# LANGUAGE TypeApplications #-}
6+
{-# LANGUAGE TypeFamilies #-}
7+
8+
module Cardano.Testnet.PlutusExample.PlutusVersion1.DatumRedeemerGuess
9+
( guessScript
10+
, guessScriptStake
11+
, datumRedeemerGuessScriptShortBs
12+
) where
13+
14+
import Cardano.Api.Shelley (PlutusScript (..), PlutusScriptV1)
15+
16+
import Prelude hiding (($), (&&), (==))
17+
18+
import Codec.Serialise
19+
import qualified Data.ByteString.Lazy as LBS
20+
import qualified Data.ByteString.Short as SBS
21+
22+
import qualified Plutus.Script.Utils.Scripts as Plutus
23+
import PlutusTx (toBuiltinData)
24+
import qualified PlutusTx
25+
import PlutusTx.Prelude hiding (Semigroup (..), unless, (.))
26+
27+
{-# INLINABLE mkValidator #-}
28+
mkValidator :: BuiltinData -> BuiltinData -> BuiltinData -> ()
29+
mkValidator datum redeemer _txContext
30+
| datum == toBuiltinData (42 :: Integer)
31+
&& redeemer == toBuiltinData (42 :: Integer) = ()
32+
| otherwise = traceError "Incorrect datum. Expected 42."
33+
34+
validator :: Plutus.Validator
35+
validator = Plutus.mkValidatorScript $$(PlutusTx.compile [|| mkValidator ||])
36+
37+
script :: Plutus.Script
38+
script = Plutus.unValidatorScript validator
39+
40+
datumRedeemerGuessScriptShortBs :: SBS.ShortByteString
41+
datumRedeemerGuessScriptShortBs = SBS.toShort . LBS.toStrict $ serialise script
42+
43+
guessScript :: PlutusScript PlutusScriptV1
44+
guessScript = PlutusScriptSerialised datumRedeemerGuessScriptShortBs
45+
46+
{-# INLINEABLE mkValidatorStake #-}
47+
mkValidatorStake :: BuiltinData -> BuiltinData -> ()
48+
mkValidatorStake redeemer _txContext
49+
| redeemer == toBuiltinData (42 :: Integer) = ()
50+
| otherwise = traceError "Incorrect datum. Expected 42."
51+
52+
validatorStake :: Plutus.StakeValidator
53+
validatorStake = Plutus.mkStakeValidatorScript $$(PlutusTx.compile [||mkValidatorStake||])
54+
55+
scriptStake :: Plutus.Script
56+
scriptStake = Plutus.unStakeValidatorScript validatorStake
57+
58+
datumRedeemerGuessScriptStakeShortBs :: SBS.ShortByteString
59+
datumRedeemerGuessScriptStakeShortBs = SBS.toShort . LBS.toStrict $ serialise scriptStake
60+
61+
guessScriptStake :: PlutusScript PlutusScriptV1
62+
guessScriptStake = PlutusScriptSerialised datumRedeemerGuessScriptStakeShortBs
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE NoImplicitPrelude #-}
3+
{-# LANGUAGE OverloadedStrings #-}
4+
{-# LANGUAGE TemplateHaskell #-}
5+
{-# LANGUAGE TypeApplications #-}
6+
{-# LANGUAGE TypeFamilies #-}
7+
8+
module Cardano.Testnet.PlutusExample.PlutusVersion1.Loop
9+
( loopScript
10+
, loopScriptShortBs
11+
) where
12+
13+
import Cardano.Api.Shelley (PlutusScript (..), PlutusScriptV1)
14+
15+
import Prelude hiding (pred, ($), (&&), (<), (==))
16+
17+
import Codec.Serialise
18+
import qualified Data.ByteString.Lazy as LBS
19+
import qualified Data.ByteString.Short as SBS
20+
21+
import qualified Plutus.Script.Utils.Scripts as Plutus
22+
import PlutusTx
23+
import PlutusTx.Builtins (unsafeDataAsI)
24+
import PlutusTx.Prelude hiding (Semigroup (..), unless, (.))
25+
26+
{-# INLINABLE mkValidator #-}
27+
mkValidator :: BuiltinData -> BuiltinData -> BuiltinData -> ()
28+
mkValidator _datum redeemer _txContext
29+
= if n < 1000000
30+
then traceError "redeemer is < 1000000"
31+
else loop n
32+
where
33+
n = unsafeDataAsI redeemer
34+
loop i = if i == 1000000 then () else loop $ pred i
35+
36+
validator :: Plutus.Validator
37+
validator = Plutus.mkValidatorScript $$(PlutusTx.compile [|| mkValidator ||])
38+
39+
script :: Plutus.Script
40+
script = Plutus.unValidatorScript validator
41+
42+
loopScriptShortBs :: SBS.ShortByteString
43+
loopScriptShortBs = SBS.toShort . LBS.toStrict $ serialise script
44+
45+
loopScript :: PlutusScript PlutusScriptV1
46+
loopScript = PlutusScriptSerialised loopScriptShortBs
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE NoImplicitPrelude #-}
3+
{-# LANGUAGE TemplateHaskell #-}
4+
{-# LANGUAGE TypeApplications #-}
5+
{-# LANGUAGE TypeFamilies #-}
6+
7+
module Cardano.Testnet.PlutusExample.PlutusVersion1.MintingScript
8+
( apiExamplePlutusMintingScript
9+
, mintingScriptShortBs
10+
) where
11+
12+
import Cardano.Api.Shelley (PlutusScript (..), PlutusScriptV1)
13+
14+
import qualified PlutusLedgerApi.V1 as V1
15+
16+
import Prelude hiding (($))
17+
18+
import Codec.Serialise
19+
import qualified Data.ByteString.Lazy as LB
20+
import qualified Data.ByteString.Short as SBS
21+
22+
import qualified Plutus.Script.Utils.Scripts as Plutus
23+
import qualified Plutus.Script.Utils.Typed as Scripts
24+
import qualified PlutusTx
25+
import PlutusTx.Prelude hiding (Semigroup (..), unless, (.))
26+
27+
{- HLINT ignore "Avoid lambda" -}
28+
29+
{-# INLINABLE mkPolicy #-}
30+
mkPolicy :: BuiltinData -> V1.ScriptContext -> Bool
31+
mkPolicy _redeemer _ctx = True
32+
33+
policy :: Plutus.MintingPolicy
34+
policy = Plutus.mkMintingPolicyScript $$(PlutusTx.compile [|| wrap ||])
35+
where
36+
wrap = Scripts.mkUntypedMintingPolicy mkPolicy
37+
38+
plutusScript :: Plutus.Script
39+
plutusScript =
40+
Plutus.unMintingPolicyScript policy
41+
42+
validator :: Plutus.Validator
43+
validator =
44+
Plutus.Validator $ Plutus.unMintingPolicyScript policy
45+
46+
scriptAsCbor :: LB.ByteString
47+
scriptAsCbor = serialise validator
48+
49+
apiExamplePlutusMintingScript :: PlutusScript PlutusScriptV1
50+
apiExamplePlutusMintingScript = PlutusScriptSerialised . SBS.toShort $ LB.toStrict scriptAsCbor
51+
52+
mintingScriptShortBs :: SBS.ShortByteString
53+
mintingScriptShortBs = SBS.toShort . LB.toStrict $ scriptAsCbor

0 commit comments

Comments
 (0)