So I'm building a simple voting system as a DAML beginner and it's working quite good so far. Now I set conditions stating that a decision is allowed to be made when 60% of the registered voters have voted (which can be found in choice Decide below). However, when I leave out any of voters while meeting the 60% condition I get the following error stating that there's an authorized voter missing:
The following errors occured: node NodeId(1)
(Identifier(0ea886b25958c473b6ab9efaacc3f4eb79975d4eec82d08a9d7e1d3e6ab4c1bb,Voti
ng:Decision))
requires authorizers Approver 1,Approver 2,Approver 4,Music Rights
Association, but only Music Rights
Association,Approver 1,Approver 2 were given.
For some reason, all the voters are required to vote due to authorization, even though voters are registered under observers. How can I ensure that each voter retains their option to vote but a decision can be made when 60% has voted?
code:
daml 1.2
module Voting where
import DA.Next.Set as S
import DA.List as L
import DA.Assert
type VotingCid = ContractId Create_Voting
type ActorCid = ContractId Actor
type CreationCid = ContractId Creation
data CreationRights = CreationRights
with
votingRight : [Party]
deriving (Eq, Show)
template Claim
with
proposer : Party
where
signatory proposer
type VotingKey = (Party, Claim)
template Creation
with
artist : Party
title : Text
votingRight : Set Party
where
signatory artist
template Voting
with
actor : Party
claim : Claim
select_creationid : ContractId Creation
artist : Party
title : Text
voters : Set Party
voted : Set Party
votes : [Bool]
where
signatory actor, voted
observer voters
key (actor, claim) : VotingKey
maintainer key._1
choice Vote : ()
with
voter : Party
accept : Bool
controller voter
do
assertMsg "Voter not added" $ member voter voters
assertMsg "Voter already voted" $ not $ member voter voted
create this with voted = S.insert voter voted; votes = accept :: votes
pure ()
choice Decide : ContractId Decision
controller actor
do
let votersdec : Decimal = intToDecimal(size voters)
let votesdec : Decimal = intToDecimal(length votes)
assertMsg "At least 60% must have voted" $ (votersdec * 0.6) < votesdec
let approvals = length $ L.filter (\v -> v) votes
let disapprovals = length $ L.filter (\v -> not v) votes
let accept = approvals > disapprovals
create Decision with actor = actor; claim = claim; voters = voters; accept = accept
template Create_Voting
with
actor : Party
claim : Claim
select_creationid : ContractId Creation
voters : Set Party
voted : Set Party
votes : [Bool]
where
signatory actor
choice Select_Creation : ContractId Voting
with
title : Text
votingRight : Set Party
controller actor
do
selectedCreation <- fetch select_creationid
--selectedActor === selectedCreation.actor
--selectedTitle === selectedCreation.title
--voters === selectedCreation.votingRight
create Voting with artist = selectedCreation.artist; title = selectedCreation.title; actor = actor; claim = claim; select_creationid = select_creationid; voters = selectedCreation.votingRight;voted = voted; votes = votes
--insertCreation <- exercise selectedCreation Create_Selected_Claim
--return(insertCreation)
template Decision
with
actor : Party
claim : Claim
voters : Set Party
accept : Bool
where
signatory actor, voters
The reason why all voters are required to authorize the Decide
choice, is that it creates a Decision
contract, where all voters are listed as signatories. You could change the Decision
contract to
template Decision
with
actor : Party
claim : Claim
voted : Set Party
voters : Set Party
accept : Bool
where
signatory actor, voted
observers voters
This would make the Decide
choice work while still informing all possible voters about the outcome.