For anything to happen on the blockhain, it has to be externally triggered. For example, in order to finish an auction, a transaction that will close it has to be submitted.
- Each Cardano epoch consists of a number of slots, where each slot lasts for one second.
- Block is a slot that contains a set of recent transactions on the network.
- Slots that are inhabited by blocks are called active slots
data Data =
Constr Integer [Data]
| Map [(Data, Data)]
| List [Data]
| I Integer
| B BS.ByteString
deriving stock (Show, Eq, Ord, Generic)
deriving anyclass (NFData)
Low-level type for Plutus Core script, analogous to JSON. See [[Haskell Primer#Data types]] and [[Haskell Primer#Typeclasses]].
Data
is defined inPlutusCore.Data
module (re-exported fromPlutusTx
module).
mkValidator :: Data -> Data -> Data -> ()
mkValidator _ _ _ = ()
The most simple Validator that will always pass the validation. Any token amount at this script address can be consumed by anyone.
mkValidator
function declaration and definition, see [[Haskell Primer#Functions]].- The parameters are Datum, Redeemer and Context (transaction info).
- The return type is [[Haskell Primer#Unit]].
- The arguments are ignored with [[Haskell Primer#Wildcard]].
validator :: Validator
validator = mkValidatorScript $$(PlutusTx.compile [|| mkValidator ||])
Transform the Haskell expression to the Plutus expression, this relies on [[Haskell Primer#Template Haskell]].
mkValidatorScript
is declared inPlutus.V1.Ledger.Scripts
module.mkValidatorScript :: CompiledCode (Data -> Data -> Data -> ()) -> Validator
.
[|| mkValidator ||]
- quote themkValidator
expression to get the abstract syntax tree (AST) of it.PlutusTx.compile
- compile the Haskell AST of themkValidator
and produce the Plutus Core AST.$$(...)
- splice the Plutus Core AST to the Plutus Core expression.mkValidatorScript
- transform the Plutus Core expression ofmkValidator
toValidator
.
{-# INLINABLE mkValidator #-}`
This pragma makes the mkValidator
function inlineable for [|| ... ||]
. See [[Haskell Primer#Pragmas]].
valHash :: Ledger.ValidatorHash
valHash = Scripts.validatorHash validator
Transform Validator
(Plutus Core script) into a validator hash represented by ValidatorHash
type.
validatorHash
is declared inPlutus.V1.Ledger.Scripts
module.validatorHash :: Validator -> ValidatorHash
scrAddress :: Ledger.Address
scrAddress = scriptAddress validator
Transform Validator
into an actual blockchain address represented by Address
type.
scriptAddress
is declared inPlutus.V1.Ledger.Address
module.scriptAddress :: Validator -> Address
mkValidator :: Data -> Data -> Data -> ()
mkValidator _ _ _ = error()
Such Validator will always fail validation.
error
is declared inPlutusTx.Builtins
module.error :: () -> a
- wherea
is any type, see [[Haskell Primer#Type variables]]
{-# LANGUAGE NoImplicitPrelude #-}
This pragma excludes Haskell Prelude
from being auto-imported to avoid name collisions with PlutusTx.Prelude
.
mkValidator :: Data -> Data -> Data -> ()
mkValidator _ _ _ = traceError "BURNT!"
This Validator will always fail with the specified error message.
traceError
is declared inPlutusTx.Prelude
module.traceError :: Builtins.BuiltinString -> a
{-# LANGUAGE OverloadedStrings #-}
This pragma allows to produce Plutus strings from string literals.
mkValidator :: Data -> Data -> Data -> ()
mkValidator _ r _
| r == I 42 = ()
| otherwise = traceError "wrong redeemer"
This Validator will pass validation only if Redeemer is 42
.
- [[Haskell Primer#Guards]] are used to test the redeemer argument.
r == I 42
- construct a newData
value asInteger
of42
and check whether it matches the RedeemerData
value.
mkValidator :: () -> Integer -> ScriptContext -> Bool
mkValidator = _ r _ = traceIfFalse "wrong redeemer" $ r == 42
This Validator requires specific types of Datum, Redeemer, and Context. It will also log the message, if Redeemer is not 42
.
traceIfFalse
is declared inPlutusTx.Prelude
module.traceIfFalse :: Builtins.BuiltinString -> Bool -> Bool
.- This function returns the given
Bool
argument, and logs the specified string, if it isFalse
.
$ r == 42
- is equivalent to(r == 42)
, see [[Haskell Primer#Dollar operator]].
data Typed
instance Scripts.ValidatorTypes Typed where
type instance DatumType Typed = ()
type instance RedeemerType Typed = Integer
Define a dummy data type Type
and make it an instance of ValidatorTypes
typeclass. See [[Haskell Primer#Data types]] and [[Haskell Primer#Typeclasses]].
ValidatorTypes
is declared inLedger.Typed.Scripts.Validators
module.
class ValidatorTypes (a :: Type) where
type RedeemerType a :: Type
type DatumType a :: Type
type instance DatumType Typed = ()
- define the type synonym for Datum as()
type instance RedeemerType Typed = Integer
- define the type synonym for Redeemer asInteger
typedValidator :: Scripts.TypedValidator Typed
typedValidator = Scripts.mkTypedValidator @Typed
$$(PlutusTx.compile [|| mkValidator ||])
$$(PlutusTx.compile [|| wrap ||])
where
wrap = Scripts.wrapValidator @() @Integer
Transform the Haskell type validator expression to the Plutus typed validator expression.
TypedValidator (a :: Type)
type is declared inLedger.Typed.Scripts.Validators
module.mkTypedValidator
function is also declared inLedger.Typed.Scripts.Validators
module.mkTypedValidator :: CompiledCode (ValidatorType a) -> CompiledCode (ValidatorType a -> WrappedValidatorType) -> TypedValidator a
typedValidator :: Scripts.TypedValidator Typed
- declaretypedValidator
function withTypedValidator Typed
return type (whereTyped
is the type argument forTypedValidator
).typedValidator = Scripts.mkTypedValidator @Typed
- giveTyped
as the type argument tomkTypedValidator
function.$$(PlutusTx.compile [|| mkValidator ||])
- splice the Haskell expression to the Plutus expression.$$(PlutusTx.compile [|| wrap ||])
- splice the typed expression to the untyped one using thewrap
function definition.wrap = Scripts.wrapValidator @() @Integer
- definewrap
function aswrapValidator
function, which transformsData
arguments to the typed arguments of Datum and Redeemer -()
andInteger
respectively.
validator :: Validator
validator = Scripts.validatorScript typedValidator
Transform the typed validator to the untyped one.
validatorScript
is declared inLedger.Typed.Scripts.Validators
module.validatorScript :: TypedValidator a -> Scripts.Validator
valHash :: Ledger.ValidatorHash
valHash = Scripts.validatorHash typeValidator
Transform TypedValidator Typed
(Plutus Core script) into a validator hash represented by ValidatorHash
type.
- The typed version of
validatorHash
is declared inLedger.Typed.Scripts.Validators
module.validatorHash :: TypedValidator a -> Scripts.ValidatorHash
Conversion between Data
and specific types is achieved by implementing an instance of IsData
typeclass declared in PlutusTx.IsData.Class
module.
class IsData (a :: Type) where
toData :: a -> Data
fromData :: Data -> Maybe a
For some types like Data
, Integer
, ByteString
, [a]
, Void
the instances are already predefined.
newtype MySillyRedeemer = MySillyRedeemer Integer
Define a simple data type MySillyRedeemer
.
PlutusTx.unstableMakeIsData ''MySillyRedeemer
At compile time, implementation of IsData
typeclass instance for MySillyRedeemer
will be generated.
unstableMakeIsData
is [[Haskell Primer#Template Haskell]] function declared inPlutusTx.IsData.TH
module.unstableMakeIsData :: TH.Name -> TH.Q [TH.Dec]
''MySillyRedeemer
- quoteMySillyRedeemer
type asTH.Name