some doc for the proposal voting sample

This commit is contained in:
fanghr 2022-05-19 17:10:33 +08:00
parent 97b3d6f023
commit ec48f4de95
2 changed files with 35 additions and 11 deletions

View file

@ -12,6 +12,7 @@ module Sample.Proposal (
proposalRef, proposalRef,
stakeRef, stakeRef,
voteOnProposal, voteOnProposal,
VotingParameters (..),
) where ) where
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -246,12 +247,19 @@ cosignProposal newSigners =
, txInfoId = "0b2086cbf8b6900f8cb65e012de4516cb66b5cb08a9aaba12a8b88be" , txInfoId = "0b2086cbf8b6900f8cb65e012de4516cb66b5cb08a9aaba12a8b88be"
} }
{- | A valid transaction of voting on a propsal. --------------------------------------------------------------------------------
-- TODO: docs -- | Parameters for creating a voting transaction.
-} data VotingParameters = VotingParameters
voteOnProposal :: ResultTag -> Integer -> TxInfo { voteFor :: ResultTag
voteOnProposal voteFor voteCount = -- ^ The outcome the transaction is voting for.
, voteCount :: Integer
-- ^ The count of votes.
}
-- | Create a valid transaction that votes on a propsal, given the parameters.
voteOnProposal :: VotingParameters -> TxInfo
voteOnProposal params =
let pst = Value.singleton proposalPolicySymbol "" 1 let pst = Value.singleton proposalPolicySymbol "" 1
sst = Value.assetClassValue stakeAssetClass 1 sst = Value.assetClassValue stakeAssetClass 1
@ -313,7 +321,7 @@ voteOnProposal voteFor voteCount =
stakeInputDatum' :: StakeDatum stakeInputDatum' :: StakeDatum
stakeInputDatum' = stakeInputDatum' =
StakeDatum StakeDatum
{ stakedAmount = Tagged voteCount { stakedAmount = Tagged params.voteCount
, owner = stakeOwner , owner = stakeOwner
, lockedBy = existingLocks , lockedBy = existingLocks
} }
@ -323,14 +331,14 @@ voteOnProposal voteFor voteCount =
stakeInput = stakeInput =
TxOut TxOut
{ txOutAddress = stakeAddress { txOutAddress = stakeAddress
, txOutValue = sst <> Value.assetClassValue (untag stake.gtClassRef) voteCount , txOutValue = sst <> Value.assetClassValue (untag stake.gtClassRef) params.voteCount
, txOutDatumHash = Just $ toDatumHash stakeInputDatum , txOutDatumHash = Just $ toDatumHash stakeInputDatum
} }
--- ---
updatedVotes :: AssocMap.Map ResultTag Integer updatedVotes :: AssocMap.Map ResultTag Integer
updatedVotes = updateMap (Just . (+ voteCount)) voteFor initialVotes updatedVotes = updateMap (Just . (+ params.voteCount)) params.voteFor initialVotes
--- ---
@ -351,7 +359,7 @@ voteOnProposal voteFor voteCount =
-- Off-chain code should do exactly like this: prepend new lock to the list. -- Off-chain code should do exactly like this: prepend new lock to the list.
updatedLocks :: [ProposalLock] updatedLocks :: [ProposalLock]
updatedLocks = ProposalLock voteFor proposalInputDatum'.proposalId : existingLocks updatedLocks = ProposalLock params.voteFor proposalInputDatum'.proposalId : existingLocks
--- ---

View file

@ -122,7 +122,15 @@ tests =
} }
) )
(Vote (ResultTag 0)) (Vote (ResultTag 0))
(ScriptContext (Proposal.voteOnProposal (ResultTag 0) 27) (Spending Proposal.proposalRef)) ( ScriptContext
( Proposal.voteOnProposal
Proposal.VotingParameters
{ Proposal.voteFor = ResultTag 0
, Proposal.voteCount = 27
}
)
(Spending Proposal.proposalRef)
)
, validatorSucceedsWith , validatorSucceedsWith
"stake" "stake"
(stakeValidator Shared.stake) (stakeValidator Shared.stake)
@ -134,7 +142,15 @@ tests =
] ]
) )
(PermitVote $ ProposalLock (ResultTag 0) (ProposalId 42)) (PermitVote $ ProposalLock (ResultTag 0) (ProposalId 42))
(ScriptContext (Proposal.voteOnProposal (ResultTag 0) 27) (Spending Proposal.stakeRef)) ( ScriptContext
( Proposal.voteOnProposal
Proposal.VotingParameters
{ Proposal.voteFor = ResultTag 0
, Proposal.voteCount = 27
}
)
(Spending Proposal.stakeRef)
)
] ]
] ]
] ]