Skip to content

Instantly share code, notes, and snippets.

assertMsg "each voter is only allowed to cast one vote per option" (notElem voter crtVotes.voted)
create this with votes = updatedVotes
VotingSlot with count = crtVotes.count + 1, voted = voter :: crtVotes.voted
updatedVotes = DA.TextMap.insert option (VotingSlot with count = crtVotes.count + 1, voted = voter :: crtVotes.voted) this.votes
crtVotes = fromOptional (VotingSlot with count = 0, voted = []) (DA.TextMap.lookup option this.votes)
do
invite <- fetch inviteId
...
let
crtVotes = fromOptional (VotingSlot with count = 0, voted = []) (DA.TextMap.lookup option this.votes)
updatedVotes = DA.TextMap.insert option (VotingSlot with count = crtVotes.count + 1, voted = voter :: crtVotes.voted) this.votes
assertMsg "each voter is only allowed to cast one vote per option" (notElem voter crtVotes.voted)
create this with votes = updatedVotes
preconsuming choice CastVote: ContractId Doodle
with
voter: Party
option: Text
inviteId: ContractId DoodleInvite
controller voter
do
invite <- fetch inviteId
assertMsg "this invite was issued for a different doodle" (invite.doodleName == name)
assertMsg "the voter casting the vote does not match the voter who received the invite" (invite.voter == voter)
nonconsuming choice Vote : ContractId Doodle
with
option: Text
controller voter
do
doodleContractId <- lookupByKey @Doodle (organizer, doodleName)
doodleContract <- fetch (fromSome doodleContractId)
assertMsg "this party does not own the doodle " (organizer == doodleContract.organizer)
exercise (fromSome doodleContractId) CastVote with voter = this.voter, option = option, inviteId = self
nonconsuming choice Vote : ContractId Doodle
with
option: Text
controller voter
do
doodleContractId <- lookupByKey @Doodle (organizer, doodleName)
doodleContract <- fetch (fromSome doodleContractId)
assertMsg "this party does not own the doodle " (organizer == doodleContract.organizer)
exercise (fromSome doodleContractId) CastVote with voter = this.voter, option = option, inviteId = self
template DoodleInvite
with
doodleName: Text
organizer: Party
voter: Party
where
signatory organizer
observer voter
key (organizer, voter, doodleName) : (Party, Party, Text)
maintainer key._1
public static <T, R> List<R> mapA(Function<T,R> function, List<T> list) {
return list.stream().map(t -> function.apply(t)).collect(Collectors.toList());
}