Skip to content

Commit

Permalink
Separate out a proposal contract
Browse files Browse the repository at this point in the history
  • Loading branch information
sjoerdvisscher committed Apr 15, 2021
1 parent ea9c766 commit 3c47130
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 92 deletions.
32 changes: 19 additions & 13 deletions plutus-use-cases/src/Plutus/Contracts/Governance.hs
Expand Up @@ -17,6 +17,7 @@
{-# OPTIONS_GHC -fno-spec-constr #-}
module Plutus.Contracts.Governance (
contract
, proposalContract
, scriptInstance
, client
, mkTokenName
Expand Down Expand Up @@ -156,8 +157,7 @@ transition Params{..} State{ stateData = s, stateValue} i = case (s, i) of

(GovState law mph Nothing, ProposeChange proposal@(Proposal{tokenName})) ->
let constraints = ownsVotingToken mph tokenName
selfVote = AssocMap.singleton tokenName True
in Just (constraints, State (GovState law mph (Just (Voting proposal selfVote))) stateValue)
in Just (constraints, State (GovState law mph (Just (Voting proposal AssocMap.empty))) stateValue)

(GovState law mph (Just (Voting p oldMap)), AddVote tokenName vote) ->
let newMap = AssocMap.insert tokenName vote oldMap
Expand All @@ -178,17 +178,7 @@ contract ::
-> Contract () Schema e ()
contract params = forever $ mapError (review _GovError) endpoints where
theClient = client params
endpoints = initLaw `select` propose `select` addVote

propose = do
proposal <- endpoint @"propose-change"
void $ SM.runStep theClient (ProposeChange proposal)

logInfo @Text "Voting started. Waiting for the voting deadline to count the votes."
void $ awaitSlot (votingDeadline proposal)

logInfo @Text "Voting finished. Counting the votes."
SM.runStep theClient FinishVoting
endpoints = initLaw `select` addVote

addVote = do
(tokenName, vote) <- endpoint @"add-vote"
Expand All @@ -201,6 +191,22 @@ contract params = forever $ mapError (review _GovError) endpoints where
let tokens = zipWith (const (mkTokenName (baseTokenName params))) (initialHolders params) [1..]
SM.runStep theClient $ ForgeTokens tokens

proposalContract ::
AsGovError e
=> Params
-> Contract () Schema e ()
proposalContract params = forever $ mapError (review _GovError) propose where
theClient = client params
propose = do
proposal <- endpoint @"propose-change"
void $ SM.runStep theClient (ProposeChange proposal)

logInfo @Text "Voting started. Waiting for the voting deadline to count the votes."
void $ awaitSlot (votingDeadline proposal)

logInfo @Text "Voting finished. Counting the votes."
SM.runStep theClient FinishVoting

PlutusTx.makeLift ''Params
PlutusTx.unstableMakeIsData ''Proposal
PlutusTx.makeLift ''Proposal
Expand Down
8 changes: 4 additions & 4 deletions plutus-use-cases/test/Spec/Governance.hs
Expand Up @@ -22,7 +22,6 @@ import qualified Ledger.Typed.Scripts as Scripts
import qualified Wallet.Emulator as EM

import Plutus.Contract.Test
-- import qualified Plutus.Contract.StateMachine as SM
import qualified Plutus.Contracts.Governance as Gov
import Plutus.Trace.Emulator (EmulatorTrace)
import qualified Plutus.Trace.Emulator as Trace
Expand Down Expand Up @@ -71,13 +70,14 @@ doVoting :: Int -> Int -> Integer -> EmulatorTrace ()
doVoting ayes nays rounds = do
let activate w = (Gov.mkTokenName baseName w,) <$> Trace.activateContractWallet (EM.Wallet w) (Gov.contract @Gov.GovError params)
namesAndHandles <- traverse activate [1..numberOfHolders]
let (_, handle1) = namesAndHandles !! 0
let (token2, handle2) = namesAndHandles !! 1
let handle1 = snd (namesAndHandles !! 0)
let token2 = fst (namesAndHandles !! 1)
proposalHandle <- Trace.activateContractWallet (EM.Wallet 2) (Gov.proposalContract @Gov.GovError params)
_ <- Trace.callEndpoint @"new-law" handle1 lawv1
_ <- Trace.waitNSlots 10
let votingRound (_, law) = do
now <- view Trace.currentSlot <$> Trace.chainState
Trace.callEndpoint @"propose-change" handle2 Gov.Proposal{ Gov.newLaw = law, Gov.votingDeadline = now + 20, Gov.tokenName = token2 }
Trace.callEndpoint @"propose-change" proposalHandle Gov.Proposal{ Gov.newLaw = law, Gov.votingDeadline = now + 20, Gov.tokenName = token2 }
_ <- Trace.waitNSlots 1
traverse_ (\(nm, hdl) -> Trace.callEndpoint @"add-vote" hdl (nm, True) >> Trace.waitNSlots 1) (take ayes namesAndHandles)
traverse_ (\(nm, hdl) -> Trace.callEndpoint @"add-vote" hdl (nm, False) >> Trace.waitNSlots 1) (take nays $ drop ayes namesAndHandles)
Expand Down
87 changes: 12 additions & 75 deletions plutus-use-cases/test/Spec/governance.pir
Expand Up @@ -7545,31 +7545,10 @@
Voting
proposal
]
[
[
{
Cons
[[Tuple2 (con bytestring)] Bool]
}
[
[
{
{
Tuple2
(con bytestring)
}
Bool
}
ds
]
True
]
]
{
Nil
[[Tuple2 (con bytestring)] Bool]
}
]
{
Nil
[[Tuple2 (con bytestring)] Bool]
}
]
]
]
Expand Down Expand Up @@ -8349,31 +8328,10 @@
Voting
proposal
]
[
[
{
Cons
[[Tuple2 (con bytestring)] Bool]
}
[
[
{
{
Tuple2
(con bytestring)
}
Bool
}
ds
]
True
]
]
{
Nil
[[Tuple2 (con bytestring)] Bool]
}
]
{
Nil
[[Tuple2 (con bytestring)] Bool]
}
]
]
]
Expand Down Expand Up @@ -9479,31 +9437,10 @@
Voting
proposal
]
[
[
{
Cons
[[Tuple2 (con bytestring)] Bool]
}
[
[
{
{
Tuple2
(con bytestring)
}
Bool
}
ds
]
True
]
]
{
Nil
[[Tuple2 (con bytestring)] Bool]
}
]
{
Nil
[[Tuple2 (con bytestring)] Bool]
}
]
]
]
Expand Down

0 comments on commit 3c47130

Please sign in to comment.