make it possible for delegatee to vote with delegated and own stakes

This commit is contained in:
Hongrui Fang 2022-11-05 00:02:33 +08:00
parent a0946a8dbc
commit 3eb71a55a3
3 changed files with 41 additions and 9 deletions

View file

@ -18,6 +18,7 @@ module Sample.Proposal.Vote (
mkTestTree, mkTestTree,
mkValidOwnerVoteBundle, mkValidOwnerVoteBundle,
mkValidDelegateeVoteBundle, mkValidDelegateeVoteBundle,
delegateeVoteWithOwnAndDelegatedStakeBundle,
transparentAssets, transparentAssets,
transactionNotAuthorized, transactionNotAuthorized,
voteForNonexistentOutcome, voteForNonexistentOutcome,
@ -99,6 +100,7 @@ newtype VoteParameters = VoteParameters {voteFor :: ResultTag}
data StakeParameters = StakeParameters data StakeParameters = StakeParameters
{ numStakes :: Integer { numStakes :: Integer
, mixInDelegateeAsOwner :: Bool
, stakeInputParameters :: StakeInputParameters , stakeInputParameters :: StakeInputParameters
, stakeOutputParameters :: StakeOutputParameters , stakeOutputParameters :: StakeOutputParameters
} }
@ -257,6 +259,16 @@ vote params =
stakeRedeemer = stakeRedeemer =
mkStakeRedeemer params.stakeParameters.stakeOutputParameters mkStakeRedeemer params.stakeParameters.stakeOutputParameters
mixOwner i datum =
if params.stakeParameters.mixInDelegateeAsOwner
&& i == 2
then
datum
{ owner = PubKeyCredential delegatee
, delegatedTo = Nothing
}
else datum
stakeBuilder :: b stakeBuilder :: b
stakeBuilder = stakeBuilder =
foldMap foldMap
@ -266,7 +278,7 @@ vote params =
mconcat mconcat
[ script stakeValidatorHash [ script stakeValidatorHash
, withValue stakeInputValue , withValue stakeInputValue
, withInlineDatum stakeInputDatum , withInlineDatum $ mixOwner i stakeInputDatum
, withRedeemer stakeRedeemer , withRedeemer stakeRedeemer
, withRef $ mkStakeRef numProposals' i , withRef $ mkStakeRef numProposals' i
] ]
@ -277,7 +289,7 @@ vote params =
mconcat mconcat
[ script stakeValidatorHash [ script stakeValidatorHash
, withValue stakeOutputValue , withValue stakeOutputValue
, withInlineDatum stakeOutputDatum , withInlineDatum $ mixOwner i stakeOutputDatum
] ]
] ]
) )
@ -420,6 +432,7 @@ mkValidOwnerVoteBundle stakes =
, stakeParameters = , stakeParameters =
StakeParameters StakeParameters
{ numStakes = stakes { numStakes = stakes
, mixInDelegateeAsOwner = False
, stakeInputParameters = , stakeInputParameters =
StakeInputParameters StakeInputParameters
{ perStakeGTs = (def :: ProposalThresholds).vote { perStakeGTs = (def :: ProposalThresholds).vote
@ -453,6 +466,16 @@ mkValidDelegateeVoteBundle stakes =
} }
} }
delegateeVoteWithOwnAndDelegatedStakeBundle :: ParameterBundle
delegateeVoteWithOwnAndDelegatedStakeBundle =
let template = mkValidDelegateeVoteBundle 5
in template
{ stakeParameters =
template.stakeParameters
{ mixInDelegateeAsOwner = True
}
}
ownerVoteWithSignleStake :: ParameterBundle ownerVoteWithSignleStake :: ParameterBundle
ownerVoteWithSignleStake = mkValidOwnerVoteBundle 1 ownerVoteWithSignleStake = mkValidOwnerVoteBundle 1

View file

@ -160,6 +160,10 @@ specs =
"transparent non-GT tokens" "transparent non-GT tokens"
Vote.transparentAssets Vote.transparentAssets
(Vote.Validity True True) (Vote.Validity True True)
, Vote.mkTestTree
"Delegatee vote with own and delegated stakes in one tx"
Vote.delegateeVoteWithOwnAndDelegatedStakeBundle
(Vote.Validity True True)
] ]
, group , group
"illegal" "illegal"

View file

@ -79,6 +79,7 @@ import Plutarch.Extra.Functor (PFunctor (pfmap))
import "liqwid-plutarch-extra" Plutarch.Extra.List (pfindJust, pmapMaybe) import "liqwid-plutarch-extra" Plutarch.Extra.List (pfindJust, pmapMaybe)
import Plutarch.Extra.Maybe ( import Plutarch.Extra.Maybe (
passertPJust, passertPJust,
pdjust,
pfromJust, pfromJust,
pfromMaybe, pfromMaybe,
pjust, pjust,
@ -341,7 +342,7 @@ mkStakeValidator impl sstSymbol pstClass gtClass =
authorizedBy <- pletC $ pauthorizedBy # authorizationContext txInfoF authorizedBy <- pletC $ pauthorizedBy # authorizationContext txInfoF
PPair allHaveSameOwner allHaveSameDelegatee <- PPair allHaveSameOwner allHaveSameOrOwnedByDelegatee <-
pmatchC $ pmatchC $
pfoldr pfoldr
# plam # plam
@ -354,11 +355,15 @@ mkStakeValidator impl sstSymbol pstClass gtClass =
allHaveSameOwner allHaveSameOwner
#&& dF.owner #&& dF.owner
#== firstStakeInputDatumF.owner #== firstStakeInputDatumF.owner
allHaveSameDelegatee' = allHaveSameOrOwnedByDelegatee' =
allHaveSameDelegatee let delegated =
#&& dF.delegatedTo dF.delegatedTo #== firstStakeInputDatumF.delegatedTo
#== firstStakeInputDatumF.delegatedTo ownedByDelagtee =
in pcon $ PPair allHaveSameOwner' allHaveSameDelegatee' pdata (pdjust # dF.owner)
#== firstStakeInputDatumF.delegatedTo
in allHaveSameDelegatee
#&& (delegated #|| ownedByDelagtee)
in pcon $ PPair allHaveSameOwner' allHaveSameOrOwnedByDelegatee'
) )
# pcon (PPair (pconstant True) (pconstant True)) # pcon (PPair (pconstant True) (pconstant True))
# restOfStakeInputDatums # restOfStakeInputDatums
@ -369,7 +374,7 @@ mkStakeValidator impl sstSymbol pstClass gtClass =
# firstStakeInputDatumF.owner # firstStakeInputDatumF.owner
delegateSignsTransaction = delegateSignsTransaction =
allHaveSameDelegatee allHaveSameOrOwnedByDelegatee
#&& pmaybeData #&& pmaybeData
# pconstant False # pconstant False
# plam ((authorizedBy #) . pfromData) # plam ((authorizedBy #) . pfromData)