From 5f5812f0b7f82ecf00ec6468ecd3ca06151666c5 Mon Sep 17 00:00:00 2001 From: Claudio Maradonna Date: Wed, 28 Jul 2021 14:18:58 +0200 Subject: [PATCH] Homeworks week 2,3 --- .gitignore | 1 + code/week02/src/Week02/Homework1.hs | 17 +++++++++++------ code/week02/src/Week02/Homework2.hs | 21 +++++++++++++-------- code/week03/src/Week03/Homework1.hs | 17 ++++++++++++++++- code/week03/src/Week03/Homework2.hs | 24 +++++++++++++++++++----- 5 files changed, 60 insertions(+), 20 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..722d5e7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode diff --git a/code/week02/src/Week02/Homework1.hs b/code/week02/src/Week02/Homework1.hs index d5a3acf..a8c0aa3 100644 --- a/code/week02/src/Week02/Homework1.hs +++ b/code/week02/src/Week02/Homework1.hs @@ -32,23 +32,28 @@ import Text.Printf (printf) {-# INLINABLE mkValidator #-} -- This should validate if and only if the two Booleans in the redeemer are equal! mkValidator :: () -> (Bool, Bool) -> ScriptContext -> Bool -mkValidator _ _ _ = True -- FIX ME! +mkValidator _ (a, b) _ = traceIfFalse "redeemers doesnt match!" $ a == b data Typed instance Scripts.ValidatorTypes Typed where --- Implement the instance! + type instance DatumType Typed = () + type instance RedeemerType Typed = (Bool, Bool) typedValidator :: Scripts.TypedValidator Typed -typedValidator = undefined -- FIX ME! +typedValidator = Scripts.mkTypedValidator @Typed + $$(PlutusTx.compile [|| mkValidator ||]) + $$(PlutusTx.compile [|| wrap ||]) + where + wrap = Scripts.wrapValidator @() @(Bool, Bool) validator :: Validator -validator = undefined -- FIX ME! +validator = Scripts.validatorScript typedValidator valHash :: Ledger.ValidatorHash -valHash = undefined -- FIX ME! +valHash = Scripts.validatorHash typedValidator scrAddress :: Ledger.Address -scrAddress = undefined -- FIX ME! +scrAddress = scriptAddress validator type GiftSchema = Endpoint "give" Integer diff --git a/code/week02/src/Week02/Homework2.hs b/code/week02/src/Week02/Homework2.hs index 454376b..24b8ff0 100644 --- a/code/week02/src/Week02/Homework2.hs +++ b/code/week02/src/Week02/Homework2.hs @@ -43,23 +43,28 @@ PlutusTx.unstableMakeIsData ''MyRedeemer {-# INLINABLE mkValidator #-} -- This should validate if and only if the two Booleans in the redeemer are equal! mkValidator :: () -> MyRedeemer -> ScriptContext -> Bool -mkValidator _ _ _ = True -- FIX ME! - -data Typed +mkValidator _ (MyRedeemer r) _ = traceIfFalse "Flags dont match" $ r.flag1 == r.flag2 +s +dat Typed instance Scripts.ValidatorTypes Typed where - -- Implement me! + type instance DatumType Typed = () + type instance RedeemerType Typed = MyRedeemer typedValidator :: Scripts.TypedValidator Typed -typedValidator = undefined -- FIX ME! +typedValidator = Scripts.mkTypedValidator @Typed -- FIX ME! + $$(PlutusTx.compile [|| mkValidator ||]) + $$(PlutusTx.compile [|| wrap ||]) + where + wrap = Scripts.wrapValidator @() @MyRedeemer validator :: Validator -validator = undefined -- FIX ME! +validator = Scripts.validatorScript typedValidator -- FIX ME! valHash :: Ledger.ValidatorHash -valHash = undefined -- FIX ME! +valHash = Scripts.validatorHash typedValidator -- FIX ME! scrAddress :: Ledger.Address -scrAddress = undefined -- FIX ME! +scrAddress = scriptAddress validator -- FIX ME! type GiftSchema = Endpoint "give" Integer diff --git a/code/week03/src/Week03/Homework1.hs b/code/week03/src/Week03/Homework1.hs index 5b9b0a9..cb786ad 100644 --- a/code/week03/src/Week03/Homework1.hs +++ b/code/week03/src/Week03/Homework1.hs @@ -46,7 +46,22 @@ PlutusTx.unstableMakeIsData ''VestingDatum -- This should validate if either beneficiary1 has signed the transaction and the current slot is before or at the deadline -- or if beneficiary2 has signed the transaction and the deadline has passed. mkValidator :: VestingDatum -> () -> ScriptContext -> Bool -mkValidator _ _ _ = False -- FIX ME! +mkValidator dat () ctx = (traceIfFalse "beneficiary 1 not signed" signedBeneficiary1 && + traceIfFalse "beneficiary 1 deadline expired" (not $ deadlineExpired)) || + (traceIfFalse "beneficiary 2 not signed" signedBeneficiary2 && + traceIfFalse "beneficiary 2 deadline not reached yet" deadlineExpired) + where + info :: TxInfo + info = scriptContextTxInfo ctx + + signedBeneficiary1 :: Bool + signedBeneficiary1 = txSignedBy info $ beneficiary1 dat + + signedBeneficiary2 :: Bool + signedBeneficiary2 = txSignedBy info $ beneficiary2 dat + + deadlineExpired :: Bool + deadlineExpired = contains (from $ deadline dat) $ txInfoValidRange info data Vesting instance Scripts.ValidatorTypes Vesting where diff --git a/code/week03/src/Week03/Homework2.hs b/code/week03/src/Week03/Homework2.hs index 06d4a2d..3ace17c 100644 --- a/code/week03/src/Week03/Homework2.hs +++ b/code/week03/src/Week03/Homework2.hs @@ -36,7 +36,17 @@ import Text.Printf (printf) {-# INLINABLE mkValidator #-} mkValidator :: PubKeyHash -> POSIXTime -> () -> ScriptContext -> Bool -mkValidator _ _ _ _ = False -- FIX ME! +mkValidator pkey dline () ctx = traceIfFalse "beneficiary signature missing" signedByBeneficiary && + traceIfFalse "deadline not reached" deadlineReached + where + info :: TxInfo + info = scriptContextTxInfo ctx + + signedByBeneficiary :: Bool + signedByBeneficiary = txSignedBy info $ pkey + + deadlineReached :: Bool + deadlineReached = contains (from $ dline) $ txInfoValidRange info data Vesting instance Scripts.ValidatorTypes Vesting where @@ -44,13 +54,17 @@ instance Scripts.ValidatorTypes Vesting where type instance RedeemerType Vesting = () typedValidator :: PubKeyHash -> Scripts.TypedValidator Vesting -typedValidator = undefined -- IMPLEMENT ME! +typedValidator p = Scripts.mkTypedValidator @Vesting + ($$(PlutusTx.compile [|| mkValidator ||]) `PlutusTx.applyCode` PlutusTx.liftCode p) + $$(PlutusTx.compile [|| wrap ||]) + where + wrap = Scripts.wrapValidator @POSIXTime @() validator :: PubKeyHash -> Validator -validator = undefined -- IMPLEMENT ME! +validator = Scripts.validatorScript . typedValidator scrAddress :: PubKeyHash -> Ledger.Address -scrAddress = undefined -- IMPLEMENT ME! +scrAddress = scriptAddress . validator data GiveParams = GiveParams { gpBeneficiary :: !PubKeyHash @@ -102,7 +116,7 @@ grab = do Just d -> d <= now endpoints :: Contract () VestingSchema Text () -endpoints = (give' `select` grab') >> endpoints +endpoints = (give' + grab') >> endpoints where give' = endpoint @"give" >>= give grab' = endpoint @"grab" >> grab