Skip to content

Instantly share code, notes, and snippets.

@caike
Created December 16, 2022 20:12
Show Gist options
  • Save caike/43f579ad6fa10835432c4c4ec1652426 to your computer and use it in GitHub Desktop.
Save caike/43f579ad6fa10835432c4c4ec1652426 to your computer and use it in GitHub Desktop.
Plutus Pioneers Week03 Homework
{-# INLINABLE mkValidator #-}
-- 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 datum _ sc = traceIfFalse "cannot grab" (beneficiary1Grab || beneficiary2Grab)
where
info :: TxInfo
info = scriptContextTxInfo sc
beneficiary1Grab :: Bool
beneficiary1Grab = currentSlotBeforeOrAtDeadline && signedByBeneficiary1
currentSlotBeforeOrAtDeadline :: Bool
-- the current slot is before or at the deadline
currentSlotBeforeOrAtDeadline = contains (to $ deadline datum) $ txInfoValidRange info
signedByBeneficiary1 :: Bool
signedByBeneficiary1 = txSignedBy info $ unPaymentPubKeyHash $ beneficiary1 datum
beneficiary2Grab :: Bool
beneficiary2Grab = currentSlotAfterDeadline && signedByBeneficiary2
currentSlotAfterDeadline :: Bool
-- the current slot is AFTER the deadline
currentSlotAfterDeadline = not $ overlaps (to $ deadline datum) $ txInfoValidRange info
signedByBeneficiary2 :: Bool
signedByBeneficiary2 = txSignedBy info $ unPaymentPubKeyHash $ beneficiary2 datum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment