HPM Education - Plutus
  • Introduction
  • The EUTxO Model
    • Introduction to the EUTxO model
      • The UTxO (Unspent Transaction Output) model
      • The EUTxO model (Extended UTxO) model
  • The Plutus Environment
    • What is Plutus?
    • System requirements for this course
    • Setting up your Plutus development environment
    • Cabal project files
  • Writing the first Plutus scripts
    • General Notes
    • Cabal setup
    • The Simplest Script
    • A Guessing Game Script
    • Exploring Script Context
  • Typed validators
    • Overview
    • A Shared Wallet Script
  • Parameterised validators
    • Overview
    • A Deadline Script
    • Another Shared Wallet Script
  • Stake Validators
    • Overview
    • A Stake Validator Script
  • Minting Policies
    • Overview
    • A Pay-to-Store Policy
  • Plutus Emulator
    • Emulating the blockchain
    • Testing a validator with the emulator
  • References
    • References
Powered by GitBook
On this page
  1. Parameterised validators

Overview

PreviousA Shared Wallet ScriptNextA Deadline Script

Last updated 1 year ago

So far, we have been using the Datum, Redeemer and Context fields to express the validation logic of our scripts. However, there is a very valuable method of creating validators that do not rely only on those three fields. Recall our where we had to put the public key hashes inside the datum to sit at the script UTxO. Imagine that instead of those hashes being specified in the datum, and then having the script look at the datum, we could instead bake in those public key hashes inside the script itself. In that case, the script would no longer need to look at the datum to determine whether a transaction is valid or not. Instead, it would already have that information inside the script itself. These types of validators are referred to as parameterised validators.

It is important to note that, as always, validator scripts are compiled down to the untyped form of BuiltinData -> BuiltinData -> BuiltinData -> (), even with parameterised validators. However, in this case, we can write our logic in a different type signature that allows us to write the validators in a more high-level way. The type signature of the mkValidator function in parameterised validators is:

ValidatorParam(s) -> DatumType -> RedeemerType -> ScriptContext -> Bool.

The new argument ValidatorParam allows us to bake in any data we want into the script itself. We can define it with as many fields as we want, but as before, unless we use types that are already instances of ToData typeclass, we have to instantiate them and additionally, with parameter types, we have to lift them. Lifting values or types means compiling the Haskell code to Plutus IR (intermediate representation, which is later compiled into Plutus Core) at runtime. The result of a lifted Integer for example is CompiledCode Integer.

As with , we can still define our own Datum and Redeemer types. And we have to create the ValidatorTypes instance to use with our validator as before.

shared wallet validator
typed validators