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 b431c0446d
commit 0570ce08cc
2 changed files with 61 additions and 27 deletions

View file

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

View file

@ -20,8 +20,9 @@ module Agora.Utils (
phashDatum,
puncurryTuple,
psubtractSortedValue,
pisSubValueOf,
pfindInputWithStateThreadToken,
pfindOutputWithStateThreadTokenAndAddress,
pisSubValueOf,
) where
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.V2 (
AmountGuarantees (NoGuarantees),
PAddress,
PCurrencySymbol,
PMaybeData (PDNothing),
PTuple,
PTxInInfo,
PTxOut,
PValue,
)
import Plutarch.Builtin (pforgetData, pserialiseData)
@ -178,6 +181,52 @@ psubtractSortedValue = phoistAcyclic $ plam $ \a b ->
# (pfmap # pnegate)
# 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 ::
forall (kg :: KeyGuarantees) (am :: AmountGuarantees) (s :: S).
Term s (PValue kg am :--> PBool)
@ -200,24 +249,3 @@ pisSubValueOf = phoistAcyclic $ plam $ \vl vr ->
#$ psubtractSortedValue
# vl
# 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