Homeworks week 2,3

This commit is contained in:
Claudio Maradonna 2021-07-28 14:18:58 +02:00
parent 03e6e4ca9d
commit 5f5812f0b7
Signed by untrusted user who does not match committer: claudiomaradonna
GPG key ID: 0CBA58694C5680D9
5 changed files with 60 additions and 20 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
.vscode

View file

@ -32,23 +32,28 @@ import Text.Printf (printf)
{-# INLINABLE mkValidator #-} {-# INLINABLE mkValidator #-}
-- This should validate if and only if the two Booleans in the redeemer are equal! -- This should validate if and only if the two Booleans in the redeemer are equal!
mkValidator :: () -> (Bool, Bool) -> ScriptContext -> Bool mkValidator :: () -> (Bool, Bool) -> ScriptContext -> Bool
mkValidator _ _ _ = True -- FIX ME! mkValidator _ (a, b) _ = traceIfFalse "redeemers doesnt match!" $ a == b
data Typed data Typed
instance Scripts.ValidatorTypes Typed where instance Scripts.ValidatorTypes Typed where
-- Implement the instance! type instance DatumType Typed = ()
type instance RedeemerType Typed = (Bool, Bool)
typedValidator :: Scripts.TypedValidator Typed 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 :: Validator
validator = undefined -- FIX ME! validator = Scripts.validatorScript typedValidator
valHash :: Ledger.ValidatorHash valHash :: Ledger.ValidatorHash
valHash = undefined -- FIX ME! valHash = Scripts.validatorHash typedValidator
scrAddress :: Ledger.Address scrAddress :: Ledger.Address
scrAddress = undefined -- FIX ME! scrAddress = scriptAddress validator
type GiftSchema = type GiftSchema =
Endpoint "give" Integer Endpoint "give" Integer

View file

@ -43,23 +43,28 @@ PlutusTx.unstableMakeIsData ''MyRedeemer
{-# INLINABLE mkValidator #-} {-# INLINABLE mkValidator #-}
-- This should validate if and only if the two Booleans in the redeemer are equal! -- This should validate if and only if the two Booleans in the redeemer are equal!
mkValidator :: () -> MyRedeemer -> ScriptContext -> Bool mkValidator :: () -> MyRedeemer -> ScriptContext -> Bool
mkValidator _ _ _ = True -- FIX ME! mkValidator _ (MyRedeemer r) _ = traceIfFalse "Flags dont match" $ r.flag1 == r.flag2
s
data Typed dat Typed
instance Scripts.ValidatorTypes Typed where instance Scripts.ValidatorTypes Typed where
-- Implement me! type instance DatumType Typed = ()
type instance RedeemerType Typed = MyRedeemer
typedValidator :: Scripts.TypedValidator Typed 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 :: Validator
validator = undefined -- FIX ME! validator = Scripts.validatorScript typedValidator -- FIX ME!
valHash :: Ledger.ValidatorHash valHash :: Ledger.ValidatorHash
valHash = undefined -- FIX ME! valHash = Scripts.validatorHash typedValidator -- FIX ME!
scrAddress :: Ledger.Address scrAddress :: Ledger.Address
scrAddress = undefined -- FIX ME! scrAddress = scriptAddress validator -- FIX ME!
type GiftSchema = type GiftSchema =
Endpoint "give" Integer Endpoint "give" Integer

View file

@ -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 -- 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. -- or if beneficiary2 has signed the transaction and the deadline has passed.
mkValidator :: VestingDatum -> () -> ScriptContext -> Bool 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 data Vesting
instance Scripts.ValidatorTypes Vesting where instance Scripts.ValidatorTypes Vesting where

View file

@ -36,7 +36,17 @@ import Text.Printf (printf)
{-# INLINABLE mkValidator #-} {-# INLINABLE mkValidator #-}
mkValidator :: PubKeyHash -> POSIXTime -> () -> ScriptContext -> Bool 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 data Vesting
instance Scripts.ValidatorTypes Vesting where instance Scripts.ValidatorTypes Vesting where
@ -44,13 +54,17 @@ instance Scripts.ValidatorTypes Vesting where
type instance RedeemerType Vesting = () type instance RedeemerType Vesting = ()
typedValidator :: PubKeyHash -> Scripts.TypedValidator 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 :: PubKeyHash -> Validator
validator = undefined -- IMPLEMENT ME! validator = Scripts.validatorScript . typedValidator
scrAddress :: PubKeyHash -> Ledger.Address scrAddress :: PubKeyHash -> Ledger.Address
scrAddress = undefined -- IMPLEMENT ME! scrAddress = scriptAddress . validator
data GiveParams = GiveParams data GiveParams = GiveParams
{ gpBeneficiary :: !PubKeyHash { gpBeneficiary :: !PubKeyHash
@ -102,7 +116,7 @@ grab = do
Just d -> d <= now Just d -> d <= now
endpoints :: Contract () VestingSchema Text () endpoints :: Contract () VestingSchema Text ()
endpoints = (give' `select` grab') >> endpoints endpoints = (give' + grab') >> endpoints
where where
give' = endpoint @"give" >>= give give' = endpoint @"give" >>= give
grab' = endpoint @"grab" >> grab grab' = endpoint @"grab" >> grab