3

The homework for Week 2 involves implementing validators for two Plutus contracts. Details are here.

Spoiler alert! This probably gives away the answer to Week 2's first homework problem:

-- ...

module Week02.Homework1 where

-- ...

-- This should validate if and only if the two Booleans in the redeemer are equal!
mkValidator :: () -> (Bool, Bool) -> ScriptContext -> Bool
mkValidator _ r _ = traceIfFalse "Invalid redeemer" $ fst r == snd r

data Typed
instance Scripts.ValidatorTypes Typed where
    type instance DatumType Typed = ()
    type instance RedeemerType Typed = (Bool, Bool)

typedValidator :: Scripts.TypedValidator Typed
typedValidator = Scripts.mkTypedValidator @Typed
    $$(PlutusTx.compile [|| mkValidator ||])
    $$(PlutusTx.compile [|| wrap ||])
  where
    wrap = Scripts.wrapValidator @() @(Bool, Bool)

validator :: Validator
validator = Scripts.validatorScript typedValidator

--
-- HERE!
--
-- This appears superfluous
--
valHash :: Ledger.ValidatorHash
valHash = undefined -- FIX ME!
--valHash = Scripts.validatorHash typedValidator

scrAddress :: Ledger.Address
scrAddress = scriptAddress validator

-- ...

mkSchemaDefinitions ''GiftSchema

mkKnownCurrencies []

As marked superfluous above...

My Plutus Playground simulations execute correctly whether this line is defined or not:

valHash = undefined -- FIX ME!

This is also true of the same function definition in the second homework assignment for Week 2.

1
  • Should one of the answers be marked as accepted?
    – gRebel
    Apr 28 at 5:29

6 Answers 6

1

the ValidatorHash represents the hash of the serialized Script of your smart contract.

you can think at the ValidatorHash as being the equivalent of PubKeyHash used for people's wallets, but for smart contracts.

it is not needed to deploy the smart contract since the serialized Script is needed for that and not its hash.

it is instead useful to use to check if a smart contract is signing a transaction.

just like in the minting policy that allows only one wallet to mint some tokens by checking the PubKeyHash to be correct. The same logic could be applied in order to only allow a smart contract to mint, but in this case you'll need the ValidatorHash since a smart contract can't have PubKeys.

N.B.

your code compiles even if valHash is defined as

valHash = undefined

thanks to the laziness of haskell

since you never need to use the valHash in this specific example, then you never evaluate it, then there is no reason not to compile

0

I think it's required only when deploying the smart contract on-chain and the redeemer provides the validator.

1
  • Sounds plausible. I'm hoping the question will get picked up for next week's Q/A. I'll mark it correct if confirmed. Jan 21 at 21:19
0

The valHash expression was added when Lars was writing Gift.hs. He added it and then did some inspections on it and scrAddress in the REPL. This was done for demonstration purposes as those two expressions are related to each other and they are important to understand when writing smart contracts.

After inspecting it in the REPL, he could have deleted valHash as it doesn't serve any purpose in any of lecture 2's scripts.

The reason you see it in Homework1.hs and Homework2.hs is because all of the scripts during week 2 are based off of the original Gift.hs.

0

valHash seems to be used by a function on the off-chain code (mustPayToOtherScript inside the give function; I am looking at the Burn.hs file). Therefore, I believe it is working here as the script address itself (at the end, the script address is composed of the script hash if I am not mistaken). So, it uses valHash to pass the address of the script to the off-chain code so it knows with which script you are trying to create a transaction.

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jan 22 at 10:58
0

Lars explains this in the week 2 video 2 lecture from 30:48 to 31:40 that "this is just boilerplate." https://www.youtube.com/watch?v=xgnmMl-eIIM&list=PLNEK_Ejlx3x0mhPmOjPSHZPtTFpfJo3Nd&index=2 Basically, he's having you take a standard toolbox to a job that in this case needs only a screwdriver. You're bringing along the hammer and wrench and pliers because that is the standard package of how you would normally do things.

3
  • Answer could be improved by following How to Answer and How to reference material written by others guidelines.
    – gRebel
    Jan 23 at 0:27
  • Answer is difficult to understand, especially if English is not your native language. It should be just the facts.
    – gRebel
    Jan 23 at 0:29
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jan 23 at 0:30
-1

It is not needed here because the Datum is (). If you recall, the Datum is passed the hash of the script. In this example however, the valHash is significant since the example datum is simply ().

1
  • the validator hash is not related to the Datum; the Datum should represent the state of the smart contract and is updated with each transaction using the off-chain code; the validator hash is something fixed, in particoular is the hash of the serialized script
    – Michele
    Jun 21 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.