add pwinner to find the winning outcome
This commit is contained in:
parent
d03a3a5c00
commit
cf5b6ebc5b
2 changed files with 37 additions and 22 deletions
|
|
@ -53,6 +53,7 @@ import Agora.Proposal (
|
||||||
ProposalStatus (Draft, Locked),
|
ProposalStatus (Draft, Locked),
|
||||||
pemptyVotesFor,
|
pemptyVotesFor,
|
||||||
proposalDatumValid,
|
proposalDatumValid,
|
||||||
|
pwinner,
|
||||||
)
|
)
|
||||||
import Agora.Proposal.Scripts (
|
import Agora.Proposal.Scripts (
|
||||||
proposalPolicy,
|
proposalPolicy,
|
||||||
|
|
@ -77,7 +78,6 @@ import Agora.Utils (
|
||||||
mustFindDatum',
|
mustFindDatum',
|
||||||
pfindTxInByTxOutRef,
|
pfindTxInByTxOutRef,
|
||||||
pisDJust,
|
pisDJust,
|
||||||
pisJust,
|
|
||||||
pisUTXOSpent,
|
pisUTXOSpent,
|
||||||
psymbolValueOf,
|
psymbolValueOf,
|
||||||
ptryFindDatum,
|
ptryFindDatum,
|
||||||
|
|
@ -605,27 +605,7 @@ governorValidator gov =
|
||||||
-- TODO: anything else to check here?
|
-- TODO: anything else to check here?
|
||||||
|
|
||||||
-- Find the highest votes and the corresponding tag.
|
-- Find the highest votes and the corresponding tag.
|
||||||
let highestVoteFolder =
|
winner <- tclet $ mustBePJust # "No winning outcome" #$ pwinner # proposalInputDatumF.votes
|
||||||
phoistAcyclic $
|
|
||||||
plam
|
|
||||||
( \pair last' ->
|
|
||||||
pif
|
|
||||||
(pisJust # last')
|
|
||||||
( unTermCont $ do
|
|
||||||
PJust last <- tcmatch last'
|
|
||||||
let lastHighestVote = pfromData $ psndBuiltin # last
|
|
||||||
thisVote = pfromData $ psndBuiltin # pair
|
|
||||||
pure $ pif (lastHighestVote #< thisVote) (pcon $ PJust pair) last'
|
|
||||||
)
|
|
||||||
(pcon $ PJust pair)
|
|
||||||
)
|
|
||||||
|
|
||||||
votesList = pto $ pto $ pfromData proposalInputDatumF.votes
|
|
||||||
|
|
||||||
maybeWinner =
|
|
||||||
pfoldr # highestVoteFolder # pcon PNothing # votesList
|
|
||||||
|
|
||||||
winner <- tclet $ mustBePJust # "No winning outcome" # maybeWinner
|
|
||||||
|
|
||||||
PDiscrete minimumVotes' <- pmatchC $ pfromData $ pfield @"execute" # proposalInputDatumF.thresholds
|
PDiscrete minimumVotes' <- pmatchC $ pfromData $ pfield @"execute" # proposalInputDatumF.thresholds
|
||||||
let highestVote = pfromData $ psndBuiltin # winner
|
let highestVote = pfromData $ psndBuiltin # winner
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,7 @@ module Agora.Proposal (
|
||||||
-- * Plutarch helpers
|
-- * Plutarch helpers
|
||||||
proposalDatumValid,
|
proposalDatumValid,
|
||||||
pemptyVotesFor,
|
pemptyVotesFor,
|
||||||
|
pwinner,
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import GHC.Generics qualified as GHC
|
import GHC.Generics qualified as GHC
|
||||||
|
|
@ -448,3 +449,37 @@ proposalDatumValid proposal =
|
||||||
, ptraceIfFalse "Proposal has fewer cosigners than the limit" $ plength # (pfromData datum.cosigners) #<= pconstant proposal.maximumCosigners
|
, ptraceIfFalse "Proposal has fewer cosigners than the limit" $ plength # (pfromData datum.cosigners) #<= pconstant proposal.maximumCosigners
|
||||||
, ptraceIfFalse "Proposal votes and effects are compatible with each other" $ pkeysEqual # datum.effects # pto (pfromData datum.votes)
|
, ptraceIfFalse "Proposal votes and effects are compatible with each other" $ pkeysEqual # datum.effects # pto (pfromData datum.votes)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
{- | Find the winning outcome (and the corresponding vote count) given the votes.
|
||||||
|
|
||||||
|
FIXME: What if two or more outcomes have the exact same vote count?
|
||||||
|
-}
|
||||||
|
pwinner ::
|
||||||
|
Term
|
||||||
|
s
|
||||||
|
( PProposalVotes
|
||||||
|
:--> PMaybe (PBuiltinPair (PAsData PResultTag) (PAsData PInteger))
|
||||||
|
)
|
||||||
|
pwinner = phoistAcyclic $
|
||||||
|
plam $ \votes ->
|
||||||
|
let l :: Term _ (PBuiltinList _)
|
||||||
|
l = pto $ pto votes
|
||||||
|
|
||||||
|
f ::
|
||||||
|
Term
|
||||||
|
_
|
||||||
|
( PBuiltinPair (PAsData PResultTag) (PAsData PInteger)
|
||||||
|
:--> PMaybe (PBuiltinPair (PAsData PResultTag) (PAsData PInteger))
|
||||||
|
:--> PMaybe (PBuiltinPair (PAsData PResultTag) (PAsData PInteger))
|
||||||
|
)
|
||||||
|
f = phoistAcyclic $
|
||||||
|
plam $ \this maybeLast -> pmatch maybeLast $ \case
|
||||||
|
PNothing -> pcon $ PJust this
|
||||||
|
PJust last ->
|
||||||
|
let lastVotes = pfromData $ psndBuiltin # last
|
||||||
|
thisVotes = pfromData $ psndBuiltin # this
|
||||||
|
in pif
|
||||||
|
(lastVotes #< thisVotes)
|
||||||
|
(pcon $ PJust this)
|
||||||
|
maybeLast
|
||||||
|
in pfoldr # f # pcon PNothing # l
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue