Skip to content

Commit

Permalink
playground: More comments in sample contracts
Browse files Browse the repository at this point in the history
* More comments
* Use `PendingTx'` where possible
  • Loading branch information
j-mueller committed Dec 8, 2018
1 parent 25e0d46 commit 7e6a2b9
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 48 deletions.
125 changes: 79 additions & 46 deletions plutus-playground/plutus-playground-server/usecases/CrowdFunding.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,52 +48,66 @@ PlutusTx.makeLift ''CampaignAction
contributionScript :: Campaign -> ValidatorScript
contributionScript cmp = ValidatorScript val where
val = Ledger.applyScript mkValidator (Ledger.lifted cmp)
mkValidator = Ledger.fromCompiledCode $$(PlutusTx.compile [|| (\Campaign{..} (act :: CampaignAction) (con :: CampaignActor) (p :: PendingTx ValidatorHash) ->
let

infixr 3 &&
(&&) :: Bool -> Bool -> Bool
(&&) = $$(PlutusTx.and)

PendingTx ps outs _ _ (Height h) _ _ = p

deadline :: Int
deadline = let Height h' = campaignDeadline in h'

collectionDeadline :: Int
collectionDeadline = let Height h' = campaignCollectionDeadline in h'

target :: Int
target = let Value v = campaignTarget in v

-- | The total value of all contributions
totalInputs :: Int
totalInputs =
let v (PendingTxIn _ _ (Value vl)) = vl in
$$(P.foldr) (\i total -> total + v i) 0 ps

isValid = case act of
Refund -> -- the "refund" branch
let

contributorTxOut :: PendingTxOut -> Bool
contributorTxOut o = case $$(pubKeyOutput) o of
Nothing -> False
Just pk -> $$(eqPubKey) pk con

-- Check that all outputs are paid to the public key
-- of the contributor (this key is provided as the data script `con`)
contributorOnly = $$(P.all) contributorTxOut outs

refundable = h > collectionDeadline && contributorOnly && $$(txSignedBy) p con

in refundable
Collect -> -- the "successful campaign" branch
let
payToOwner = h > deadline && h <= collectionDeadline && totalInputs >= target && $$(txSignedBy) p campaignOwner
in payToOwner
in
if isValid then () else $$(P.error) ()) ||])
mkValidator = Ledger.fromCompiledCode $$(PlutusTx.compile [||

-- The validator script is a function of for arguments:
-- 1. The 'Campaign' definition. This argument is provided by the Plutus client, using 'Ledger.applyScript'.
-- As a result, the 'Campaign' definition is part of the script address, and different campaigns have different addresses.
-- The Campaign{..} syntax means that all fields of the 'Campaign' value are in scope (for example 'campaignDeadline' in l. 70).
-- See note [RecordWildCards].
--
-- 2. A 'CampaignAction'. This is the redeemer script. It is provided by the redeeming transaction.
--
-- 3. A 'CampaignActor'. This is the data script. It is provided by the producing transaction (the contribution)
--
-- 4. A 'PendingTx' value. It contains information about the current transaction and is provided by the slot leader.
-- See note [PendingTx]
\Campaign{..} (act :: CampaignAction) (con :: CampaignActor) (p :: PendingTx') ->
let

infixr 3 &&
(&&) :: Bool -> Bool -> Bool
(&&) = $$(PlutusTx.and)

PendingTx ps outs _ _ (Height h) _ _ = p

deadline :: Int
deadline = let Height h' = campaignDeadline in h'

collectionDeadline :: Int
collectionDeadline = let Height h' = campaignCollectionDeadline in h'

target :: Int
target = let Value v = campaignTarget in v

-- | The total value of all contributions
totalInputs :: Int
totalInputs =
let v (PendingTxIn _ _ (Value vl)) = vl in
$$(P.foldr) (\i total -> total + v i) 0 ps

isValid = case act of
Refund -> -- the "refund" branch
let

contributorTxOut :: PendingTxOut -> Bool
contributorTxOut o = case $$(pubKeyOutput) o of
Nothing -> False
Just pk -> $$(eqPubKey) pk con

-- Check that all outputs are paid to the public key
-- of the contributor (this key is provided as the data script `con`)
contributorOnly = $$(P.all) contributorTxOut outs

refundable = h > collectionDeadline && contributorOnly && $$(txSignedBy) p con

in refundable
Collect -> -- the "successful campaign" branch
let
payToOwner = h > deadline && h <= collectionDeadline && totalInputs >= target && $$(txSignedBy) p campaignOwner
in payToOwner
in
if isValid then () else $$(P.error) () ||])

-- | The address of a [[Campaign]]
campaignAddress :: Campaign -> Ledger.Address'
Expand Down Expand Up @@ -142,3 +156,22 @@ refundHandler txid cmp = EventHandler (\_ -> do

$(mkFunction 'scheduleCollection)
$(mkFunction 'contribute)

{- note [RecordWildCards]
We can use the syntax "Campaign{..}" here because the 'RecordWildCards'
extension is enabled automatically by the Playground backend.
The extension is documented here:
* https://downloads.haskell.org/~ghc/7.2.1/docs/html/users_guide/syntax-extns.html
A list of extensions that are enabled by default for the Playground can be found here:
* https://github.com/input-output-hk/plutus/blob/b0f49a0cc657cd1a4eaa4af72a6d69996b16d07a/plutus-playground/plutus-playground-server/src/Playground/Interpreter.hs#L44
-}

{- note [PendingTx]
This part of the API (the PendingTx argument) is experimental and subject to change.
-}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ mkRedeemerScript word =

gameValidator :: ValidatorScript
gameValidator = ValidatorScript (Ledger.fromCompiledCode $$(PlutusTx.compile [||
\(ClearString guess) (HashedString actual) (p :: PendingTx ValidatorHash) ->
\(ClearString guess) (HashedString actual) (p :: PendingTx') ->

if $$(P.equalsByteString) actual ($$(P.sha2_256) guess)
then ()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ validatorScriptHash =
validatorScript :: Vesting -> ValidatorScript
validatorScript v = ValidatorScript val where
val = Ledger.applyScript inner (Ledger.lifted v)
inner = Ledger.fromCompiledCode $$(PlutusTx.compile [|| \Vesting{..} () VestingData{..} (p :: PendingTx ValidatorHash) ->
inner = Ledger.fromCompiledCode $$(PlutusTx.compile [|| \Vesting{..} () VestingData{..} (p :: PendingTx') ->
let

eqPk :: PubKey -> PubKey -> Bool
Expand Down

0 comments on commit 7e6a2b9

Please sign in to comment.