Find governor output, no longer require it to be only one

This commit is contained in:
nini-faroux 2023-04-13 16:56:49 +01:00 committed by Emily Martins
parent 9b55f9c5cc
commit e5605514f5
2 changed files with 61 additions and 27 deletions

View file

@ -27,7 +27,7 @@ import Agora.Governor (
) )
import Agora.Proposal (PProposalId) import Agora.Proposal (PProposalId)
import Agora.SafeMoney (AuthorityTokenTag, GovernorSTTag) import Agora.SafeMoney (AuthorityTokenTag, GovernorSTTag)
import Agora.Utils (pfindInputWithStateThreadToken) import Agora.Utils (pfindInputWithStateThreadToken, pfindOutputWithStateThreadTokenAndAddress)
import Generics.SOP qualified as SOP import Generics.SOP qualified as SOP
import Plutarch.Api.V1 (PCurrencySymbol) import Plutarch.Api.V1 (PCurrencySymbol)
import Plutarch.Api.V2 ( import Plutarch.Api.V2 (
@ -55,7 +55,7 @@ import Plutarch.Extra.ScriptContext (
import Plutarch.Extra.Tagged (PTagged) import Plutarch.Extra.Tagged (PTagged)
import Plutarch.Lift (PConstantDecl, PLifted, PUnsafeLiftDecl) import Plutarch.Lift (PConstantDecl, PLifted, PUnsafeLiftDecl)
import PlutusTx qualified import PlutusTx qualified
import "liqwid-plutarch-extra" Plutarch.Extra.List (ptryFromSingleton)
import "liqwid-plutarch-extra" Plutarch.Extra.TermCont (pguardC, pletC, pletFieldsC) import "liqwid-plutarch-extra" Plutarch.Extra.TermCont (pguardC, pletC, pletFieldsC)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -204,6 +204,8 @@ mutateGovernorValidator =
governorRef = pfield @"outRef" # governorInput governorRef = pfield @"outRef" # governorInput
governorInputAddress = pfield @"address" #$ pfield @"resolved" # governorInput
governorInputDatum = governorInputDatum =
ptrace "Resolve governor input datum" $ ptrace "Resolve governor input datum" $
pfromData $ pfromData $
@ -240,7 +242,7 @@ mutateGovernorValidator =
governorRedeemer = governorRedeemer =
pfromData $ pfromData $
passertPJust passertPJust
# "Govenor redeemer should be resolved" # "Governor redeemer should be resolved"
#$ ptryFromRedeemer @(PAsData PGovernorRedeemer) #$ ptryFromRedeemer @(PAsData PGovernorRedeemer)
# mkRecordConstr PSpending (#_0 .= governorRef) # mkRecordConstr PSpending (#_0 .= governorRef)
# txInfoF.redeemers # txInfoF.redeemers
@ -252,8 +254,12 @@ mutateGovernorValidator =
let let
governorOutput = governorOutput =
ptrace "Only governor output is allowed" $ passertPJust
ptryFromSingleton # pfromData txInfoF.outputs # "No governor output found"
#$ pfindOutputWithStateThreadTokenAndAddress
# pfromData gstSymbol
# governorInputAddress
# pfromData txInfoF.outputs
governorOutputDatum = governorOutputDatum =
ptrace "Resolve governor outoput datum" $ ptrace "Resolve governor outoput datum" $

View file

@ -20,8 +20,9 @@ module Agora.Utils (
phashDatum, phashDatum,
puncurryTuple, puncurryTuple,
psubtractSortedValue, psubtractSortedValue,
pisSubValueOf,
pfindInputWithStateThreadToken, pfindInputWithStateThreadToken,
pfindOutputWithStateThreadTokenAndAddress,
pisSubValueOf,
) where ) where
import Plutarch.Api.V1 (AmountGuarantees (Positive), KeyGuarantees (Sorted)) import Plutarch.Api.V1 (AmountGuarantees (Positive), KeyGuarantees (Sorted))
@ -30,10 +31,12 @@ import Plutarch.Api.V1.AssocMap qualified as AssocMap
import Plutarch.Api.V1.Scripts (PDatumHash (PDatumHash)) import Plutarch.Api.V1.Scripts (PDatumHash (PDatumHash))
import Plutarch.Api.V2 ( import Plutarch.Api.V2 (
AmountGuarantees (NoGuarantees), AmountGuarantees (NoGuarantees),
PAddress,
PCurrencySymbol, PCurrencySymbol,
PMaybeData (PDNothing), PMaybeData (PDNothing),
PTuple, PTuple,
PTxInInfo, PTxInInfo,
PTxOut,
PValue, PValue,
) )
import Plutarch.Builtin (pforgetData, pserialiseData) import Plutarch.Builtin (pforgetData, pserialiseData)
@ -178,6 +181,52 @@ psubtractSortedValue = phoistAcyclic $ plam $ \a b ->
# (pfmap # pnegate) # (pfmap # pnegate)
# pto b # pto b
{- | Find an input containing exactly one token with the given currency symbol
@since 1.0.0
-}
pfindInputWithStateThreadToken ::
forall tag.
ClosedTerm
( PTagged tag PCurrencySymbol
:--> PBuiltinList PTxInInfo
:--> PMaybe PTxInInfo
)
pfindInputWithStateThreadToken = plam $ \tokenSymbol inputs ->
pfind
# ( plam $ \input ->
ptaggedSymbolValueOf
# tokenSymbol
# (pfield @"value" # (pfield @"resolved" # input))
#== 1
)
# inputs
{- | Find an output containing exactly one token with the given currency symbol,
and with a PAddress that matches the given one.
@since 1.0.0
-}
pfindOutputWithStateThreadTokenAndAddress ::
forall tag.
ClosedTerm
( PTagged tag PCurrencySymbol
:--> PAddress
:--> PBuiltinList PTxOut
:--> PMaybe PTxOut
)
pfindOutputWithStateThreadTokenAndAddress = plam $ \tokenSymbol address outputs ->
pfind
# ( plam $ \output ->
( ptaggedSymbolValueOf
# tokenSymbol
# (pfield @"value" # output)
#== 1
)
#&& (address #== (pfield @"address" # output))
)
# outputs
pisNonNegativeValue :: pisNonNegativeValue ::
forall (kg :: KeyGuarantees) (am :: AmountGuarantees) (s :: S). forall (kg :: KeyGuarantees) (am :: AmountGuarantees) (s :: S).
Term s (PValue kg am :--> PBool) Term s (PValue kg am :--> PBool)
@ -200,24 +249,3 @@ pisSubValueOf = phoistAcyclic $ plam $ \vl vr ->
#$ psubtractSortedValue #$ psubtractSortedValue
# vl # vl
# vr # vr
{- | Find an input containing exactly one token with the given currency symbol
@since 1.0.0
-}
pfindInputWithStateThreadToken ::
forall tag.
ClosedTerm
( PTagged tag PCurrencySymbol
:--> PBuiltinList PTxInInfo
:--> PMaybe PTxInInfo
)
pfindInputWithStateThreadToken = plam $ \tokenSymbol inputs ->
pfind
# ( plam $ \input ->
ptaggedSymbolValueOf
# tokenSymbol
# (pfield @"value" # (pfield @"resolved" # input))
#== 1
)
# inputs