<p>Although there may have been changes, this remains a valid question.<br />
The code shown in the question is the boilerplate code that accompanies a validator implementation for on-chain execution. A simple validator implementation can be seen below. It is the recent 42 redeemer validator code of Pioneer (before switch from <code>Data</code> to <code>BuildinData</code>).</p>
<pre><code>-- This is the validator implementation
-- It must be inlinable so it can be compiled into Plutus code
{-# INLINABLE mkValidator #-}
mkValidator :: Data -&gt; Data -&gt; Data -&gt; ()
mkValidator _ r _
    | r == I 42 = ()
    | otherwise = traceError &quot;wrong redeemer&quot;

-- boilerplate: compiles validator to Plutus code
validator :: Validator
validator = mkValidatorScript $$(PlutusTx.compile [|| mkValidator ||])

-- boilerplate: get the hash of the validator script
valHash :: Ledger.ValidatorHash
valHash = Scripts.validatorHash validator

-- boilerplate: get the script address of the validator
scrAddress :: Ledger.Address
scrAddress = scriptAddress validator
</code></pre>
<p>The first code block is the validator implementation in Haskell, here named <code>mkValidator</code>.</p>
<p>The actual question refers to the next three code blocks marked as boilerplate.<br />
<code>validator</code> inlines the <code>mkValidator</code> implementation and compiles it to Plutus code. This is necessary so the validator can be executed on-chain.<br />
<code>valHash</code> and <code>scrAddress</code> are auxiliary functions that return hash and address of the the validator script.<br />
One can see them used in the off-chain code to construct the transactions.</p>
<p>The reason for this roundabout way of implementing the validator is to enable code sharing between on-chain and off-chain executed code.</p>
<p>More complex validator implementations will result in a more complex Plutus code compiler code block, but the principle remains the same.</p>

CC BY-SA 4.0
2021-08-07T19:48:22.023Z
2021-08-07T19:48:22.023Z
vcwebco
<p>I need someone to explain to me line by line. The code is from plutu tutorials. <a href="https://docs.cardano.org/projects/plutus/en/latest/plutus/tutorials/basic-validators.html" rel="noreferrer">https://docs.cardano.org/projects/plutus/en/latest/plutus/tutorials/basic-validators.html</a></p>
<p>All the code in that page I can understand. Except the last one. I don't really get it. the code that I don't understand:</p>
<pre><code>dateInstance :: ScriptInstance DateValidator
dateInstance = validator @DateValidator
    -- The first argument is the compiled validator.
    $$(compile [|| validateDateTyped ||])
    -- The second argument is a compiled wrapper.
    -- Unfortunately we can't just inline wrapValidator here for technical reasons.
    $$(compile [|| wrap ||])
    where
        wrap = wrapValidator

dateValidatorHash :: ValidatorHash
dateValidatorHash = scriptHash dateInstance

dateValidator :: Validator
dateValidator = validatorScript dateInstance
</code></pre>

CC BY-SA 4.0
2021-06-08T08:16:06.357Z
Tbh I'm not sure that this example is up to date (from my understanding we don't need to compute ValidatorHash anymore). I would suggest you check Plutus Pioneer lectures on Youtube and plutus repository with example on GitHub (https://github.com/input-output-hk/plutus/tree/master/plutus-use-cases/src/Plutus/Contracts)
Yanis Urbis
<plutus><smart-contract><plutus-playground><plutustx>
Writing basic validator scripts
# Writing basic validator scripts

(Asked by: username_0 on 2021-06-08)

I need someone to explain to me line by line. The code is from plutu tutorials. <https://docs.cardano.org/projects/plutus/en/latest/plutus/tutorials/basic-validators.html>

All the code in that page I can understand. Except the last one. I don't really get it. the code that I don't understand:

```
dateInstance :: ScriptInstance DateValidator
dateInstance = validator @DateValidator
    -- The first argument is the compiled validator.
    $$(compile [|| validateDateTyped ||])
    -- The second argument is a compiled wrapper.
    -- Unfortunately we can't just inline wrapValidator here for technical reasons.
    $$(compile [|| wrap ||])
    where
        wrap = wrapValidator

dateValidatorHash :: ValidatorHash
dateValidatorHash = scriptHash dateInstance

dateValidator :: Validator
dateValidator = validatorScript dateInstance

```

**Tags:** plutus, smart-contract, plutus-playground, plutustx

> **Comments:**
> **username_1**: Tbh I'm not sure that this example is up to date (from my understanding we don't need to compute ValidatorHash anymore). I would suggest you check Plutus Pioneer lectures on Youtube and plutus repository with example on GitHub (https://github.com/input-output-hk/plutus/tree/master/plutus-use-cases/src/Plutus/Contracts)  

---

## 1 answers

### Answer by username_2 on 2021-08-07. Score: 2

Although there may have been changes, this remains a valid question.  
The code shown in the question is the boilerplate code that accompanies a validator implementation for on-chain execution. A simple validator implementation can be seen below. It is the recent 42 redeemer validator code of Pioneer (before switch from `Data` to `BuildinData`).

```
-- This is the validator implementation
-- It must be inlinable so it can be compiled into Plutus code
{-# INLINABLE mkValidator #-}
mkValidator :: Data -> Data -> Data -> ()
mkValidator _ r _
    | r == I 42 = ()
    | otherwise = traceError "wrong redeemer"

-- boilerplate: compiles validator to Plutus code
validator :: Validator
validator = mkValidatorScript $$(PlutusTx.compile [|| mkValidator ||])

-- boilerplate: get the hash of the validator script
valHash :: Ledger.ValidatorHash
valHash = Scripts.validatorHash validator

-- boilerplate: get the script address of the validator
scrAddress :: Ledger.Address
scrAddress = scriptAddress validator

```

The first code block is the validator implementation in Haskell, here named `mkValidator`.

The actual question refers to the next three code blocks marked as boilerplate.  
`validator` inlines the `mkValidator` implementation and compiles it to Plutus code. This is necessary so the validator can be executed on-chain.  
`valHash` and `scrAddress` are auxiliary functions that return hash and address of the the validator script.  
One can see them used in the off-chain code to construct the transactions.

The reason for this roundabout way of implementing the validator is to enable code sharing between on-chain and off-chain executed code.

More complex validator implementations will result in a more complex Plutus code compiler code block, but the principle remains the same.


---

